KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > extras > BindingSelector


1 /*
2 Copyright (c) 2003-2004, 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 org.jibx.extras;
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 /**
43  * Binding selector that supports versioned XML documents. This looks for a
44  * <i>version</i> attribute on the root element of the document, and selects the
45  * mapping to be used for unmarshalling based on the value. It also supports
46  * selecting the version for marshalling based on a supplied version argument
47  * value.
48  *
49  * @author Dennis M. Sosnoski
50  * @version 1.0
51  */

52
53 public class BindingSelector
54 {
55     /** URI of version selection attribute. */
56     private final String JavaDoc m_attributeUri;
57     
58     /** Name of version selection attribute. */
59     private final String JavaDoc m_attributeName;
60     
61     /** Array of version names. */
62     private final String JavaDoc[] m_versionTexts;
63     
64     /** Array of bindings corresponding to versions. */
65     private final String JavaDoc[] m_versionBindings;
66     
67     /** Basic unmarshalling context used to determine document version. */
68     private final UnmarshallingContext m_context;
69     
70     /** Stream for marshalling output. */
71     private OutputStream JavaDoc m_outputStream;
72     
73     /** Encoding for output stream. */
74     private String JavaDoc m_outputEncoding;
75     
76     /** Output writer for marshalling. */
77     private Writer JavaDoc m_outputWriter;
78     
79     /** Indentation for marshalling. */
80     private int m_outputIndent;
81     
82     /**
83      * Constructor.
84      *
85      * @param uri version selection attribute URI (<code>null</code> if none)
86      * @param name version selection attribute name
87      * @param versions array of version texts (first is default)
88      * @param bindings array of binding names corresponding to versions
89      */

90     
91     public BindingSelector(String JavaDoc uri, String JavaDoc name, String JavaDoc[] versions,
92         String JavaDoc[] bindings) {
93         m_attributeUri = uri;
94         m_attributeName = name;
95         m_versionTexts = versions;
96         m_versionBindings = bindings;
97         m_context = new UnmarshallingContext();
98         m_outputIndent = -1;
99     }
100     
101     /**
102      * Get initial unmarshalling context. This gives access to the unmarshalling
103      * context used before the specific version is determined. The document
104      * information must be set for this context before calling {@link
105      * #unmarshalVersioned}.
106      *
107      * @return initial unmarshalling context
108      */

109     
110     public IUnmarshallingContext getContext() {
111         return m_context;
112     }
113     
114     /**
115      * Set output stream and encoding.
116      *
117      * @param outs stream for document data output
118      * @param enc document output encoding, or <code>null</code> for default
119      */

120      
121     public void setOutput(OutputStream JavaDoc outs, String JavaDoc enc) {
122         m_outputStream = outs;
123         m_outputEncoding = enc;
124     }
125     
126     /**
127      * Set output writer.
128      *
129      * @param outw writer for document data output
130      */

131     
132     public void setOutput(Writer JavaDoc outw) {
133         m_outputWriter = outw;
134     }
135     
136     /**
137      * Set nesting indent spaces.
138      *
139      * @param indent number of spaces to indent per level, or disable
140      * indentation if negative
141      */

142     
143     public void setIndent(int indent) {
144         m_outputIndent = indent;
145     }
146     
147     /**
148      * Marshal according to supplied version.
149      *
150      * @param obj root object to be marshalled
151      * @param version identifier for version to be used in marshalling
152      * @throws JiBXException if error in marshalling
153      */

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

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