de.tilman.mathParser
Class MathMLParser

java.lang.Object
  extended byde.tilman.mathParser.MathMLParser

public class MathMLParser
extends java.lang.Object

Author:
Tilman Walther, Martin Wilke

Field Summary
protected  java.lang.String COMMENT_PREFIX
          The comment prefix for the substitutions file.
protected  java.lang.String endTag
           
protected  java.lang.StringBuffer entity
           
protected  java.lang.StringBuffer entitySubst
           
protected  java.lang.String PH_BLOCK
          The place holder for blocks in substitutions.
protected  int pos
           
protected  java.lang.StringBuffer result
           
protected  char[] specialCharacters
           
protected  java.lang.String startTag
           
protected  java.lang.StringBuffer strBuf
           
protected  java.util.Hashtable substitutions
           
protected  java.lang.String SUBSTITUTIONS_FILE
          The default path of the substitutions file
protected  java.lang.StringBuffer tagBuf
           
protected  boolean wrappedEntities
           
 
Constructor Summary
MathMLParser()
          Generates the substitution table from the default file path in field SUBSTITUTIONS_FILE.
MathMLParser(java.lang.String substitutionsFile)
          Generates the substitution table from the given file path.
 
Method Summary
(package private)  int getBlockEnd(java.lang.String startTag, java.lang.String endTag)
          Seeks the end of the block defined by the 'startTag' parameter skipping subblocks of the same type.
(package private)  java.util.Hashtable getSubstitutionTable(java.lang.String filePath)
          Parses the substitution table from the given file.
 java.lang.StringBuffer parse(java.lang.StringBuffer strBuf, boolean wrappedEntities)
          Parses MathML code into LaTeX code using the substitution table genereated by the constructor.
(package private)  java.lang.StringBuffer parseArea(int areaEnd)
          Parses an area of strBuf recursively into LaTeX code.
(package private)  java.lang.String parseBlockContent(java.lang.String s)
           
(package private)  void readNextTag()
          Jumps to the next tag, reads it into 'startTag' an generates the corresponding 'endTag'.
(package private)  void skipClosingTag()
          Skips the next tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUBSTITUTIONS_FILE

protected final java.lang.String SUBSTITUTIONS_FILE
The default path of the substitutions file

See Also:
Constant Field Values

COMMENT_PREFIX

protected final java.lang.String COMMENT_PREFIX
The comment prefix for the substitutions file. All lines of the substitutions file beginning with the comment prefix are ignored.

See Also:
Constant Field Values

PH_BLOCK

protected final java.lang.String PH_BLOCK
The place holder for blocks in substitutions. If a substitution contains a block place holder it is replaced by the LaTeX representation of the followig block.

See Also:
Constant Field Values

specialCharacters

protected final char[] specialCharacters

substitutions

protected java.util.Hashtable substitutions

result

protected java.lang.StringBuffer result

strBuf

protected java.lang.StringBuffer strBuf

pos

protected int pos

wrappedEntities

protected boolean wrappedEntities

startTag

protected java.lang.String startTag

endTag

protected java.lang.String endTag

tagBuf

protected java.lang.StringBuffer tagBuf

entity

protected java.lang.StringBuffer entity

entitySubst

protected java.lang.StringBuffer entitySubst
Constructor Detail

MathMLParser

public MathMLParser()
             throws java.io.FileNotFoundException,
                    java.io.IOException
Generates the substitution table from the default file path in field SUBSTITUTIONS_FILE.

Throws:
java.io.FileNotFoundException - if no file was found at the default position
java.io.IOException - if an I/O error occurs

MathMLParser

public MathMLParser(java.lang.String substitutionsFile)
             throws java.io.FileNotFoundException,
                    java.io.IOException
Generates the substitution table from the given file path.

Parameters:
substitutionsFile - the path of the text file with the substitution table.
Throws:
java.io.FileNotFoundException - if the file could not be found
java.io.IOException - if an I/O error occurs
Method Detail

parse

public java.lang.StringBuffer parse(java.lang.StringBuffer strBuf,
                                    boolean wrappedEntities)
                             throws java.lang.Exception
Parses MathML code into LaTeX code using the substitution table genereated by the constructor.
Only presentation markup can be parsed properly, no use for parsing content markup.

For example the presentation markup code

 <mrow>
   <msup>
     <mfenced>
       <mrow>
         <mi>a</mi>
         <mo>+</mo>
         <mi>b</mi>
       </mrow>
     </mfenced>
     <mn>2</mn>
   </msup>
 </mrow>
 
can be parsed by this method, while the equivalent content markup
 <mrow>
   <apply>
     <power/>
     <apply>
       <plus/>
       <ci>a</ci>
       <ci>b</ci>
     </apply>
     <cn>2</cn>
   </apply>
 </mrow>
 
can not be parsed.

Both notations of entities can be parsed: The plain MathML notation, starting with an ampersand sign (e.g. '&equals;'), or the "HTML wrapped" notation startig with an entity for the ampersand sign (e.g. '&amp;equals;').

Parameters:
strBuf - a StringBuffer containig the MathML code to parse
wrappedEntities - indicates whether the entities in the MathML code are HTML wrapped (e.g. &amp;PlusMinus;), or not (e.g. &PlusMinus;)
Returns:
a StringBuffer containig the LaTeX representation of the input
Throws:
java.lang.Exception - if an error occurs while parsing

parseArea

java.lang.StringBuffer parseArea(int areaEnd)
                           throws java.lang.Exception
Parses an area of strBuf recursively into LaTeX code.

Pseudocode:

 while (pos <= areaEnd) {
   if (insideOfInnerstBlock) {
     result.append(convertToLatexSyntax(area));
   }
   else {
     tmpTag = getNextTag();   // pos = pos + tmpTag.length();
     if (substitutionAvailable(tmpTag)) {
       while (substitutionContainsBlock) { 
         addSubstitutionUpToPlaceHolderOfBlockToOutput();
         parseArea(getAreaOfNextBlock());
       }
       addRestOfSubstitutionOutPut();
     }
     else {
       parseArea(pos, getAreaEnd(tmpTag));
     }
     skipClosingTag();
   }
 }
 

Parameters:
areaEnd - the end of the area to parse
Returns:
a StringBuffer with the LaTeX representation of the input
Throws:
java.lang.Exception

readNextTag

void readNextTag()
Jumps to the next tag, reads it into 'startTag' an generates the corresponding 'endTag'.


skipClosingTag

void skipClosingTag()
Skips the next tag.


getBlockEnd

int getBlockEnd(java.lang.String startTag,
                java.lang.String endTag)
Seeks the end of the block defined by the 'startTag' parameter skipping subblocks of the same type.

Parameters:
startTag - the tag that opened the block
endTag - the end tag to seek
Returns:
the index of the closing tag

parseBlockContent

java.lang.String parseBlockContent(java.lang.String s)
                             throws java.lang.Exception
Throws:
java.lang.Exception

getSubstitutionTable

java.util.Hashtable getSubstitutionTable(java.lang.String filePath)
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Parses the substitution table from the given file.

Parameters:
filePath - the path of the substitutions file
Returns:
a hashtable containing all found substitutions
Throws:
java.io.FileNotFoundException - if the substitutions file could not be found
java.io.IOException - if an I/O error occurs