1 /*
2 * Copyright 2001-2004 (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 * $Id: NamedTypeResolver.java,v 1.6 2004/06/25 08:03:34 maartenc Exp $
8 */
9
10 package org.dom4j.datatype;
11
12 import com.sun.msv.datatype.xsd.XSDatatype;
13
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17
18 import org.dom4j.DocumentFactory;
19 import org.dom4j.Element;
20 import org.dom4j.QName;
21
22
23 /*** <p><code>NamedTypeResolver</code> resolves named types for a given QName.</p>
24 *
25 * @author Yuxin Ruan
26 * @version $Revision: 1.6 $
27 */
28 class NamedTypeResolver {
29
30 Map complexTypeMap=new HashMap();
31 Map simpleTypeMap=new HashMap();
32 Map typedElementMap=new HashMap();
33 Map elementFactoryMap=new HashMap();
34
35 DocumentFactory documentFactory;
36
37 NamedTypeResolver(DocumentFactory documentFactory) {
38 this.documentFactory=documentFactory;
39 }
40
41 void registerComplexType(QName type,DocumentFactory factory) {
42 complexTypeMap.put(type,factory);
43 }
44
45 void registerSimpleType(QName type,XSDatatype datatype) {
46 simpleTypeMap.put(type,datatype);
47 }
48
49 void registerTypedElement(Element element,QName type,DocumentFactory parentFactory) {
50 typedElementMap.put(element,type);
51 elementFactoryMap.put(element,parentFactory);
52 }
53
54 void resolveElementTypes() {
55 Iterator iterator=typedElementMap.keySet().iterator();
56 while (iterator.hasNext()) {
57 Element element=(Element)iterator.next();
58 QName elementQName=getQNameOfSchemaElement(element);
59 QName type=(QName)typedElementMap.get(element);
60
61 if (complexTypeMap.containsKey(type)) {
62 DocumentFactory factory=
63 (DocumentFactory)complexTypeMap.get(type);
64 elementQName.setDocumentFactory(factory);
65 } else if (simpleTypeMap.containsKey(type)) {
66 XSDatatype datatype=(XSDatatype)simpleTypeMap.get(type);
67 DocumentFactory factory=
68 (DocumentFactory)elementFactoryMap.get(element);
69 if (factory instanceof DatatypeElementFactory) {
70 ((DatatypeElementFactory)factory).setChildElementXSDatatype(elementQName,datatype);
71 }
72 }
73 }
74 }
75
76 void resolveNamedTypes() {
77 resolveElementTypes();
78 }
79
80 private QName getQNameOfSchemaElement(Element element) {
81 String name=element.attributeValue("name");
82 return getQName(name);
83 }
84
85 private QName getQName( String name ) {
86 return documentFactory.createQName(name);
87 }
88
89 }
90
91
92
93
94 /*
95 * Redistribution and use of this software and associated documentation
96 * ("Software"), with or without modification, are permitted provided
97 * that the following conditions are met:
98 *
99 * 1. Redistributions of source code must retain copyright
100 * statements and notices. Redistributions must also contain a
101 * copy of this document.
102 *
103 * 2. Redistributions in binary form must reproduce the
104 * above copyright notice, this list of conditions and the
105 * following disclaimer in the documentation and/or other
106 * materials provided with the distribution.
107 *
108 * 3. The name "DOM4J" must not be used to endorse or promote
109 * products derived from this Software without prior written
110 * permission of MetaStuff, Ltd. For written permission,
111 * please contact dom4j-info@metastuff.com.
112 *
113 * 4. Products derived from this Software may not be called "DOM4J"
114 * nor may "DOM4J" appear in their names without prior written
115 * permission of MetaStuff, Ltd. DOM4J is a registered
116 * trademark of MetaStuff, Ltd.
117 *
118 * 5. Due credit should be given to the DOM4J Project -
119 * http://www.dom4j.org
120 *
121 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
122 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
123 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
124 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
125 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
126 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
127 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
128 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132 * OF THE POSSIBILITY OF SUCH DAMAGE.
133 *
134 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
135 *
136 * $Id: NamedTypeResolver.java,v 1.6 2004/06/25 08:03:34 maartenc Exp $
137 */