001/* Plotter applet that is capable of reading PlotML files. 002 003 @Author: Edward A. Lee 004 005 @Version: $Id$ 006 007 @Copyright (c) 1997-2016 The Regents of the University of California. 008 All rights reserved. 009 010 Permission is hereby granted, without written agreement and without 011 license or royalty fees, to use, copy, modify, and distribute this 012 software and its documentation for any purpose, provided that the 013 above copyright notice and the following two paragraphs appear in all 014 copies of this software. 015 016 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 017 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 018 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 019 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 020 SUCH DAMAGE. 021 022 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 023 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 024 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 025 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 026 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 027 ENHANCEMENTS, OR MODIFICATIONS. 028 029 PT_COPYRIGHT_VERSION_2 030 COPYRIGHTENDKEY 031 */ 032package ptolemy.plot.plotml; 033 034import java.io.BufferedInputStream; 035import java.io.IOException; 036import java.io.InputStream; 037import java.net.URL; 038 039import com.microstar.xml.XmlException; 040 041import ptolemy.plot.Plot; 042import ptolemy.plot.PlotApplet; 043 044/////////////////////////////////////////////////////////////////// 045//// PlotMLApplet 046 047/** 048 An Applet that can plot data in PlotML format from a URL. 049 The URL should be specified using the dataurl applet parameter. 050 051 @author Edward A. Lee 052 @version $Id$ 053 @since Ptolemy II 0.4 054 @Pt.ProposedRating red (eal) 055 @Pt.AcceptedRating red (cxh) 056 @see ptolemy.plot.PlotBox 057 @see ptolemy.plot.Plot 058 */ 059@SuppressWarnings("serial") 060public class PlotMLApplet extends PlotApplet { 061 /** Return a string describing this applet. 062 */ 063 @Override 064 public String getAppletInfo() { 065 return "PlotMLApplet 2.0: A data plotter.\n" 066 + "By: Edward A. Lee and\n " + "Christopher Hylands\n" 067 + "($Id$)"; 068 } 069 070 /////////////////////////////////////////////////////////////////// 071 //// protected methods //// 072 073 /** Read the specified stream. This method checks to see whether 074 * the data is PlotML data, and if so, creates a parser to read it. 075 * If not, it defers to the parent class to read it. 076 * @param in The input stream. 077 * @exception IOException If the stream cannot be read. 078 */ 079 @Override 080 protected void _read(InputStream in) throws IOException { 081 // Create a buffered input stream so that mark and reset 082 // are supported. 083 BufferedInputStream bin = new BufferedInputStream(in); 084 085 // Peek at the file... 086 bin.mark(9); 087 088 // Read 8 bytes in case 16-bit encoding is being used. 089 byte[] peek = new byte[8]; 090 int bytesRead = bin.read(peek); 091 if (bytesRead != peek.length) { 092 throw new IOException("Read only " + bytesRead + "bytes, expecting " 093 + peek.length); 094 } 095 bin.reset(); 096 097 if (new String(peek).startsWith("<?xm")) { 098 // file is an XML file. 099 PlotMLParser parser = _newParser(); 100 101 try { 102 URL docBase = getDocumentBase(); 103 parser.parse(docBase, bin); 104 } catch (Exception ex) { 105 String msg; 106 107 if (ex instanceof XmlException) { 108 XmlException xmlex = (XmlException) ex; 109 msg = "PlotMLApplet: failed to parse PlotML data:\n" 110 + "line: " + xmlex.getLine() + ", column: " 111 + xmlex.getColumn() + "\nIn entity: " 112 + xmlex.getSystemId() + "\n"; 113 } else { 114 msg = "PlotMLApplet: failed to parse PlotML data:\n"; 115 } 116 117 System.err.println(msg + ex.toString()); 118 ex.printStackTrace(); 119 } 120 } else { 121 super._read(bin); 122 } 123 } 124 125 /** Create a new parser object for the applet. Derived classes can 126 * redefine this method to return a different type of parser. 127 * @return A new PlotMLParser. 128 */ 129 protected PlotMLParser _newParser() { 130 return new PlotMLParser((Plot) plot()); 131 } 132}