KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > DefaultDOMHandlerImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: DefaultDOMHandlerImpl.java,v 1.4 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19
20 import java.util.HashMap JavaDoc;
21
22 import org.eclipse.emf.ecore.EObject;
23 import org.eclipse.emf.ecore.EStructuralFeature;
24 import org.eclipse.emf.ecore.util.ExtendedMetaData;
25 import org.eclipse.emf.ecore.xmi.DOMHandler;
26 import org.eclipse.emf.ecore.xmi.DOMHelper;
27 import org.w3c.dom.Attr JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30
31 /**
32  * Implementation of the default {@link DOMHandler} and {@link DOMHelper}
33  * @since 2.1.0
34  */

35 public class DefaultDOMHandlerImpl implements DOMHandler, DOMHelper
36 {
37   /** store node to actual value mapping */
38   protected final HashMap JavaDoc nodeToObject = new HashMap JavaDoc();
39
40   /** store node to containment feature mapping */
41   protected final HashMap JavaDoc nodeToFeature = new HashMap JavaDoc();
42
43   /** store node to container. used only to record some text/cdata nodes */
44   protected final HashMap JavaDoc nodeToContainer = new HashMap JavaDoc();
45
46   protected ExtendedMetaData extendedMetaData;
47
48   void setExtendedMetaData(ExtendedMetaData extendedMetaData)
49   {
50     this.extendedMetaData = extendedMetaData;
51   }
52
53   public EObject getContainer(Node JavaDoc node)
54   {
55     short type = node.getNodeType();
56     switch (type)
57     {
58       case Node.ELEMENT_NODE:
59       {
60         Object JavaDoc o = nodeToObject.get(node);
61         if (o != null && o instanceof EObject)
62         {
63           return ((EObject)o).eContainer();
64         }
65         return (EObject)nodeToObject.get(node.getParentNode());
66       }
67       case Node.TEXT_NODE:
68       case Node.CDATA_SECTION_NODE:
69       {
70         Object JavaDoc o = nodeToContainer.get(node);
71         if (o != null)
72         {
73           return (EObject)o;
74         }
75         return (EObject)nodeToObject.get(node.getParentNode().getParentNode());
76       }
77       case Node.ATTRIBUTE_NODE:
78         return (EObject)nodeToObject.get(((Attr JavaDoc)node).getOwnerElement());
79       default:
80         return null;
81     }
82   }
83
84   /* (non-Javadoc)
85    * @see org.eclipse.emf.ecore.xmi.DOMHelper#getValue(org.w3c.dom.Node)
86    */

87   public Object JavaDoc getValue(Node JavaDoc node)
88   {
89     Object JavaDoc value = nodeToObject.get(node);
90     if (value == null)
91     {
92       if (node.getNodeType() == Node.TEXT_NODE)
93       {
94         value = nodeToObject.get(node.getParentNode());
95       }
96     }
97     return value;
98   }
99
100   public EStructuralFeature getEStructuralFeature(Node JavaDoc node)
101   {
102     short type = node.getNodeType();
103     switch (type)
104     {
105       case Node.ELEMENT_NODE:
106         return (EStructuralFeature)nodeToFeature.get(node);
107       case Node.ATTRIBUTE_NODE:
108       {
109         EObject obj = (EObject)nodeToObject.get(((Attr JavaDoc)node).getOwnerElement());
110         if (extendedMetaData == null)
111         {
112           return obj.eClass().getEStructuralFeature(node.getLocalName());
113         }
114         else if (obj != null)
115         {
116           return extendedMetaData.getAttribute(obj.eClass(), node.getNamespaceURI(), node.getLocalName());
117         }
118       }
119       case Node.TEXT_NODE:
120       case Node.CDATA_SECTION_NODE:
121       {
122         EStructuralFeature feature = (EStructuralFeature)nodeToFeature.get(node);
123         if (feature == null)
124         {
125           feature = (EStructuralFeature)nodeToFeature.get(node.getParentNode());
126         }
127         return feature;
128       }
129       default:
130         return null;
131     }
132   }
133
134   public void recordValues(Node JavaDoc node, EObject container, EStructuralFeature feature, Object JavaDoc value)
135   {
136     debug(node, container, feature, value);
137
138     short type = node.getNodeType();
139     switch (type)
140     {
141       case Node.ELEMENT_NODE:
142       {
143         nodeToFeature.put(node, feature);
144         // fall through
145
}
146       case Node.ATTRIBUTE_NODE:
147       {
148         if (value != null)
149         {
150           nodeToObject.put(node, value);
151         }
152         break;
153       }
154       case Node.TEXT_NODE:
155       {
156         if (nodeToObject.get(node.getParentNode()) == value)
157         {
158           break;
159         }
160         //fall through...
161
}
162       case Node.CDATA_SECTION_NODE:
163       {
164         nodeToFeature.put(node, feature);
165         nodeToContainer.put(node, container);
166         nodeToObject.put(node, value);
167       }
168     }
169   }
170
171   public DOMHelper getDOMHelper()
172   {
173     return this;
174   }
175
176   final static boolean DEBUG = false;
177
178   private static final void debug(Node JavaDoc node, EObject container, EStructuralFeature feature, Object JavaDoc value)
179   {
180     if (DEBUG)
181     {
182       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
183
184       buf.append("recordValues( ");
185       buf.append(" {");
186       switch (node.getNodeType())
187       {
188         case Node.ELEMENT_NODE:
189           buf.append("ELEMENT_NODE ");
190           break;
191         case Node.ATTRIBUTE_NODE:
192           buf.append("ATTRIBUTE_NODE ");
193           break;
194         case Node.TEXT_NODE:
195           buf.append("TEXT_NODE ");
196           break;
197         case Node.CDATA_SECTION_NODE:
198           buf.append("CDATA_SECTION_NODE ");
199           break;
200         default:
201           buf.append("UNKNOWN ");
202           break;
203       }
204       buf.append(node.getNodeName());
205       buf.append("{ " + node.getNodeValue() + " }, ");
206       if (container != null)
207       {
208         buf.append(container.eClass().getName() + ", ");
209       }
210       else
211       {
212         buf.append("null, ");
213       }
214       if (feature != null)
215       {
216         buf.append(feature.getName() + ", ");
217       }
218       else
219       {
220         buf.append("null, ");
221       }
222       if (value != null)
223       {
224         buf.append(value.getClass().getName() + ": " + value.toString() + ");");
225       }
226       else
227       {
228         buf.append("null);");
229       }
230       System.out.println(buf.toString());
231     }
232   }
233 }
234
Popular Tags