1 7 8 package org.dom4j.datatype; 9 10 import com.sun.msv.datatype.xsd.XSDatatype; 11 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import org.dom4j.DocumentFactory; 17 import org.dom4j.Element; 18 import org.dom4j.QName; 19 20 28 class NamedTypeResolver { 29 protected Map complexTypeMap = new HashMap (); 30 31 protected Map simpleTypeMap = new HashMap (); 32 33 protected Map typedElementMap = new HashMap (); 34 35 protected Map elementFactoryMap = new HashMap (); 36 37 protected DocumentFactory documentFactory; 38 39 NamedTypeResolver(DocumentFactory documentFactory) { 40 this.documentFactory = documentFactory; 41 } 42 43 void registerComplexType(QName type, DocumentFactory factory) { 44 complexTypeMap.put(type, factory); 45 } 46 47 void registerSimpleType(QName type, XSDatatype datatype) { 48 simpleTypeMap.put(type, datatype); 49 } 50 51 void registerTypedElement(Element element, QName type, 52 DocumentFactory parentFactory) { 53 typedElementMap.put(element, type); 54 elementFactoryMap.put(element, parentFactory); 55 } 56 57 void resolveElementTypes() { 58 Iterator iterator = typedElementMap.keySet().iterator(); 59 60 while (iterator.hasNext()) { 61 Element element = (Element) iterator.next(); 62 QName elementQName = getQNameOfSchemaElement(element); 63 QName type = (QName) typedElementMap.get(element); 64 65 if (complexTypeMap.containsKey(type)) { 66 DocumentFactory factory = (DocumentFactory) complexTypeMap 67 .get(type); 68 elementQName.setDocumentFactory(factory); 69 } else if (simpleTypeMap.containsKey(type)) { 70 XSDatatype datatype = (XSDatatype) simpleTypeMap.get(type); 71 DocumentFactory factory = (DocumentFactory) elementFactoryMap 72 .get(element); 73 74 if (factory instanceof DatatypeElementFactory) { 75 ((DatatypeElementFactory) factory) 76 .setChildElementXSDatatype(elementQName, datatype); 77 } 78 } 79 } 80 } 81 82 void resolveNamedTypes() { 83 resolveElementTypes(); 84 } 85 86 private QName getQNameOfSchemaElement(Element element) { 87 String name = element.attributeValue("name"); 88 89 return getQName(name); 90 } 91 92 private QName getQName(String name) { 93 return documentFactory.createQName(name); 94 } 95 } 96 97 133 | Popular Tags |