KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > testsuite > impl > runtime > GrammarInfoImpl


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.3-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2005.06.14 at 12:02:17 PDT
6
//
7

8 package com.sun.japex.testsuite.impl.runtime;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.xml.bind.JAXBException;
18 import javax.xml.namespace.QName JavaDoc;
19
20 import com.sun.xml.bind.Messages;
21
22 /**
23  * Keeps the information about the grammar as a whole.
24  *
25  * This object is immutable and thread-safe.
26  *
27  * @author
28  * <a HREF="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
29  */

30 public class GrammarInfoImpl implements GrammarInfo
31 {
32     /**
33      * Map from {@link QName}s (root tag names) to {@link Class}es of the
34      * content interface that should be instanciated.
35      */

36     private final Map JavaDoc rootTagMap;
37     
38     /**
39      * Enclosing ObjectFactory class. Used to load resources.
40      */

41     private final Class JavaDoc objectFactoryClass;
42     
43     /**
44      * Map from {@link Class}es that represent content interfaces
45      * to {@link String}s that represent names of the corresponding
46      * implementation classes.
47      */

48     private final Map JavaDoc defaultImplementationMap;
49     
50     /**
51      * ClassLoader that should be used to load impl classes.
52      */

53     private final ClassLoader JavaDoc classLoader;
54     
55     public GrammarInfoImpl( Map JavaDoc _rootTagMap, Map JavaDoc _defaultImplementationMap, Class JavaDoc _objectFactoryClass ) {
56         this.rootTagMap = _rootTagMap;
57         this.defaultImplementationMap = _defaultImplementationMap;
58         this.objectFactoryClass = _objectFactoryClass;
59         // the assumption is that the content interfaces and their impls
60
// are loaded from the same class loader.
61
this.classLoader = objectFactoryClass.getClassLoader();
62     }
63     
64     /**
65      * @return the name of the content interface that is registered with
66      * the specified element name.
67      */

68     private final Class JavaDoc lookupRootMap( String JavaDoc nsUri, String JavaDoc localName ) {
69         // note that the value of rootTagMap could be null.
70
QName JavaDoc qn;
71         
72         qn = new QName JavaDoc(nsUri,localName);
73         if(rootTagMap.containsKey(qn)) return (Class JavaDoc)rootTagMap.get(qn);
74
75         qn = new QName JavaDoc(nsUri,"*");
76         if(rootTagMap.containsKey(qn)) return (Class JavaDoc)rootTagMap.get(qn);
77
78         qn = new QName JavaDoc("*","*");
79         return (Class JavaDoc)rootTagMap.get(qn);
80     }
81     
82     public final Class JavaDoc getRootElement(String JavaDoc namespaceUri, String JavaDoc localName) {
83         Class JavaDoc intfCls = lookupRootMap(namespaceUri,localName);
84         if(intfCls==null) return null;
85         else return getDefaultImplementation(intfCls);
86     }
87
88     public final UnmarshallingEventHandler createUnmarshaller(
89         String JavaDoc namespaceUri, String JavaDoc localName, UnmarshallingContext context ) {
90         
91         Class JavaDoc impl = getRootElement(namespaceUri,localName);
92         if(impl==null) return null;
93         
94         try {
95             return ((UnmarshallableObject)impl.newInstance()).createUnmarshaller(context);
96         } catch (InstantiationException JavaDoc e) {
97             throw new InstantiationError JavaDoc(e.toString());
98         } catch (IllegalAccessException JavaDoc e) {
99             throw new IllegalAccessError JavaDoc(e.toString());
100         }
101     }
102     
103     public final String JavaDoc[] getProbePoints() {
104         List JavaDoc r = new ArrayList JavaDoc();
105         for (Iterator JavaDoc itr = rootTagMap.keySet().iterator(); itr.hasNext();) {
106             QName JavaDoc qn = (QName JavaDoc) itr.next();
107             r.add(qn.getNamespaceURI());
108             r.add(qn.getLocalPart());
109         }
110         return (String JavaDoc[]) r.toArray(new String JavaDoc[r.size()]);
111     }
112     
113     public final boolean recognize( String JavaDoc nsUri, String JavaDoc localName ) {
114         return lookupRootMap(nsUri,localName)!=null;
115     }
116     
117     public final Class JavaDoc getDefaultImplementation( Class JavaDoc javaContentInterface ) {
118         try {
119             // by caching the obtained Class objects.
120
return Class.forName((String JavaDoc)defaultImplementationMap.get(javaContentInterface), true, classLoader );
121         } catch (ClassNotFoundException JavaDoc e) {
122             throw new NoClassDefFoundError JavaDoc(e.toString());
123         }
124     }
125
126     /**
127      * Gets the MSV AGM which can be used to validate XML during
128      * marshalling/unmarshalling.
129      */

130     public final com.sun.msv.grammar.Grammar getGrammar() throws JAXBException {
131         try {
132             InputStream JavaDoc is = objectFactoryClass.getResourceAsStream("bgm.ser");
133             
134             if( is==null ) {
135                 // unable to find bgm.ser
136
String JavaDoc name = objectFactoryClass.getName();
137                 int idx = name.lastIndexOf('.');
138                 name = '/'+name.substring(0,idx+1).replace('.','/')+"bgm.ser";
139                 throw new JAXBException(
140                     Messages.format( Messages.NO_BGM, name ) );
141             }
142             
143             // deserialize the bgm
144
ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc( is );
145             com.sun.xml.bind.GrammarImpl g = (com.sun.xml.bind.GrammarImpl)ois.readObject();
146             ois.close();
147             
148             g.connect(new com.sun.msv.grammar.Grammar[]{g}); // connect to itself
149

150             return g;
151         } catch( Exception JavaDoc e ) {
152             throw new JAXBException(
153                 Messages.format( Messages.UNABLE_TO_READ_BGM ),
154                 e );
155         }
156     }
157     
158     /**
159      * @see com.sun.tools.xjc.runtime.GrammarInfo#castToXMLSerializable(java.lang.Object)
160      */

161     public XMLSerializable castToXMLSerializable(Object JavaDoc o) {
162         if( o instanceof XMLSerializable ) {
163              return (XMLSerializable)o;
164         } else {
165             return null;
166         }
167     }
168     
169     /**
170      * @see com.sun.tools.xjc.runtime.GrammarInfo#castToValidatableObject(java.lang.Object)
171      */

172     public ValidatableObject castToValidatableObject(Object JavaDoc o) {
173         if( o instanceof ValidatableObject ) {
174              return (ValidatableObject)o;
175         } else {
176             return null;
177         }
178     }
179 }
180
Popular Tags