001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DatatypeElementFactory.java,v 1.3 2001/11/02 10:30:22 jstrachan Exp $ 008 */ 009 010package org.dom4j.datatype; 011 012import com.sun.msv.datatype.xsd.XSDatatype; 013 014import java.util.HashMap; 015import java.util.Map; 016 017 018import org.dom4j.Attribute; 019import org.dom4j.Document; 020import org.dom4j.DocumentFactory; 021import org.dom4j.Element; 022import org.dom4j.QName; 023 024import org.xml.sax.Attributes; 025 026/** <p><code>DatatypeElementFactory</code> is a factory for a specific Element 027 * in an XML Schema.</p> 028 * 029 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 030 * @author Yuxin Ruan 031 * @version $Revision: 1.3 $ 032 */ 033public class DatatypeElementFactory extends DocumentFactory { 034 035 private QName elementQName; 036 037 038 public DatatypeElementFactory(QName elementQName) { 039 this.elementQName = elementQName; 040 } 041 042 /** Cache of <code>XSDatatype</code> instances per 043 * Attribute <code>QName</code> */ 044 private Map attributeXSDatatypes = new HashMap(); 045 046 /** Cache of <code>XSDatatype</code> instances per 047 * child Element <code>QName</code> */ 048 private Map childrenXSDatatypes = new HashMap(); 049 050 051 052 /** @return the QName this element factory is associated with */ 053 public QName getQName() { 054 return elementQName; 055 } 056 057 /** @return the <code>XSDatatype</code> associated with the given Attribute 058 * QName 059 */ 060 public XSDatatype getAttributeXSDatatype( QName attributeQName ) { 061 return (XSDatatype) attributeXSDatatypes.get( attributeQName ); 062 } 063 064 /** Registers the given <code>XSDatatype</code> for the given 065 * <attribute> QNames 066 */ 067 public void setAttributeXSDatatype( QName attributeQName, XSDatatype dataType ) { 068 attributeXSDatatypes.put( attributeQName, dataType ); 069 } 070 071 072 /** @return the <code>XSDatatype</code> associated with the given child 073 * Element QName 074 */ 075 public XSDatatype getChildElementXSDatatype( QName qname ) { 076 return (XSDatatype) childrenXSDatatypes.get( qname ); 077 } 078 079 public void setChildElementXSDatatype( QName qname, XSDatatype dataType ) { 080 childrenXSDatatypes.put( qname, dataType ); 081 } 082 083 084 // DocumentFactory methods 085 //------------------------------------------------------------------------- 086 public Element createElement(QName qname) { 087 //the element may have its own element factory! 088 //use factory from the qname for datatype 089 XSDatatype dataType = getChildElementXSDatatype( qname ); 090 if ( dataType != null ) { 091 return new DatatypeElement(qname, dataType); 092 } 093 DocumentFactory documentFactory = qname.getDocumentFactory(); 094 if ( documentFactory instanceof DatatypeElementFactory ) { 095 DatatypeElementFactory factory = (DatatypeElementFactory) documentFactory; 096 dataType = factory.getChildElementXSDatatype( qname ); 097 if ( dataType != null ) { 098 return new DatatypeElement(qname, dataType); 099 } 100 } 101 return super.createElement( qname ); 102 } 103 104 public Attribute createAttribute(Element owner, QName qname, String value) { 105 XSDatatype dataType = getAttributeXSDatatype(qname); 106 if ( dataType == null ) { 107 return super.createAttribute( owner, qname, value ); 108 } 109 else { 110 return new DatatypeAttribute( qname, dataType, value ); 111 } 112 } 113} 114 115 116 117 118/* 119 * Redistribution and use of this software and associated documentation 120 * ("Software"), with or without modification, are permitted provided 121 * that the following conditions are met: 122 * 123 * 1. Redistributions of source code must retain copyright 124 * statements and notices. Redistributions must also contain a 125 * copy of this document. 126 * 127 * 2. Redistributions in binary form must reproduce the 128 * above copyright notice, this list of conditions and the 129 * following disclaimer in the documentation and/or other 130 * materials provided with the distribution. 131 * 132 * 3. The name "DOM4J" must not be used to endorse or promote 133 * products derived from this Software without prior written 134 * permission of MetaStuff, Ltd. For written permission, 135 * please contact dom4j-info@metastuff.com. 136 * 137 * 4. Products derived from this Software may not be called "DOM4J" 138 * nor may "DOM4J" appear in their names without prior written 139 * permission of MetaStuff, Ltd. DOM4J is a registered 140 * trademark of MetaStuff, Ltd. 141 * 142 * 5. Due credit should be given to the DOM4J Project 143 * (http://dom4j.org/). 144 * 145 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 146 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 147 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 148 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 149 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 150 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 151 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 152 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 153 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 154 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 155 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 156 * OF THE POSSIBILITY OF SUCH DAMAGE. 157 * 158 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 159 * 160 * $Id: DatatypeElementFactory.java,v 1.3 2001/11/02 10:30:22 jstrachan Exp $ 161 */