1 31 package org.objectweb.proactive.core.xml.io; 32 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NamedNodeMap ; 36 37 38 46 public class DOMAdaptor { 47 48 private XMLHandler targetHandler; 49 50 public DOMAdaptor(XMLHandler targetHandler) { 51 this.targetHandler = targetHandler; 52 } 53 54 55 59 public void read(Element rootElement) throws java.io.IOException { 60 try { 61 domWalker(rootElement); 62 } catch (org.xml.sax.SAXException e) { 63 throw new java.io.IOException (e.getMessage()); 64 } 65 } 66 67 68 72 private void domWalker(Node node) throws org.xml.sax.SAXException { 73 String localName = node.getNodeName(); 74 NamedNodeMap nodeMap = node.getAttributes(); 75 java.util.Vector prefixes = null; 76 if (nodeMap == null) 77 targetHandler.startElement(localName, new EmptyAttributesImpl()); 78 else { 79 prefixes = notifyStartPrefixMapping(nodeMap); 80 targetHandler.startElement(localName, new AttributesImpl(nodeMap)); 81 } 82 processChilds(node); 83 targetHandler.endElement(localName); 84 if (prefixes != null) notifyEndPrefixMapping(prefixes); 85 } 86 87 88 private void processChilds(Node node) throws org.xml.sax.SAXException { 89 StringBuffer sb = null; 90 Node child = node.getFirstChild(); 91 while (child != null) { 92 switch (child.getNodeType()) { 93 case Node.TEXT_NODE : 94 case Node.CDATA_SECTION_NODE: 95 if (sb==null) sb = new StringBuffer (); 96 sb.append(((org.w3c.dom.CharacterData )child).getData()); 97 break; 98 99 default: 100 domWalker(child); 101 break; 102 } 103 child = child.getNextSibling(); 104 } 105 if (sb != null) targetHandler.readValue(sb.toString()); 106 } 107 108 109 private java.util.Vector notifyStartPrefixMapping(NamedNodeMap nodeMap) throws org.xml.sax.SAXException { 110 java.util.Vector prefixes = null; 111 int n = nodeMap.getLength(); 112 for (int i=0; i<n; i++) { 113 Node attributeNode = nodeMap.item(i); 114 String attributeName = attributeNode.getNodeName(); 115 if (attributeName.startsWith("xmlns:")) { 116 if (prefixes == null) prefixes = new java.util.Vector (); 118 String prefix = attributeName.substring(6); 119 String URI = attributeNode.getNodeValue(); 120 prefixes.addElement(prefix); 121 targetHandler.startPrefixMapping(prefix,URI); 122 } 123 } 124 return prefixes; 125 } 126 127 private void notifyEndPrefixMapping(java.util.Vector prefixes) throws org.xml.sax.SAXException { 128 int n = prefixes.size(); 129 for (int i=0; i<n; i++) { 130 targetHandler.endPrefixMapping((String ) prefixes.elementAt(i)); 131 } 132 } 133 134 135 139 private class AttributesImpl implements Attributes { 140 141 private NamedNodeMap attributes; 142 143 AttributesImpl(NamedNodeMap attributes) { 144 this.attributes = attributes; 145 } 146 147 public String getValue(int index) { 148 Node node = attributes.item(index); 149 if (node == null) return null; 150 return node.getNodeValue(); 151 } 152 153 public String getValue(String qName) { 154 Node node = attributes.getNamedItem(qName); 155 if (node == null) return null; 156 return node.getNodeValue(); 157 } 158 159 public String getValue(String uri, String localPart) { 160 Node node = attributes.getNamedItemNS(uri,localPart); 161 if (node == null) return null; 162 return node.getNodeValue(); 163 } 164 165 public int getLength() { 166 return attributes.getLength(); 167 } 168 } 169 170 protected class EmptyAttributesImpl implements Attributes { 171 EmptyAttributesImpl() {} 172 public String getValue(int index) { return null; } 173 public String getValue(String qName) { return null; } 174 public String getValue(String uri, String localPart) { return null; } 175 public int getLength() { return 0; } 176 } 177 178 } | Popular Tags |