Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/datatype/NamedTypeResolver.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: NamedTypeResolver.java,v 1.6 2004/06/25 08:03:34 maartenc Exp $
   */
 
  package org.dom4j.datatype;
 
  import com.sun.msv.datatype.xsd.XSDatatype;
 
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
 
  import org.dom4j.DocumentFactory;
  import org.dom4j.Element;
  import org.dom4j.QName;
 
 
  /** <p><code>NamedTypeResolver</code> resolves named types for a given QName.</p>
   *
   * @author Yuxin Ruan
   * @version $Revision: 1.6 $
   */
  class NamedTypeResolver {
 
42x       Map complexTypeMap=new HashMap();
42x       Map simpleTypeMap=new HashMap();
42x       Map typedElementMap=new HashMap();
42x       Map elementFactoryMap=new HashMap();
 
      DocumentFactory documentFactory;
 
42x       NamedTypeResolver(DocumentFactory documentFactory) {
42x           this.documentFactory=documentFactory;
42x       }
 
      void registerComplexType(QName type,DocumentFactory factory) {
4x           complexTypeMap.put(type,factory);
4x       }
 
      void registerSimpleType(QName type,XSDatatype datatype) {
2x           simpleTypeMap.put(type,datatype);
2x       }
 
      void registerTypedElement(Element element,QName type,DocumentFactory parentFactory) {
6x           typedElementMap.put(element,type);
6x           elementFactoryMap.put(element,parentFactory);
6x       }
 
      void resolveElementTypes() {
8x           Iterator iterator=typedElementMap.keySet().iterator();
2/2 14x           while (iterator.hasNext()) {
6x               Element element=(Element)iterator.next();
6x               QName elementQName=getQNameOfSchemaElement(element);
6x               QName type=(QName)typedElementMap.get(element);
 
2/2 6x               if (complexTypeMap.containsKey(type)) {
4x                   DocumentFactory factory=
                      (DocumentFactory)complexTypeMap.get(type);
4x                   elementQName.setDocumentFactory(factory);
1/2 2x               } else if (simpleTypeMap.containsKey(type)) {
2x                   XSDatatype datatype=(XSDatatype)simpleTypeMap.get(type);
2x                   DocumentFactory factory=
                          (DocumentFactory)elementFactoryMap.get(element);
1/2 2x                   if (factory instanceof DatatypeElementFactory) {
2x                       ((DatatypeElementFactory)factory).setChildElementXSDatatype(elementQName,datatype);
                  }
              }
          }
8x       }
 
      void resolveNamedTypes() {
8x           resolveElementTypes();
8x       }
 
      private QName getQNameOfSchemaElement(Element element) {
6x           String name=element.attributeValue("name");
6x           return getQName(name);
      }
 
      private QName getQName( String name ) {
6x           return documentFactory.createQName(name);
      }
 
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: NamedTypeResolver.java,v 1.6 2004/06/25 08:03:34 maartenc Exp $
   */