|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (c) 2017-2021, Carlos Amengual. |
| 4 | +
|
| 5 | + SPDX-License-Identifier: BSD-3-Clause |
| 6 | +
|
| 7 | + Licensed under a BSD-style License. You can find the license here: |
| 8 | + https://css4j.github.io/LICENSE.txt |
| 9 | +
|
| 10 | + */ |
| 11 | + |
| 12 | +package io.sf.carte.example; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.Reader; |
| 19 | +import java.io.StringReader; |
| 20 | +import java.io.StringWriter; |
| 21 | + |
| 22 | +import javax.xml.parsers.DocumentBuilder; |
| 23 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 24 | +import javax.xml.transform.OutputKeys; |
| 25 | +import javax.xml.transform.Transformer; |
| 26 | +import javax.xml.transform.TransformerFactory; |
| 27 | +import javax.xml.transform.dom.DOMSource; |
| 28 | +import javax.xml.transform.stream.StreamResult; |
| 29 | + |
| 30 | +import org.dom4j.io.HTMLWriter; |
| 31 | +import org.dom4j.io.OutputFormat; |
| 32 | +import org.dom4j.io.SAXReader; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Test; |
| 35 | +import org.w3c.dom.Document; |
| 36 | +import org.xml.sax.ErrorHandler; |
| 37 | +import org.xml.sax.InputSource; |
| 38 | +import org.xml.sax.helpers.DefaultHandler; |
| 39 | + |
| 40 | +import io.sf.carte.doc.dom.CSSDOMImplementation; |
| 41 | +import io.sf.carte.doc.dom.HTMLDocument; |
| 42 | +import io.sf.carte.doc.dom.XMLDocumentBuilder; |
| 43 | +import io.sf.carte.doc.dom4j.XHTMLDocument; |
| 44 | +import io.sf.carte.doc.dom4j.XHTMLDocumentFactory; |
| 45 | +import io.sf.carte.doc.style.css.CSSDocument; |
| 46 | +import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet; |
| 47 | +import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheetFactory; |
| 48 | +import io.sf.carte.doc.style.css.om.DOMCSSStyleSheetFactory; |
| 49 | +import io.sf.carte.doc.xml.dtd.DefaultEntityResolver; |
| 50 | +import nu.validator.htmlparser.common.XmlViolationPolicy; |
| 51 | +import nu.validator.htmlparser.sax.HtmlParser; |
| 52 | + |
| 53 | +/** |
| 54 | + * The purpose of this test is to verify that the css4j examples in the website |
| 55 | + * can be run with the given version of Java, its binaries being correctly |
| 56 | + * downloaded from its repository. |
| 57 | + */ |
| 58 | +public class Css4jTest { |
| 59 | + |
| 60 | + private static final String htmlString1 = "<html><head><title>Example</title><style>div>p{font-size:14pt}</style></head><body><div><p>Hi</p></div></body></html>"; |
| 61 | + |
| 62 | + private static final String xmlString1 = "<html><head><title>Example</title><style><![CDATA[div>p{font-size:14pt}]]></style></head><body><div><p>Hi</p></div></body></html>"; |
| 63 | + |
| 64 | + /* |
| 65 | + * DOM4J does not serialize the STYLE element as raw, nor is |
| 66 | + * being appropriately transformed by the Transformer. |
| 67 | + */ |
| 68 | + private static final String dom4jXmlString1 = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Example</title><style>div>p{font-size:14pt}</style></head><body><div><p>Hi</p></div></body></html>"; |
| 69 | + |
| 70 | + private static final String sheet1 = "html{font-size:12pt}p{font-size:10pt}"; |
| 71 | + |
| 72 | + private static Transformer transformer; |
| 73 | + |
| 74 | + @BeforeClass |
| 75 | + public static void setUpBeforeClass() throws Exception { |
| 76 | + TransformerFactory tf = TransformerFactory.newInstance(); |
| 77 | + transformer = tf.newTransformer(); |
| 78 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 79 | + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
| 80 | + transformer.setOutputProperty(OutputKeys.INDENT, "no"); |
| 81 | + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
| 82 | + transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "script style"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testUsageNativeDOM() throws Exception { |
| 87 | + // Instantiate DOM implementation (with default settings: no IE hacks accepted) |
| 88 | + // and configure it |
| 89 | + CSSDOMImplementation impl = new CSSDOMImplementation(); |
| 90 | + // Alternatively, impl = new CSSDOMImplementation(flags); |
| 91 | + // Now load default HTML user agent sheets |
| 92 | + impl.setDefaultHTMLUserAgentSheet(); |
| 93 | + // Prepare parser |
| 94 | + HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); |
| 95 | + parser.setCommentPolicy(XmlViolationPolicy.ALLOW); |
| 96 | + parser.setXmlnsPolicy(XmlViolationPolicy.ALLOW); |
| 97 | + // Prepare builder |
| 98 | + XMLDocumentBuilder builder = new XMLDocumentBuilder(impl); |
| 99 | + builder.setHTMLProcessing(true); |
| 100 | + builder.setXMLReader(parser); |
| 101 | + |
| 102 | + // Read the document to parse, and prepare source object |
| 103 | + Reader re = new StringReader(htmlString1); |
| 104 | + InputSource source = new InputSource(re); |
| 105 | + // Parse. If the document is not HTML, you want to use DOMDocument instead |
| 106 | + HTMLDocument document = (HTMLDocument) builder.parse(source); |
| 107 | + re.close(); |
| 108 | + |
| 109 | + // Set document URI |
| 110 | + document.setDocumentURI("http://www.example.com/mydocument.html"); |
| 111 | + |
| 112 | + assertEquals(htmlString1, document.toString()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testUsageDOMWrapper() throws Exception { |
| 117 | + DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 118 | + docbuilder.setEntityResolver(new DefaultEntityResolver()); |
| 119 | + |
| 120 | + // Read the document to parse, and prepare source object |
| 121 | + Reader re = new StringReader(htmlString1); |
| 122 | + InputSource source = new InputSource(re); |
| 123 | + // Parse |
| 124 | + Document otherDOMdocument = docbuilder.parse(source); |
| 125 | + re.close(); |
| 126 | + |
| 127 | + // Set document URI |
| 128 | + otherDOMdocument.setDocumentURI("http://www.example.com/mydocument.html"); |
| 129 | + |
| 130 | + StringWriter sw = new StringWriter(xmlString1.length()); |
| 131 | + transformer.transform(new DOMSource(otherDOMdocument), new StreamResult(sw)); |
| 132 | + |
| 133 | + assertEquals(xmlString1, sw.toString()); |
| 134 | + |
| 135 | + DOMCSSStyleSheetFactory cssFactory = new DOMCSSStyleSheetFactory(); |
| 136 | + CSSDocument document = cssFactory.createCSSDocument(otherDOMdocument); |
| 137 | + |
| 138 | + sw = new StringWriter(xmlString1.length()); |
| 139 | + transformer.transform(new DOMSource(document), new StreamResult(sw)); |
| 140 | + |
| 141 | + assertEquals(xmlString1, sw.toString()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testUsageDOM4J() throws Exception { |
| 146 | + XHTMLDocumentFactory factory = XHTMLDocumentFactory.getInstance(); |
| 147 | + // Next line is optional: default is TRUE, and is probably what you want |
| 148 | + // factory.getStyleSheetFactory().setLenientSystemValues(false); |
| 149 | + |
| 150 | + // Prepare parser |
| 151 | + HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); |
| 152 | + parser.setCommentPolicy(XmlViolationPolicy.ALLOW); |
| 153 | + parser.setXmlnsPolicy(XmlViolationPolicy.ALLOW); |
| 154 | + // Configure the SAXReader with the HtmlParser |
| 155 | + SAXReader builder = new SAXReader(factory); |
| 156 | + builder.setXMLReader(parser); |
| 157 | + // Provide an error handler to avoid exceptions |
| 158 | + ErrorHandler errorHandler = new DefaultHandler(); |
| 159 | + builder.setErrorHandler(errorHandler); |
| 160 | + // We do not set the EntityResolver, the HtmlParser does not need it |
| 161 | + |
| 162 | + // Read the document to parse |
| 163 | + Reader re = new StringReader(htmlString1); |
| 164 | + XHTMLDocument document = (XHTMLDocument) builder.read(re); |
| 165 | + re.close(); |
| 166 | + |
| 167 | + // Set document URI |
| 168 | + document.setDocumentURI("http://www.example.com/mydocument.html"); |
| 169 | + |
| 170 | + String s = serializeDOM4J(document); |
| 171 | + |
| 172 | + assertEquals(dom4jXmlString1, s); |
| 173 | + |
| 174 | + StringWriter sw = new StringWriter(xmlString1.length()); |
| 175 | + transformer.transform(new DOMSource(document), new StreamResult(sw)); |
| 176 | + |
| 177 | + assertEquals(dom4jXmlString1, sw.toString()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testUsageDOM4J_XMLBuilder() throws Exception { |
| 182 | + XHTMLDocumentFactory factory = XHTMLDocumentFactory.getInstance(); |
| 183 | + // Next line is optional: default is TRUE, and is probably what you want |
| 184 | + // factory.getStyleSheetFactory().setLenientSystemValues(false); |
| 185 | + |
| 186 | + // Prepare parser |
| 187 | + HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); |
| 188 | + parser.setCommentPolicy(XmlViolationPolicy.ALLOW); |
| 189 | + parser.setXmlnsPolicy(XmlViolationPolicy.ALLOW); |
| 190 | + XMLDocumentBuilder builder = new XMLDocumentBuilder(factory); |
| 191 | + builder.setHTMLProcessing(true); |
| 192 | + builder.setXMLReader(parser); |
| 193 | + // We do not set the EntityResolver, the HtmlParser does not need it |
| 194 | + |
| 195 | + // Read the document to parse, and prepare source object |
| 196 | + Reader re = new StringReader(htmlString1); |
| 197 | + InputSource source = new InputSource(re); |
| 198 | + XHTMLDocument document = (XHTMLDocument) builder.parse(source); |
| 199 | + re.close(); |
| 200 | + |
| 201 | + // Set document URI |
| 202 | + document.setDocumentURI("http://www.example.com/mydocument.html"); |
| 203 | + |
| 204 | + String s = serializeDOM4J(document); |
| 205 | + |
| 206 | + assertEquals(dom4jXmlString1, s); |
| 207 | + } |
| 208 | + |
| 209 | + private String serializeDOM4J(XHTMLDocument document) throws IOException { |
| 210 | + OutputFormat format = OutputFormat.createPrettyPrint(); |
| 211 | + format.setIndent(false); |
| 212 | + format.setNewlines(false); |
| 213 | + format.setNewLineAfterDeclaration(false); |
| 214 | + format.setXHTML(true); |
| 215 | + StringWriter sw = new StringWriter(dom4jXmlString1.length()); |
| 216 | + HTMLWriter writer = new HTMLWriter(sw, format); |
| 217 | + writer.write(document); |
| 218 | + return sw.toString(); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testUsageCSSFactory() throws Exception { |
| 223 | + AbstractCSSStyleSheetFactory cssFactory = new CSSDOMImplementation(); |
| 224 | + AbstractCSSStyleSheet sheet = cssFactory.createStyleSheet(null, null); |
| 225 | + Reader re = new StringReader(sheet1); |
| 226 | + assertTrue(sheet.parseStyleSheet(re)); |
| 227 | + re.close(); |
| 228 | + |
| 229 | + assertEquals(sheet1, sheet.toMinifiedString()); |
| 230 | + } |
| 231 | + |
| 232 | +} |
0 commit comments