View Javadoc

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      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       * &lt;attribute&gt; 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     // DocumentFactory methods
102     // -------------------------------------------------------------------------
103     public Element createElement(QName qname) {
104         // the element may have its own element factory!
105         // use factory from the qname for datatype
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  * 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  */