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 parsers; 25 26 import java.io.File; 27 import java.io.FileReader; 28 import java.io.Reader; 29 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 33 import org.testng.Assert; 34 import org.testng.annotations.Listeners; 35 import org.testng.annotations.Test; 36 import org.w3c.dom.Document; 37 import org.w3c.dom.NamedNodeMap; 38 import org.w3c.dom.Node; 39 import org.w3c.dom.NodeList; 40 import org.xml.sax.InputSource; 41 42 /* 43 * @bug 6518733 44 * @summary Test SAX parser handles several attributes with containing ">". 45 */ 46 @Listeners({jaxp.library.FilePolicy.class}) 47 public class Bug6760982 { 48 49 @Test 50 public void test() { 51 try { 52 Document xmlDoc = _Parse(new File(getClass().getResource("bug6760982.xml").getFile())); 53 Node node = xmlDoc.getDocumentElement(); 54 55 _ProcessNode(node, 0); 56 _Flush(); 57 } catch (Exception e) { 58 _ErrPrintln("Exception: " + e.toString()); 59 Assert.fail("Exception: " + e.getMessage()); 60 } 61 } 62 63 private static void _Flush() { 64 System.out.flush(); 65 System.err.flush(); 66 } 67 68 private static void _Println(String str, int level) { 69 for (int i = 0; i < level; i++) 70 System.out.print(" "); 71 72 System.out.println(str); 73 System.out.flush(); 74 } 75 76 private static void _ErrPrintln(String aStr) { 77 System.out.flush(); 78 System.err.println(aStr); 79 System.err.flush(); 80 } 81 82 private static Document _Parse(File f) throws Exception { 83 FileReader rd = new FileReader(f); 84 Document doc = _Parse(rd); 85 86 rd.close(); 87 88 return doc; 89 } 90 91 private static Document _Parse(Reader src) throws Exception { 92 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 93 94 dbf.setValidating(false); // to improve performance 95 96 DocumentBuilder xmlParser = dbf.newDocumentBuilder(); 97 InputSource is = new InputSource(src); 98 99 return xmlParser.parse(is); 100 } 101 102 private static void _PrintAttributes(Node n, int level) { 103 NamedNodeMap nnmap = n.getAttributes(); 104 105 if (nnmap != null && nnmap.getLength() > 0) { 106 _Println("<attribs> (" + nnmap.getClass() + "):", level + 1); 107 108 for (int i = 0; i < nnmap.getLength(); i++) { 109 Node an = nnmap.item(i); 110 111 String nameStr = an.getNodeName(); 112 String valueStr = an.getNodeValue(); 113 114 if (valueStr != "") 115 nameStr += " = " + valueStr; 116 117 _Println(nameStr, level + 2); 118 } 119 } 120 } 121 122 private static void _ProcessChildren(Node n, int level) throws Exception { 123 NodeList nlist = n.getChildNodes(); 124 125 if (nlist != null) 126 for (int i = 0; i < nlist.getLength(); i++) 127 _ProcessNode(nlist.item(i), level + 1); 128 } 129 130 private static void _ProcessNode(Node n, int level) throws Exception { 131 n.getAttributes(); 132 n.getChildNodes(); 133 134 // At this point, for JVM 1.6 and Xerces <= 1.3.1, 135 // Test-XML.xml::mytest:Y's attribute is (already) bad. 136 137 switch (n.getNodeType()) { 138 139 case Node.TEXT_NODE: 140 String str = n.getNodeValue().trim(); 141 142 /* ...Only print non-empty strings... */ 143 if (str.length() > 0) { 144 String valStr = n.getNodeValue(); 145 146 _Println(valStr, level); 147 } 148 break; 149 150 case Node.COMMENT_NODE: 151 break; 152 153 default: { 154 String nodeNameStr = n.getNodeName(); 155 156 _Println(nodeNameStr + " (" + n.getClass() + "):", level); 157 158 /* ...Print children... */ 159 _ProcessChildren(n, level); 160 161 /* ...Print optional node attributes... */ 162 _PrintAttributes(n, level); 163 } 164 } 165 } 166 }