home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 October / pcp156b.iso / alphawrk / TEXML / TEXML.ZIP / com / ibm / texml / TeXMLatte.java < prev    next >
Encoding:
Java Source  |  1999-06-03  |  3.9 KB  |  141 lines

  1. /*
  2.  * (C) Copyright IBM Corp. 1998  All rights reserved.
  3.  *
  4.  * US Government Users Restricted Rights Use, duplication or
  5.  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6.  *
  7.  * The program is provided "as is" without any warranty express or
  8.  * implied, including the warranty of non-infringement and the implied
  9.  * warranties of merchantibility and fitness for a particular purpose.
  10.  * IBM will not be liable for any damages suffered by you as a result
  11.  * of using the Program. In no event will IBM be liable for any
  12.  * special, indirect or consequential damages or lost profits even if
  13.  * IBM has been advised of the possibility of their occurrence. IBM
  14.  * will not be liable for any third party claims against you.
  15.  */
  16.  
  17. package com.ibm.texml;
  18.  
  19. import java.io.*;
  20. import com.ibm.xml.parsers.*;
  21. import org.w3c.dom.*;
  22. import org.xml.sax.*;
  23.  
  24. /**
  25. Provides static methods for reading an XML file that conforms to
  26. TeXML.dtd and outputting TeX.
  27. Provides a main method for doing the same.
  28. */
  29. public class TeXMLatte
  30.   {
  31.   /**
  32.   Invoke the XML parser on the file.
  33.   @param texML file handle of TeXML source.
  34.   @return the Document node of the DOM.
  35.   */
  36.   public static Document parseTeXML(File texML)
  37.   throws SAXException, IOException
  38.     {
  39.     NonValidatingDOMParser texParser = new NonValidatingDOMParser();
  40.     texParser.setExpandEntityReferences(true);
  41.     texParser.parse(texML.getAbsolutePath());
  42.     Document texParse = texParser.getDocument();
  43.     return texParse;
  44.     }
  45.  
  46.   /**
  47.   Read TeXML input file, output TeX.
  48.   @param texML file handle for TeXML input file
  49.   @param tex file handle for TeX output file
  50.   */
  51.   public static void processTeXML(File texML, OutputStream tex)
  52.     {
  53.     try
  54.       {
  55.       Document texParse = parseTeXML(texML);
  56.       TeXML latte = new TeXML();
  57.       latte.processTeXML(texParse, tex);
  58.       }
  59.     catch (SAXException e)
  60.       {
  61.       System.out.println("Failed to parse file, "+texML);
  62.       }
  63.     catch (IOException e)
  64.       {
  65.       System.out.println("Failed to read file, "+texML);
  66.       }
  67.     }
  68.  
  69.   /**
  70.   Read TeXML input file, output TeX.
  71.   @param texML file handle for TeXML input file
  72.   @param tex file handle for TeX output file
  73.   */
  74.   public static void processTeXML(File texML, File tex)
  75.     {
  76.     try
  77.       {
  78.       FileOutputStream ostream = new FileOutputStream(tex);
  79.       processTeXML(texML, ostream);
  80.       ostream.close();
  81.       }
  82.     catch (IOException e)
  83.       {
  84.       System.out.println("Output problem with file, "+tex+" is "+e);
  85.       }
  86.     }
  87.  
  88.   /**
  89.   Read TeXML input file, output TeX.
  90.   @param texML name of TeXML input file
  91.   @param tex name of TeX output file
  92.   */
  93.   public static void processTeXML(String texML, String tex)
  94.     {
  95.     File ftexML = new File(texML);
  96.     File ftex = new File(tex);
  97.     if (ftex.equals(ftexML))
  98.       {
  99.       System.out.println("Error: Input and output file must be different.");
  100.       }
  101.     else
  102.       {
  103.       processTeXML(new File(texML), new File(tex));
  104.       }
  105.     }
  106.  
  107.   private static void usage()
  108.     {
  109.     System.out.println("supply the TeXML source file name.");
  110.     System.out.println("Follow that with the TeX destination file name.");
  111.     System.out.println("If the second parameter is omitted, output will");
  112.     System.out.println("  occur on stdout.");
  113.     }
  114.  
  115.   /**
  116.   Main program driver which reads an XML file that conforms to
  117.   TeXML.dtd and outputs TeX.
  118.  
  119.   @param args First element is the TeXML source.  Second is the output file
  120.   name.  Output will go to System.out if there is no second element.
  121.   */
  122.   public static void main (String[] args)
  123.     {
  124.     if (args.length < 1)
  125.       {
  126.       usage();
  127.       }
  128.     else
  129.       {
  130.       if (args.length == 1)
  131.         {
  132.         processTeXML(new File(args[0]), System.out);
  133.         }
  134.       else
  135.         {
  136.         processTeXML(args[0], args[1]);
  137.         }
  138.       }
  139.     }
  140.   }
  141.