Package org.exolab.castor.net

Examples of org.exolab.castor.net.URILocation


            final SchemaUnmarshallerState state)
    throws XMLException {
        super(schemaContext);
        setURIResolver(uriResolver);

        URILocation uri = null;
        //-- Get schemaLocation
        String schemaLocation = atts.getValue(SchemaNames.SCHEMALOCATION_ATTR);
        //-- Get namespace
        String namespace = atts.getValue("namespace");

        if ((schemaLocation == null) && (namespace == null)) {
            //-- A legal <import/> element...just return
            return;
        }

        boolean hasLocation = (schemaLocation != null);
        if (hasLocation) {

            if (schemaLocation.indexOf("\\") != -1) {
                String err = "'" + schemaLocation +
                    "' is not a valid URI as defined by IETF RFC 2396.";
                err += "The URI mustn't contain '\\'.";
                throw new SchemaException(err);
            }

            if (namespace == null) namespace = "";

            try {
                String documentBase = locator.getSystemId();
                if (documentBase != null) {
                    if (!documentBase.endsWith("/"))
                        documentBase = documentBase.substring(0, documentBase.lastIndexOf('/') +1 );
                }
                uri = getURIResolver().resolve(schemaLocation, documentBase);
                if (uri != null) {
                    schemaLocation = uri.getAbsoluteURI();
                }
            }
            catch (URIException urix) {
                throw new XMLException(urix);
            }
        }
        else {
            schemaLocation = namespace;
            try {
                uri = getURIResolver().resolveURN(namespace);
            }
            catch (URIException urix) {
                throw new XMLException(urix);
            }
            if (uri == null) {
                String err = "Unable to resolve Schema corresponding " +
                    "to namespace '" + namespace + "'.";
                throw new SchemaException(err);

            }
        }

        //-- Make sure targetNamespace is not the same as the
        //-- importing schema, see section 4.2.3 in the
        //-- XML Schema Recommendation
        if (namespace.equals(schema.getTargetNamespace()) )
            throw new SchemaException("the 'namespace' attribute in the <import> element cannot be the same of the targetNamespace of the global schema");

        //-- Schema object to hold import schema
        boolean addSchema = false;
        Schema importedSchema = schema.getImportedSchema(namespace, true);

        //-- Have we already imported this XML Schema file?
        if (state.processed(schemaLocation)) {
           if (importedSchema == null)
               schema.addImportedSchema(state.getSchema(schemaLocation));
           return;
        }

        boolean alreadyLoaded = false;
        if (importedSchema == null) {
            if (uri instanceof SchemaLocation) {
                importedSchema = ((SchemaLocation)uri).getSchema();
                schema.addImportedSchema(importedSchema);
                alreadyLoaded = true;
            }
            else {
                importedSchema = new Schema();
                addSchema = true;
            }
        }
        else {
            // check schema location, if different, allow merge
            if (hasLocation) {
                String tmpLocation = importedSchema.getSchemaLocation();
                alreadyLoaded = schemaLocation.equals(tmpLocation) || importedSchema.includeProcessed(schemaLocation);
                //-- keep track of the original schemaLocation as an include
                if(! alreadyLoaded) {
                    importedSchema.addInclude(tmpLocation);
                }
            } else {
                //-- only namespace can be used, no way to distinguish
                //-- multiple imports...mark as alreadyLoaded
                //-- see W3C XML Schema 1.0 Recommendation (part 1)
                //-- section 4.2.3...
                //-- <quote>... Given that the schemaLocation [attribute] is only
                //--   a hint, it is open to applications to ignore all but the
                //--   first <import> for a given namespace, regardless of the
                //--   <em>actual value</em> of schemaLocation, but such a strategy
                //--   risks missing useful information when new schemaLocations
                //--  are offered.</quote>
                alreadyLoaded = true;
            }
        }

        state.markAsProcessed(schemaLocation, importedSchema);

        if (alreadyLoaded) return;

        //-- Parser Schema
        Parser parser = null;
        try {
            parser = getSchemaContext().getParser();
        }
        catch(RuntimeException rte) {}
        if (parser == null) {
            throw new SchemaException("Error failed to create parser for import");
        }
    //-- Create Schema object and setup unmarshaller
    SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
          schemaUnmarshaller.setURIResolver(getURIResolver());
    schemaUnmarshaller.setSchema(importedSchema);
    Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
    parser.setDocumentHandler(handler);
    parser.setErrorHandler(handler);

        try {
            InputSource source = new InputSource(uri.getReader());
            source.setSystemId(uri.getAbsoluteURI());
            parser.parse(source);
        }
        catch(java.io.IOException ioe) {
            throw new SchemaException("Error reading import file '"+schemaLocation+"': "+ ioe);
        }
View Full Code Here


          String err = SchemaNames.REDEFINE + " must be used with an existing parent XML Schema.";
          throw new SchemaException(err);
        }
        setURIResolver(uriResolver);

        URILocation uri = null;
    //-- Get schemaLocation
    String schemaLocation = atts.getValue(SchemaNames.SCHEMALOCATION_ATTR);
    _schema = schema;
   
    if (schemaLocation == null) {
        //-- <redefine/> or <redefine> <annotation>(*) </redefine>
        _redefineSchema = new RedefineSchema(schema);
        _schema.addRedefineSchema(_redefineSchema);
      return;
    }

       
        if (schemaLocation.indexOf("\\") != -1) {
            String err = "'" + schemaLocation +
                "' is not a valid URI as defined by IETF RFC 2396.";
            err += "The URI mustn't contain '\\'.";
            error(err);
      }

        try {
            String documentBase = locator.getSystemId();
            if (documentBase != null) {
                if (!documentBase.endsWith("/"))
                    documentBase = documentBase.substring(0, documentBase.lastIndexOf('/') +1 );
            }
          uri = getURIResolver().resolve(schemaLocation, documentBase);
            if (uri != null) {
                schemaLocation = uri.getAbsoluteURI();
            }
        }
        catch (URIException urix) {
            throw new XMLException(urix);
        }

        //-- Schema object to hold import schema
    boolean addSchema = false;
    _redefineSchema = schema.getRedefineSchema(schemaLocation);
    Schema importedSchema = null;
   
    boolean alreadyLoaded = false;
   
        //-- The schema is not yet loaded
    if (_redefineSchema == null) {
            if (uri instanceof SchemaLocation) {
                importedSchema = ((SchemaLocation)uri).getSchema();
                //-- set the main schema in order to handle
                //-- redefinition at runtime
               
                // importedSchema.addMainSchema(schema);
               
                _redefineSchema = new RedefineSchema(schema, importedSchema);
          schema.addRedefineSchema(_redefineSchema);
          alreadyLoaded = true;
            }
            else {
          importedSchema = new Schema();
          addSchema = true;
      }
    }
    else {
      //-- check schema location, if different, allow merge
        String tmpLocation = _redefineSchema.getOriginalSchema().getSchemaLocation();
        alreadyLoaded = schemaLocation.equals(tmpLocation);
    }

        state.markAsProcessed(schemaLocation, importedSchema);

        if (alreadyLoaded) return;
       
        //-- Parser Schema
    Parser parser = null;
    try {
        parser = getSchemaContext().getParser();
    }
    catch(RuntimeException rte) {}
    if (parser == null) {
        throw new SchemaException("Error failed to create parser for import");
    }
    //-- Create Schema object and setup unmarshaller
    SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
    schemaUnmarshaller.setURIResolver(getURIResolver());
    schemaUnmarshaller.setSchema(importedSchema);
    Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
    parser.setDocumentHandler(handler);
    parser.setErrorHandler(handler);

    try {
        InputSource source = new InputSource(uri.getReader());
            source.setSystemId(uri.getAbsoluteURI());
            parser.parse(source);
    }
    catch(java.io.IOException ioe) {
        throw new SchemaException("Error reading import file '"+schemaLocation+"': "+ ioe);
    }
