Debian Codegraph @

texk/tex4htk/xv4ht.java

raw
1 /**********************************************************/
2 /* xv4ht.java 2006-07-03-21:41 */
3 /* Copyright (C) 2005--2006 Eitan M. Gurari */
4 /* */
5 /* This work may be distributed and/or modified under the */
6 /* conditions of the LaTeX Project Public License, either */
7 /* version 1.3 of this license or (at your option) any */
8 /* later version. The latest version of this license is */
9 /* in */
10 /* http://www.latex-project.org/lppl.txt */
11 /* and version 1.3 or later is part of all distributions */
12 /* of LaTeX version 2003/12/01 or later. */
13 /* */
14 /* This work has the LPPL maintenance status "maintained".*/
15 /* */
16 /* This Current Maintainer of this work */
17 /* is Eitan M. Gurari. */
18 /* */
19 /* If you modify this program your changing its signature */
20 /* with a directive of the following form will be */
21 /* appreciated. */
22 /* #define PLATFORM "signature" */
23 /* */
24 /* [email protected] */
25 /* http://www.cse.ohio-state.edu/~gurari */
26 /**********************************************************/
27
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.Hashtable;
32 import javax.xml.parsers.ParserConfigurationException;
33 import javax.xml.parsers.SAXParser;
34 import javax.xml.parsers.SAXParserFactory;
35 import org.xml.sax.EntityResolver;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.SAXNotRecognizedException;
39 import org.xml.sax.SAXNotSupportedException;
40 import org.xml.sax.SAXParseException;
41 import org.xml.sax.XMLReader;
42 import org.xml.sax.ext.LexicalHandler;
43 import org.xml.sax.helpers.DefaultHandler;
44
45 class Xv4htContentHandler extends DefaultHandler {
46
47 String catalog;
48 boolean withinDTD = false;
49 Xv4htErrorHandler errHandler;
50 Xv4htLexicalHandler lexicalHandler;
51
52 public Xv4htContentHandler(XMLReader xmlReader, String catalog){
53 super();
54 this.catalog = catalog;
55 lexicalHandler = new Xv4htLexicalHandler();
56 try {
57 xmlReader.setProperty(
58 "http://xml.org/sax/properties/lexical-handler",
59 lexicalHandler
60 );
61 }
62 catch (SAXNotRecognizedException e) {
63 System.err.println(
64 "err 1");
65 return;
66 }
67 catch (SAXNotSupportedException e) {
68 System.err.println(
69 "err 2");
70 return;
71 }
72
73 errHandler = new Xv4htErrorHandler();
74 xmlReader.setErrorHandler( errHandler );
75
76 xmlReader.setEntityResolver( new Xv4htEntityResolver() );
77
78 }
79
80 public void startPrefixMapping(String prefix, String uri){
81 }
82
83 public void endPrefixMapping(String prefix) {
84 }
85
86 public void ignorableWhitespace(
87 char[] ch, int start, int length) {
88 }
89
90 public void notationDecl(
91 String name, String publicId, String systemId) {
92 }
93 public class Xv4htEntityResolver implements EntityResolver {
94
95 private Hashtable entities = new Hashtable();
96
97 public Xv4htEntityResolver() {
98 try{
99 FileReader myIn = new FileReader( catalog );
100 while( true ){
101 int c;
102 String publicID = "";
103 String systemID = "";
104
105 while( (c = myIn.read()) != '\"' ){
106 if( c == -1 ){ break; }
107 }
108 if( c == -1 ){ break; }
109 while( (c = myIn.read()) != '\"' ){
110 publicID += (char) c;
111 if( c == -1 ){ break; }
112 }
113 if( c == -1 ){ break; }
114
115 while( (c = myIn.read()) != '\"' ){
116 if( c == -1 ){ break; }
117 }
118 if( c == -1 ){ break; }
119 while( (c = myIn.read()) != '\"' ){
120 systemID += (char) c;
121 if( c == -1 ){ break; }
122 }
123 if( c == -1 ){ break; }
124
125 entities.put(publicID, systemID);
126 }
127 } catch(java.io.IOException e){ }
128
129 }
130
131 public InputSource resolveEntity(String publicID,
132 String systemID) throws SAXException {
133 if( publicID== null ){ return null; }
134 if( entities.containsKey(publicID) ){
135 String url = (String) entities.get(publicID);
136 InputSource local = new InputSource(url);
137 return local;
138 }
139 return null;
140 }
141
142 }
143
144 class Xv4htLexicalHandler implements LexicalHandler {
145 public void startDTD(String name, String publicId, String systemId)
146 throws SAXException {
147 withinDTD = true;
148 errHandler.errorOff();
149 }
150
151 public void endDTD() throws SAXException {
152 withinDTD = false;
153 errHandler.errorOn();
154 }
155 public void comment (char[] text, int start, int length)
156 throws SAXException {
157 }
158 public void startEntity(String name) throws SAXException {}
159 public void endEntity(String name) throws SAXException {}
160 public void startCDATA() throws SAXException {}
161 public void endCDATA() throws SAXException {}
162 }
163
164
165 }
166
167 class Xv4htErrorHandler extends DefaultHandler {
168 private boolean errOn = true;
169
170 public void warning(SAXParseException e) {
171 showError( e, "Warning" );
172 }
173
174 public void error(SAXParseException e) {
175 showError( e, "Warning" );
176 }
177
178 public void fatalError(SAXParseException e) {
179 showError( e, "Warning" );
180 }
181
182 public void errorOff() {
183 errOn = false;
184 }
185
186 public void errorOn() {
187 errOn = true;
188 }
189
190 private void showError(SAXParseException e, String m){
191 if( errOn ){
192 int c = e.getColumnNumber();
193 System.out.println(
194 m + ": " + e.getSystemId() + " " + e.getLineNumber()
195 + ((c==-1)? "" : ("," + c))
196 + ": " + e.getMessage()
197 );
198 } }
199 }
200
201 public class xv4ht{
202 static public void main (String[] args)
203 throws SAXException,
204 ParserConfigurationException,
205 IOException
206 {
207 SAXParserFactory factory =
208 SAXParserFactory.newInstance();
209 factory.setValidating( true );
210 factory.setNamespaceAware( true );
211 SAXParser saxParser = factory.newSAXParser();
212 XMLReader xmlReader = saxParser.getXMLReader();
213 if( args.length != 2 ){
214 System.out.print(
215 "--- A command line of the following form is required ---"
216 + "\n java [-cp <path>] xv4ht <filename> <catalog>"
217 + "\nreceived:\n java"
218 );
219 for(int i=0; i<args.length; i++ ){
220 System.out.print( " " + args[i] );
221 }
222 System.out.println();
223 System.exit( 1 );
224 }
225
226 xmlReader.setContentHandler( new Xv4htContentHandler(xmlReader, args[1])
227 );
228 xmlReader.parse ( new File(args[0]).toURL().toString() );
229 }
230 }
231