1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 20 import java.util.HashMap ; 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 ; 28 import org.w3c.dom.Node ; 29 30 31 35 public class DefaultDOMHandlerImpl implements DOMHandler, DOMHelper 36 { 37 38 protected final HashMap nodeToObject = new HashMap (); 39 40 41 protected final HashMap nodeToFeature = new HashMap (); 42 43 44 protected final HashMap nodeToContainer = new HashMap (); 45 46 protected ExtendedMetaData extendedMetaData; 47 48 void setExtendedMetaData(ExtendedMetaData extendedMetaData) 49 { 50 this.extendedMetaData = extendedMetaData; 51 } 52 53 public EObject getContainer(Node node) 54 { 55 short type = node.getNodeType(); 56 switch (type) 57 { 58 case Node.ELEMENT_NODE: 59 { 60 Object 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 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 )node).getOwnerElement()); 79 default: 80 return null; 81 } 82 } 83 84 87 public Object getValue(Node node) 88 { 89 Object 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 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 )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 node, EObject container, EStructuralFeature feature, Object 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 } 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 } 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 node, EObject container, EStructuralFeature feature, Object value) 179 { 180 if (DEBUG) 181 { 182 StringBuffer buf = new StringBuffer (); 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 |