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: DatatypeElementFactory.java,v 1.7 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.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 // DocumentFactory methods
81 //-------------------------------------------------------------------------
82 public Element createElement(QName qname) {
83 //the element may have its own element factory!
84 //use factory from the qname for datatype
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 * Redistribution and use of this software and associated documentation
116 * ("Software"), with or without modification, are permitted provided
117 * that the following conditions are met:
118 *
119 * 1. Redistributions of source code must retain copyright
120 * statements and notices. Redistributions must also contain a
121 * copy of this document.
122 *
123 * 2. Redistributions in binary form must reproduce the
124 * above copyright notice, this list of conditions and the
125 * following disclaimer in the documentation and/or other
126 * materials provided with the distribution.
127 *
128 * 3. The name "DOM4J" must not be used to endorse or promote
129 * products derived from this Software without prior written
130 * permission of MetaStuff, Ltd. For written permission,
131 * please contact dom4j-info@metastuff.com.
132 *
133 * 4. Products derived from this Software may not be called "DOM4J"
134 * nor may "DOM4J" appear in their names without prior written
135 * permission of MetaStuff, Ltd. DOM4J is a registered
136 * trademark of MetaStuff, Ltd.
137 *
138 * 5. Due credit should be given to the DOM4J Project -
139 * http://www.dom4j.org
140 *
141 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
142 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
143 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
144 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
145 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
146 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
147 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
148 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
149 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
150 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
151 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
152 * OF THE POSSIBILITY OF SUCH DAMAGE.
153 *
154 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
155 *
156 * $Id: DatatypeElementFactory.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
157 */