michael-o commented on a change in pull request #286: [MNG-6656] Introduce base for build/consumer process URL: https://github.com/apache/maven/pull/286#discussion_r386405991
########## File path: maven-xml/src/test/java/org/apache/maven/xml/sax/LexicalHandlerVerifier.java ########## @@ -0,0 +1,277 @@ +package org.apache.maven.xml.sax; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import static org.xmlunit.assertj.XmlAssert.assertThat; + +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Arrays; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.apache.maven.xml.Factories; +import org.apache.maven.xml.sax.filter.AbstractSAXFilter; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLFilter; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.helpers.XMLFilterImpl; + +/** + * Some tests to help understand the chain of events regarding XMLFilters, XMLReaders, LexicalHandlers and Transformers + * + * @author Robert Scholte + * + */ +public class LexicalHandlerVerifier +{ + private static final String SAX_PROPERTIES_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler"; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void parseXmlReader() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + expectedException.expectMessage( "LexicalHandlerVerifier" ); + + XMLReader reader = Factories.newXMLReader(); + reader.setProperty( SAX_PROPERTIES_LEXICAL_HANDLER, new UnsupportedOperationExceptionLexicalHandler() ); + + InputSource inputSource = new InputSource( new StringReader( "<root><!-- COMMENT --></root>" ) ); + reader.parse( inputSource ); + } + + @Test + public void parseXmlFilter() throws Exception + { + expectedException.expect( UnsupportedOperationException.class ); + expectedException.expectMessage( "LexicalHandlerVerifier" ); + + XMLReader reader = Factories.newXMLReader(); + reader.setProperty( SAX_PROPERTIES_LEXICAL_HANDLER, new UnsupportedOperationExceptionLexicalHandler() ); + + XMLFilter filter = new XMLFilterImpl( reader ); + + InputSource inputSource = new InputSource( new StringReader( "<root><!-- COMMENT --></root>" ) ); + filter.parse( inputSource ); + } + + @Test + public void transformXmlReader() throws Exception + { + Writer writer = new StringWriter(); + StreamResult result = new StreamResult( writer ); + + SAXTransformerFactory transformerFactory = (SAXTransformerFactory) Factories.newTransformerFactory(); + TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(); + transformerHandler.setResult( result ); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" ); + + Source xmlSource = new StreamSource( new StringReader( "<root><!--COMMENT--></root>" ) ); + + SAXResult transformResult = new SAXResult( transformerHandler ); + transformResult.setLexicalHandler( new SortCommentLexicalHandler( transformerHandler ) ); + transformer.transform( xmlSource, transformResult ); + + assertThat( writer.toString() ).and( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" Review comment: Are you certain that this will work on non-UTF-8 encodings? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
