|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DatatypeElementFactory.java | 87,5% | 95% | 87,5% | 91,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.Map; | |
14 | ||
15 | import org.dom4j.Attribute; | |
16 | import org.dom4j.DocumentFactory; | |
17 | import org.dom4j.Element; | |
18 | import org.dom4j.QName; | |
19 | ||
20 | /** | |
21 | * <p> | |
22 | * <code>DatatypeElementFactory</code> is a factory for a specific Element in | |
23 | * an XML Schema. | |
24 | * </p> | |
25 | * | |
26 | * @author <a href="mailto:jstrachan@apache.org">James Strachan </a> | |
27 | * @author Yuxin Ruan | |
28 | * @version $Revision: 1.9 $ | |
29 | */ | |
30 | public class DatatypeElementFactory extends DocumentFactory { | |
31 | private QName elementQName; | |
32 | ||
33 | /** | |
34 | * Cache of <code>XSDatatype</code> instances per Attribute | |
35 | * <code>QName</code> | |
36 | */ | |
37 | private Map attributeXSDatatypes = new HashMap(); | |
38 | ||
39 | /** | |
40 | * Cache of <code>XSDatatype</code> instances per child Element | |
41 | * <code>QName</code> | |
42 | */ | |
43 | private Map childrenXSDatatypes = new HashMap(); | |
44 | ||
45 | 403 | public DatatypeElementFactory(QName elementQName) { |
46 | 403 | this.elementQName = elementQName; |
47 | } | |
48 | ||
49 | /** | |
50 | * DOCUMENT ME! | |
51 | * | |
52 | * @return the QName this element factory is associated with | |
53 | */ | |
54 | 0 | public QName getQName() { |
55 | 0 | return elementQName; |
56 | } | |
57 | ||
58 | /** | |
59 | * DOCUMENT ME! | |
60 | * | |
61 | * @param attributeQName | |
62 | * DOCUMENT ME! | |
63 | * | |
64 | * @return the <code>XSDatatype</code> associated with the given Attribute | |
65 | * QName | |
66 | */ | |
67 | 686 | public XSDatatype getAttributeXSDatatype(QName attributeQName) { |
68 | 686 | return (XSDatatype) attributeXSDatatypes.get(attributeQName); |
69 | } | |
70 | ||
71 | /** | |
72 | * Registers the given <code>XSDatatype</code> for the given | |
73 | * <attribute> QNames | |
74 | * | |
75 | * @param attributeQName | |
76 | * DOCUMENT ME! | |
77 | * @param type | |
78 | * DOCUMENT ME! | |
79 | */ | |
80 | 265 | public void setAttributeXSDatatype(QName attributeQName, XSDatatype type) { |
81 | 265 | attributeXSDatatypes.put(attributeQName, type); |
82 | } | |
83 | ||
84 | /** | |
85 | * DOCUMENT ME! | |
86 | * | |
87 | * @param qname | |
88 | * DOCUMENT ME! | |
89 | * | |
90 | * @return the <code>XSDatatype</code> associated with the given child | |
91 | * Element QName | |
92 | */ | |
93 | 3311 | public XSDatatype getChildElementXSDatatype(QName qname) { |
94 | 3311 | return (XSDatatype) childrenXSDatatypes.get(qname); |
95 | } | |
96 | ||
97 | 144 | public void setChildElementXSDatatype(QName qname, XSDatatype dataType) { |
98 | 144 | childrenXSDatatypes.put(qname, dataType); |
99 | } | |
100 | ||
101 | // DocumentFactory methods | |
102 | // ------------------------------------------------------------------------- | |
103 | 1656 | public Element createElement(QName qname) { |
104 | // the element may have its own element factory! | |
105 | // use factory from the qname for datatype | |
106 | 1656 | XSDatatype dataType = getChildElementXSDatatype(qname); |
107 | ||
108 | 1656 | if (dataType != null) { |
109 | 1 | return new DatatypeElement(qname, dataType); |
110 | } | |
111 | ||
112 | 1655 | DocumentFactory factory = qname.getDocumentFactory(); |
113 | ||
114 | 1655 | if (factory instanceof DatatypeElementFactory) { |
115 | 1655 | DatatypeElementFactory dtFactory = (DatatypeElementFactory) factory; |
116 | 1655 | dataType = dtFactory.getChildElementXSDatatype(qname); |
117 | ||
118 | 1655 | if (dataType != null) { |
119 | 1304 | return new DatatypeElement(qname, dataType); |
120 | } | |
121 | } | |
122 | ||
123 | 351 | return super.createElement(qname); |
124 | } | |
125 | ||
126 | 686 | public Attribute createAttribute(Element owner, QName qname, String value) { |
127 | 686 | XSDatatype dataType = getAttributeXSDatatype(qname); |
128 | ||
129 | 686 | if (dataType == null) { |
130 | 41 | return super.createAttribute(owner, qname, value); |
131 | } else { | |
132 | 645 | return new DatatypeAttribute(qname, dataType, value); |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | /* | |
138 | * Redistribution and use of this software and associated documentation | |
139 | * ("Software"), with or without modification, are permitted provided that the | |
140 | * following conditions are met: | |
141 | * | |
142 | * 1. Redistributions of source code must retain copyright statements and | |
143 | * notices. Redistributions must also contain a copy of this document. | |
144 | * | |
145 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
146 | * this list of conditions and the following disclaimer in the documentation | |
147 | * and/or other materials provided with the distribution. | |
148 | * | |
149 | * 3. The name "DOM4J" must not be used to endorse or promote products derived | |
150 | * from this Software without prior written permission of MetaStuff, Ltd. For | |
151 | * written permission, please contact dom4j-info@metastuff.com. | |
152 | * | |
153 | * 4. Products derived from this Software may not be called "DOM4J" nor may | |
154 | * "DOM4J" appear in their names without prior written permission of MetaStuff, | |
155 | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd. | |
156 | * | |
157 | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org | |
158 | * | |
159 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND | |
160 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
161 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
162 | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE | |
163 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
164 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
165 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
166 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
167 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
168 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
169 | * POSSIBILITY OF SUCH DAMAGE. | |
170 | * | |
171 | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. | |
172 | */ |
|