KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > xml > io > DOMAdaptor


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.xml.io;
32
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NamedNodeMap JavaDoc;
36
37
38 /**
39  *
40  * Adaptor between a DOM and the XMLHandler
41  *
42  * @author Lionel Mestre
43  * @version 0.91
44  *
45  */

46 public class DOMAdaptor {
47   
48   private XMLHandler targetHandler;
49   
50   public DOMAdaptor(XMLHandler targetHandler) {
51     this.targetHandler = targetHandler;
52   }
53
54   
55   //
56
// -- PUBLIC METHOD ------------------------------------------------------
57
//
58

59   public void read(Element JavaDoc rootElement) throws java.io.IOException JavaDoc {
60     try {
61       domWalker(rootElement);
62     } catch (org.xml.sax.SAXException JavaDoc e) {
63       throw new java.io.IOException JavaDoc(e.getMessage());
64     }
65   }
66   
67     
68   //
69
// -- PRIVATE METHODS ------------------------------------------------------
70
//
71

72   private void domWalker(Node JavaDoc node) throws org.xml.sax.SAXException JavaDoc {
73     String JavaDoc localName = node.getNodeName();
74     NamedNodeMap JavaDoc nodeMap = node.getAttributes();
75     java.util.Vector JavaDoc 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 JavaDoc node) throws org.xml.sax.SAXException JavaDoc {
89     StringBuffer JavaDoc sb = null;
90     Node JavaDoc 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 JavaDoc();
96           sb.append(((org.w3c.dom.CharacterData JavaDoc)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 JavaDoc notifyStartPrefixMapping(NamedNodeMap JavaDoc nodeMap) throws org.xml.sax.SAXException JavaDoc {
110     java.util.Vector JavaDoc prefixes = null;
111     int n = nodeMap.getLength();
112     for (int i=0; i<n; i++) {
113       Node JavaDoc attributeNode = nodeMap.item(i);
114       String JavaDoc attributeName = attributeNode.getNodeName();
115       if (attributeName.startsWith("xmlns:")) {
116         // found a namespace attribute
117
if (prefixes == null) prefixes = new java.util.Vector JavaDoc();
118         String JavaDoc prefix = attributeName.substring(6);
119         String JavaDoc 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 JavaDoc prefixes) throws org.xml.sax.SAXException JavaDoc {
128     int n = prefixes.size();
129     for (int i=0; i<n; i++) {
130       targetHandler.endPrefixMapping((String JavaDoc) prefixes.elementAt(i));
131     }
132   }
133  
134  
135   //
136
// -- INNER CLASSES ------------------------------------------------------
137
//
138

139   private class AttributesImpl implements Attributes {
140     
141     private NamedNodeMap JavaDoc attributes;
142     
143     AttributesImpl(NamedNodeMap JavaDoc attributes) {
144       this.attributes = attributes;
145     }
146     
147     public String JavaDoc getValue(int index) {
148       Node JavaDoc node = attributes.item(index);
149       if (node == null) return null;
150       return node.getNodeValue();
151     }
152     
153     public String JavaDoc getValue(String JavaDoc qName) {
154       Node JavaDoc node = attributes.getNamedItem(qName);
155       if (node == null) return null;
156       return node.getNodeValue();
157     }
158     
159     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localPart) {
160       Node JavaDoc 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 JavaDoc getValue(int index) { return null; }
173     public String JavaDoc getValue(String JavaDoc qName) { return null; }
174     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localPart) { return null; }
175     public int getLength() { return 0; }
176   }
177
178 }
Popular Tags