1 /* 2 * Copyright (c) 2014, 2016, 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.EventsTest; 25 26 import java.io.StringReader; 27 import java.io.StringWriter; 28 import java.util.Iterator; 29 import java.util.List; 30 31 import javax.xml.namespace.QName; 32 import javax.xml.stream.XMLEventFactory; 33 import javax.xml.stream.XMLEventReader; 34 import javax.xml.stream.XMLInputFactory; 35 import javax.xml.stream.XMLOutputFactory; 36 import javax.xml.stream.XMLStreamConstants; 37 import javax.xml.stream.XMLStreamException; 38 import javax.xml.stream.events.Attribute; 39 import javax.xml.stream.events.Characters; 40 import javax.xml.stream.events.Comment; 41 import javax.xml.stream.events.DTD; 42 import javax.xml.stream.events.EndDocument; 43 import javax.xml.stream.events.EndElement; 44 import javax.xml.stream.events.Namespace; 45 import javax.xml.stream.events.ProcessingInstruction; 46 import javax.xml.stream.events.StartDocument; 47 import javax.xml.stream.events.StartElement; 48 import javax.xml.stream.events.XMLEvent; 49 50 import org.testng.Assert; 51 import org.testng.annotations.Listeners; 52 import org.testng.annotations.Test; 53 54 /* 55 * @bug 6631268 56 * @summary Test XMLEvent.writeAsEncodedUnicode can output the event content. 57 */ 58 @Listeners({jaxp.library.BasePolicy.class}) 59 public class Issue41Test { 60 61 public java.io.File input; 62 public final String filesDir = "./"; 63 protected XMLInputFactory inputFactory; 64 protected XMLOutputFactory outputFactory; 65 66 @Test 67 public void testEvents() { 68 XMLEventFactory f = XMLEventFactory.newInstance(); 69 final String contents = "test <some> text & more! [[]] --"; 70 final String prefix = "prefix"; 71 final String uri = "http://foo"; 72 final String localName = "elem"; 73 74 try { 75 StartDocument sd = f.createStartDocument(); 76 writeAsEncodedUnicode(sd); 77 78 Comment c = f.createComment("some comments"); 79 writeAsEncodedUnicode(c); 80 81 StartElement se = f.createStartElement(prefix, uri, localName); 82 83 ProcessingInstruction pi = f.createProcessingInstruction("target", "data"); 84 writeAsEncodedUnicode(pi); 85 86 Namespace ns = f.createNamespace(prefix, uri); 87 writeAsEncodedUnicode(ns); 88 89 Characters characters = f.createCharacters(contents); 90 writeAsEncodedUnicode(characters); 91 // CData 92 Characters cdata = f.createCData(contents); 93 writeAsEncodedUnicode(cdata); 94 95 // Attribute 96 QName attrName = new QName("http://test.com", "attr", "ns"); 97 Attribute attr = f.createAttribute(attrName, "value"); 98 writeAsEncodedUnicode(attr); 99 100 // prefix, uri, localName 101 EndElement ee = f.createEndElement(prefix, uri, localName); 102 writeAsEncodedUnicode(ee); 103 104 EndDocument ed = f.createEndDocument(); 105 writeAsEncodedUnicode(ed); 106 } catch (Exception e) { 107 Assert.fail(e.getMessage()); 108 } 109 110 } 111 112 /** 113 * DTDEvent instances constructed via event reader are missing the notation 114 * and entity declaration information 115 */ 116 @Test 117 public void testDTDEvent() { 118 String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [\n" + "<!ENTITY intEnt 'internal'>\n" + "<!ENTITY extParsedEnt SYSTEM 'url:dummy'>\n" 119 + "<!NOTATION notation PUBLIC 'notation-public-id'>\n" + "<!NOTATION notation2 SYSTEM 'url:dummy'>\n" 120 + "<!ENTITY extUnparsedEnt SYSTEM 'url:dummy2' NDATA notation>\n" + "]>" + "<root />"; 121 122 try { 123 XMLEventReader er = getReader(XML); 124 XMLEvent evt = er.nextEvent(); // StartDocument 125 evt = er.nextEvent(); // DTD 126 if (evt.getEventType() != XMLStreamConstants.DTD) { 127 Assert.fail("Expected DTD event"); 128 } 129 DTD dtd = (DTD) evt; 130 writeAsEncodedUnicode(dtd); 131 List entities = dtd.getEntities(); 132 if (entities == null) { 133 Assert.fail("No entity found. Expected 3."); 134 } else { 135 writeAsEncodedUnicode((XMLEvent) entities.get(0)); 136 writeAsEncodedUnicode((XMLEvent) entities.get(1)); 137 writeAsEncodedUnicode((XMLEvent) entities.get(2)); 138 } 139 140 List notations = dtd.getNotations(); 141 if (notations == null) { 142 Assert.fail("No notation found. Expected 2."); 143 } else { 144 writeAsEncodedUnicode((XMLEvent) notations.get(0)); 145 writeAsEncodedUnicode((XMLEvent) notations.get(1)); 146 } 147 } catch (Exception e) { 148 Assert.fail(e.getMessage()); 149 } 150 } 151 152 private XMLEventReader getReader(String XML) throws Exception { 153 inputFactory = XMLInputFactory.newInstance(); 154 155 // Check if event reader returns the correct event 156 XMLEventReader er = inputFactory.createXMLEventReader(new StringReader(XML)); 157 return er; 158 } 159 160 161 162 /** 163 * The return of XMLEvent writeAsEncodedUnicode method is not defined This 164 * method merely tests that the output exists 165 */ 166 public void writeAsEncodedUnicode(XMLEvent evt) throws XMLStreamException { 167 if (evt.getEventType() == XMLStreamConstants.END_DOCUMENT) { 168 return; 169 } 170 StringWriter sw = new StringWriter(); 171 evt.writeAsEncodedUnicode(sw); 172 173 Assert.assertTrue(sw.toString().length() > 0); 174 System.out.println(sw.toString()); 175 } 176 }