File: XSLT4HTML5.java

package info (click to toggle)
libhtml5parser-java 1.4%2Br20250916-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,900 kB
  • sloc: java: 28,661; xml: 144; sh: 136; makefile: 3
file content (237 lines) | stat: -rw-r--r-- 9,664 bytes parent folder | download | duplicates (4)
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
232
233
234
235
236
237
/*
 * Copyright (c) 2007 Henri Sivonen
 * Copyright (c) 2007 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package nu.validator.htmlparser.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;

import nu.validator.htmlparser.common.XmlViolationPolicy;
import nu.validator.htmlparser.dom.HtmlDocumentBuilder;
import nu.validator.htmlparser.sax.HtmlParser;
import nu.validator.htmlparser.sax.HtmlSerializer;
import nu.validator.htmlparser.sax.XmlSerializer;
import nu.validator.htmlparser.test.SystemErrErrorHandler;

import org.w3c.dom.Document;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;

public class XSLT4HTML5 {

    private enum Mode {
        STREAMING_SAX, BUFFERED_SAX, DOM,
    }

    private static final String TEMPLATE = "--template=";

    private static final String INPUT_HTML = "--input-html=";

    private static final String INPUT_XML = "--input-xml=";

    private static final String OUTPUT_HTML = "--output-html=";

    private static final String OUTPUT_XML = "--output-xml=";

    private static final String MODE = "--mode=";

    /**
     * @param args
     * @throws ParserConfigurationException 
     * @throws SAXException 
     * @throws IOException 
     * @throws MalformedURLException 
     * @throws TransformerException 
     */
    public static void main(String[] args) throws SAXException,
            ParserConfigurationException, MalformedURLException, IOException, TransformerException {
        if (args.length == 0) {
            System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]");
            System.exit(0);
        }
        String template = null;
        String input = null;
        boolean inputHtml = false;
        String output = null;
        boolean outputHtml = false;
        Mode mode = null;
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg.startsWith(TEMPLATE)) {
                if (template == null) {
                    template = arg.substring(TEMPLATE.length());
                } else {
                    System.err.println("Tried to set template twice.");
                    System.exit(1);
                }
            } else if (arg.startsWith(INPUT_HTML)) {
                if (input == null) {
                    input = arg.substring(INPUT_HTML.length());
                    inputHtml = true;
                } else {
                    System.err.println("Tried to set input twice.");
                    System.exit(2);
                }
            } else if (arg.startsWith(INPUT_XML)) {
                if (input == null) {
                    input = arg.substring(INPUT_XML.length());
                    inputHtml = false;
                } else {
                    System.err.println("Tried to set input twice.");
                    System.exit(2);
                }
            } else if (arg.startsWith(OUTPUT_HTML)) {
                if (output == null) {
                    output = arg.substring(OUTPUT_HTML.length());
                    outputHtml = true;
                } else {
                    System.err.println("Tried to set output twice.");
                    System.exit(3);
                }
            } else if (arg.startsWith(OUTPUT_XML)) {
                if (output == null) {
                    output = arg.substring(OUTPUT_XML.length());
                    outputHtml = false;
                } else {
                    System.err.println("Tried to set output twice.");
                    System.exit(3);
                }
            } else if (arg.startsWith(MODE)) {
                if (mode == null) {
                    String modeStr = arg.substring(MODE.length());
                    if ("dom".equals(modeStr)) {
                        mode = Mode.DOM;
                    } else if ("sax-buffered".equals(modeStr)) {
                        mode = Mode.BUFFERED_SAX;
                    } else if ("sax-streaming".equals(modeStr)) {
                        mode = Mode.STREAMING_SAX;
                    } else {
                        System.err.println("Unrecognized mode.");
                        System.exit(5);
                    }
                } else {
                    System.err.println("Tried to set mode twice.");
                    System.exit(4);
                }
            }
        }

        if (template == null) {
            System.err.println("No template specified.");
            System.exit(6);
        }
        if (input == null) {
            System.err.println("No input specified.");
            System.exit(7);
        }
        if (output == null) {
            System.err.println("No output specified.");
            System.exit(8);
        }
        if (mode == null) {
            mode = Mode.BUFFERED_SAX;
        }
        
        SystemErrErrorHandler errorHandler = new SystemErrErrorHandler();

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        XMLReader reader = factory.newSAXParser().getXMLReader();
        reader.setErrorHandler(errorHandler);

        SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
        transformerFactory.setErrorListener(errorHandler);
        TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler();
        reader.setContentHandler(templatesHandler);
        reader.parse(new File(template).toURI().toASCIIString());

        Templates templates = templatesHandler.getTemplates();

        FileOutputStream outputStream = new FileOutputStream(output);
        ContentHandler serializer;
        if (outputHtml) {
            serializer = new HtmlSerializer(outputStream);
        } else {
            serializer = new XmlSerializer(outputStream);
        }
        SAXResult result = new SAXResult(new XmlnsDropper(serializer));
        result.setLexicalHandler((LexicalHandler) serializer);

        if (mode == Mode.DOM) {
            Document inputDoc;
            DocumentBuilder builder;
            if (inputHtml) {
                builder = new HtmlDocumentBuilder(XmlViolationPolicy.ALTER_INFOSET);
            } else {
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(true);
                try {
                    builder = builderFactory.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    throw new RuntimeException(e);
                }
            }
            inputDoc = builder.parse(new File(input));
            DOMSource inputSource = new DOMSource(inputDoc,
                    new File(input).toURI().toASCIIString());
            Transformer transformer = templates.newTransformer();
            transformer.setErrorListener(errorHandler);
            transformer.transform(inputSource, result);
        } else {
            if (inputHtml) {
                reader = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET);
                if (mode == Mode.STREAMING_SAX) {
                    reader.setProperty("http://validator.nu/properties/streamability-violation-policy", XmlViolationPolicy.FATAL);
                }
            }
            TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(templates);
            transformerHandler.setResult(result);
            reader.setErrorHandler(errorHandler);
            reader.setContentHandler(transformerHandler);
            reader.setProperty("http://xml.org/sax/properties/lexical-handler", transformerHandler);
            reader.parse(new File(input).toURI().toASCIIString());
        }
        outputStream.flush();
        outputStream.close();
    }

}