home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-05-13 | 2.9 KB | 117 lines |
- package com.ibm.texml;
-
- import org.w3c.dom.*;
- import java.io.*;
-
- class TextML
- {
- public static final String TAG_Text = "Text";
- public static final String TAG_P = "p";
- public static final String TAG_Line = "br";
-
- private static ProcDOM proc = new ProcDOM();
-
- private static String getValueFromTextChildren(Node n)
- {
- StringBuffer buffer = new StringBuffer();
- proc.processChildren(n, buffer);
- return buffer.toString();
- }
-
- private static final NodeHandler textHandler = new NodeHandler()
- {
- public void processNode(Node n, Object context)
- {
- TextProc txt = (TextProc)context;
- CharacterData chard = (CharacterData)n;
- String value = chard.getData();
- txt.appendText(value);
- }
- };
-
- private static final NodeHandler pHandler = new NodeHandler()
- {
- public void processNode(Node n, Object context)
- {
- TextProc txt = (TextProc)context;
- txt.startSection();
- proc.processChildren(n, txt);
- txt.endSection();
- }
- };
-
- private static final NodeHandler lineHandler = new NodeHandler()
- {
- public void processNode(Node n, Object context)
- {
- TextProc txt = (TextProc)context;
- txt.appendLine();
- }
- };
-
- static
- {
- proc.setRootElementTag(TAG_Text);
- proc.setElementHandler(TAG_Text, proc.processChildren);
- proc.setElementHandler(TAG_P, pHandler);
- proc.setElementHandler(TAG_Line, lineHandler);
- proc.setTextHandler(textHandler);
- }
-
- static void processTextML(TextProc textOut, Document dom)
- {
- proc.processDOM(dom, textOut);
- }
-
- static void processTextML(TextProc textOut, String fileName)
- {
- Document dom = ProcXML.parseXML(fileName);
- if (dom != null)
- {
- processTextML(textOut, dom);
- }
- }
-
- static void processTextML(Writer output, String fileName)
- {
- processTextML(new TextProc(output), fileName);
- }
-
- static void processTextML(String output, String input)
- {
- try
- {
- System.err.println("opening stream "+output);
- OutputStream ostream = new FileOutputStream(output);
- System.err.println("opening writer");
- PrintWriter writer = new PrintWriter(ostream);
- System.err.println("processing "+input);
- processTextML(writer, input);
- writer.close();
- System.err.println("writer closed.");
- ostream.close();
- System.err.println("stream "+output+" closed");
- }
- catch (Exception e)
- {
- System.err.println("Failed to write to output "+output);
- e.printStackTrace(System.err);
- }
- }
-
- /*
- Unit test
- */
- public static void main (String[] args)
- {
- if (args.length < 2)
- {
- System.err.println("Usage: TextML <input> <output>");
- }
- else
- {
- processTextML(args[1], args[0]);
- }
- }
- }
-