home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-06-03 | 3.9 KB | 141 lines |
- /*
- * (C) Copyright IBM Corp. 1998 All rights reserved.
- *
- * US Government Users Restricted Rights Use, duplication or
- * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *
- * The program is provided "as is" without any warranty express or
- * implied, including the warranty of non-infringement and the implied
- * warranties of merchantibility and fitness for a particular purpose.
- * IBM will not be liable for any damages suffered by you as a result
- * of using the Program. In no event will IBM be liable for any
- * special, indirect or consequential damages or lost profits even if
- * IBM has been advised of the possibility of their occurrence. IBM
- * will not be liable for any third party claims against you.
- */
-
- package com.ibm.texml;
-
- import java.io.*;
- import com.ibm.xml.parsers.*;
- import org.w3c.dom.*;
- import org.xml.sax.*;
-
- /**
- Provides static methods for reading an XML file that conforms to
- TeXML.dtd and outputting TeX.
- Provides a main method for doing the same.
- */
- public class TeXMLatte
- {
- /**
- Invoke the XML parser on the file.
- @param texML file handle of TeXML source.
- @return the Document node of the DOM.
- */
- public static Document parseTeXML(File texML)
- throws SAXException, IOException
- {
- NonValidatingDOMParser texParser = new NonValidatingDOMParser();
- texParser.setExpandEntityReferences(true);
- texParser.parse(texML.getAbsolutePath());
- Document texParse = texParser.getDocument();
- return texParse;
- }
-
- /**
- Read TeXML input file, output TeX.
- @param texML file handle for TeXML input file
- @param tex file handle for TeX output file
- */
- public static void processTeXML(File texML, OutputStream tex)
- {
- try
- {
- Document texParse = parseTeXML(texML);
- TeXML latte = new TeXML();
- latte.processTeXML(texParse, tex);
- }
- catch (SAXException e)
- {
- System.out.println("Failed to parse file, "+texML);
- }
- catch (IOException e)
- {
- System.out.println("Failed to read file, "+texML);
- }
- }
-
- /**
- Read TeXML input file, output TeX.
- @param texML file handle for TeXML input file
- @param tex file handle for TeX output file
- */
- public static void processTeXML(File texML, File tex)
- {
- try
- {
- FileOutputStream ostream = new FileOutputStream(tex);
- processTeXML(texML, ostream);
- ostream.close();
- }
- catch (IOException e)
- {
- System.out.println("Output problem with file, "+tex+" is "+e);
- }
- }
-
- /**
- Read TeXML input file, output TeX.
- @param texML name of TeXML input file
- @param tex name of TeX output file
- */
- public static void processTeXML(String texML, String tex)
- {
- File ftexML = new File(texML);
- File ftex = new File(tex);
- if (ftex.equals(ftexML))
- {
- System.out.println("Error: Input and output file must be different.");
- }
- else
- {
- processTeXML(new File(texML), new File(tex));
- }
- }
-
- private static void usage()
- {
- System.out.println("supply the TeXML source file name.");
- System.out.println("Follow that with the TeX destination file name.");
- System.out.println("If the second parameter is omitted, output will");
- System.out.println(" occur on stdout.");
- }
-
- /**
- Main program driver which reads an XML file that conforms to
- TeXML.dtd and outputs TeX.
-
- @param args First element is the TeXML source. Second is the output file
- name. Output will go to System.out if there is no second element.
- */
- public static void main (String[] args)
- {
- if (args.length < 1)
- {
- usage();
- }
- else
- {
- if (args.length == 1)
- {
- processTeXML(new File(args[0]), System.out);
- }
- else
- {
- processTeXML(args[0], args[1]);
- }
- }
- }
- }
-