001 /* 002 * Copyright (c) 2007 Henri Sivonen 003 * Copyright (c) 2007 Mozilla Foundation 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining a 006 * copy of this software and associated documentation files (the "Software"), 007 * to deal in the Software without restriction, including without limitation 008 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 009 * and/or sell copies of the Software, and to permit persons to whom the 010 * Software is furnished to do so, subject to the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be included in 013 * all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 018 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 020 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 021 * DEALINGS IN THE SOFTWARE. 022 */ 023 024 package nu.validator.htmlparser.tools; 025 026 import java.io.File; 027 import java.io.FileOutputStream; 028 import java.io.IOException; 029 030 import org.xml.sax.SAXException; 031 032 import nu.validator.htmlparser.common.XmlViolationPolicy; 033 import nu.validator.htmlparser.sax.HtmlSerializer; 034 import nu.validator.htmlparser.test.SystemErrErrorHandler; 035 import nu.validator.htmlparser.xom.HtmlBuilder; 036 import nu.xom.Builder; 037 import nu.xom.Document; 038 import nu.xom.Element; 039 import nu.xom.Nodes; 040 import nu.xom.ParsingException; 041 import nu.xom.Serializer; 042 import nu.xom.ValidityException; 043 import nu.xom.converters.SAXConverter; 044 import nu.xom.xslt.XSLException; 045 import nu.xom.xslt.XSLTransform; 046 047 public class XSLT4HTML5XOM { 048 049 private static final String TEMPLATE = "--template="; 050 051 private static final String INPUT_HTML = "--input-html="; 052 053 private static final String INPUT_XML = "--input-xml="; 054 055 private static final String OUTPUT_HTML = "--output-html="; 056 057 private static final String OUTPUT_XML = "--output-xml="; 058 059 /** 060 * @param args 061 * @throws IOException 062 * @throws ParsingException 063 * @throws ValidityException 064 * @throws XSLException 065 * @throws SAXException 066 */ 067 public static void main(String[] args) throws ValidityException, 068 ParsingException, IOException, XSLException, SAXException { 069 if (args.length == 0) { 070 System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]"); 071 System.exit(0); 072 } 073 String template = null; 074 String input = null; 075 boolean inputHtml = false; 076 String output = null; 077 boolean outputHtml = false; 078 for (int i = 0; i < args.length; i++) { 079 String arg = args[i]; 080 if (arg.startsWith(TEMPLATE)) { 081 if (template == null) { 082 template = arg.substring(TEMPLATE.length()); 083 } else { 084 System.err.println("Tried to set template twice."); 085 System.exit(1); 086 } 087 } else if (arg.startsWith(INPUT_HTML)) { 088 if (input == null) { 089 input = arg.substring(INPUT_HTML.length()); 090 inputHtml = true; 091 } else { 092 System.err.println("Tried to set input twice."); 093 System.exit(2); 094 } 095 } else if (arg.startsWith(INPUT_XML)) { 096 if (input == null) { 097 input = arg.substring(INPUT_XML.length()); 098 inputHtml = false; 099 } else { 100 System.err.println("Tried to set input twice."); 101 System.exit(2); 102 } 103 } else if (arg.startsWith(OUTPUT_HTML)) { 104 if (output == null) { 105 output = arg.substring(OUTPUT_HTML.length()); 106 outputHtml = true; 107 } else { 108 System.err.println("Tried to set output twice."); 109 System.exit(3); 110 } 111 } else if (arg.startsWith(OUTPUT_XML)) { 112 if (output == null) { 113 output = arg.substring(OUTPUT_XML.length()); 114 outputHtml = false; 115 } else { 116 System.err.println("Tried to set output twice."); 117 System.exit(3); 118 } 119 } 120 } 121 122 if (template == null) { 123 System.err.println("No template specified."); 124 System.exit(6); 125 } 126 if (input == null) { 127 System.err.println("No input specified."); 128 System.exit(7); 129 } 130 if (output == null) { 131 System.err.println("No output specified."); 132 System.exit(8); 133 } 134 135 Builder builder = new Builder(); 136 137 Document transformationDoc = builder.build(new File(template)); 138 139 XSLTransform transform = new XSLTransform(transformationDoc); 140 141 FileOutputStream outputStream = new FileOutputStream(output); 142 143 Document inputDoc; 144 if (inputHtml) { 145 builder = new HtmlBuilder(XmlViolationPolicy.ALTER_INFOSET); 146 } 147 inputDoc = builder.build(new File(input)); 148 Nodes result = transform.transform(inputDoc); 149 Document outputDoc = new Document((Element) result.get(0)); 150 if (outputHtml) { 151 HtmlSerializer htmlSerializer = new HtmlSerializer(outputStream); 152 SAXConverter converter = new SAXConverter(htmlSerializer); 153 converter.setLexicalHandler(htmlSerializer); 154 converter.convert(outputDoc); 155 } else { 156 Serializer serializer = new Serializer(outputStream); 157 serializer.write(outputDoc); 158 } 159 outputStream.flush(); 160 outputStream.close(); 161 } 162 163 }