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 1999-2004 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: ImportUnmarshaller.java 6931 2007-04-06 14:27:15Z wguttmn $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: //-- imported classes and packages
047: import org.exolab.castor.net.*;
048: import org.exolab.castor.xml.*;
049: import org.exolab.castor.xml.schema.*;
050: import org.xml.sax.*;
051:
052: public class ImportUnmarshaller extends ComponentReader {
053:
054: public ImportUnmarshaller(Schema schema, AttributeSet atts,
055: Resolver resolver, URIResolver uriResolver,
056: Locator locator, SchemaUnmarshallerState state)
057: throws XMLException {
058: super ();
059: setResolver(resolver);
060: setURIResolver(uriResolver);
061:
062: URILocation uri = null;
063: //-- Get schemaLocation
064: String schemaLocation = atts
065: .getValue(SchemaNames.SCHEMALOCATION_ATTR);
066: //-- Get namespace
067: String namespace = atts.getValue("namespace");
068:
069: if ((schemaLocation == null) && (namespace == null)) {
070: //-- A legal <import/> element...just return
071: return;
072: }
073:
074: boolean hasLocation = (schemaLocation != null);
075: if (schemaLocation != null) {
076:
077: if (schemaLocation.indexOf("\\") != -1) {
078: String err = "'"
079: + schemaLocation
080: + "' is not a valid URI as defined by IETF RFC 2396.";
081: err += "The URI mustn't contain '\\'.";
082: throw new SchemaException(err);
083: }
084:
085: if (namespace == null)
086: namespace = "";
087:
088: try {
089: String documentBase = locator.getSystemId();
090: if (documentBase != null) {
091: if (!documentBase.endsWith("/"))
092: documentBase = documentBase.substring(0,
093: documentBase.lastIndexOf('/') + 1);
094: }
095: uri = getURIResolver().resolve(schemaLocation,
096: documentBase);
097: if (uri != null) {
098: schemaLocation = uri.getAbsoluteURI();
099: }
100: } catch (URIException urix) {
101: throw new XMLException(urix);
102: }
103: } else {
104: schemaLocation = namespace;
105: try {
106: uri = getURIResolver().resolveURN(namespace);
107: } catch (URIException urix) {
108: throw new XMLException(urix);
109: }
110: if (uri == null) {
111: String err = "Unable to resolve Schema corresponding "
112: + "to namespace '" + namespace + "'.";
113: throw new SchemaException(err);
114:
115: }
116: }
117:
118: //-- Make sure targetNamespace is not the same as the
119: //-- importing schema, see section 4.2.3 in the
120: //-- XML Schema Recommendation
121: if (namespace.equals(schema.getTargetNamespace()))
122: throw new SchemaException(
123: "the 'namespace' attribute in the <import> element cannot be the same of the targetNamespace of the global schema");
124:
125: //-- Schema object to hold import schema
126: boolean addSchema = false;
127: Schema importedSchema = schema.getImportedSchema(namespace,
128: true);
129:
130: //-- Have we already imported this XML Schema file?
131: if (state.processed(schemaLocation)) {
132: if (importedSchema == null)
133: schema.addImportedSchema(state
134: .getSchema(schemaLocation));
135: return;
136: }
137:
138: boolean alreadyLoaded = false;
139: if (importedSchema == null) {
140: if (uri instanceof SchemaLocation) {
141: importedSchema = ((SchemaLocation) uri).getSchema();
142: schema.addImportedSchema(importedSchema);
143: alreadyLoaded = true;
144: } else {
145: importedSchema = new Schema();
146: addSchema = true;
147: }
148: } else {
149: // check schema location, if different, allow merge
150: if (hasLocation) {
151: String tmpLocation = importedSchema.getSchemaLocation();
152: alreadyLoaded = schemaLocation.equals(tmpLocation)
153: || importedSchema
154: .includeProcessed(schemaLocation);
155: //-- keep track of the original schemaLocation as an include
156: if (!alreadyLoaded) {
157: importedSchema.addInclude(tmpLocation);
158: }
159: } else {
160: //-- only namespace can be used, no way to distinguish
161: //-- multiple imports...mark as alreadyLoaded
162: //-- see W3C XML Schema 1.0 Recommendation (part 1)
163: //-- section 4.2.3...
164: //-- <quote>... Given that the schemaLocation [attribute] is only
165: //-- a hint, it is open to applications to ignore all but the
166: //-- first <import> for a given namespace, regardless of the
167: //-- <em>actual value</em> of schemaLocation, but such a strategy
168: //-- risks missing useful information when new schemaLocations
169: //-- are offered.</quote>
170: alreadyLoaded = true;
171: }
172: }
173:
174: state.markAsProcessed(schemaLocation, importedSchema);
175:
176: if (alreadyLoaded)
177: return;
178:
179: //-- Parser Schema
180: Parser parser = null;
181: try {
182: parser = state.getConfiguration().getParser();
183: } catch (RuntimeException rte) {
184: }
185: if (parser == null) {
186: throw new SchemaException(
187: "Error failed to create parser for import");
188: }
189: //-- Create Schema object and setup unmarshaller
190: SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(
191: state);
192: schemaUnmarshaller.setURIResolver(getURIResolver());
193: schemaUnmarshaller.setSchema(importedSchema);
194: Sax2ComponentReader handler = new Sax2ComponentReader(
195: schemaUnmarshaller);
196: parser.setDocumentHandler(handler);
197: parser.setErrorHandler(handler);
198:
199: try {
200: InputSource source = new InputSource(uri.getReader());
201: source.setSystemId(uri.getAbsoluteURI());
202: parser.parse(source);
203: } catch (java.io.IOException ioe) {
204: throw new SchemaException("Error reading import file '"
205: + schemaLocation + "': " + ioe);
206: } catch (org.xml.sax.SAXException sx) {
207: throw new SchemaException(sx);
208: }
209:
210: //-- Add schema to list of imported schemas (if not already present)
211: if (addSchema) {
212: importedSchema.setSchemaLocation(schemaLocation);
213: schema.addImportedSchema(importedSchema);
214: }
215: }
216:
217: /**
218: * Sets the name of the element that this UnknownUnmarshaller handles
219: **/
220: public String elementName() {
221: return SchemaNames.IMPORT;
222: } //-- elementName
223:
224: /**
225: * Returns the Object created by this ComponentReader
226: * @return the Object created by this ComponentReader
227: **/
228: public Object getObject() {
229: return null;
230: } //-- getObject
231:
232: }
|