00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.xml.io;
00032 
00033 import org.w3c.dom.Element;
00034 import org.w3c.dom.NamedNodeMap;
00035 import org.w3c.dom.Node;
00036 
00037 
00046 public class DOMAdaptor {
00047     private XMLHandler targetHandler;
00048 
00049     public DOMAdaptor(XMLHandler targetHandler) {
00050         this.targetHandler = targetHandler;
00051     }
00052 
00053     
00054     
00055     
00056     public void read(Element rootElement) throws java.io.IOException {
00057         try {
00058             domWalker(rootElement);
00059         } catch (org.xml.sax.SAXException e) {
00060             throw new java.io.IOException(e.getMessage());
00061         }
00062     }
00063 
00064     
00065     
00066     
00067     private void domWalker(Node node) throws org.xml.sax.SAXException {
00068         String localName = node.getNodeName();
00069         NamedNodeMap nodeMap = node.getAttributes();
00070         java.util.Vector prefixes = null;
00071         if (nodeMap == null) {
00072             targetHandler.startElement(localName, new EmptyAttributesImpl());
00073         } else {
00074             prefixes = notifyStartPrefixMapping(nodeMap);
00075             targetHandler.startElement(localName, new AttributesImpl(nodeMap));
00076         }
00077         processChilds(node);
00078         targetHandler.endElement(localName);
00079         if (prefixes != null) {
00080             notifyEndPrefixMapping(prefixes);
00081         }
00082     }
00083 
00084     private void processChilds(Node node) throws org.xml.sax.SAXException {
00085         StringBuilder sb = null;
00086         Node child = node.getFirstChild();
00087         while (child != null) {
00088             switch (child.getNodeType()) {
00089             case Node.TEXT_NODE:
00090             case Node.CDATA_SECTION_NODE:
00091                 if (sb == null) {
00092                     sb = new StringBuilder();
00093                 }
00094                 sb.append(((org.w3c.dom.CharacterData) child).getData());
00095                 break;
00096             default:
00097                 domWalker(child);
00098                 break;
00099             }
00100             child = child.getNextSibling();
00101         }
00102         if (sb != null) {
00103             targetHandler.readValue(sb.toString());
00104         }
00105     }
00106 
00107     private java.util.Vector notifyStartPrefixMapping(NamedNodeMap nodeMap)
00108         throws org.xml.sax.SAXException {
00109         java.util.Vector prefixes = null;
00110         int n = nodeMap.getLength();
00111         for (int i = 0; i < n; i++) {
00112             Node attributeNode = nodeMap.item(i);
00113             String attributeName = attributeNode.getNodeName();
00114             if (attributeName.startsWith("xmlns:")) {
00115                 
00116                 if (prefixes == null) {
00117                     prefixes = new java.util.Vector();
00118                 }
00119                 String prefix = attributeName.substring(6);
00120                 String URI = attributeNode.getNodeValue();
00121                 prefixes.addElement(prefix);
00122                 targetHandler.startPrefixMapping(prefix, URI);
00123             }
00124         }
00125         return prefixes;
00126     }
00127 
00128     private void notifyEndPrefixMapping(java.util.Vector prefixes)
00129         throws org.xml.sax.SAXException {
00130         int n = prefixes.size();
00131         for (int i = 0; i < n; i++) {
00132             targetHandler.endPrefixMapping((String) prefixes.elementAt(i));
00133         }
00134     }
00135 
00136     
00137     
00138     
00139     private class AttributesImpl implements Attributes {
00140         private NamedNodeMap attributes;
00141 
00142         AttributesImpl(NamedNodeMap attributes) {
00143             this.attributes = attributes;
00144         }
00145 
00146         public String getValue(int index) {
00147             Node node = attributes.item(index);
00148             if (node == null) {
00149                 return null;
00150             }
00151             return node.getNodeValue();
00152         }
00153 
00154         public String getValue(String qName) {
00155             Node node = attributes.getNamedItem(qName);
00156             if (node == null) {
00157                 return null;
00158             }
00159             return node.getNodeValue();
00160         }
00161 
00162         public String getValue(String uri, String localPart) {
00163             Node node = attributes.getNamedItemNS(uri, localPart);
00164             if (node == null) {
00165                 return null;
00166             }
00167             return node.getNodeValue();
00168         }
00169 
00170         public int getLength() {
00171             return attributes.getLength();
00172         }
00173     }
00174 
00175     protected class EmptyAttributesImpl implements Attributes {
00176         EmptyAttributesImpl() {
00177         }
00178 
00179         public String getValue(int index) {
00180             return null;
00181         }
00182 
00183         public String getValue(String qName) {
00184             return null;
00185         }
00186 
00187         public String getValue(String uri, String localPart) {
00188             return null;
00189         }
00190 
00191         public int getLength() {
00192             return 0;
00193         }
00194     }
00195 }