Package ptolemy.plot.plotml
Class PlotBoxMLParser
- java.lang.Object
-
- com.microstar.xml.HandlerBase
-
- ptolemy.plot.plotml.PlotBoxMLParser
-
- All Implemented Interfaces:
XmlHandler
- Direct Known Subclasses:
PlotMLParser
public class PlotBoxMLParser extends HandlerBase
This class constructs a plot from specifications in PlotML (Plot Markup Language), which is an XML language. This class supports only the subset that applies to the PlotBox base class. It ignores all other elements in the DTD. The class contains an instance of the Microstar Ælfred XML parser and implements callback methods to interpret the parsed XML. The way to use this class is to construct it with a reference to a PlotBox object and then call its parse() method.- Since:
- Ptolemy II 0.4
- Version:
- $Id$
- Author:
- Edward A. Lee
- Pt.AcceptedRating:
- Red (cxh)
- Pt.ProposedRating:
- Red (eal)
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Hashtable
_attributes
Attributes associated with an entity.protected java.lang.StringBuffer
_currentCharData
The current character data for the current element.protected XmlParser
_parser
The parser.protected PlotBoxInterface
_plot
The plot object to which to apply commands.static java.lang.String
PlotML_DTD_1
The standard PlotML DTD, represented as a string.
-
Constructor Summary
Constructors Modifier Constructor Description protected
PlotBoxMLParser()
Protected constructor allows derived classes to set _plot differently.PlotBoxMLParser(PlotBoxInterface plot)
Construct an parser to parse commands for the specified plot object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_checkForNull(java.lang.Object object, java.lang.String message)
If the argument is null, throw an exception with the given message.protected java.lang.String
_currentExternalEntity()
Get the the URI for the current external entity.void
attribute(java.lang.String name, java.lang.String value, boolean specified)
Handle an attribute assignment that is part of an XML element.void
charData(char[] chars, int offset, int length)
Handle character data.void
endDocument()
End the document.void
endElement(java.lang.String elementName)
End an element.void
error(java.lang.String message, java.lang.String systemID, int line, int column)
Indicate a fatal XML parsing error.void
parse(java.net.URL base, java.io.InputStream input)
Parse the given stream as a PlotML file.void
parse(java.net.URL base, java.io.Reader reader)
Parse the given stream as a PlotML file.void
parse(java.net.URL base, java.lang.String text)
Parse the given text as PlotML.java.lang.Object
resolveEntity(java.lang.String publicID, java.lang.String systemID)
Resolve an external entity.void
startDocument()
Start a document.void
startElement(java.lang.String elementName)
Start an element.void
startExternalEntity(java.lang.String systemID)
Handle the start of an external entity.-
Methods inherited from class com.microstar.xml.HandlerBase
doctypeDecl, endExternalEntity, ignorableWhitespace, processingInstruction
-
-
-
-
Field Detail
-
PlotML_DTD_1
public static final java.lang.String PlotML_DTD_1
The standard PlotML DTD, represented as a string. This is used to parse PlotML data when a compatible PUBLIC DTD is specified.- See Also:
- Constant Field Values
-
_attributes
protected java.util.Hashtable _attributes
Attributes associated with an entity.
-
_currentCharData
protected java.lang.StringBuffer _currentCharData
The current character data for the current element.
-
_parser
protected XmlParser _parser
The parser.
-
_plot
protected PlotBoxInterface _plot
The plot object to which to apply commands.
-
-
Constructor Detail
-
PlotBoxMLParser
public PlotBoxMLParser(PlotBoxInterface plot)
Construct an parser to parse commands for the specified plot object.- Parameters:
plot
- The plot object to which to apply the commands.
-
PlotBoxMLParser
protected PlotBoxMLParser()
Protected constructor allows derived classes to set _plot differently.
-
-
Method Detail
-
attribute
public void attribute(java.lang.String name, java.lang.String value, boolean specified) throws XmlException
Handle an attribute assignment that is part of an XML element. This method is called prior to the corresponding startElement() call, so it simply accumulates attributes in a hashtable for use by startElement().- Specified by:
attribute
in interfaceXmlHandler
- Overrides:
attribute
in classHandlerBase
- Parameters:
name
- The name of the attribute.value
- The value of the attribute, or null if the attribute is#IMPLIED
and not specified.specified
- True if the value is specified, false if the value comes from the default value in the DTD rather than from the XML file.- Throws:
XmlException
- If the name or value is null.- See Also:
XmlHandler.attribute(java.lang.String, java.lang.String, boolean)
-
charData
public void charData(char[] chars, int offset, int length)
Handle character data. In this implementation, the character data is accumulated in a buffer until the end element. Ælfred will call this method once for each chunk of character data found in the contents of elements. Note that the parser may break up a long sequence of characters into smaller chunks and call this method once for each chunk.- Specified by:
charData
in interfaceXmlHandler
- Overrides:
charData
in classHandlerBase
- Parameters:
chars
- The character data.offset
- The starting position in the array.length
- The number of characters available.- See Also:
XmlHandler.charData(char[], int, int)
-
endDocument
public void endDocument() throws java.lang.Exception
End the document. In this implementation, do nothing. Ælfred will call this method once, when it has finished parsing the XML document. It is guaranteed that this will be the last method called.- Specified by:
endDocument
in interfaceXmlHandler
- Overrides:
endDocument
in classHandlerBase
- Throws:
java.lang.Exception
- Derived methods may throw exceptions.- See Also:
XmlHandler.endDocument()
-
endElement
public void endElement(java.lang.String elementName) throws java.lang.Exception
End an element. For most elements this method calls the appropriate PlotBox method. Ælfred will call this method at the end of each element (including EMPTY elements).- Specified by:
endElement
in interfaceXmlHandler
- Overrides:
endElement
in classHandlerBase
- Parameters:
elementName
- The element type name.- Throws:
java.lang.Exception
- If thrown while invoking a method on the Plot object.- See Also:
XmlHandler.endElement(java.lang.String)
-
error
public void error(java.lang.String message, java.lang.String systemID, int line, int column) throws XmlException
Indicate a fatal XML parsing error. Ælfred will call this method whenever it encounters a serious error. This method simply throws an XmlException.- Specified by:
error
in interfaceXmlHandler
- Overrides:
error
in classHandlerBase
- Parameters:
message
- The error message.systemID
- The URI of the entity that caused the error.line
- The approximate line number of the error.column
- The approximate column number of the error.- Throws:
XmlException
- If called.- See Also:
XmlHandler.error(java.lang.String, java.lang.String, int, int)
-
parse
public void parse(java.net.URL base, java.io.InputStream input) throws java.lang.Exception
Parse the given stream as a PlotML file. For example, an applet might use this method as follows:PlotBoxMLParser parser = new PlotBoxMLParser(); URL docBase = getDocumentBase(); URL xmlFile = new URL(docBase, modelURL); parser.parse(xmlFile.openStream());
A variety of exceptions might be thrown if the parsed data does not represent a valid PlotML file.- Parameters:
base
- The base URL for relative references, or null if there is none.input
- The stream from which to read XML.- Throws:
java.lang.Exception
- If the parser fails.
-
parse
public void parse(java.net.URL base, java.io.Reader reader) throws java.lang.Exception
Parse the given stream as a PlotML file. A variety of exceptions might be thrown if the parsed data does not represent a valid PlotML file.- Parameters:
base
- The base URL for relative references, or null if there is none.reader
- The stream from which to read XML.- Throws:
java.lang.Exception
- If the parser fails.
-
parse
public void parse(java.net.URL base, java.lang.String text) throws java.lang.Exception
Parse the given text as PlotML. A variety of exceptions might be thrown if the parsed data does not represent valid PlotML data.- Parameters:
base
- The base URL for relative references, or null if there is none.text
- The PlotML data.- Throws:
java.lang.Exception
- If the parser fails.
-
resolveEntity
public java.lang.Object resolveEntity(java.lang.String publicID, java.lang.String systemID)
Resolve an external entity. If the first argument is the name of the PlotML PUBLIC DTD ("-//UC Berkeley//DTD PlotML 1//EN"), then return a StringReader that will read the locally cached version of this DTD (the public variable PlotML_DTD_1). Otherwise, return null, which has the effect of deferring to Ælfred for resolution of the URI. Derived classes may return a a modified URI (a string), an InputStream, or a Reader. In the latter two cases, the input character stream is provided.- Specified by:
resolveEntity
in interfaceXmlHandler
- Overrides:
resolveEntity
in classHandlerBase
- Parameters:
publicID
- The public identifier, or null if none was supplied.systemID
- The system identifier.- Returns:
- Null, indicating to use the default system identifier.
- See Also:
XmlHandler.resolveEntity(java.lang.String, java.lang.String)
-
startDocument
public void startDocument()
Start a document. This method is called just before the parser attempts to read the first entity (the root of the document). It is guaranteed that this will be the first method called.- Specified by:
startDocument
in interfaceXmlHandler
- Overrides:
startDocument
in classHandlerBase
- See Also:
XmlHandler.startDocument()
-
startElement
public void startElement(java.lang.String elementName) throws XmlException
Start an element. This is called at the beginning of each XML element. By the time it is called, all of the attributes for the element will already have been reported using the attribute() method. Unrecognized elements are ignored.- Specified by:
startElement
in interfaceXmlHandler
- Overrides:
startElement
in classHandlerBase
- Parameters:
elementName
- The element type name.- Throws:
XmlException
- If the element produces an error in constructing the model.- See Also:
XmlHandler.startElement(java.lang.String)
-
startExternalEntity
public void startExternalEntity(java.lang.String systemID)
Handle the start of an external entity. This pushes the stack so that error reporting correctly reports the external entity that causes the error.- Specified by:
startExternalEntity
in interfaceXmlHandler
- Overrides:
startExternalEntity
in classHandlerBase
- Parameters:
systemID
- The URI for the external entity.- See Also:
XmlHandler.startExternalEntity(java.lang.String)
-
_checkForNull
protected void _checkForNull(java.lang.Object object, java.lang.String message) throws XmlException
If the argument is null, throw an exception with the given message.- Parameters:
object
- The reference to check for null.message
- The message to issue if the reference is null.- Throws:
XmlException
- If the object argument is null.
-
_currentExternalEntity
protected java.lang.String _currentExternalEntity()
Get the the URI for the current external entity.- Returns:
- A string giving the URI of the external entity being read, or null if none.
-
-