1
2
3
4
5
6
7
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.Map;
16
17 import org.dom4j.Attribute;
18 import org.dom4j.DocumentFactory;
19 import org.dom4j.Element;
20 import org.dom4j.QName;
21
22 /*** <p><code>DatatypeElementFactory</code> is a factory for a specific Element
23 * in an XML Schema.</p>
24 *
25 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
26 * @author Yuxin Ruan
27 * @version $Revision: 1.7 $
28 */
29 public class DatatypeElementFactory extends DocumentFactory {
30
31 private QName elementQName;
32
33
34 public DatatypeElementFactory(QName elementQName) {
35 this.elementQName = elementQName;
36 }
37
38 /*** Cache of <code>XSDatatype</code> instances per
39 * Attribute <code>QName</code> */
40 private Map attributeXSDatatypes = new HashMap();
41
42 /*** Cache of <code>XSDatatype</code> instances per
43 * child Element <code>QName</code> */
44 private Map childrenXSDatatypes = new HashMap();
45
46
47
48 /*** @return the QName this element factory is associated with */
49 public QName getQName() {
50 return elementQName;
51 }
52
53 /*** @return the <code>XSDatatype</code> associated with the given Attribute
54 * QName
55 */
56 public XSDatatype getAttributeXSDatatype( QName attributeQName ) {
57 return (XSDatatype) attributeXSDatatypes.get( attributeQName );
58 }
59
60 /*** Registers the given <code>XSDatatype</code> for the given
61 * <attribute> QNames
62 */
63 public void setAttributeXSDatatype( QName attributeQName, XSDatatype dataType ) {
64 attributeXSDatatypes.put( attributeQName, dataType );
65 }
66
67
68 /*** @return the <code>XSDatatype</code> associated with the given child
69 * Element QName
70 */
71 public XSDatatype getChildElementXSDatatype( QName qname ) {
72 return (XSDatatype) childrenXSDatatypes.get( qname );
73 }
74
75 public void setChildElementXSDatatype( QName qname, XSDatatype dataType ) {
76 childrenXSDatatypes.put( qname, dataType );
77 }
78
79
80
81
82 public Element createElement(QName qname) {
83
84
85 XSDatatype dataType = getChildElementXSDatatype( qname );
86 if ( dataType != null ) {
87 return new DatatypeElement(qname, dataType);
88 }
89 DocumentFactory documentFactory = qname.getDocumentFactory();
90 if ( documentFactory instanceof DatatypeElementFactory ) {
91 DatatypeElementFactory factory = (DatatypeElementFactory) documentFactory;
92 dataType = factory.getChildElementXSDatatype( qname );
93 if ( dataType != null ) {
94 return new DatatypeElement(qname, dataType);
95 }
96 }
97 return super.createElement( qname );
98 }
99
100 public Attribute createAttribute(Element owner, QName qname, String value) {
101 XSDatatype dataType = getAttributeXSDatatype(qname);
102 if ( dataType == null ) {
103 return super.createAttribute( owner, qname, value );
104 }
105 else {
106 return new DatatypeAttribute( qname, dataType, value );
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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