View Javadoc

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: 2004.12.16 at 07:09:42 EST 
6   //
7   
8   package net.sf.jour.config.impl.runtime;
9   
10  import java.io.InputStream;
11  import java.io.ObjectInputStream;
12  import java.util.ArrayList;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  
17  import javax.xml.bind.JAXBException;
18  import javax.xml.namespace.QName;
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 rootTagMap;
37      
38      /***
39       * Enclosing ObjectFactory class. Used to load resources.
40       */
41      private final Class 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 defaultImplementationMap;
49      
50      /***
51       * ClassLoader that should be used to load impl classes.
52       */
53      private final ClassLoader classLoader;
54      
55      public GrammarInfoImpl( Map _rootTagMap, Map _defaultImplementationMap, Class _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 lookupRootMap( String nsUri, String localName ) {
69          // note that the value of rootTagMap could be null.
70          QName qn;
71          
72          qn = new QName(nsUri,localName);
73          if(rootTagMap.containsKey(qn))    return (Class)rootTagMap.get(qn);
74  
75          qn = new QName(nsUri,"*");
76          if(rootTagMap.containsKey(qn))    return (Class)rootTagMap.get(qn);
77  
78          qn = new QName("*","*");
79          return (Class)rootTagMap.get(qn);
80      }
81      
82      public final Class getRootElement(String namespaceUri, String localName) {
83          Class intfCls = lookupRootMap(namespaceUri,localName);
84          if(intfCls==null)    return null;
85          else                return getDefaultImplementation(intfCls);
86      }
87  
88      public final UnmarshallingEventHandler createUnmarshaller(
89          String namespaceUri, String localName, UnmarshallingContext context ) {
90          
91          Class impl = getRootElement(namespaceUri,localName);
92          if(impl==null)        return null;
93          
94          try {
95              return ((UnmarshallableObject)impl.newInstance()).createUnmarshaller(context);
96          } catch (InstantiationException e) {
97              throw new InstantiationError(e.toString());
98          } catch (IllegalAccessException e) {
99              throw new IllegalAccessError(e.toString());
100         }
101     }
102     
103     public final String[] getProbePoints() {
104         List r = new ArrayList();
105         for (Iterator itr = rootTagMap.keySet().iterator(); itr.hasNext();) {
106             QName qn = (QName) itr.next();
107             r.add(qn.getNamespaceURI());
108             r.add(qn.getLocalPart());
109         }
110         return (String[]) r.toArray(new String[r.size()]);
111     }
112     
113     public final boolean recognize( String nsUri, String localName ) {
114         return lookupRootMap(nsUri,localName)!=null;
115     }
116     
117     public final Class getDefaultImplementation( Class javaContentInterface ) {
118         try {
119             // by caching the obtained Class objects.
120             return Class.forName((String)defaultImplementationMap.get(javaContentInterface), true, classLoader );
121         } catch (ClassNotFoundException e) {
122             throw new NoClassDefFoundError(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 is = objectFactoryClass.getResourceAsStream("bgm.ser");
133             
134             if( is==null ) {
135                 // unable to find bgm.ser
136                 String 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 ois = new ObjectInputStream( 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 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 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 o) {
173         if( o instanceof ValidatableObject ) {
174              return (ValidatableObject)o;
175         } else {
176             return null;
177         }
178     }
179 }