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 sax; 25 26 import java.io.IOException; 27 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.SAXParser; 30 import javax.xml.parsers.SAXParserFactory; 31 32 import org.testng.Assert; 33 import org.testng.AssertJUnit; 34 import org.testng.annotations.Listeners; 35 import org.testng.annotations.Test; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXNotRecognizedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.ext.DefaultHandler2; 40 import org.xml.sax.helpers.DefaultHandler; 41 import org.xml.sax.helpers.ParserAdapter; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import org.xml.sax.helpers.XMLReaderFactory; 44 45 /* 46 * @summary Test DefaultHandler2. 47 */ 48 @Listeners({jaxp.library.FilePolicy.class}) 49 public class DefaultHandler2Test { 50 51 @Test 52 public void testParse01() { 53 System.out.println("===in testParse01==="); 54 try { 55 DefaultHandler handler = new MyDefaultHandler2(); 56 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 57 System.out.println(saxFac.getFeature("http://xml.org/sax/features/use-locator2")); 58 59 // set use-entity-resolver2 as FALSE to use EntityResolver firstly. 60 saxFac.setFeature("http://xml.org/sax/features/use-entity-resolver2", false); 61 saxFac.setValidating(true); 62 63 SAXParser parser = saxFac.newSAXParser(); 64 parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 65 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 66 67 parser.parse(this.getClass().getResource("toys.xml").getFile(), handler); 68 } catch (ParserConfigurationException e) { 69 e.printStackTrace(); 70 Assert.fail("ParserConfigurationException in testParse01()"); 71 } catch (SAXException e) { 72 e.printStackTrace(); 73 Assert.fail("SAXException in testParse01()"); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 Assert.fail("IOException in testParse01()"); 77 } 78 } 79 80 @Test 81 public void testParse02() { 82 System.out.println("===in testParse02==="); 83 try { 84 DefaultHandler handler = new MyDefaultHandler2(); 85 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 86 System.out.println(saxFac.getFeature("http://xml.org/sax/features/use-locator2")); 87 88 // Enable namespace parsing 89 System.out.println(saxFac.getFeature("http://xml.org/sax/features/namespaces")); 90 saxFac.setNamespaceAware(true); 91 92 saxFac.setValidating(true); 93 SAXParser parser = saxFac.newSAXParser(); 94 parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 95 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 96 97 parser.parse(this.getClass().getResource("toys.xml").getFile(), handler); 98 } catch (ParserConfigurationException e) { 99 e.printStackTrace(); 100 Assert.fail("ParserConfigurationException in testParse02()"); 101 } catch (SAXException e) { 102 e.printStackTrace(); 103 Assert.fail("SAXException in testParse02()"); 104 } catch (IOException e) { 105 e.printStackTrace(); 106 Assert.fail("IOException in testParse02()"); 107 } 108 } 109 110 @Test 111 public void testParse03() { 112 System.out.println("===in testParse03==="); 113 try { 114 DefaultHandler handler = new MyDefaultHandler2(); 115 116 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 117 xmlReader.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 118 System.out.println("XMLReader : " + xmlReader.getProperty("http://xml.org/sax/properties/declaration-handler")); 119 120 SAXParserFactory saxFac = SAXParserFactory.newInstance(); 121 SAXParser parser = saxFac.newSAXParser(); 122 parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 123 System.out.println("SAXParser : " + parser.getProperty("http://xml.org/sax/properties/declaration-handler")); 124 125 // From https://docs.oracle.com/javase/7/docs/api, 126 // ParserAdapter.setProperty() and ParserAdapter.getProperty() does 127 // not support any property currently. 128 try { 129 ParserAdapter adapter = new ParserAdapter(parser.getParser()); 130 System.out.println("ParserAdapter : " + adapter.getProperty("http://xml.org/sax/properties/declaration-handler")); 131 } catch (SAXNotRecognizedException e) { 132 System.out.println("Expected SAXNotRecognizedException since ParserAdapter.getProperty() does not support any property currently"); 133 } 134 try { 135 ParserAdapter adapter = new ParserAdapter(parser.getParser()); 136 adapter.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 137 } catch (SAXNotRecognizedException e) { 138 System.out.println("Expected SAXNotRecognizedException since ParserAdapter.setProperty() does not support any property currently"); 139 } 140 } catch (SAXException e) { 141 e.printStackTrace(); 142 Assert.fail("SAXException in testParse03()"); 143 } catch (ParserConfigurationException e) { 144 e.printStackTrace(); 145 Assert.fail("ParserConfigurationException in testParse03()"); 146 } 147 148 } 149 150 @Test 151 public void testParse04() { 152 System.out.println("===in testParse04==="); 153 try { 154 DefaultHandler handler = new MyDefaultHandler2(); 155 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 156 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 157 xmlReader.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 158 xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 159 xmlReader.setContentHandler(handler); 160 161 xmlReader.parse(this.getClass().getResource("toys.xml").getFile()); 162 163 } catch (SAXException e) { 164 e.printStackTrace(); 165 Assert.fail("SAXException in testParse04()"); 166 } catch (IOException e) { 167 e.printStackTrace(); 168 Assert.fail("IOException in testParse04()"); 169 } 170 } 171 172 @Test 173 public void testParse05() { 174 System.out.println("===in testParse05==="); 175 try { 176 DefaultHandler handler = new MyDefaultHandler2(); 177 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 178 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 179 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 180 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 181 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 182 filterImpl.setContentHandler(handler); 183 184 filterImpl.parse(this.getClass().getResource("toys.xml").getFile()); 185 186 } catch (SAXException e) { 187 e.printStackTrace(); 188 Assert.fail("SAXException in testParse05()"); 189 } catch (IOException e) { 190 e.printStackTrace(); 191 Assert.fail("IOException in testParse05()"); 192 } 193 } 194 195 @Test 196 public void testParse06() { 197 System.out.println("===in testParse06==="); 198 try { 199 DefaultHandler handler = new MyDefaultHandler2(); 200 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 201 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 202 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 203 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 204 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 205 filterImpl.setContentHandler(handler); 206 207 AssertJUnit.assertTrue(filterImpl.getProperty("http://xml.org/sax/properties/declaration-handler") instanceof DefaultHandler2); 208 209 // filterImpl.setFeature("http://xml.org/sax/features/external-general-entities", 210 // false) ; 211 // filterImpl.setFeature("http://xml.org/sax/features/external-parameter-entities", 212 // false) ; 213 filterImpl.skippedEntity("name2"); 214 215 filterImpl.parse(this.getClass().getResource("toys.xml").getFile()); 216 } catch (SAXException e) { 217 e.printStackTrace(); 218 Assert.fail("SAXException in testParse06()"); 219 } catch (IOException e) { 220 e.printStackTrace(); 221 Assert.fail("IOException in testParse06()"); 222 } 223 } 224 225 @Test 226 public void testParse07() { 227 System.out.println("===in testParse07==="); 228 try { 229 DefaultHandler handler = new MyDefaultHandler2(); 230 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 231 XMLFilterImpl filterImpl = new XMLFilterImpl(xmlReader); 232 System.out.println(xmlReader.getFeature("http://xml.org/sax/features/namespaces")); 233 filterImpl.setProperty("http://xml.org/sax/properties/declaration-handler", handler); 234 filterImpl.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 235 filterImpl.setContentHandler(handler); 236 filterImpl.setErrorHandler(handler); 237 AssertJUnit.assertTrue(filterImpl.getProperty("http://xml.org/sax/properties/declaration-handler") instanceof DefaultHandler2); 238 239 filterImpl.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); 240 filterImpl.parse(this.getClass().getResource("toys_error.xml").getFile()); 241 } catch (SAXException e) { 242 e.printStackTrace(); 243 Assert.fail("SAXException in testParse07()"); 244 } catch (IOException e) { 245 e.printStackTrace(); 246 Assert.fail("IOException in testParse07()"); 247 } 248 } 249 }