com.sun.xml.tree
Class SimpleElementFactory

java.lang.Object
  |
  +--com.sun.xml.tree.SimpleElementFactory

public class SimpleElementFactory
extends java.lang.Object
implements ElementFactory

This is a convenience class for creating application-specific elements associated with specified (or default) XML namespaces. It maintains tables mapping element tag names to classes, and uses them as needed to instantiate classes. The string *Element, which is not a legal XML element name, may be used to map otherwise unrecognized tags to a particular class. If this factory is not configured, then all mappings are to the ElementNode class. Erroneous mappings are fatal errors.

A suggested XML syntax for recording these bindings, which may in the future be explicitly supported, is:

 <bindings xmlns="...">
     <!-- first, bindings for the "default" namespace -->
     <binding tag="..." class="..."/>
     <binding tag="*Element" class="..."/>
     ...

     <!-- then bindings for other namespaces -->
     <namespace uri="...">
         <binding tag="..." class="..."/>
         ...
     </namespace>

     <!-- can specify JAR files for namespaces -->
     <namespace uri="..." jar="...">
         <binding tag="..." class="..."/>
         ...
     </namespace>
     ...
 </bindings>
 

Note that while most URIs used to identify namespaces will be URLs, such as http://www.example.com/xml/purchasing, some may also be URNs like urn:uuid:221ffe10-ae3c-11d1-b66c-00805f8a2676. You can't assume that the URIs are associated with web-accessible data; they must be treated as being no more than distinguishable strings.

Applications classes configuring an element factory will need to provide their own class loader (this.class.getClassLoader) to get the desired behavior in common cases. Classes loaded via some URL will similarly need to use a network class loader.


Constructor Summary
SimpleElementFactory()
          Constructs an unconfigured element factory.
 
Method Summary
 void addMapping(java.util.Dictionary dict, java.lang.ClassLoader loader)
          Records a default element name to namespace mapping, for use by namespace-unaware DOM construction and when a specific namespace mapping is not available.
 void addMapping(java.lang.String namespace, java.util.Dictionary dict, java.lang.ClassLoader loader)
          Records a namespace-specific mapping between element names and classes.
 ElementEx createElementEx(java.lang.String tag)
          Creates an element by using the default mapping.
 ElementEx createElementEx(java.lang.String namespace, java.lang.String tag)
          Creates an element by using the mapping associated with the specified namespace, or the default namespace as appropriate.
 void setDefaultNamespace(java.lang.String ns)
          Defines a URI to be treated as the "default" namespace.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleElementFactory

public SimpleElementFactory()
Constructs an unconfigured element factory.
Method Detail

addMapping

public void addMapping(java.util.Dictionary dict,
                       java.lang.ClassLoader loader)
Records a default element name to namespace mapping, for use by namespace-unaware DOM construction and when a specific namespace mapping is not available.
Parameters:
dict - Keys are element names, and values are either class names (interpreted with respect to loader) or class objects. This value may not be null, and the dictionary is retained and modified by the factory.
loader - If non-null, this is used instead of the bootstrap class loader when mapping from class names to class objects.

addMapping

public void addMapping(java.lang.String namespace,
                       java.util.Dictionary dict,
                       java.lang.ClassLoader loader)
Records a namespace-specific mapping between element names and classes.
Parameters:
namespace - A URI identifying the namespace for which the mapping is defined
dict - Keys are element names, and values are either class names (interpreted with respect to loader) or class objects. This value may not be null, and the dictionary is retained and modified by the factory.
loader - If non-null, this is used instead of the bootstrap class loader when mapping from class names to class objects.

setDefaultNamespace

public void setDefaultNamespace(java.lang.String ns)
Defines a URI to be treated as the "default" namespace. This is used only when choosing element classes, and may not be visible when instances are asked for their namespaces.

createElementEx

public ElementEx createElementEx(java.lang.String namespace,
                                 java.lang.String tag)
Creates an element by using the mapping associated with the specified namespace, or the default namespace as appropriate. If no mapping associated with that namespace is defined, then the default mapping is used.
Specified by:
createElementEx in interface ElementFactory
Parameters:
namespace - URI for namespace; null indicates use of the default namespace, if any.
tag - element tag, without any embedded colon

createElementEx

public ElementEx createElementEx(java.lang.String tag)
Creates an element by using the default mapping.
Specified by:
createElementEx in interface ElementFactory
Parameters:
tag - element tag


Submit Feedback to xml-feedback@java.sun.com