1 /*
2 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 */
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 /***
21 * <p>
22 * <code>NamedTypeResolver</code> resolves named types for a given QName.
23 * </p>
24 *
25 * @author Yuxin Ruan
26 * @version $Revision: 1.8 $
27 */
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 /*
98 * Redistribution and use of this software and associated documentation
99 * ("Software"), with or without modification, are permitted provided that the
100 * following conditions are met:
101 *
102 * 1. Redistributions of source code must retain copyright statements and
103 * notices. Redistributions must also contain a copy of this document.
104 *
105 * 2. Redistributions in binary form must reproduce the above copyright notice,
106 * this list of conditions and the following disclaimer in the documentation
107 * and/or other materials provided with the distribution.
108 *
109 * 3. The name "DOM4J" must not be used to endorse or promote products derived
110 * from this Software without prior written permission of MetaStuff, Ltd. For
111 * written permission, please contact dom4j-info@metastuff.com.
112 *
113 * 4. Products derived from this Software may not be called "DOM4J" nor may
114 * "DOM4J" appear in their names without prior written permission of MetaStuff,
115 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
116 *
117 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
118 *
119 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
120 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
121 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
122 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
123 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
124 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
125 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
126 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
127 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
128 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
129 * POSSIBILITY OF SUCH DAMAGE.
130 *
131 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
132 */