1 21 22 package nu.xom.converters; 23 24 import nu.xom.Attribute; 25 import nu.xom.Comment; 26 import nu.xom.DocType; 27 import nu.xom.Document; 28 import nu.xom.Element; 29 import nu.xom.Node; 30 import nu.xom.Nodes; 31 import nu.xom.ParentNode; 32 import nu.xom.ProcessingInstruction; 33 import nu.xom.Text; 34 35 import org.xml.sax.ContentHandler ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.ext.LexicalHandler ; 38 import org.xml.sax.helpers.AttributesImpl ; 39 import org.xml.sax.helpers.LocatorImpl ; 40 41 50 public class SAXConverter { 51 52 53 private ContentHandler contentHandler; 54 private LexicalHandler lexicalHandler; 55 private LocatorImpl locator; 56 57 58 69 public SAXConverter(ContentHandler handler) { 70 setContentHandler(handler); 71 } 72 73 74 85 public void setContentHandler(ContentHandler handler) { 86 87 if (handler == null) { 88 throw new NullPointerException ( 89 "ContentHandler must be non-null." 90 ); 91 } 92 this.contentHandler = handler; 93 94 } 95 96 97 104 public ContentHandler getContentHandler() { 105 return this.contentHandler; 106 } 107 108 109 119 public void setLexicalHandler(LexicalHandler handler) { 120 this.lexicalHandler = handler; 121 } 122 123 124 133 public LexicalHandler getLexicalHandler() { 134 return this.lexicalHandler; 135 } 136 137 138 154 public void convert(Document doc) throws SAXException { 155 156 locator = new LocatorImpl (); 157 locator.setSystemId(doc.getBaseURI()); 158 contentHandler.setDocumentLocator(locator); 159 contentHandler.startDocument(); 160 for (int i = 0; i < doc.getChildCount(); i++) { 161 process(doc.getChild(i)); 162 } 163 contentHandler.endDocument(); 164 165 } 166 167 168 private void process(Node node) throws SAXException { 169 170 if (node instanceof Element) { 171 convertElement((Element) node); 172 } 173 else if (node instanceof Text) { 174 String data = node.getValue(); 175 contentHandler.characters( 176 data.toCharArray(), 0, data.length()); 177 } 178 else if (node instanceof ProcessingInstruction) { 179 ProcessingInstruction instruction 180 = (ProcessingInstruction) node; 181 contentHandler.processingInstruction( 182 instruction.getTarget(), instruction.getValue()); 183 } 184 else if (node instanceof Comment && lexicalHandler != null) { 185 String data = node.getValue(); 186 lexicalHandler.comment( 187 data.toCharArray(), 0, data.length()); 188 } 189 else if (node instanceof DocType && lexicalHandler != null) { 190 DocType type = (DocType) node; 191 lexicalHandler.startDTD(type.getRootElementName(), 192 type.getPublicID(), type.getSystemID()); 193 lexicalHandler.endDTD(); 194 } 195 196 } 197 198 199 private void convertNamespace(Element element, String prefix) 200 throws SAXException { 201 202 String uri = element.getNamespaceURI(prefix); 203 ParentNode parentNode = element.getParent(); 204 Element parent = null; 205 if (parentNode instanceof Element) { 206 parent = (Element) parentNode; 207 } 208 209 if (parent != null && uri.equals(parent.getNamespaceURI(prefix))) { 210 return; 211 } 212 else if (parent == null && "".equals(uri)) { 213 return; 216 } 217 contentHandler.startPrefixMapping(prefix, element.getNamespaceURI(prefix)); 218 219 } 220 221 222 private void convertElement(Element element) throws SAXException { 223 224 locator.setSystemId(element.getBaseURI()); 225 226 ParentNode parentNode = element.getParent(); 227 Element parent = null; 228 if (parentNode instanceof Element) { 229 parent = (Element) parentNode; 230 } 231 232 for (int i = 0; 234 i < element.getNamespaceDeclarationCount(); 235 i++) { 236 String prefix = element.getNamespacePrefix(i); 237 convertNamespace(element, prefix); 238 } 239 if (parent != null) { 240 String prefix = element.getNamespacePrefix(); 242 if (!element.getNamespaceURI(prefix) 243 .equals(parent.getNamespaceURI(prefix))) { 244 contentHandler.startPrefixMapping(prefix, 245 element.getNamespaceURI(prefix)); 246 } 247 248 for (int i = 0; i < element.getAttributeCount(); i++) { 250 Attribute att = element.getAttribute(i); 251 String attPrefix = att.getNamespacePrefix(); 252 if (!element.getNamespaceURI(attPrefix) 253 .equals(parent.getNamespaceURI(attPrefix)) 254 && !element.getNamespacePrefix() 255 .equals(attPrefix) 256 && !"xml".equals(attPrefix)) { 258 contentHandler.startPrefixMapping(attPrefix, 259 element.getNamespaceURI(attPrefix)); 260 } 261 } 262 } 263 else { String prefix = element.getNamespacePrefix(); 265 if (!prefix.equals("") && !"xml".equals(prefix)) { 266 contentHandler.startPrefixMapping(prefix, 267 element.getNamespaceURI()); 268 } 269 270 for (int i = 0; i < element.getAttributeCount(); i++) { 272 Attribute att = element.getAttribute(i); 273 String attPrefix = att.getNamespacePrefix(); 274 if ("xml".equals(attPrefix)) { 275 continue; 276 } 277 else if (!attPrefix.equals("") && 278 !attPrefix.equals(element.getNamespacePrefix())){ 279 contentHandler.startPrefixMapping(attPrefix, 280 att.getNamespaceURI()); 281 } 282 } 283 284 } 285 286 287 AttributesImpl saxAttributes = new AttributesImpl (); 289 for (int i = 0; i < element.getAttributeCount(); i++) { 290 Attribute attribute = element.getAttribute(i); 291 if ("base".equals(attribute.getLocalName()) 296 && "http://www.w3.org/XML/1998/namespace".equals(attribute.getNamespaceURI())) { 297 continue; 298 } 299 saxAttributes.addAttribute(attribute.getNamespaceURI(), 300 attribute.getLocalName(), 301 attribute.getQualifiedName(), 302 getSAXType(attribute), 303 attribute.getValue()); 304 } 305 306 307 contentHandler.startElement( 308 element.getNamespaceURI(), 309 element.getLocalName(), 310 element.getQualifiedName(), 311 saxAttributes); 312 for (int i = 0; i < element.getChildCount(); i++) { 313 process(element.getChild(i)); 314 } 315 contentHandler.endElement(element.getNamespaceURI(), 316 element.getLocalName(), element.getQualifiedName()); 317 318 for (int i = 0; 320 i < element.getNamespaceDeclarationCount(); 321 i++) { 322 String prefix = element.getNamespacePrefix(i); 323 if (parent == null) { 324 String uri = element.getNamespaceURI(prefix); 325 if ("".equals(uri)) continue; 326 } 327 contentHandler.endPrefixMapping(prefix); 328 } 329 if (parent != null) { 330 String prefix = element.getNamespacePrefix(); 332 if (!element.getNamespaceURI(prefix) 333 .equals(parent.getNamespaceURI(prefix))) { 334 contentHandler.endPrefixMapping(prefix); 335 } 336 337 for (int i = 0; i < element.getAttributeCount(); i++) { 339 Attribute att = element.getAttribute(i); 340 String attPrefix = att.getNamespacePrefix(); 341 if (!element.getNamespaceURI(attPrefix) 342 .equals(parent.getNamespaceURI(attPrefix)) 343 && !element.getNamespacePrefix().equals( 344 attPrefix) 345 && !"xml".equals(attPrefix)) { 346 contentHandler.endPrefixMapping(attPrefix); 347 } 348 } 349 } 350 else { String prefix = element.getNamespacePrefix(); 352 if (!prefix.equals("") && !"xml".equals(prefix)) { 353 contentHandler.endPrefixMapping(prefix); 354 } 355 356 for (int i = 0; i < element.getAttributeCount(); i++) { 358 Attribute att = element.getAttribute(i); 359 String attPrefix = att.getNamespacePrefix(); 360 if (!attPrefix.equals("") && !attPrefix 361 .equals(element.getNamespacePrefix()) 362 && !"xml".equals(attPrefix)) { 363 contentHandler.endPrefixMapping(attPrefix); 364 } 365 } 366 367 } 368 } 369 370 371 private static String getSAXType(Attribute attribute) { 372 373 Attribute.Type type = attribute.getType(); 374 if (type.equals(Attribute.Type.UNDECLARED)) return "CDATA"; 375 if (type.equals(Attribute.Type.CDATA)) return "CDATA"; 376 if (type.equals(Attribute.Type.ID)) return "ID"; 377 if (type.equals(Attribute.Type.IDREF)) return "IDREF"; 378 if (type.equals(Attribute.Type.IDREFS)) return "IDREFS"; 379 if (type.equals(Attribute.Type.NMTOKEN)) return "NMTOKEN"; 380 if (type.equals(Attribute.Type.NMTOKENS)) return "NMTOKENS"; 381 if (type.equals(Attribute.Type.ENTITY)) return "ENTITY"; 382 if (type.equals(Attribute.Type.ENTITIES)) return "ENTITIES"; 383 if (type.equals(Attribute.Type.NOTATION)) return "NOTATION"; 384 return "NMTOKEN"; 386 } 387 388 389 403 public void convert(Nodes nodes) throws SAXException { 404 405 if (nodes.size() == 1 && nodes.get(0) instanceof Document) { 406 convert((Document) nodes.get(0)); 407 } 408 else { 409 locator = new LocatorImpl (); 410 contentHandler.setDocumentLocator(locator); 411 contentHandler.startDocument(); 412 for (int i = 0; i < nodes.size(); i++) { 413 process(nodes.get(i)); 414 } 415 contentHandler.endDocument(); 416 } 417 418 } 419 420 421 } 422 | Popular Tags |