View Javadoc

1   /* $Id: BasicUpConversionExample.java 525 2010-01-05 14:07:36Z davemckain $
2    *
3    * Copyright (c) 2010, The University of Edinburgh.
4    * All Rights Reserved
5    */
6   package uk.ac.ed.ph.snuggletex.upconversion.samples;
7   
8   import uk.ac.ed.ph.snuggletex.SnuggleEngine;
9   import uk.ac.ed.ph.snuggletex.SnuggleInput;
10  import uk.ac.ed.ph.snuggletex.SnuggleSession;
11  import uk.ac.ed.ph.snuggletex.XMLStringOutputOptions;
12  import uk.ac.ed.ph.snuggletex.upconversion.UpConvertingPostProcessor;
13  import uk.ac.ed.ph.snuggletex.upconversion.internal.UpConversionPackageDefinitions;
14  
15  import java.io.IOException;
16  
17  /**
18   * Basic example of up-converting some simple LaTeX input to Content MathML and Maxima forms.
19   * 
20   * <h2>Running Notes</h2>
21   * 
22   * You will need the following in your ClassPath:
23   * 
24   * <ul>
25   *   <li>snuggletex-core.jar</li>
26   *   <li>snuggletex-upconversion.jar</li>
27   *   <li>saxon9.jar, saxon9-dom.jar</li> (These are required as the conversion process uses XSLT 2.0)
28   * </ul>
29   * 
30   * @since 1.1.0
31   *
32   * @author  David McKain
33   * @version $Revision: 525 $
34   */
35  public final class BasicUpConversionExample {
36      
37      public static void main(String[] args) throws IOException {
38          /* We will up-convert this LaTeX input */
39          String input = "$$ \\frac{2x-y^2}{\\sin xy(x-2)} $$";
40          
41          /* Set up SnuggleEngine, remembering to register package providing up-conversion support */
42          SnuggleEngine engine = new SnuggleEngine();
43          engine.addPackage(UpConversionPackageDefinitions.getPackage());
44          
45          /* Create session in usual way */
46          SnuggleSession session = engine.createSession();
47          
48          /* Parse input. I won't bother checking it here */
49          session.parseInput(new SnuggleInput(input));
50  
51          /* Create an UpConvertingPostProcesor that hooks into the DOM generation
52           * process to do all of the work. We'll use its (sensible) default behaviour
53           * here; options can be passed to this constructor to tweak things.
54           */
55          UpConvertingPostProcessor upConverter = new UpConvertingPostProcessor();
56          
57          /* We're going to create a simple XML String output, which we configure
58           * as follow. Note how we hook the up-conversion into this options Object.
59           */
60          XMLStringOutputOptions xmlStringOutputOptions = new XMLStringOutputOptions();
61          xmlStringOutputOptions.addDOMPostProcessors(upConverter);
62          xmlStringOutputOptions.setIndenting(true);
63          xmlStringOutputOptions.setUsingNamedEntities(true);
64          
65          /* Build up the resulting XML */
66          String result = session.buildXMLString(xmlStringOutputOptions);
67          System.out.println("Up-Conversion process generated: " + result);
68      }
69  }