Package org.exolab.castor.xml.schema

Examples of org.exolab.castor.xml.schema.SchemaException


            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);
        }
        catch(org.xml.sax.SAXException sx) {
            throw new SchemaException(sx);
        }

        //-- Add schema to list of imported schemas (if not already present)
        if (addSchema)
        {
View Full Code Here


        else {
            StringBuffer err = new StringBuffer("illegal element <");
            err.append(name);
            err.append("> found in <group>.");
            throw new SchemaException(err.toString());
        }

    } //-- startElement
View Full Code Here

        //-- check for name mismatches
        if (unmarshaller != null) {
            if (!name.equals(unmarshaller.elementName())) {
                String err = "missing end element for ";
                err += unmarshaller.elementName();
                throw new SchemaException(err);
            }
        }

        //-- have unmarshaller perform any necessary clean up
        unmarshaller.finish();
View Full Code Here

        }
        else {
            StringBuffer err = new StringBuffer("illegal element <");
            err.append(name);
            err.append("> found in <group>.");
            throw new SchemaException(err.toString());
        }

    } //-- startElement
View Full Code Here

        //-- check for name mismatches
        if (unmarshaller != null) {
            if (!name.equals(unmarshaller.elementName())) {
                String err = "missing end element for ";
                err += unmarshaller.elementName();
                throw new SchemaException(err);
            }
        }

        if (SchemaNames.ANNOTATION.equals(name)) {
            Annotation ann = (Annotation)unmarshaller.getObject();
View Full Code Here

            final SchemaUnmarshallerState state)
    throws XMLException {
        super(schemaContext);
        if (schema == null) {
          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);
    }
    catch(org.xml.sax.SAXException sx) {
        throw new SchemaException(sx);
    }
   
    //-- namespace checking
    String namespace = importedSchema.getTargetNamespace();
    if ( namespace != null ) {
View Full Code Here

          error(err);
        }
      }
      else {
        String err = "error: missing start element for " + name;
        throw new SchemaException(err);
      }

      //-- call unmarshaller.finish() to perform any necessary cleanup
      _unmarshaller.finish();
View Full Code Here

            StringBuffer err = new StringBuffer("illegal element <");
            err.append(name);
            err.append("> found in <");
            err.append(_element);
            err.append(">");
            throw new SchemaException(err.toString());
        }

    } //-- startElement
View Full Code Here

        //-- check for name mismatches
        if (unmarshaller != null) {
            if (!name.equals(unmarshaller.elementName())) {
                String err = "missing end element for ";
                err += unmarshaller.elementName();
                throw new SchemaException(err);
            }
        }

        //-- have unmarshaller perform any necessary clean up
        unmarshaller.finish();
View Full Code Here

                else
                    _complexType.setBaseType(baseType);
            }
            else if (derivedBy.equals(KEYWORD_RESTRICTIONS)) {
                String err = "restrictions not yet supported for <type>.";
                throw new SchemaException(err);
            }
            else {
                String err = "invalid value for derivedBy attribute of ";
                err += "<type>: " + derivedBy;
                throw new SchemaException(err);
            }

        }

        //-- read @abstract attribute
View Full Code Here

TOP

Related Classes of org.exolab.castor.xml.schema.SchemaException

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.