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 sax; 25 26 import java.io.IOException; 27 28 import org.xml.sax.Attributes; 29 import org.xml.sax.InputSource; 30 import org.xml.sax.Locator; 31 import org.xml.sax.SAXException; 32 import org.xml.sax.SAXParseException; 33 import org.xml.sax.ext.Attributes2Impl; 34 import org.xml.sax.ext.DefaultHandler2; 35 import org.xml.sax.ext.Locator2; 36 import org.xml.sax.ext.Locator2Impl; 37 import org.xml.sax.helpers.XMLFilterImpl; 38 import org.xml.sax.helpers.XMLReaderAdapter; 39 40 public class MyDefaultHandler2 extends DefaultHandler2 { 41 Locator2Impl locator = new Locator2Impl(); 42 StringBuffer currentValue = new StringBuffer(); 43 String version = "customVersion"; 44 String encoding = "customEncoding"; 45 46 public void setDocumentLocator(Locator locator) { 47 this.locator = new Locator2Impl((Locator2) locator); 48 this.locator.setXMLVersion(version); 49 this.locator.setEncoding(encoding); 50 } 51 52 public void startDocument() throws SAXException { 53 super.startDocument(); 54 System.out.println("startDocument() is invoked"); 55 System.out.println(locator.getXMLVersion()); 56 System.out.println(locator.getEncoding()); 57 } 58 59 public void attributeDecl(String ename, String aname, String type, String mode, String value) throws SAXException { 60 super.attributeDecl(ename, aname, type, mode, value); 61 System.out.println("attributeDecl() is invoked for attr :" + aname); 62 } 63 64 public void elementDecl(String name, String model) throws SAXException { 65 super.elementDecl(name, model); 66 System.out.println("elementDecl() is invoked for element : " + name); 67 } 68 69 public void internalEntityDecl(String name, String value) throws SAXException { 70 super.internalEntityDecl(name, value); 71 System.out.println("internalEntityDecl() is invoked for entity : " + name); 72 } 73 74 public void externalEntityDecl(String name, String publicId, String systemId) throws SAXException { 75 super.externalEntityDecl(name, publicId, systemId); 76 System.out.println("externalEntityDecl() is invoked for entity : " + name); 77 } 78 79 public void comment(char[] ch, int start, int length) throws SAXException { 80 super.comment(ch, start, length); 81 System.out.println(new String(ch, start, length)); 82 } 83 84 public void endDocument() throws SAXException { 85 super.endDocument(); 86 System.out.println("\nendDocument() is invoked"); 87 } 88 89 public void startCDATA() throws SAXException { 90 super.startCDATA(); 91 System.out.println("startCDATA() is invoked"); 92 } 93 94 public void endCDATA() throws SAXException { 95 super.endCDATA(); 96 System.out.println("endCDATA() is invoked"); 97 } 98 99 public void startEntity(String name) throws SAXException { 100 super.startEntity(name); 101 // System.out.println("startEntity() is invoked for entity : " + name) ; 102 } 103 104 public void endEntity(String name) throws SAXException { 105 super.endEntity(name); 106 // System.out.println("endEntity() is invoked for entity : " + name) ; 107 } 108 109 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 110 super.startElement(uri, localName, qName, attributes); 111 if (qName.equals("toy")) { 112 Attributes2Impl impl = new Attributes2Impl(); 113 impl.setAttributes(attributes); 114 System.out.println("\ntoy id=" + impl.getValue("id")); 115 } else if (qName.equals("price") || qName.equals("name")) { 116 System.out.print(" " + qName + " : "); 117 currentValue = new StringBuffer(); 118 } 119 } 120 121 public void endElement(String uri, String localName, String qName) throws SAXException { 122 super.endElement(uri, localName, qName); 123 if (qName.equals("price") || qName.equals("name")) { 124 System.out.print(currentValue.toString()); 125 } 126 } 127 128 public void startDTD(String name, String publicId, String systemId) throws SAXException { 129 super.startDTD(name, publicId, systemId); 130 System.out.println("startDTD() is invoked"); 131 } 132 133 public void endDTD() throws SAXException { 134 super.endDTD(); 135 System.out.println("endDTD() is invoked"); 136 } 137 138 public void characters(char[] ch, int start, int length) { 139 // System.out.println(start + " " + length) ; 140 currentValue.append(ch, start, length); 141 } 142 143 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 144 System.out.println("resolveEntity(publicId, systemId) is invoked"); 145 return super.resolveEntity(publicId, systemId); 146 } 147 148 public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException { 149 System.out.println("resolveEntity(name, publicId, baseURI, systemId) is invoked"); 150 return super.resolveEntity(name, publicId, baseURI, systemId); 151 } 152 153 public InputSource getExternalSubset(String name, String baseURI) throws SAXException, IOException { 154 System.out.println("getExternalSubset() is invoked"); 155 return super.getExternalSubset(name, baseURI); 156 } 157 158 public void startPrefixMapping(String prefix, String uri) { 159 System.out.println("startPrefixMapping() is invoked for " + prefix + " : " + uri); 160 try { 161 new XMLReaderAdapter().startPrefixMapping(prefix, uri); 162 } catch (SAXException e) { 163 e.printStackTrace(); 164 } 165 } 166 167 public void endPrefixMapping(String prefix) { 168 System.out.println("\nendPrefixMapping() is invoked for " + prefix); 169 try { 170 new XMLReaderAdapter().endPrefixMapping(prefix); 171 } catch (SAXException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 public void skippedEntity(String name) { 177 try { 178 System.out.println("skippedEntity() is invoked for : " + name); 179 new XMLReaderAdapter().skippedEntity(name); 180 } catch (SAXException e) { 181 e.printStackTrace(); 182 } 183 } 184 185 public void error(SAXParseException e) throws SAXException { 186 System.out.println("error() is invoked for in ErrorHandler"); 187 new XMLFilterImpl().warning(e); 188 } 189 190 public void fatalError(SAXParseException e) throws SAXException { 191 System.out.println("fatalError() is invoked for in ErrorHandler"); 192 new XMLFilterImpl().warning(e); 193 } 194 195 public void warning(SAXParseException e) throws SAXException { 196 System.out.println("warning() is invoked for in ErrorHandler"); 197 new XMLFilterImpl().warning(e); 198 } 199 200 }