|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
NamedTypeResolver.java | 75% | 100% | 100% | 94,7% |
|
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 | 27 | NamedTypeResolver(DocumentFactory documentFactory) { |
40 | 27 | this.documentFactory = documentFactory; |
41 | } | |
42 | ||
43 | 2 | void registerComplexType(QName type, DocumentFactory factory) { |
44 | 2 | complexTypeMap.put(type, factory); |
45 | } | |
46 | ||
47 | 1 | void registerSimpleType(QName type, XSDatatype datatype) { |
48 | 1 | simpleTypeMap.put(type, datatype); |
49 | } | |
50 | ||
51 | 3 | void registerTypedElement(Element element, QName type, |
52 | DocumentFactory parentFactory) { | |
53 | 3 | typedElementMap.put(element, type); |
54 | 3 | elementFactoryMap.put(element, parentFactory); |
55 | } | |
56 | ||
57 | 25 | void resolveElementTypes() { |
58 | 25 | Iterator iterator = typedElementMap.keySet().iterator(); |
59 | ||
60 | 25 | while (iterator.hasNext()) { |
61 | 3 | Element element = (Element) iterator.next(); |
62 | 3 | QName elementQName = getQNameOfSchemaElement(element); |
63 | 3 | QName type = (QName) typedElementMap.get(element); |
64 | ||
65 | 3 | if (complexTypeMap.containsKey(type)) { |
66 | 2 | DocumentFactory factory = (DocumentFactory) complexTypeMap |
67 | .get(type); | |
68 | 2 | elementQName.setDocumentFactory(factory); |
69 | 1 | } else if (simpleTypeMap.containsKey(type)) { |
70 | 1 | XSDatatype datatype = (XSDatatype) simpleTypeMap.get(type); |
71 | 1 | DocumentFactory factory = (DocumentFactory) elementFactoryMap |
72 | .get(element); | |
73 | ||
74 | 1 | if (factory instanceof DatatypeElementFactory) { |
75 | 1 | ((DatatypeElementFactory) factory) |
76 | .setChildElementXSDatatype(elementQName, datatype); | |
77 | } | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | 25 | void resolveNamedTypes() { |
83 | 25 | resolveElementTypes(); |
84 | } | |
85 | ||
86 | 3 | private QName getQNameOfSchemaElement(Element element) { |
87 | 3 | String name = element.attributeValue("name"); |
88 | ||
89 | 3 | return getQName(name); |
90 | } | |
91 | ||
92 | 3 | private QName getQName(String name) { |
93 | 3 | 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 | */ |
|