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.Test; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.SAXException; 40 41 /* 42 * @summary Test XMLStreamWriter shall escape the illegal characters. 43 */ 44 public class AttributeEscapeTest { 45 46 /** 47 * XML content for testing the escaping of <, >, &, ', ". 48 */ 49 private static final String XML_CONTENT = "Testing escaping: lt=<, gt=>, amp=&, apos=', dquote=\""; 50 51 @Test 52 public void testCR6420953() { 53 54 try { 55 XMLOutputFactory xof = XMLOutputFactory.newInstance(); 56 StringWriter sw = new StringWriter(); 57 XMLStreamWriter w = xof.createXMLStreamWriter(sw); 58 59 w.writeStartDocument(); 60 w.writeStartElement("element"); 61 62 w.writeDefaultNamespace(XML_CONTENT); 63 w.writeNamespace("prefix", XML_CONTENT); 64 65 w.writeAttribute("attribute", XML_CONTENT); 66 w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT); 67 w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT); 68 69 w.writeCharacters("\n"); 70 w.writeCharacters(XML_CONTENT); 71 w.writeCharacters("\n"); 72 w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length()); 73 w.writeCharacters("\n"); 74 75 w.writeEndElement(); 76 w.writeEndDocument(); 77 w.flush(); 78 79 System.out.println(sw); 80 81 // make sure that the generated XML parses 82 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 83 dbf.setNamespaceAware(true); 84 dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString()))); 85 } catch (XMLStreamException xmlStreamException) { 86 xmlStreamException.printStackTrace(); 87 Assert.fail(xmlStreamException.toString()); 88 } catch (SAXException saxException) { 89 saxException.printStackTrace(); 90 Assert.fail(saxException.toString()); 91 } catch (ParserConfigurationException parserConfigurationException) { 92 parserConfigurationException.printStackTrace(); 93 Assert.fail(parserConfigurationException.toString()); 94 } catch (IOException ioException) { 95 ioException.printStackTrace(); 96 Assert.fail(ioException.toString()); 97 } 98 } 99 }