1
2
3
4
5
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 public DatatypeElementFactory(QName elementQName) {
46 this.elementQName = elementQName;
47 }
48
49 /***
50 * DOCUMENT ME!
51 *
52 * @return the QName this element factory is associated with
53 */
54 public QName getQName() {
55 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 public XSDatatype getAttributeXSDatatype(QName attributeQName) {
68 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 public void setAttributeXSDatatype(QName attributeQName, XSDatatype type) {
81 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 public XSDatatype getChildElementXSDatatype(QName qname) {
94 return (XSDatatype) childrenXSDatatypes.get(qname);
95 }
96
97 public void setChildElementXSDatatype(QName qname, XSDatatype dataType) {
98 childrenXSDatatypes.put(qname, dataType);
99 }
100
101
102
103 public Element createElement(QName qname) {
104
105
106 XSDatatype dataType = getChildElementXSDatatype(qname);
107
108 if (dataType != null) {
109 return new DatatypeElement(qname, dataType);
110 }
111
112 DocumentFactory factory = qname.getDocumentFactory();
113
114 if (factory instanceof DatatypeElementFactory) {
115 DatatypeElementFactory dtFactory = (DatatypeElementFactory) factory;
116 dataType = dtFactory.getChildElementXSDatatype(qname);
117
118 if (dataType != null) {
119 return new DatatypeElement(qname, dataType);
120 }
121 }
122
123 return super.createElement(qname);
124 }
125
126 public Attribute createAttribute(Element owner, QName qname, String value) {
127 XSDatatype dataType = getAttributeXSDatatype(qname);
128
129 if (dataType == null) {
130 return super.createAttribute(owner, qname, value);
131 } else {
132 return new DatatypeAttribute(qname, dataType, value);
133 }
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172