KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > DefaultJAXBContextImpl


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.DatatypeConverter;
4 import javax.xml.bind.JAXBContext;
5 import javax.xml.bind.JAXBException;
6 import javax.xml.bind.Marshaller;
7 import javax.xml.bind.PropertyException;
8 import javax.xml.bind.Unmarshaller;
9 import javax.xml.bind.Validator;
10
11 import com.sun.xml.bind.Messages;
12 import com.sun.xml.bind.DatatypeConverterImpl;
13
14 /**
15  * This class provides the default implementation of JAXBContext. It
16  * also creates the GrammarInfoFacade that unifies all of the grammar
17  * info from packages on the contextPath.
18  *
19  * @version $Revision: 1.2 $
20  */

21 public class DefaultJAXBContextImpl extends JAXBContext {
22     
23     /**
24      * This object keeps information about the grammar.
25      *
26      * When more than one package are specified,
27      * GrammarInfoFacade is used.
28      */

29     private GrammarInfo gi = null;
30
31     /**
32      * This is the constructor used by javax.xml.bind.FactoryFinder which
33      * bootstraps the RI. It causes the construction of a JAXBContext that
34      * contains a GrammarInfoFacade which is the union of all the generated
35      * JAXBContextImpl objects on the contextPath.
36      */

37     public DefaultJAXBContextImpl( String JavaDoc contextPath, ClassLoader JavaDoc classLoader )
38         throws JAXBException {
39             
40         this( GrammarInfoFacade.createGrammarInfoFacade( contextPath, classLoader ) );
41
42         // initialize datatype converter with ours
43
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
44     }
45     
46     /**
47      * This constructor is used by the default no-arg constructor in the
48      * generated JAXBContextImpl objects. It is also used by the
49      * bootstrapping constructor in this class.
50      */

51     public DefaultJAXBContextImpl( GrammarInfo gi ) {
52         this.gi = gi;
53     }
54         
55     public GrammarInfo getGrammarInfo() {
56         return gi;
57     }
58     
59     
60     
61 // META-IF(W)
62
/**
63      * Once we load a grammar, we will cache the value here.
64      */

65     private com.sun.msv.grammar.Grammar grammar = null;
66     
67     /**
68      * Loads a grammar object for the unmarshal-time validation.
69      *
70      * <p>
71      * getGrammar is normally very expensive, so it's worth
72      * synchronizing to avoid unnecessary invocation.
73      */

74     public synchronized com.sun.msv.grammar.Grammar getGrammar() throws JAXBException {
75         if( grammar==null )
76             grammar = gi.getGrammar();
77         return grammar;
78     }
79 // META-ENDIF
80

81     
82     /**
83      * Create a <CODE>Marshaller</CODE> object that can be used to convert a
84      * java content-tree into XML data.
85      *
86      * @return a <CODE>Marshaller</CODE> object
87      * @throws JAXBException if an error was encountered while creating the
88      * <code>Marshaller</code> object
89      */

90     public Marshaller createMarshaller() throws JAXBException {
91         if( MetaVariable.M ) { // META-IF(M)
92
return new MarshallerImpl( this );
93         } else { // META-ELSE
94
throw new UnsupportedOperationException JavaDoc(
95                 "When generating this code, the compiler option was specified not to generate the marshaller");
96         } // META-ENDIF
97
}
98        
99     /**
100      * Create an <CODE>Unmarshaller</CODE> object that can be used to convert XML
101      * data into a java content-tree.
102      *
103      * @return an <CODE>Unmarshaller</CODE> object
104      * @throws JAXBException if an error was encountered while creating the
105      * <code>Unmarshaller</code> object
106      */

107     public Unmarshaller createUnmarshaller() throws JAXBException {
108         if( MetaVariable.U ) { // META-IF(U)
109
return new UnmarshallerImpl( this, gi );
110         } else { // META-ELSE
111
throw new UnsupportedOperationException JavaDoc(
112                 "When generating this code, the compiler option was specified not to generate the unmarshaller");
113         } // META-ENDIF
114
}
115         
116     /**
117      * Create a <CODE>Validator</CODE> object that can be used to validate a
118      * java content-tree.
119      *
120      * @return an <CODE>Unmarshaller</CODE> object
121      * @throws JAXBException if an error was encountered while creating the
122      * <code>Validator</code> object
123      */

124     public Validator createValidator() throws JAXBException {
125         if( MetaVariable.V ) { // META-IF(V)
126
return new ValidatorImpl( this );
127         } else { // META-ELSE
128
throw new UnsupportedOperationException JavaDoc(
129                 "When generating this code, the compiler option was specified not to generate the validator");
130         } // META-ENDIF
131
}
132     
133
134     
135     /**
136      * Create an instance of the specified Java content interface.
137      *
138      * @param javaContentInterface the Class object
139      * @return an instance of the Java content interface
140      * @exception JAXBException
141      */

142     public Object JavaDoc newInstance( Class JavaDoc javaContentInterface )
143         throws JAXBException {
144
145         if( javaContentInterface == null ) {
146             throw new JAXBException( Messages.format( Messages.CI_NOT_NULL ) );
147         }
148
149         try {
150             Class JavaDoc c = gi.getDefaultImplementation( javaContentInterface );
151             if(c==null)
152                 throw new JAXBException(
153                     Messages.format( Messages.MISSING_INTERFACE, javaContentInterface ));
154             
155             return c.newInstance();
156         } catch( Exception JavaDoc e ) {
157             throw new JAXBException( e );
158         }
159     }
160     
161     /**
162      * There are no required properties, so simply throw an exception. Other
163      * providers may have support for properties on Validator, but the RI doesn't
164      */

165     public void setProperty( String JavaDoc name, Object JavaDoc value )
166         throws PropertyException {
167         
168         throw new PropertyException(name, value);
169     }
170     
171     /**
172      * There are no required properties, so simply throw an exception. Other
173      * providers may have support for properties on Validator, but the RI doesn't
174      */

175     public Object JavaDoc getProperty( String JavaDoc name )
176         throws PropertyException {
177             
178         throw new PropertyException(name);
179     }
180     
181     
182 }
183
Popular Tags