org.jscience.ml.openmath.io
Class OMXMLReader

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.jscience.ml.openmath.io.OMXMLReader
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class OMXMLReader
extends org.xml.sax.helpers.DefaultHandler

Constructs an OpenMath Object (OMObject) from the XML-input.


Field Summary
protected  int foreign
          Stores if we are inside a foreign object section.
protected  boolean keep
          Stores if we should keep the outer OMOBJ.
protected  java.util.Vector mElementStack
          Stores an vector with the last active element as the last in the vector.
protected  org.xml.sax.InputSource mInputSource
          Stores the input-source.
protected  OMObject mOMObject
          Stores the OMObject for this reader.
protected static java.lang.String[] sOMObjects
          Stores a static array with all the OpenMath element-names in use.
 
Constructor Summary
OMXMLReader()
          Constructor.
OMXMLReader(org.xml.sax.InputSource fInputSource)
          Constructor.
OMXMLReader(java.lang.String fString)
          Constructor taking a String as the InputSource.
OMXMLReader(java.lang.String newString, boolean newKeep)
          Constructor taking a String as the InputSource and a boolean indicating if we want to keep the outer OMOBJ.
 
Method Summary
 void characters(char[] fCharacters, int fStart, int fLength)
          We've encountered a PCDATA section.
 void endDocument()
          End the document.
 void endElement(java.lang.String fNamespaceURI, java.lang.String fLocalName, java.lang.String fRawName)
          End a XML element.
 OMObject getObject()
          Get the OpenMath object.
 void ignorableWhitespace(char[] tCharacter, int tStart, int tLength)
          Process ignorable whitespace.
 void processingInstruction(java.lang.String fTarget, java.lang.String fData)
          Process the processing instruction.
 OMObject readObject()
          Read an OpenMath object.
 void setDocumentLocator(org.xml.sax.Locator fLocator)
          Set the document-locator.
 void startDocument()
          Signals the start of the document.
 void startElement(java.lang.String fNamespaceURI, java.lang.String fLocalName, java.lang.String fRawName, org.xml.sax.Attributes fAttributes)
          Start a XML element.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, resolveEntity, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sOMObjects

protected static java.lang.String[] sOMObjects
Stores a static array with all the OpenMath element-names in use.


mOMObject

protected OMObject mOMObject
Stores the OMObject for this reader.


mElementStack

protected java.util.Vector mElementStack
Stores an vector with the last active element as the last in the vector. This vector grows/shrinks during parsing. It is used to easily keep track of the current parent object in the tree.


mInputSource

protected org.xml.sax.InputSource mInputSource
Stores the input-source.


keep

protected boolean keep
Stores if we should keep the outer OMOBJ.


foreign

protected int foreign
Stores if we are inside a foreign object section.

Note: if we find another foreign inside the foreign we add to this number to make sure we can handle the endElements correctly.

Constructor Detail

OMXMLReader

public OMXMLReader()
Constructor.


OMXMLReader

public OMXMLReader(org.xml.sax.InputSource fInputSource)
Constructor.

Parameters:
fInputSource - the InputSource to read from.

OMXMLReader

public OMXMLReader(java.lang.String fString)
Constructor taking a String as the InputSource.

Parameters:
fString - the string to read from.

OMXMLReader

public OMXMLReader(java.lang.String newString,
                   boolean newKeep)
Constructor taking a String as the InputSource and a boolean indicating if we want to keep the outer OMOBJ.

Parameters:
newString - the input source
newKeep - the boolean indicating if we keep the OMOBJ
Method Detail

characters

public void characters(char[] fCharacters,
                       int fStart,
                       int fLength)
                throws org.xml.sax.SAXException
We've encountered a PCDATA section.

Check if we have an enclosing OMInteger or OMString and set the data-members accordingly.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Parameters:
fCharacters - the characters to handle.
fStart - the start index into the characters array.
fLength - the number of characters to read from the start.
Throws:
org.xml.sax.SAXException - when something is seriously wrong.

endDocument

public void endDocument()
End the document.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler

endElement

public void endElement(java.lang.String fNamespaceURI,
                       java.lang.String fLocalName,
                       java.lang.String fRawName)
                throws org.xml.sax.SAXException
End a XML element.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
fNamespaceURI - the URI of the namespace.
fLocalName - the locale name of the element.
fRawName - the raw name of the element.
Throws:
org.xml.sax.SAXException - when something is seriously wrong.

ignorableWhitespace

public void ignorableWhitespace(char[] tCharacter,
                                int tStart,
                                int tLength)
Process ignorable whitespace.

Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler
Parameters:
tCharacter - the characters that can be ignored.
tStart - the start index into the characters array.
tLength - the number of characters to ignore.

processingInstruction

public void processingInstruction(java.lang.String fTarget,
                                  java.lang.String fData)
Process the processing instruction.

Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Overrides:
processingInstruction in class org.xml.sax.helpers.DefaultHandler
Parameters:
fTarget - the target of the processing instruction.
fData - the data of the processing instruction.

readObject

public OMObject readObject()
                    throws java.io.IOException
Read an OpenMath object.

Returns:
the OpenMath object to was read.
Throws:
java.io.IOException - when reading failed.

getObject

public OMObject getObject()
Get the OpenMath object.

Note: if you didn't set this reader up from an input-source nor from a String, you will need this method to get the actual object at the end of reading. This method will only garantuee the proper result if you call it after 'endDocument' has be recognized.

Returns:
the parsed OMObject or null

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator fLocator)
Set the document-locator.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
Parameters:
fLocator - the document locator to use.

startDocument

public void startDocument()
Signals the start of the document.

We reinitiliaze all the data-structures used during parsing, note that this parser is not reentrant and should only be used sequentially.

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler

startElement

public void startElement(java.lang.String fNamespaceURI,
                         java.lang.String fLocalName,
                         java.lang.String fRawName,
                         org.xml.sax.Attributes fAttributes)
                  throws org.xml.sax.SAXException
Start a XML element.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
fNamespaceURI - the namespace URI.
fLocalName - the local name of the element.
fRawName - the raw name of the element.
fAttributes - the attributes of the element.
Throws:
org.xml.sax.SAXException - when something is seriously wrong.