KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example15 > BindingSelector


1 /*
2 Copyright (c) 2003, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package example15;
30
31 import java.io.OutputStream JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 import org.jibx.runtime.BindingDirectory;
35 import org.jibx.runtime.IBindingFactory;
36 import org.jibx.runtime.IMarshallable;
37 import org.jibx.runtime.IUnmarshallingContext;
38 import org.jibx.runtime.JiBXException;
39 import org.jibx.runtime.impl.MarshallingContext;
40 import org.jibx.runtime.impl.UnmarshallingContext;
41
42 public class BindingSelector
43 {
44     /** URI of version selection attribute. */
45     private final String JavaDoc m_attributeUri;
46     
47     /** Name of version selection attribute. */
48     private final String JavaDoc m_attributeName;
49     
50     /** Array of version names. */
51     private final String JavaDoc[] m_versionTexts;
52     
53     /** Array of bindings corresponding to versions. */
54     private final String JavaDoc[] m_versionBindings;
55     
56     /** Basic unmarshalling context used to determine document version. */
57     private final UnmarshallingContext m_context;
58     
59     /** Stream for marshalling output. */
60     private OutputStream JavaDoc m_outputStream;
61     
62     /** Encoding for output stream. */
63     private String JavaDoc m_outputEncoding;
64     
65     /** Output writer for marshalling. */
66     private Writer JavaDoc m_outputWriter;
67     
68     /** Indentation for marshalling. */
69     private int m_outputIndent;
70     
71     /**
72      * Constructor.
73      *
74      * @param uri version selection attribute URI (<code>null</code> if none)
75      * @param name version selection attribute name
76      * @param versions array of version texts (first is default)
77      * @param bindings array of binding names corresponding to versions
78      */

79     
80     public BindingSelector(String JavaDoc uri, String JavaDoc name, String JavaDoc[] versions,
81         String JavaDoc[] bindings) {
82         m_attributeUri = uri;
83         m_attributeName = name;
84         m_versionTexts = versions;
85         m_versionBindings = bindings;
86         m_context = new UnmarshallingContext();
87         m_outputIndent = -1;
88     }
89     
90     /**
91      * Get initial unmarshalling context. This gives access to the unmarshalling
92      * context used before the specific version is determined. The document
93      * information must be set for this context before calling {@link
94      * #unmarshalVersioned}.
95      *
96      * @return initial unmarshalling context
97      */

98     
99     public IUnmarshallingContext getContext() {
100         return m_context;
101     }
102     
103     /**
104      * Set output stream and encoding.
105      *
106      * @param outs stream for document data output
107      * @param enc document output encoding, or <code>null</code> for default
108      */

109      
110     void setOutput(OutputStream JavaDoc outs, String JavaDoc enc) {
111         m_outputStream = outs;
112         m_outputEncoding = enc;
113     }
114     
115     /**
116      * Set output writer.
117      *
118      * @param outw writer for document data output
119      */

120     
121     void setOutput(Writer JavaDoc outw) {
122         m_outputWriter = outw;
123     }
124     
125     /**
126      * Set nesting indent spaces.
127      *
128      * @param indent number of spaces to indent per level, or disable
129      * indentation if negative
130      */

131     
132     void setIndent(int indent) {
133         m_outputIndent = indent;
134     }
135     
136     /**
137      * Marshal according to supplied version.
138      *
139      * @param obj root object to be marshalled
140      * @param version identifier for version to be used in marshalling
141      * @throws JiBXException if error in marshalling
142      */

143     
144     public void marshalVersioned(Object JavaDoc obj, String JavaDoc version)
145         throws JiBXException {
146         
147         // look up version in defined list
148
String JavaDoc match = (version == null) ? m_versionTexts[0] : version;
149         for (int i = 0; i < m_versionTexts.length; i++) {
150             if (match.equals(m_versionTexts[i])) {
151                 
152                 // version found, create marshaller for the associated binding
153
IBindingFactory fact = BindingDirectory.
154                     getFactory(m_versionBindings[i], obj.getClass());
155                 MarshallingContext context =
156                     (MarshallingContext)fact.createMarshallingContext();
157                 
158                 // configure marshaller for writing document
159
context.setIndent(m_outputIndent);
160                 if (m_outputWriter == null) {
161                     if (m_outputStream == null) {
162                         throw new JiBXException("Output not configured");
163                     } else {
164                         context.setOutput(m_outputStream, m_outputEncoding);
165                     }
166                 } else {
167                     context.setOutput(m_outputWriter);
168                 }
169                 
170                 // output object as document
171
context.startDocument(m_outputEncoding, null);
172                 ((IMarshallable)obj).marshal(context);
173                 context.endDocument();
174                 return;
175                 
176             }
177         }
178         
179         // error if unknown version in document
180
throw new JiBXException("Unrecognized document version " + version);
181     }
182     
183     /**
184      * Unmarshal according to document version.
185      *
186      * @param clas expected class mapped to root element of document (used only
187      * to look up the binding)
188      * @return root object unmarshalled from document
189      * @throws JiBXException if error in unmarshalling
190      */

191     
192     public Object JavaDoc unmarshalVersioned(Class JavaDoc clas) throws JiBXException {
193         
194         // get the version attribute value (using first value as default)
195
m_context.toStart();
196         String JavaDoc version = m_context.attributeText(m_attributeUri,
197             m_attributeName, m_versionTexts[0]);
198         
199         // look up version in defined list
200
for (int i = 0; i < m_versionTexts.length; i++) {
201             if (version.equals(m_versionTexts[i])) {
202                 
203                 // version found, create unmarshaller for the associated binding
204
IBindingFactory fact = BindingDirectory.
205                     getFactory(m_versionBindings[i], clas);
206                 UnmarshallingContext context =
207                     (UnmarshallingContext)fact.createUnmarshallingContext();
208                 
209                 // return object unmarshalled using binding for document version
210
context.setFromContext(m_context);
211                 return context.unmarshalElement();
212                 
213             }
214         }
215         
216         // error if unknown version in document
217
throw new JiBXException("Unrecognized document version " + version);
218     }
219 }
Popular Tags