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