1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package stream.XMLStreamWriterTest; 25 26 import java.io.IOException; 27 import java.io.StringReader; 28 import java.io.StringWriter; 29 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.stream.XMLOutputFactory; 33 import javax.xml.stream.XMLStreamException; 34 import javax.xml.stream.XMLStreamWriter; 35 36 import org.testng.Assert; 37 import org.testng.annotations.Listeners; 38 import org.testng.annotations.Test; 39 import org.xml.sax.InputSource; 40 import org.xml.sax.SAXException; 41 42 /* 43 * @summary Test XMLStreamWriter shall escape the illegal characters. 44 */ 45 @Listeners({jaxp.library.BasePolicy.class}) 46 public class AttributeEscapeTest { 47 48 /** 49 * XML content for testing the escaping of <, >, &, ', ". 50 */ 51 private static final String XML_CONTENT = "Testing escaping: lt=<, gt=>, amp=&, apos=', dquote=\""; 52 53 @Test 54 public void testCR6420953() { 55 56 try { 57 XMLOutputFactory xof = XMLOutputFactory.newInstance(); 58 StringWriter sw = new StringWriter(); 59 XMLStreamWriter w = xof.createXMLStreamWriter(sw); 60 61 w.writeStartDocument(); 62 w.writeStartElement("element"); 63 64 w.writeDefaultNamespace(XML_CONTENT); 65 w.writeNamespace("prefix", XML_CONTENT); 66 67 w.writeAttribute("attribute", XML_CONTENT); 68 w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT); 69 w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT); 70 71 w.writeCharacters("\n"); 72 w.writeCharacters(XML_CONTENT); 73 w.writeCharacters("\n"); 74 w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length()); 75 w.writeCharacters("\n"); 76 77 w.writeEndElement(); 78 w.writeEndDocument(); 79 w.flush(); 80 81 System.out.println(sw); 82 83 // make sure that the generated XML parses 84 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 85 dbf.setNamespaceAware(true); 86 dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString()))); 87 } catch (XMLStreamException xmlStreamException) { 88 xmlStreamException.printStackTrace(); 89 Assert.fail(xmlStreamException.toString()); 90 } catch (SAXException saxException) { 91 saxException.printStackTrace(); 92 Assert.fail(saxException.toString()); 93 } catch (ParserConfigurationException parserConfigurationException) { 94 parserConfigurationException.printStackTrace(); 95 Assert.fail(parserConfigurationException.toString()); 96 } catch (IOException ioException) { 97 ioException.printStackTrace(); 98 Assert.fail(ioException.toString()); 99 } 100 } 101 }