1 21 22 23 package nu.xom; 24 25 30 class NonVerifyingHandler extends XOMHandler { 31 32 NonVerifyingHandler(NodeFactory factory) { 33 super(factory); 34 } 35 36 37 public void startElement(String namespaceURI, String localName, 38 String qualifiedName, org.xml.sax.Attributes attributes) { 39 40 flushText(); 41 Element element = Element.build(qualifiedName, namespaceURI); 42 if (parent == document) { document.setRootElement(element); 44 inProlog = false; 45 } 46 47 current = element; 48 parents.push(element); 50 51 if (parent != document) { 52 parent.fastInsertChild(element, parent.getChildCount()); 54 } 55 if (locator != null) { 61 String baseURI = locator.getSystemId(); 62 if (baseURI != null && !baseURI.equals(documentBaseURI)) { 63 element.setActualBaseURI(baseURI); 64 } 65 } 66 67 int length = attributes.getLength(); 70 71 for (int i = 0; i < length; i++) { 78 String qName = attributes.getQName(i); 79 if (qName.startsWith("xmlns:") || qName.equals("xmlns")) { 80 continue; 81 } 82 else { 83 String namespace = attributes.getURI(i); 84 String value = attributes.getValue(i); 85 Attribute attribute = Attribute.build( 86 qName, 87 namespace, 88 value, 89 convertStringToType(attributes.getType(i)) 90 ); 91 element.fastAddAttribute(attribute); 92 } 93 } 94 95 for (int i = 0; i < length; i++) { 97 String qName = attributes.getQName(i); 98 if (qName.startsWith("xmlns:")) { 99 String namespaceName = attributes.getValue(i); 100 String namespacePrefix = qName.substring(6); 101 String currentValue 102 = element.getNamespaceURI(namespacePrefix); 103 if (!namespaceName.equals(currentValue)) { 104 element.addNamespaceDeclaration( 105 namespacePrefix, namespaceName); 106 } 107 } 108 else if (qName.equals("xmlns")) { 109 String namespaceName = attributes.getValue(i); 110 String namespacePrefix = ""; 111 String currentValue 112 = element.getNamespaceURI(namespacePrefix); 113 if (!namespaceName.equals(currentValue)) { 114 element.addNamespaceDeclaration(namespacePrefix, 115 namespaceName); 116 } 117 } 118 } 119 120 parent = element; 122 } 123 124 125 public void endElement( 126 String namespaceURI, String localName, String qualifiedName) { 127 128 current = (ParentNode) parents.pop(); 131 flushText(); 132 133 parent = current.getParent(); 134 135 if (parent.isDocument()) { Document doc = (Document) parent; 137 boolean beforeRoot = true; 138 doc.setRootElement((Element) current); 139 beforeRoot = false; 140 } 141 142 } 143 144 145 protected void flushText() { 147 148 if (buffer.length() > 0) { 149 Text result; 150 if (!inCDATA) { 151 result = Text.build(buffer.toString()); 152 } 153 else { 154 result = CDATASection.build(buffer.toString()); 155 } 156 parent.fastInsertChild(result, parent.getChildCount()); 157 buffer = new StringBuffer (); 158 } 159 inCDATA = false; 160 finishedCDATA = false; 161 162 } 163 164 165 public void processingInstruction(String target, String data) { 166 167 if (!inDTD) flushText(); 168 if (inExternalSubset) return; 169 ProcessingInstruction result = ProcessingInstruction.build(target, data); 170 171 if (!inDTD) { 172 if (inProlog) { 173 parent.fastInsertChild(result, position); 174 position++; 175 } 176 else { 177 parent.fastInsertChild(result, parent.getChildCount()); 178 } 179 } 180 else { 181 internalDTDSubset.append(" "); 182 internalDTDSubset.append(result.toXML()); 183 internalDTDSubset.append("\n"); 184 } 185 186 } 187 188 189 public void startDTD(String rootName, String publicID, 191 String systemID) { 192 193 inDTD = true; 194 DocType doctype = DocType.build(rootName, publicID, systemID); 195 document.fastInsertChild(doctype, position); 196 position++; 197 internalDTDSubset = new StringBuffer (); 198 this.doctype = doctype; 199 200 } 201 202 203 public void comment(char[] text, int start, int length) { 204 205 if (!inDTD) flushText(); 206 if (inExternalSubset) return; 207 208 Comment result = Comment.build(new String (text, start, length)); 209 ; 210 if (!inDTD) { 211 if (inProlog) { 212 parent.insertChild(result, position); 213 position++; 214 } 215 else { 216 parent.fastInsertChild(result, parent.getChildCount()); 217 } 218 } 219 else { 220 internalDTDSubset.append(" "); 221 internalDTDSubset.append(result.toXML()); 222 internalDTDSubset.append("\n"); 223 } 224 225 } 226 227 } | Popular Tags |