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: NamedTypeResolver.java,v 1.2 2001/11/13 12:27:44 jstrachan Exp $
008 */
009
010package org.dom4j.datatype;
011
012import com.sun.msv.datatype.xsd.XSDatatype;
013
014import java.util.HashMap;
015import java.util.Iterator;
016import java.util.Map;
017
018import org.dom4j.Attribute;
019import org.dom4j.Document;
020import org.dom4j.DocumentFactory;
021import org.dom4j.Element;
022import org.dom4j.Node;
023import org.dom4j.QName;
024
025
026/** <p><code>NamedTypeResolver</code> resolves named types for a given QName.</p>
027 *
028 * @author Yuxin Ruan
029 * @version $Revision: 1.2 $
030 */
031class NamedTypeResolver {
032
033    Map complexTypeMap=new HashMap();
034    Map simpleTypeMap=new HashMap();
035    Map typedElementMap=new HashMap();
036    Map elementFactoryMap=new HashMap();
037
038    DocumentFactory documentFactory;
039
040    NamedTypeResolver(DocumentFactory documentFactory) {
041        this.documentFactory=documentFactory;
042    }
043
044    void registerComplexType(QName type,DocumentFactory factory) {
045        complexTypeMap.put(type,factory);
046    }
047
048    void registerSimpleType(QName type,XSDatatype datatype) {
049        simpleTypeMap.put(type,datatype);
050    }
051
052    void registerTypedElement(Element element,QName type,DocumentFactory parentFactory) {
053        typedElementMap.put(element,type);
054        elementFactoryMap.put(element,parentFactory);
055    }
056
057    void resolveElementTypes() {
058        Iterator iterator=typedElementMap.keySet().iterator();
059        while (iterator.hasNext()) {
060            Element element=(Element)iterator.next();
061            QName elementQName=getQNameOfSchemaElement(element);
062            QName type=(QName)typedElementMap.get(element);
063
064            if (complexTypeMap.containsKey(type)) {
065                DocumentFactory factory=
066                    (DocumentFactory)complexTypeMap.get(type);
067                elementQName.setDocumentFactory(factory);
068            } else if (simpleTypeMap.containsKey(type)) {
069                XSDatatype datatype=(XSDatatype)simpleTypeMap.get(type);
070                DocumentFactory factory=
071                        (DocumentFactory)elementFactoryMap.get(element);
072                if (factory instanceof DatatypeElementFactory) {
073                    ((DatatypeElementFactory)factory).setChildElementXSDatatype(elementQName,datatype);
074                }
075            }
076        }
077    }
078
079    void resolveNamedTypes() {
080        resolveElementTypes();
081    }
082
083    private QName getQNameOfSchemaElement(Element element) {
084        String name=element.attributeValue("name");
085        return getQName(name);
086    }
087
088    private QName getQName( String name ) {
089        return documentFactory.createQName(name);
090    }
091
092}
093
094
095
096
097/*
098 * Redistribution and use of this software and associated documentation
099 * ("Software"), with or without modification, are permitted provided
100 * that the following conditions are met:
101 *
102 * 1. Redistributions of source code must retain copyright
103 *    statements and notices.  Redistributions must also contain a
104 *    copy of this document.
105 *
106 * 2. Redistributions in binary form must reproduce the
107 *    above copyright notice, this list of conditions and the
108 *    following disclaimer in the documentation and/or other
109 *    materials provided with the distribution.
110 *
111 * 3. The name "DOM4J" must not be used to endorse or promote
112 *    products derived from this Software without prior written
113 *    permission of MetaStuff, Ltd.  For written permission,
114 *    please contact dom4j-info@metastuff.com.
115 *
116 * 4. Products derived from this Software may not be called "DOM4J"
117 *    nor may "DOM4J" appear in their names without prior written
118 *    permission of MetaStuff, Ltd. DOM4J is a registered
119 *    trademark of MetaStuff, Ltd.
120 *
121 * 5. Due credit should be given to the DOM4J Project
122 *    (http://dom4j.org/).
123 *
124 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
125 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
126 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
127 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
128 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
129 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
130 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
131 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
132 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
133 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
134 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
135 * OF THE POSSIBILITY OF SUCH DAMAGE.
136 *
137 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
138 *
139 * $Id: NamedTypeResolver.java,v 1.2 2001/11/13 12:27:44 jstrachan Exp $
140 */