Package org.apache.xmlbeans

Examples of org.apache.xmlbeans.SchemaTypeLoader


        // classloader rather than the thread context classloader.  This is
        // because in some situations (such as when being invoked by ant
        // underneath the ide) the context classloader is potentially weird
        // (because of the design of ant).

        SchemaTypeLoader loader = XmlBeans.typeLoaderForClassLoader(SchemaDocument.class.getClassLoader());

        // step 1, parse all the XSD files.
        ArrayList scontentlist = new ArrayList();
        if (xsdFiles != null)
        {
            for (int i = 0; i < xsdFiles.length; i++)
            {
                try
                {
                    XmlOptions options = new XmlOptions();
                    options.setLoadLineNumbers();
                    options.setLoadMessageDigest();
                    options.setLoadSubstituteNamespaces(MAP_COMPATIBILITY_CONFIG_URIS);

                    XmlObject schemadoc = loader.parse(xsdFiles[i], null, options);
                    if (!(schemadoc instanceof SchemaDocument))
                    {
                        StscState.addError(errorListener, "Document " + xsdFiles[i] + " is not a schema file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, schemadoc);
                    }
                    else
                    {
                        StscState.addInfo(errorListener, "Loading schema file " + xsdFiles[i]);
                        XmlOptions opts = new XmlOptions().setErrorListener(errorListener);
                        if (schemadoc.validate(opts))
                            scontentlist.add(((SchemaDocument)schemadoc).getSchema());
                    }
                }
                catch (XmlException e)
                {
                    errorListener.add(e.getError());
                }
                catch (Exception e)
                {
                    StscState.addError(errorListener, "Cannot load file " + xsdFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_FILE, xsdFiles[i]);
                }
            }
        }

        // step 2, parse all WSDL files
        if (wsdlFiles != null)
        {
            for (int i = 0; i < wsdlFiles.length; i++)
            {
                try
                {
                    XmlOptions options = new XmlOptions();
                    options.setLoadLineNumbers();
                    options.setLoadSubstituteNamespaces(Collections.singletonMap(
                            "http://schemas.xmlsoap.org/wsdl/", "http://www.apache.org/internal/xmlbeans/wsdlsubst"
                    ));


                    XmlObject wsdldoc = loader.parse(wsdlFiles[i], null, options);

                    if (!(wsdldoc instanceof org.apache.internal.xmlbeans.wsdlsubst.DefinitionsDocument))
                        StscState.addError(errorListener, "Document " + wsdlFiles[i] + " is not a wsdl file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdldoc);
                    else
                    {
                        if (wsdlContainsEncoded(wsdldoc))
                            StscState.addWarning(errorListener, "The WSDL " + wsdlFiles[i] + " uses SOAP encoding. SOAP encoding is not compatible with literal XML Schema.", XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdldoc);
                        StscState.addInfo(errorListener, "Loading wsdl file " + wsdlFiles[i]);
                        XmlObject[] types = ((org.apache.internal.xmlbeans.wsdlsubst.DefinitionsDocument)wsdldoc).getDefinitions().getTypesArray();
                        int count = 0;
                        for (int j = 0; j < types.length; j++)
                        {
                            // explicit cast for paranoia
                            SchemaDocument.Schema[] schemas = (SchemaDocument.Schema[])types[j].selectPath("declare namespace xs=\"http://www.w3.org/2001/XMLSchema\" xs:schema");
                            for (int k = 0; k < schemas.length; k++)
                            {
                                if (schemas[k].validate(new XmlOptions().setErrorListener(errorListener)))
                                    scontentlist.add(schemas[k]);
                            }
                            count += schemas.length;
                        }
                        StscState.addInfo(errorListener, "Processing " + count + " schema(s) in " + wsdlFiles[i].toString());
                    }
                }
                catch (XmlException e)
                {
                    errorListener.add(e.getError());
                }
                catch (Exception e)
                {
                    StscState.addError(errorListener, "Cannot load file " + wsdlFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_FILE, wsdlFiles[i]);
                }
            }
        }

        SchemaDocument.Schema[] sdocs = (SchemaDocument.Schema[])scontentlist.toArray(new SchemaDocument.Schema[scontentlist.size()]);

        // now the config files.
        ArrayList cdoclist = new ArrayList();
        if (configFiles != null)
        {
            for (int i = 0; i < configFiles.length; i++)
            {
                try
                {
                    XmlOptions options = new XmlOptions();
                    options.put( XmlOptions.LOAD_LINE_NUMBERS );
                    options.setLoadSubstituteNamespaces(MAP_COMPATIBILITY_CONFIG_URIS);

                    XmlObject configdoc = loader.parse(configFiles[i], null, options);
                    if (!(configdoc instanceof ConfigDocument))
                        StscState.addError(errorListener, "Document " + configFiles[i] + " is not an xsd config file", XmlErrorContext.CANNOT_LOAD_XSD_FILE, configdoc);
                    else
                    {
                        StscState.addInfo(errorListener, "Loading config file " + configFiles[i]);
                        if (configdoc.validate(new XmlOptions().setErrorListener(errorListener)))
                            cdoclist.add(((ConfigDocument)configdoc).getConfig());
                    }
                }
                catch (XmlException e)
                {
                    errorListener.add(e.getError());
                }
                catch (Exception e)
                {
                    StscState.addError(errorListener, "Cannot load xsd config file " + configFiles[i] + ": " + e, XmlErrorContext.CANNOT_LOAD_XSD_CONFIG_FILE, configFiles[i]);
                }
            }
        }
        ConfigDocument.Config[] cdocs = (ConfigDocument.Config[])cdoclist.toArray(new ConfigDocument.Config[cdoclist.size()]);

        SchemaTypeLoader linkTo = SchemaTypeLoaderImpl.build(null, cpResourceLoader, null);

        URI baseURI = null;
        if (baseDir != null)
            baseURI = baseDir.toURI();
View Full Code Here


    public void doTest (
        String[] schemas, QName docType,
        String[] validInstances, String[] invalidInstances, boolean startOnDocument )
            throws Exception
    {
        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );

        XmlOptions options = new XmlOptions();

        if (docType != null)
        {
            SchemaType docSchema = stl.findDocumentType( docType );

            Assert.assertTrue( docSchema != null );
           
            options.put( XmlOptions.DOCUMENT_TYPE, docSchema );
        }

        for ( int i = 0 ; i < validInstances.length ; i++ )
        {
            XmlObject x =
                stl.parse( (String) validInstances[ i ], null, options );

            if (!startOnDocument)
            {
                XmlCursor c = x.newCursor();
                c.toFirstChild();
                x = c.getObject();
                c.dispose();
            }
           
            List xel = new ArrayList();
           
            options.put( XmlOptions.ERROR_LISTENER, xel );
           
            boolean isValid = x.validate( options );
           
            if (!isValid)
            {
                System.err.println( "Invalid doc, expected a valid doc: " );
                System.err.println( "Instance(" + i + "): " );
                System.err.println( x.xmlText() );
                Root.dump( x );
                System.err.println( "Errors: " );
                for ( int j = 0 ; j < xel.size() ; j++ )
                    System.err.println( xel.get( j ) );
                System.err.println();
            }
           
            Assert.assertTrue( isValid );
        }

        for ( int i = 0 ; i < invalidInstances.length ; i++ )
        {
            XmlObject x;
           
            try
            {
                x = stl.parse( (String) invalidInstances[ i ], null, options );

                if (! startOnDocument)
                {
                    XmlCursor c = x.newCursor();
                    c.toFirstChild();
View Full Code Here

            "</xs:schema>" +
            "";
       
        String[] schemas = { schema };
       
        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );
       
        //
        // One which uses ##targetNamespace on a wildcard
        //
       
        schema =
            "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
            "  <xs:complexType name='foo'>\n" +
            "    <xs:sequence>\n" +
            "      <xs:any namespace='##targetNamespace'/>\n" +
            "    </xs:sequence>\n" +
            "  </xs:complexType>\n" +
            "</xs:schema>" +
            "";
       
        String[] schemas99 = { schema };
       
        stl = makeSchemaTypeLoader( schemas99 );
       
        //
        // A big, nasty schema :-)
        //
       
        File file = TestEnv.getRootFile();
        File schemeFile = new File( file, "src/xsdschema/schema/XMLSchema.xsd" );
        File xmlFile = new File( file, "src/xmlschema/schema/XML.xsd" );

        File[] schemasF = { schemeFile, xmlFile };

        Root.disableStoreValidation();
        try
        {
            stl = makeSchemaTypeLoader( schemasF );
        }
        finally
        {
            Root.enableStoreValidation();
        }

        SchemaType type =
            stl.findDocumentType(
                new QName( "http://www.w3.org/2001/XMLSchema", "schema" ) );

        Assert.assertTrue( type != null );

View Full Code Here

            "</xs:schema>\n" +
            "";

        String[] schemas = { schema };

        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );

        XmlObject x =
            stl.parse(
                "<any " + ns + " xsi:type='kindaPrime'>100</any>",
                null, null );

        // Make sure the unvalidated stream is OK

        XMLInputStream xis = x.newXMLInputStream();

        while ( xis.hasNext() )
            xis.next();

        // Make sure the validated stream fails

        boolean blewChunks = false;

        xis =
            stl.newValidatingXMLInputStream(
                x.newXMLInputStream(), null, null );

        try
        {
            while ( xis.hasNext() )
View Full Code Here

            "</xs:schema>\n" +
            "";

        String[] schemas = { schema };

        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );

        XmlObject x =
            stl.parse(
                "<hee yee='3'><haw>66</haw></hee>",
                    null, null );

        XmlCursor c = x.newCursor();

        for ( ; ; )
        {
            XmlObject obj = c.getObject();

            if (obj != null)
                obj.validate();

            if (c.toNextToken().isNone())
                break;
        }

        // invalid

        x =
            stl.parse(
                "<hee yee='five'><haw>66</haw></hee>",
                null, null );

        Assert.assertTrue( ! x.validate() );
View Full Code Here

            "</xs:schema>" +
            "";

        String[] schemas = { schema };

        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );

        XmlObject x =
            stl.parse(
                "<base><foo/></base>", null, null );

        Assert.assertTrue( x.validate() );

        XmlCursor c = x.newCursor();

        c.toFirstChild();

        XmlObject base = c.getObject();

        c.toEndToken();
        c.insertElement( "bar" );

        Assert.assertTrue( !x.validate() );

        c.toPrevSibling();

        c.removeXml();

        Assert.assertTrue( x.validate() );

        base.changeType( stl.findType( new QName( "derived" ) ) );

        c.insertElement( "bar" );

        Assert.assertTrue( x.validate() );
    }
View Full Code Here

            "</xs:schema>" +
            "";
       
        String[] schemas = { schema };
       
        SchemaTypeLoader stl = makeSchemaTypeLoader( schemas );

        XmlOptions validate = new XmlOptions().setValidateOnSet();
        XmlOptions noValidate = new XmlOptions();

        SchemaType st = stl.findType(new QName("", "dec-restriction"));

        XmlDecimal dec = (XmlDecimal)stl.newInstance(st, validate);

        try {
            dec.set("200");
            fail("Expected XmlValueOutOfRangeException");
        }
        catch (XmlValueOutOfRangeException e) {}

        dec = (XmlDecimal)stl.newInstance(st, noValidate);

        try {
            dec.set("200");
        }
        catch (XmlValueOutOfRangeException e) {
View Full Code Here

  {
    String name = context.getCursor().getName().getLocalPart();
    SchemaTypeSystem sts = XmlBeans.compileXsd( new XmlObject[] { XmlObject.Factory
        .parse( "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"><element name=\"" + name + "\">" + xsd
            + "</element></schema>" ) }, XmlBeans.getBuiltinTypeSystem(), null );
    SchemaTypeLoader stl = XmlBeans.typeLoaderUnion( new SchemaTypeLoader[] { sts, XmlBeans.getBuiltinTypeSystem() } );
    if( !stl.parse( context.getCursor().xmlText(), null, null ).validate() )
      throw new XmlException( "Element '" + name + "' does not validate for custom type!" );
    return this;
  }
View Full Code Here

    validate( new File( "src\\test-resources\\test5\\TestService.wsdl" ).toURL().toString(), 4 );
  }

  public void testHttpImport6() throws Exception
  {
    SchemaTypeLoader schemaTypes = validate( "http://localhost:8082/test6/TestService.wsdl", 4 );
    assertNotNull( schemaTypes.findType( new QName( "http://schemas.eviware.com/TestService/v2/", "TestType" ) ) );

    schemaTypes = validate( new File( "src\\test-resources\\test6\\TestService.wsdl" ).toURL().toString(), 4 );
    assertNotNull( schemaTypes.findType( new QName( "http://schemas.eviware.com/TestService/v2/", "TestType" ) ) );
  }
View Full Code Here

    assertNotNull( schemaTypes.findType( new QName( "http://schemas.eviware.com/TestService/v2/", "TestType" ) ) );
  }

  public void testHttpImport7() throws Exception
  {
    SchemaTypeLoader schemaTypes = validate( "http://localhost:8082/test7/TestService.wsdl", 4 );
    assertNotNull( schemaTypes.findType( new QName( "http://schemas.eviware.com/TestService/v2/", "TestType" ) ) );

    schemaTypes = validate( new File( "src\\test-resources\\test7\\TestService.wsdl" ).toURL().toString(), 4 );
    assertNotNull( schemaTypes.findType( new QName( "http://schemas.eviware.com/TestService/v2/", "TestType" ) ) );
  }
View Full Code Here

TOP

Related Classes of org.apache.xmlbeans.SchemaTypeLoader

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.