View Full Code Here

            //-- imports/includes
            String uri = _source.getSystemId();
            if (uri != null) {
                URIResolver resolver = schemaUnmarshaller.getURIResolver();
                try {
                    URILocation location = resolver.resolve(uri, null);
                    if (location != null) uri = location.toString();
                }
                catch(URIException except) {
                    throw new NestedIOException(except);
                }
                state.markAsProcessed(uri, schemaUnmarshaller.getSchema());
View Full Code Here

   * @return the URILocation for the URI
  **/
  public URILocation resolve(String href, String documentBase)
      throws URIException
  {
      URILocation uriLocation = null;
        boolean stripHostSeparator = (java.io.File.separatorChar == '\\');
        //--true if the path in the href is absolute
        boolean absolute = false;

        //1--Is there  a scheme? If yes the URI is absolute.
View Full Code Here

            final SchemaUnmarshallerState state)
    throws XMLException {
        super(schemaContext);

        setURIResolver(uriResolver);
        URILocation uri = null;
    //-- Get schemaLocation
    String include = atts.getValue("schemaLocation");
    if (include == null)
      throw new SchemaException("'schemaLocation' attribute missing on 'include'");

    if (include.indexOf("\\") != -1) {
            String err = include+" is not a valid URI as defined by IETF RFC 2396.";
            err += "The URI must not contain '\\'.";
            throw new SchemaException(err);
    }

        try {
            String documentBase = locator.getSystemId();
            if (documentBase != null) {
                if (!documentBase.endsWith("/"))
                    documentBase = documentBase.substring(0, documentBase.lastIndexOf("/") +1 );
            }
        uri = getURIResolver().resolve(include, documentBase);
        } catch (URIException ure) {
            throw new XMLException(ure);
        }

        if (uri != null)
            include = uri.getAbsoluteURI();

        //-- Has this schema location been included yet?
        if (schema.includeProcessed(include)) {
            return;
        }
        else if (include.equals(schema.getSchemaLocation())) {
            return;
        }
       
        Schema includedSchema = null;
        boolean alreadyLoaded = false;

        //-- caching is on
        if (state.cacheIncludedSchemas) {
            if (uri instanceof SchemaLocation) {
            includedSchema = ((SchemaLocation)uri).getSchema();
            schema.cacheIncludedSchema(includedSchema);
            alreadyLoaded = true;
          }
          //-- Have we already imported this XML Schema file?
          if (state.processed(include)) {
            includedSchema = state.getSchema(include);
            schema.cacheIncludedSchema(includedSchema);
            alreadyLoaded = true;
          }
        }
     
        if (includedSchema == null)
        includedSchema = new Schema();
        else
          state.markAsProcessed(include, includedSchema);
       
        //-- keep track of the schemaLocation
        schema.addInclude(include);

        if (alreadyLoaded)
          return;
    Parser parser = null;
    try {
        parser = getSchemaContext().getParser();
    }
    catch(RuntimeException rte) {}
    if (parser == null) {
        throw new SchemaException("Error failed to create parser for include");
    }
    SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), true, state, getURIResolver());

    if (state.cacheIncludedSchemas)
        schemaUnmarshaller.setSchema(includedSchema);
    else
      schemaUnmarshaller.setSchema(schema);
   
    Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
    parser.setDocumentHandler(handler);
    parser.setErrorHandler(handler);

    try {
            InputSource source = new InputSource(uri.getReader());
            source.setSystemId(uri.getAbsoluteURI());
            parser.parse(source);
    }
    catch(java.io.IOException ioe) {
        throw new SchemaException("Error reading include file '"+include+"'");
    }
View Full Code Here

            //-- imports/includes
            String uri = _source.getSystemId();
            if (uri != null) {
                URIResolver resolver = schemaUnmarshaller.getURIResolver();
                try {
                    URILocation location = resolver.resolve(uri, null);
                    if (location != null) uri = location.toString();
                }
                catch(URIException except) {
                    throw new NestedIOException(except);
                }
                state.markAsProcessed(uri, schemaUnmarshaller.getSchema());
View Full Code Here

   * @return the URILocation for the URI
  **/
  public URILocation resolve(String href, String documentBase)
      throws URIException
  {
      URILocation uriLocation = null;
        boolean stripHostSeparator = (java.io.File.separatorChar == '\\');
        //--true if the path in the href is absolute
        boolean absolute = false;

        //1--Is there  a scheme? If yes the URI is absolute.
View Full Code Here

TOP

Related Classes of org.exolab.castor.net.URILocation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.