home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 October / pcp156b.iso / alphawrk / TEXML / TEXML.ZIP / com / ibm / texml / TextML.java < prev    next >
Encoding:
Java Source  |  1999-05-13  |  2.9 KB  |  117 lines

  1. package com.ibm.texml;
  2.  
  3. import org.w3c.dom.*;
  4. import java.io.*;
  5.  
  6. class TextML
  7.   {
  8.   public static final String TAG_Text = "Text";
  9.   public static final String TAG_P = "p";
  10.   public static final String TAG_Line = "br";
  11.  
  12.   private static ProcDOM proc = new ProcDOM();
  13.  
  14.   private static String getValueFromTextChildren(Node n)
  15.     {
  16.     StringBuffer buffer = new StringBuffer();
  17.     proc.processChildren(n, buffer);
  18.     return buffer.toString();
  19.     }
  20.  
  21.   private static final NodeHandler textHandler = new NodeHandler()
  22.     {
  23.     public void processNode(Node n, Object context)
  24.       {
  25.       TextProc txt = (TextProc)context;
  26.       CharacterData chard = (CharacterData)n;
  27.       String value = chard.getData();
  28.       txt.appendText(value);
  29.       }
  30.     };
  31.  
  32.   private static final NodeHandler pHandler = new NodeHandler()
  33.     {
  34.     public void processNode(Node n, Object context)
  35.       {
  36.       TextProc txt = (TextProc)context;
  37.       txt.startSection();
  38.       proc.processChildren(n, txt);
  39.       txt.endSection();
  40.       }
  41.     };
  42.  
  43.   private static final NodeHandler lineHandler = new NodeHandler()
  44.     {
  45.     public void processNode(Node n, Object context)
  46.       {
  47.       TextProc txt = (TextProc)context;
  48.       txt.appendLine();
  49.       }
  50.     };
  51.  
  52.   static
  53.     {
  54.     proc.setRootElementTag(TAG_Text);
  55.     proc.setElementHandler(TAG_Text, proc.processChildren);
  56.     proc.setElementHandler(TAG_P, pHandler);
  57.     proc.setElementHandler(TAG_Line, lineHandler);
  58.     proc.setTextHandler(textHandler);
  59.     }
  60.  
  61.   static void processTextML(TextProc textOut, Document dom)
  62.     {
  63.     proc.processDOM(dom, textOut);
  64.     }
  65.  
  66.   static void processTextML(TextProc textOut, String fileName)
  67.     {
  68.     Document dom = ProcXML.parseXML(fileName);
  69.     if (dom != null)
  70.       {
  71.       processTextML(textOut, dom);
  72.       }
  73.     }
  74.  
  75.   static void processTextML(Writer output, String fileName)
  76.     {
  77.     processTextML(new TextProc(output), fileName);
  78.     }
  79.  
  80.   static void processTextML(String output, String input)
  81.     {
  82.     try
  83.       {
  84.       System.err.println("opening stream "+output);
  85.       OutputStream ostream = new FileOutputStream(output);
  86.       System.err.println("opening writer");
  87.       PrintWriter writer = new PrintWriter(ostream);
  88.       System.err.println("processing "+input);
  89.       processTextML(writer, input);
  90.       writer.close();
  91.       System.err.println("writer closed.");
  92.       ostream.close();
  93.       System.err.println("stream "+output+" closed");
  94.       }
  95.     catch (Exception e)
  96.       {
  97.       System.err.println("Failed to write to output "+output);
  98.       e.printStackTrace(System.err);
  99.       }
  100.     }
  101.  
  102.   /*
  103.   Unit test
  104.   */
  105.   public static void main (String[] args)
  106.     {
  107.     if (args.length < 2)
  108.       {
  109.       System.err.println("Usage: TextML <input> <output>");
  110.       }
  111.     else
  112.       {
  113.       processTextML(args[1], args[0]);
  114.       }
  115.     }
  116.   }
  117.