org.apache.crimson.tree
Class SimpleElementFactory

java.lang.Object
  extended byorg.apache.crimson.tree.SimpleElementFactory
All Implemented Interfaces:
ElementFactory

public class SimpleElementFactory
extends 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.

Author:
David Brownell

Nested Class Summary
private static class SimpleElementFactory.ClassLoaderFinder
           
(package private) static class SimpleElementFactory.ClassLoaderFinderConcrete
           
 
Field Summary
private  ClassLoader defaultLoader
           
private  Dictionary defaultMapping
           
private  String defaultNs
           
private  Locale locale
           
private  Dictionary nsLoaders
           
private  Dictionary nsMappings
           
 
Constructor Summary
SimpleElementFactory()
          Constructs an unconfigured element factory.
 
Method Summary
 void addMapping(Dictionary dict, 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(String namespace, Dictionary dict, ClassLoader loader)
          Records a namespace-specific mapping between element names and classes.
 ElementEx createElementEx(String tag)
          Creates an element by using the default mapping.
 ElementEx createElementEx(String namespace, String localName)
          Creates an element by using the mapping associated with the specified namespace, or the default namespace as appropriate.
private  ElementNode doMap(String tagName, Dictionary node2class, ClassLoader loader)
           
private static ClassLoader findClassLoader()
          Figure out which ClassLoader to use.
(package private)  String getMessage(String messageId)
           
(package private)  String getMessage(String messageId, Object[] parameters)
           
private  Class map2Class(String key, Dictionary node2class, ClassLoader loader)
           
 void setDefaultNamespace(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
 

Field Detail

defaultMapping

private Dictionary defaultMapping

defaultLoader

private ClassLoader defaultLoader

defaultNs

private String defaultNs

nsMappings

private Dictionary nsMappings

nsLoaders

private Dictionary nsLoaders

locale

private Locale locale
Constructor Detail

SimpleElementFactory

public SimpleElementFactory()
Constructs an unconfigured element factory.

Method Detail

addMapping

public void addMapping(Dictionary dict,
                       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(String namespace,
                       Dictionary dict,
                       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(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.


map2Class

private Class map2Class(String key,
                        Dictionary node2class,
                        ClassLoader loader)

doMap

private ElementNode doMap(String tagName,
                          Dictionary node2class,
                          ClassLoader loader)

createElementEx

public ElementEx createElementEx(String namespace,
                                 String localName)
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.
localName - element tag, without any embedded colon

createElementEx

public ElementEx createElementEx(String tag)
Creates an element by using the default mapping.

Specified by:
createElementEx in interface ElementFactory
Parameters:
tag - element tag

getMessage

String getMessage(String messageId)

getMessage

String getMessage(String messageId,
                  Object[] parameters)

findClassLoader

private static ClassLoader findClassLoader()
Figure out which ClassLoader to use. For JDK 1.2 and later use the context ClassLoader if possible. Note: we defer linking the class that calls an API only in JDK 1.2 until runtime so that we can catch LinkageError so that this code will run in older non-Sun JVMs such as the Microsoft JVM in IE.