File: XmlToHtml.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (132 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.jclark.xsl.sax;

import org.xml.sax.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileDescriptor;
import java.net.URL;

public class XmlToHtml {
  static class EmptyAttributeList implements AttributeList {
    public int getLength() { return 0; }
    public String getName(int i) {
      return null;
    }
    public String getType(int i) {
      return null;
    }
    public String getValue(int i) {
      return null;
    }
    public String getValue(String name) {
      return null;
    }
    public String getType(String name) {
      return null;
    }
  }

  static class SingleAttributeList implements AttributeList {
    String name;
    String value;
    SingleAttributeList(String name, String value) {
      this.name = name;
      this.value = value;
    }

    public int getLength() { return 0; }
    public String getName(int i) {
      if (i == 0)
	return name;
      return null;
    }
    public String getType(int i) {
      if (i == 0)
	return "CDATA";
      return null;
    }
    public String getValue(int i) {
      if (i == 0)
	return value;
      return null;
    }
    public String getValue(String name) {
      if (name.equals(this.name))
	return value;
      return null;
    }
    public String getType(String name) {
      if (name.equals(this.name))
	return "CDATA";
      return null;
    }
  }

  public static void main(String[] args) throws IOException {
    if (args.length != 2) {
      System.err.println("xmltohtml xml html");
      System.exit(1);
    }
    Parser parser = createParser();
    HTMLOutputHandler handler = new HTMLOutputHandler();
    handler.init(new FileDestination(args[1]), new SingleAttributeList("encoding", "iso-8859-1"));
    parser.setDocumentHandler(handler);
    try {
      parser.parse(fileInputSource(args[0]));
    }
    catch (SAXException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }
  }

  static Parser createParser() {
    String parserClass = System.getProperty("com.jclark.xsl.sax.parser");
    if (parserClass == null)
      parserClass = System.getProperty("org.xml.sax.parser");
    if (parserClass == null)
      parserClass = "com.jclark.xml.sax.CommentDriver";
    try {
      return (Parser)Class.forName(parserClass).newInstance();
    }
    catch (ClassNotFoundException e) {
      System.err.println(e.toString());
    }
    catch (InstantiationException e) {
      System.err.println(e.toString());
    }
    catch (IllegalAccessException e) {
      System.err.println(e.toString());
    }
    catch (ClassCastException e) {
      System.err.println(parserClass + " is not a SAX driver");
    }
    return null;
  }

  /**
   * Generates an <code>InputSource</code> from a file name.
   */
  static public InputSource fileInputSource(String str) {
    return fileInputSource(new File(str));
  }

  static public InputSource fileInputSource(File file) {
    String path = file.getAbsolutePath();
    String fSep = System.getProperty("file.separator");
    if (fSep != null && fSep.length() == 1)
      path = path.replace(fSep.charAt(0), '/');
    if (path.length() > 0 && path.charAt(0) != '/')
      path = '/' + path;
    try {
      return new InputSource(new URL("file", "", path).toString());
    }
    catch (java.net.MalformedURLException e) {
      /* According to the spec this could only happen if the file
	 protocol were not recognized. */
      throw new Error("unexpected MalformedURLException");
    }
  }

}