001: /*
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2001 (C) Intalio Inc. All Rights Reserved.
042: *
043: * $Id: BindingLoader.java 6936 2007-04-09 12:40:02Z wguttmn $
044: */
045: package org.exolab.castor.builder.binding;
046:
047: //-Castor imports
048: import java.io.IOException;
049: import java.net.MalformedURLException;
050: import java.net.URL;
051: import java.util.Enumeration;
052:
053: import org.exolab.castor.builder.binding.xml.AutomaticNamingType;
054: import org.exolab.castor.builder.binding.xml.Binding;
055: import org.exolab.castor.builder.binding.xml.ComponentBindingType;
056: import org.exolab.castor.builder.binding.xml.IncludeType;
057: import org.exolab.castor.builder.binding.xml.NamingXMLType;
058: import org.exolab.castor.builder.binding.xml.PackageType;
059: import org.exolab.castor.xml.MarshalException;
060: import org.exolab.castor.xml.Unmarshaller;
061: import org.exolab.castor.xml.ValidationException;
062: import org.xml.sax.EntityResolver;
063: import org.xml.sax.InputSource;
064: import org.xml.sax.SAXException;
065:
066: /**
067: * This class is responsible for loading a binding document into an in-memory
068: * representation that is meant to be used by the SourceGenerator.
069: *
070: * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
071: * @version $Revision: 6936 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
072: */
073: public final class BindingLoader {
074: // TODO Implement the enumeration handling
075:
076: /**
077: * The Source Generator Binding File loaded by this BindingLoader.
078: */
079: private ExtendedBinding _binding;
080:
081: /**
082: * The binding resolver used for resolving entities.
083: */
084: private BindingResolver _resolver = new BindingResolver();
085:
086: /**
087: * No-arg constructor.
088: */
089: public BindingLoader() {
090: // Nothing to do
091: }
092:
093: /**
094: * Loads the binding file from the {@link URL} given, and populates
095: * the {@link ExtendedBinding} instance from the values given.
096: * @param url The URL for the binding file to process.
097: * @throws BindingException If the binding file cannnot be processed properly.
098: */
099: public void loadBinding(final String url) throws BindingException {
100: InputSource source;
101: try {
102: source = _resolver.resolveEntity(null, url);
103: if (source == null) {
104: source = new InputSource(url);
105: }
106: if (source.getSystemId() == null) {
107: source.setSystemId(url);
108: }
109: loadBinding(source);
110: } catch (SAXException ex) {
111: throw new BindingException(ex);
112: } catch (IOException ioe) {
113: throw new BindingException(ioe);
114: }
115: }
116:
117: /**
118: * Loads a Binding Document. This method will load the binding document into
119: * a binding object and load all the included bindings along the way into a
120: * single collection.
121: *
122: * @param source
123: * The binding document to load.
124: * @throws BindingException
125: * thrown when an error occured during the unmarshalling.
126: */
127: public void loadBinding(final InputSource source)
128: throws BindingException {
129: Binding loaded = null;
130: if (_binding == null) {
131: _binding = new ExtendedBinding();
132: }
133:
134: //do not use the static method to ensure validation is turned on
135: Unmarshaller unmarshaller = new Unmarshaller(Binding.class);
136: unmarshaller.setValidation(true);
137:
138: try {
139: loaded = (Binding) unmarshaller.unmarshal(source);
140:
141: //--Copy one by one the components loaded in the root binding
142: _binding.setDefaultBindingType(loaded
143: .getDefaultBindingType());
144:
145: //--packages
146: Enumeration packages = loaded.enumeratePackage();
147: while (packages.hasMoreElements()) {
148: PackageType tempPackage = (PackageType) packages
149: .nextElement();
150: _binding.addPackage(tempPackage);
151: }
152:
153: //--NamingXML
154: NamingXMLType naming = loaded.getNamingXML();
155: if (naming != null) {
156: _binding.setNamingXML(naming);
157: }
158:
159: //--NamingXML
160: AutomaticNamingType automaticNaming = loaded
161: .getAutomaticNaming();
162: if (automaticNaming != null) {
163: _binding.setAutomaticNaming(automaticNaming);
164: _binding.handleAutomaticNaming(automaticNaming);
165: }
166:
167: //--elementBindings
168: Enumeration elements = loaded.enumerateElementBinding();
169: while (elements.hasMoreElements()) {
170: ComponentBindingType tempComp = (ComponentBindingType) elements
171: .nextElement();
172: _binding.addElementBinding(tempComp);
173: }
174:
175: //--attributeBindings
176: Enumeration attributes = loaded.enumerateAttributeBinding();
177: while (attributes.hasMoreElements()) {
178: ComponentBindingType tempComp = (ComponentBindingType) attributes
179: .nextElement();
180: _binding.addAttributeBinding(tempComp);
181: }
182:
183: //--ComplexTypeBindings
184: Enumeration complexTypes = loaded
185: .enumerateComplexTypeBinding();
186: while (complexTypes.hasMoreElements()) {
187: ComponentBindingType tempComp = (ComponentBindingType) complexTypes
188: .nextElement();
189: _binding.addComplexTypeBinding(tempComp);
190: }
191:
192: //--SimpleTypeBindings
193: Enumeration sts = loaded.enumerateSimpleTypeBinding();
194: while (sts.hasMoreElements()) {
195: ComponentBindingType tempComp = (ComponentBindingType) sts
196: .nextElement();
197: _binding.addSimpleTypeBinding(tempComp);
198: }
199:
200: //--groupBindings
201: Enumeration groups = loaded.enumerateGroupBinding();
202: while (groups.hasMoreElements()) {
203: ComponentBindingType tempComp = (ComponentBindingType) groups
204: .nextElement();
205: _binding.addGroupBinding(tempComp);
206: }
207:
208: //--enumBinding
209: Enumeration enums = loaded.enumerateEnumBinding();
210: while (enums.hasMoreElements()) {
211: ComponentBindingType tempEnum = (ComponentBindingType) enums
212: .nextElement();
213: _binding.addEnumBinding(tempEnum);
214: }
215:
216: //--included schemas
217: Enumeration includes = loaded.enumerateInclude();
218: while (includes.hasMoreElements()) {
219: IncludeType tempInclude = (IncludeType) includes
220: .nextElement();
221: try {
222: loadBinding(tempInclude.getURI());
223: } catch (Exception except) {
224: throw new BindingException(except);
225: }
226: }
227: } catch (MarshalException e) {
228: throw new BindingException(e);
229: } catch (ValidationException e) {
230: throw new BindingException(e);
231: }
232: }
233:
234: /**
235: * Returns the binding loaded by the BindingLoader.
236: *
237: * @return the binding loaded by this BindingLoader. This will return null
238: * if no call to loadBinding has been previously made.
239: */
240: public ExtendedBinding getBinding() {
241: return _binding;
242: }
243:
244: /**
245: * Sets the base URL for the binding and related files. If the base URL is
246: * known, files can be included using relative names. Any URL can be passed,
247: * if the URL can serve as a base URL it will be used.
248: *
249: * @param url
250: * The base URL
251: */
252: public void setBaseURL(final String url) {
253: try {
254: _resolver.setBaseURL(new URL(url));
255: } catch (MalformedURLException except) {
256: throw new IllegalArgumentException(except.getMessage());
257: }
258: }
259:
260: /**
261: * Factory method that returns a binding given an InputSource. The
262: * InputSource identifies a Binding Document meant to be loaded.
263: *
264: * @param source the InputSource identifying the binding document to be
265: * loaded.
266: * @return a binding that contains the different component bindings to be
267: * used in the source generator.
268: * @throws BindingException thrown when the given InputSource doesn't refer
269: * to a valid Binding document.
270: */
271: public static ExtendedBinding createBinding(final InputSource source)
272: throws BindingException {
273: BindingLoader loader = new BindingLoader();
274: loader.loadBinding(source);
275: return loader.getBinding();
276: }
277:
278: /**
279: * Factory method for unmarshalling an {@link ExtendedBinding} instance from the
280: * binding file as identified by the given file name.
281: * @param fileName Binding file name.
282: * @return An {@link ExtendedBinding} instance populated from the given binding file (name).
283: * @throws BindingException If the binding file cannot be processed properly.
284: */
285: public static ExtendedBinding createBinding(final String fileName)
286: throws BindingException {
287: BindingLoader loader = new BindingLoader();
288: InputSource source = new InputSource(fileName);
289: loader.loadBinding(source);
290: return loader.getBinding();
291: }
292:
293: /**
294: * EntityResolver specific to resolving entities related to the Castor XML
295: * code generator binding file.
296: * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
297: */
298: class BindingResolver implements EntityResolver {
299:
300: /**
301: * PUBLIC ID for the Castor XML code generator binding file.
302: */
303: private static final String BINDING_PUBLICID = "-//EXOLAB/Castor Binding Schema Version 1.0//EN";
304: /**
305: * SYSTEM ID for the Castor XML code generator binding file.
306: */
307: private static final String BINDING_SYSTEMID = "http://exolab.castor.org/binding.xsd";
308: /**
309: * Classpath-based URL to the binding XML schema as shipped in the
310: * Castor XML code generator binary JAR.
311: */
312: private static final String BINDING_RESOURCE = "/org/exolab/castor/builder/binding/binding.xsd";
313:
314: /**
315: * Base URL, if known.
316: */
317: private URL _baseUrl;
318:
319: /**
320: * Sets a base URL for relative processing.
321: * @param baseUrl Base URL for relative processing.
322: */
323: public void setBaseURL(final URL baseUrl) {
324: _baseUrl = baseUrl;
325: }
326:
327: /**
328: * Returns the base URL for relative processing.
329: * @return base URL for relative processing
330: */
331: public URL getBaseURL() {
332: return _baseUrl;
333: }
334:
335: /**
336: * Code adapted from DTDResolver written by Assaf Arkin.
337: *
338: * @param publicId The public identifier of the external entity being
339: * referenced, or null if none was supplied.
340: * @param systemId The system identifier of the external entity being
341: * referenced.
342: * @return An InputSource object describing the new input source, or
343: * null to request that the parser open a regular URI connection
344: * to the system identifier.
345: * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping
346: * another exception.
347: * @throws java.io.IOException A Java-specific IO exception, possibly
348: * the result of creating a new InputStream or Reader for the
349: * InputSource.
350: * @see org.exolab.castor.util.DTDResolver#resolveEntity(java.lang.String,
351: * java.lang.String)
352: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
353: * java.lang.String)
354: */
355: public InputSource resolveEntity(final String publicId,
356: final String systemId) throws IOException, SAXException {
357: InputSource source = null;
358:
359: // First, resolve the schema if any
360: if (publicId != null && publicId.equals(BINDING_PUBLICID)) {
361: source = new InputSource(getClass()
362: .getResourceAsStream(BINDING_RESOURCE));
363: source.setPublicId(publicId);
364: return source;
365: }
366:
367: if (systemId != null && systemId.equals(BINDING_SYSTEMID)) {
368: source = new InputSource(getClass()
369: .getResourceAsStream(BINDING_RESOURCE));
370: source.setSystemId(systemId);
371: return source;
372: }
373:
374: // Can't resolve public id, but might be able to resolve relative
375: // system id, since we have a base URI.
376: if (systemId != null && _baseUrl != null) {
377: URL url;
378: try {
379: url = new URL(systemId);
380: source = new InputSource(url.openStream());
381: source.setSystemId(systemId);
382: return source;
383: } catch (MalformedURLException except) {
384: try {
385: url = new URL(_baseUrl, systemId);
386: source = new InputSource(url.openStream());
387: source.setSystemId(systemId);
388: return source;
389: } catch (MalformedURLException ex2) {
390: throw new SAXException(ex2);
391: }
392: }
393: }
394: // No resolving.
395: return null;
396: }
397:
398: } //--BindingResolver
399:
400: }
|