001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-fcs
003: // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
004: // Any modifications to this file will be lost upon recompilation of the source schema.
005: // Generated on: 2005.03.04 at 10:20:40 PST
006: //
007:
008: package com.nabhinc.portal.config.impl.runtime;
009:
010: import java.io.InputStream;
011: import java.io.ObjectInputStream;
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016:
017: import javax.xml.bind.JAXBException;
018: import javax.xml.namespace.QName;
019:
020: import com.sun.xml.bind.Messages;
021:
022: /**
023: * Keeps the information about the grammar as a whole.
024: *
025: * This object is immutable and thread-safe.
026: *
027: * @author
028: * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
029: */
030: public class GrammarInfoImpl implements GrammarInfo {
031: /**
032: * Map from {@link QName}s (root tag names) to {@link Class}es of the
033: * content interface that should be instanciated.
034: */
035: private final Map rootTagMap;
036:
037: /**
038: * Enclosing ObjectFactory class. Used to load resources.
039: */
040: private final Class objectFactoryClass;
041:
042: /**
043: * Map from {@link Class}es that represent content interfaces
044: * to {@link String}s that represent names of the corresponding
045: * implementation classes.
046: */
047: private final Map defaultImplementationMap;
048:
049: /**
050: * ClassLoader that should be used to load impl classes.
051: */
052: private final ClassLoader classLoader;
053:
054: public GrammarInfoImpl(Map _rootTagMap,
055: Map _defaultImplementationMap, Class _objectFactoryClass) {
056: this .rootTagMap = _rootTagMap;
057: this .defaultImplementationMap = _defaultImplementationMap;
058: this .objectFactoryClass = _objectFactoryClass;
059: // the assumption is that the content interfaces and their impls
060: // are loaded from the same class loader.
061: this .classLoader = objectFactoryClass.getClassLoader();
062: }
063:
064: /**
065: * @return the name of the content interface that is registered with
066: * the specified element name.
067: */
068: private final Class lookupRootMap(String nsUri, String localName) {
069: // note that the value of rootTagMap could be null.
070: QName qn;
071:
072: qn = new QName(nsUri, localName);
073: if (rootTagMap.containsKey(qn))
074: return (Class) rootTagMap.get(qn);
075:
076: qn = new QName(nsUri, "*");
077: if (rootTagMap.containsKey(qn))
078: return (Class) rootTagMap.get(qn);
079:
080: qn = new QName("*", "*");
081: return (Class) rootTagMap.get(qn);
082: }
083:
084: public final Class getRootElement(String namespaceUri,
085: String localName) {
086: Class intfCls = lookupRootMap(namespaceUri, localName);
087: if (intfCls == null)
088: return null;
089: else
090: return getDefaultImplementation(intfCls);
091: }
092:
093: public final UnmarshallingEventHandler createUnmarshaller(
094: String namespaceUri, String localName,
095: UnmarshallingContext context) {
096:
097: Class impl = getRootElement(namespaceUri, localName);
098: if (impl == null)
099: return null;
100:
101: try {
102: return ((UnmarshallableObject) impl.newInstance())
103: .createUnmarshaller(context);
104: } catch (InstantiationException e) {
105: throw new InstantiationError(e.toString());
106: } catch (IllegalAccessException e) {
107: throw new IllegalAccessError(e.toString());
108: }
109: }
110:
111: public final String[] getProbePoints() {
112: List r = new ArrayList();
113: for (Iterator itr = rootTagMap.keySet().iterator(); itr
114: .hasNext();) {
115: QName qn = (QName) itr.next();
116: r.add(qn.getNamespaceURI());
117: r.add(qn.getLocalPart());
118: }
119: return (String[]) r.toArray(new String[r.size()]);
120: }
121:
122: public final boolean recognize(String nsUri, String localName) {
123: return lookupRootMap(nsUri, localName) != null;
124: }
125:
126: public final Class getDefaultImplementation(
127: Class javaContentInterface) {
128: try {
129: // by caching the obtained Class objects.
130: return Class.forName((String) defaultImplementationMap
131: .get(javaContentInterface), true, classLoader);
132: } catch (ClassNotFoundException e) {
133: throw new NoClassDefFoundError(e.toString());
134: }
135: }
136:
137: /**
138: * Gets the MSV AGM which can be used to validate XML during
139: * marshalling/unmarshalling.
140: */
141: public final com.sun.msv.grammar.Grammar getGrammar()
142: throws JAXBException {
143: try {
144: InputStream is = objectFactoryClass
145: .getResourceAsStream("bgm.ser");
146:
147: if (is == null) {
148: // unable to find bgm.ser
149: String name = objectFactoryClass.getName();
150: int idx = name.lastIndexOf('.');
151: name = '/'
152: + name.substring(0, idx + 1).replace('.', '/')
153: + "bgm.ser";
154: throw new JAXBException(Messages.format(
155: Messages.NO_BGM, name));
156: }
157:
158: // deserialize the bgm
159: ObjectInputStream ois = new ObjectInputStream(is);
160: com.sun.xml.bind.GrammarImpl g = (com.sun.xml.bind.GrammarImpl) ois
161: .readObject();
162: ois.close();
163:
164: g.connect(new com.sun.msv.grammar.Grammar[] { g }); // connect to itself
165:
166: return g;
167: } catch (Exception e) {
168: throw new JAXBException(Messages
169: .format(Messages.UNABLE_TO_READ_BGM), e);
170: }
171: }
172:
173: /**
174: * @see com.sun.tools.xjc.runtime.GrammarInfo#castToXMLSerializable(java.lang.Object)
175: */
176: public XMLSerializable castToXMLSerializable(Object o) {
177: if (o instanceof XMLSerializable) {
178: return (XMLSerializable) o;
179: } else {
180: return null;
181: }
182: }
183:
184: /**
185: * @see com.sun.tools.xjc.runtime.GrammarInfo#castToValidatableObject(java.lang.Object)
186: */
187: public ValidatableObject castToValidatableObject(Object o) {
188: if (o instanceof ValidatableObject) {
189: return (ValidatableObject) o;
190: } else {
191: return null;
192: }
193: }
194: }
|