summaryrefslogtreecommitdiff
path: root/Build/source/texk/tex4htk/xv4ht.java
blob: 3b76bc06ff0e00b9c204ef2bd71637ba2a03c734 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**********************************************************/
/* xv4ht.java                            2006-07-03-21:41 */
/* Copyright (C) 2005--2006    Eitan M. Gurari            */
/*                                                        */
/* This work may be distributed and/or modified under the */
/* conditions of the LaTeX Project Public License, either */
/* version 1.3 of this license or (at your option) any    */
/* later version. The latest version of this license is   */
/* in                                                     */
/*   http://www.latex-project.org/lppl.txt                */
/* and version 1.3 or later is part of all distributions  */
/* of LaTeX version 2003/12/01 or later.                  */
/*                                                        */
/* This work has the LPPL maintenance status "maintained".*/
/*                                                        */
/* This Current Maintainer of this work                   */
/* is Eitan M. Gurari.                                    */
/*                                                        */
/* If you modify this program your changing its signature */
/* with a directive of the following form will be         */
/* appreciated.                                           */
/*      #define PLATFORM "signature"                      */
/*                                                        */
/*                             gurari@cse.ohio-state.edu  */
/*                 http://www.cse.ohio-state.edu/~gurari  */
/**********************************************************/

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

class Xv4htContentHandler extends DefaultHandler {

   String catalog;
   boolean withinDTD = false;
   Xv4htErrorHandler errHandler;
   Xv4htLexicalHandler lexicalHandler;

   public Xv4htContentHandler(XMLReader xmlReader, String catalog){
     super();
     this.catalog = catalog;
     lexicalHandler = new Xv4htLexicalHandler();
try {
  xmlReader.setProperty(
     "http://xml.org/sax/properties/lexical-handler",
     lexicalHandler
  );
}
catch (SAXNotRecognizedException e) {
  System.err.println(
   "err 1");
  return;
}
catch (SAXNotSupportedException e) {
  System.err.println(
   "err 2");
  return;
}

     errHandler = new Xv4htErrorHandler();
xmlReader.setErrorHandler( errHandler );

     xmlReader.setEntityResolver( new Xv4htEntityResolver() );

   }

   public void startPrefixMapping(String prefix, String uri){
   }

   public void endPrefixMapping(String prefix)  {
   }

   public void ignorableWhitespace(
                           char[] ch, int start, int length) {
   }

   public void notationDecl(
             String name, String publicId, String systemId) {
   }
   public class Xv4htEntityResolver implements EntityResolver {

  private Hashtable entities = new Hashtable();

  public Xv4htEntityResolver() {
     try{
      FileReader myIn  = new FileReader( catalog );
      while( true ){
        int c;
        String publicID = "";
        String systemID = "";

        while( (c = myIn.read()) != '\"' ){
           if( c == -1 ){ break; }
        }
        if( c == -1 ){ break; }
        while( (c = myIn.read()) != '\"' ){
           publicID += (char) c;
           if( c == -1 ){ break; }
        }
        if( c == -1 ){ break; }

        while( (c = myIn.read()) != '\"' ){
           if( c == -1 ){ break; }
        }
        if( c == -1 ){ break; }
        while( (c = myIn.read()) != '\"' ){
           systemID += (char) c;
           if( c == -1 ){ break; }
        }
        if( c == -1 ){ break; }

        entities.put(publicID, systemID);
      }
} catch(java.io.IOException e){   }

  }

  public InputSource resolveEntity(String publicID,
   String systemID) throws SAXException {
     if( publicID== null ){ return null; }
     if( entities.containsKey(publicID) ){
       String url = (String) entities.get(publicID);
       InputSource local = new InputSource(url);
       return local;
     }
     return null;
  }

}

   class Xv4htLexicalHandler implements LexicalHandler {
  public void startDTD(String name, String publicId, String systemId)
   throws SAXException {
     withinDTD = true;
     errHandler.errorOff();
  }

  public void endDTD() throws SAXException {
     withinDTD = false;
     errHandler.errorOn();
  }
public void comment (char[] text, int start, int length)
   throws SAXException {
}
  public void startEntity(String name) throws SAXException {}
  public void endEntity(String name) throws SAXException {}
  public void startCDATA() throws SAXException {}
  public void endCDATA() throws SAXException {}
}

   
}

class Xv4htErrorHandler extends DefaultHandler {
  private boolean errOn = true;

  public void warning(SAXParseException e) {
    showError( e, "Warning" );
  }

  public void error(SAXParseException e) {
    showError( e, "Warning" );
  }

  public void fatalError(SAXParseException e) {
    showError( e, "Warning" );
  }

  public void errorOff() {
     errOn = false;
  }

  public void errorOn() {
     errOn = true;
  }

  private void showError(SAXParseException e, String m){
    if( errOn ){
      int c = e.getColumnNumber();
      System.out.println(
        m + ": " + e.getSystemId() + " " + e.getLineNumber()
        + ((c==-1)? "" : ("," + c))
        + ": " + e.getMessage()
      );
  } }
}

public class xv4ht{
  static public void main (String[] args)
     throws SAXException,
            ParserConfigurationException,
            IOException
  {
     SAXParserFactory factory =
         SAXParserFactory.newInstance();
     factory.setValidating( true );
     factory.setNamespaceAware( true );
     SAXParser saxParser = factory.newSAXParser();
     XMLReader xmlReader = saxParser.getXMLReader();
     if( args.length != 2 ){
  System.out.print(
       "--- A command line of the following form is required ---"
     + "\n      java [-cp <path>] xv4ht <filename> <catalog>"
     + "\nreceived:\n      java"
  );
  for(int i=0; i<args.length; i++ ){
     System.out.print( " " + args[i] );
  }
  System.out.println();
  System.exit( 1 );
}

     xmlReader.setContentHandler( new Xv4htContentHandler(xmlReader, args[1])
 );
     xmlReader.parse ( new File(args[0]).toURL().toString() );
  }
}