1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package dom; 19 20 import org.w3c.dom.DOMConfiguration; 21 import org.w3c.dom.DOMError; 22 import org.w3c.dom.DOMErrorHandler; 23 import org.w3c.dom.Document; 24 import org.w3c.dom.Element; 25 import org.w3c.dom.Node; 26 import org.w3c.dom.bootstrap.DOMImplementationRegistry; 27 import org.w3c.dom.ls.DOMImplementationLS; 28 import org.w3c.dom.ls.LSOutput; 29 import org.w3c.dom.ls.LSParser; 30 import org.w3c.dom.ls.LSParserFilter; 31 import org.w3c.dom.ls.LSSerializer; 32 import org.w3c.dom.traversal.NodeFilter; 33 34 /** 35 * This sample program illustrates how to use DOM L3 36 * DOMBuilder, DOMBuilderFilter DOMWriter and other DOM L3 functionality 37 * to preparse, revalidate and safe document. 38 */ 39 public class DOM3 implements DOMErrorHandler, LSParserFilter { 40 41 /** Default namespaces support (true). */ 42 protected static final boolean DEFAULT_NAMESPACES = true; 43 44 /** Default validation support (false). */ 45 protected static final boolean DEFAULT_VALIDATION = false; 46 47 /** Default Schema validation support (false). */ 48 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 49 50 static LSParser builder; 51 public static void main( String[] argv) { 52 53 if (argv.length == 0) { 54 printUsage(); 55 System.exit(1); 56 } 57 58 59 try { 60 61 // get DOM Implementation using DOM Registry 62 System.setProperty(DOMImplementationRegistry.PROPERTY,"org.apache.xerces.dom.DOMXSImplementationSourceImpl"); 63 DOMImplementationRegistry registry = 64 DOMImplementationRegistry.newInstance(); 65 66 DOMImplementationLS impl = 67 (DOMImplementationLS)registry.getDOMImplementation("LS"); 68 69 // create DOMBuilder 70 builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 71 72 DOMConfiguration config = builder.getDomConfig(); 73 74 // create Error Handler 75 DOMErrorHandler errorHandler = new DOM3(); 76 77 // create filter 78 LSParserFilter filter = new DOM3(); 79 80 builder.setFilter(filter); 81 82 // set error handler 83 config.setParameter("error-handler", errorHandler); 84 85 86 // set validation feature 87 //config.setParameter("validate", Boolean.FALSE); 88 config.setParameter("validate",Boolean.TRUE); 89 90 // set schema language 91 config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); 92 //config.setParameter("psvi",Boolean.TRUE); 93 //config.setParameter("schema-type","http://www.w3.org/TR/REC-xml"); 94 95 // set schema location 96 config.setParameter("schema-location","personal.xsd"); 97 98 // parse document 99 System.out.println("Parsing "+argv[0]+"..."); 100 Document doc = builder.parseURI(argv[0]); 101 102 // set error handler on the Document 103 config = doc.getDomConfig(); 104 105 config.setParameter("error-handler", errorHandler); 106 107 // set validation feature 108 config.setParameter("validate", Boolean.TRUE); 109 config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); 110 //config.setParameter("schema-type","http://www.w3.org/TR/REC-xml"); 111 config.setParameter("schema-location","data/personal.xsd"); 112 113 // remove comments from the document 114 config.setParameter("comments", Boolean.FALSE); 115 116 System.out.println("Normalizing document... "); 117 doc.normalizeDocument(); 118 119 120 // create DOMWriter 121 LSSerializer domWriter = impl.createLSSerializer(); 122 123 System.out.println("Serializing document... "); 124 config = domWriter.getDomConfig(); 125 config.setParameter("xml-declaration", Boolean.FALSE); 126 //config.setParameter("validate",errorHandler); 127 128 // serialize document to standard output 129 //domWriter.writeNode(System.out, doc); 130 LSOutput dOut = impl.createLSOutput(); 131 dOut.setByteStream(System.out); 132 domWriter.write(doc,dOut); 133 134 } catch ( Exception ex ) { 135 ex.printStackTrace(); 136 } 137 } 138 139 140 private static void printUsage() { 141 142 System.err.println("usage: java dom.DOM3 uri ..."); 143 System.err.println(); 144 System.err.println("NOTE: You can only validate DOM tree against XML Schemas."); 145 146 } // printUsage() 147 148 149 public boolean handleError(DOMError error){ 150 short severity = error.getSeverity(); 151 if (severity == DOMError.SEVERITY_ERROR) { 152 System.out.println("[dom3-error]: "+error.getMessage()); 153 } 154 155 if (severity == DOMError.SEVERITY_WARNING) { 156 System.out.println("[dom3-warning]: "+error.getMessage()); 157 } 158 return true; 159 160 } 161 /** 162 * @see org.w3c.dom.ls.LSParserFilter#acceptNode(Node) 163 */ 164 public short acceptNode(Node enode) { 165 return NodeFilter.FILTER_ACCEPT; 166 } 167 168 /** 169 * @see org.w3c.dom.ls.LSParserFilter#getWhatToShow() 170 */ 171 public int getWhatToShow() { 172 return NodeFilter.SHOW_ELEMENT; 173 } 174 175 /** 176 * @see org.w3c.dom.ls.LSParserFilter#startElement(Element) 177 */ 178 public short startElement(Element elt) { 179 return LSParserFilter.FILTER_ACCEPT; 180 } 181 182 } 183 184 185