001: /*
002: * Created on Feb 5, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.geoserver.wfs.xml;
008:
009: import java.lang.reflect.Constructor;
010: import java.util.HashMap;
011: import java.util.Map;
012:
013: /**
014: * NameSpaceTranslatorFactory purpose.
015: * <p>
016: * Follows the factory pattern. Creates and stores a list of name space translators.
017: * </p>
018: * @see NameSpaceTranslator
019: * @author dzwiers, Refractions Research, Inc.
020: * @author $Author: dmzwiers $ (last modification)
021: * @version $Id: NameSpaceTranslatorFactory.java 6177 2007-02-19 10:11:27Z aaime $
022: */
023: public class NameSpaceTranslatorFactory {
024: /** map of namespace names as Strings -> Class representations of NameSpaceTranslators */
025: private Map namespaceTranslators;
026:
027: /** map of prefixs as String -> Instances of NameSpaceTranslators */
028: private Map namespaceTranslatorInstances;
029:
030: /** the only instance */
031: private final static NameSpaceTranslatorFactory instance = new NameSpaceTranslatorFactory();
032:
033: /**
034: * NameSpaceTranslatorFactory constructor.
035: * <p>
036: * Loads some default prefixes into memory when the class is first loaded.
037: * </p>
038: *
039: */
040: private NameSpaceTranslatorFactory() {
041: namespaceTranslators = new HashMap();
042: namespaceTranslatorInstances = new HashMap();
043:
044: //TODO replace null for these default namespaces.
045: namespaceTranslators.put("http://www.w3.org/2001/XMLSchema",
046: XMLSchemaTranslator.class);
047: namespaceTranslators.put("http://www.opengis.net/gml",
048: GMLSchemaTranslator.class);
049:
050: addNameSpaceTranslator("xs", "http://www.w3.org/2001/XMLSchema");
051: addNameSpaceTranslator("xsd",
052: "http://www.w3.org/2001/XMLSchema");
053: addNameSpaceTranslator("gml", "http://www.opengis.net/gml");
054: }
055:
056: /**
057: * getInstance purpose.
058: * <p>
059: * Completes the singleton pattern of this factory class.
060: * </p>
061: * @return NameSpaceTranslatorFactory The instance.
062: */
063: public static NameSpaceTranslatorFactory getInstance() {
064: return instance;
065: }
066:
067: /**
068: * addNameSpaceTranslator purpose.
069: * <p>
070: * Adds a new translator for the namespace specified if a
071: * NameSpaceTranslator was registered for that namespace.
072: * </p>
073: * <p>
074: * Some the magic for creating instances using the classloader occurs here
075: * (ie. the translators are not loaded lazily)
076: * </p>
077: * @param prefix The desired namespace prefix
078: * @param namespace The desired namespace.
079: */
080: public void addNameSpaceTranslator(String prefix, String namespace) {
081: if ((prefix == null) || (namespace == null)) {
082: throw new NullPointerException();
083: }
084:
085: try {
086: Class nstClass = (Class) namespaceTranslators
087: .get(namespace);
088:
089: if (nstClass == null) {
090: return;
091: }
092:
093: Constructor nstConstructor = nstClass
094: .getConstructor(new Class[] { String.class, });
095: NameSpaceTranslator nst = (NameSpaceTranslator) nstConstructor
096: .newInstance(new Object[] { prefix, });
097: namespaceTranslatorInstances.put(prefix, nst);
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102:
103: /**
104: * getNameSpaceTranslator purpose.
105: * <p>
106: * Description ...
107: * </p>
108: * @param prefix the prefix of the translator to get.
109: * @return the translator, or null if it was not found
110: */
111: public NameSpaceTranslator getNameSpaceTranslator(String prefix) {
112: return (NameSpaceTranslator) namespaceTranslatorInstances
113: .get(prefix);
114: }
115:
116: /**
117: * registerNameSpaceTranslator purpose.
118: * <p>
119: * Registers a namespace and it's translator with the factory. good for adding additional namespaces :)
120: * </p>
121: * @param namespace The namespace.
122: * @param nameSpaceTranslator The translator class for this namespace.
123: */
124: public void registerNameSpaceTranslator(String namespace,
125: Class nameSpaceTranslator) {
126: if ((nameSpaceTranslator != null)
127: && NameSpaceTranslator.class
128: .isAssignableFrom(nameSpaceTranslator)) {
129: namespaceTranslators.put(namespace, nameSpaceTranslator);
130: }
131: }
132: }
|