Package javax.xml.validation

Examples of javax.xml.validation.SchemaFactory


public class BundleUnmarshallingTest {
    @Test
    public void testValidBundleUnamrashalling() throws Exception {
        Unmarshaller unmarshaller = JAXBContext.newInstance(
                org.apache.falcon.oozie.bundle.BUNDLEAPP.class).createUnmarshaller();
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema = schemaFactory.newSchema(this.getClass().getResource("/oozie-bundle-0.1.xsd"));
        unmarshaller.setSchema(schema);
        Object bundle = unmarshaller.unmarshal(
                new StreamSource(BundleUnmarshallingTest.class.getResourceAsStream("/oozie/xmls/bundle.xml")),
                BUNDLEAPP.class);
        BUNDLEAPP bundleApp = ((JAXBElement<BUNDLEAPP>) bundle).getValue();
View Full Code Here


            return;
        }

        try {
            // Create SchemaFactory and configure for the default schema language - XMLSchema
            SchemaFactory factory = SchemaFactory.newInstance(DEFAULT_SCHEMA_LANGUAGE);
            factory.setErrorHandler(errorHandler);

            // set any features on/off as requested
            iter = properties.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String value = (String) entry.getValue();
                factory.setFeature(
                    (String) entry.getKey(), value != null && "true".equals(value));
            }

            Schema schema = null;

            StreamSource[] sources = new StreamSource[schemaKeys.size()];
            iter = schemaKeys.iterator();
            int i = 0;
            while (iter.hasNext()) {
                String propName = (String) iter.next();
                sources[i++] = Util.getStreamSource(
                    msgCtx.getConfiguration().getProperty(propName));
            }
            schema = factory.newSchema(sources);

            // Setup validator and input source
            // Features set for the SchemaFactory get propagated to Schema and Validator (JAXP 1.4)
            validator = schema.newValidator();
            validator.setErrorHandler(errorHandler);
View Full Code Here

                XSDefinition xsdDefinition = (XSDefinition)componentProperty.getXSDDefinition();
               
                if (xsdDefinition != null) {
                    try {
                        // create schema factory for XML schema
                        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                                     
      // Equivalent to getSchema().getSchemaDocument(), but allows us to support older versions of XmlSchema
                        Document schemaDom = xsdDefinition.getSchema().getAllSchemas()[0];
                       
                        String valueSchema = null;
                        Schema schema = null;
                       
                        if (componentProperty.getXSDType().getNamespaceURI().equals(Constants.SCA11_NS)){
                            // include the referenced schema as it's already in the OASIS namespace
                            valueSchema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
                                          "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" "+
                                                  "xmlns:sca=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" "+
                                                  "xmlns:__tmp=\"" + componentProperty.getXSDType().getNamespaceURI() + "\" "+
                                                  "targetNamespace=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" " +
                                                  "elementFormDefault=\"qualified\">" +
                                              "<include schemaLocation=\"" + xsdDefinition.getLocation() + "\"/>" +
//                                              "<element name=\"value\" type=\"" + "__tmp:" + componentProperty.getXSDType().getLocalPart() + "\"/>" +
                                          "</schema>";
//                            Source sources[] = {new StreamSource(new StringReader(valueSchema))};
                            Source sources[] = {new DOMSource(schemaDom)};
                            schema = factory.newSchema(sources);
                           
                            // The SCA schema already contains a "value" element so I can't create this schema
                            // the SCA value element is an any so return assuming that it validates.
                            return;
                        } else {
                            // import the referenced schema
                            valueSchema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
                                      "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" "+
                                                  "xmlns:sca=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" "+
                                                  "xmlns:__tmp=\"" + componentProperty.getXSDType().getNamespaceURI() + "\" "+
                                                  "targetNamespace=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" " +
                                                  "elementFormDefault=\"qualified\">" +
                                              "<import namespace=\"" + componentProperty.getXSDType().getNamespaceURI() + "\"/>" +
                                              "<element name=\"value\" type=\"" + "__tmp:" + componentProperty.getXSDType().getLocalPart() + "\"/>" +
                                          "</schema>";
                            Source sources[] = {new DOMSource(schemaDom), new StreamSource(new StringReader(valueSchema))};
                            schema = factory.newSchema(sources);
                        }
                                               
                        // get the value child of the property element
                        Document property = (Document)componentProperty.getValue();
                        Element value = (Element)property.getDocumentElement().getFirstChild();
View Full Code Here

        return clone;
    }

   
    public void validateSchema(Element ele, String uri) throws ToolException {
        SchemaFactory schemaFact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        DOMSource domSrc = new DOMSource(ele, uri);
        try {
            schemaFact.newSchema(domSrc);
        } catch (SAXException e) {
            if (e.getLocalizedMessage().indexOf("src-resolve.4.2") > -1)  {
                //Ignore schema resolve error and do nothing
            } else {
                throw new ToolException("Schema Error : " + e.getLocalizedMessage(), e);
View Full Code Here

   
   
    private static Schema createSchema(ServiceInfo serviceInfo) {
        Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
        if (schema == null) {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
            Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();

            try {
                for (SchemaInfo si : serviceInfo.getSchemas()) {
                    Element el = si.getElement();
                    String baseURI = null;
                    try {
                        baseURI = el.getBaseURI();
                    } catch (Exception ex) {
                        //ignore - not DOM level 3
                    }
                    if (baseURI == null) {
                        baseURI = si.getSystemId();
                    }
                    DOMSource ds = new DOMSource(el, baseURI);  
                    schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
                    LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                    StaxUtils.copy(el, StaxUtils.createXMLStreamWriter(out));
                    schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
                }

               
                for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
                    if (sch.getSourceURI() != null
                        && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":"
                                                         + sch.getTargetNamespace())) {
                       
                        InputStream ins = null;
                        try {
                            URL url = new URL(sch.getSourceURI());
                            ins = url.openStream();
                        } catch (Exception e) {
                            //ignore, we'll just use what we have.  (though
                            //bugs in XmlSchema could make this less useful)
                        }
                       
                        LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                        if (ins == null) {
                            sch.write(out);
                        } else {
                            IOUtils.copyAndCloseInput(ins, out);
                        }

                        schemaSourcesMap.put(sch.getSourceURI() + ":"
                                             + sch.getTargetNamespace(), out.toByteArray());
                       
                        Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
                        schemaSourcesMap2.put(sch.getSourceURI() + ":"
                                              + sch.getTargetNamespace(), source);
                    }
                }


                factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap));
                schema = factory.newSchema(schemaSourcesMap2.values()
                                           .toArray(new Source[schemaSourcesMap2.size()]));
               
               
            } catch (Exception ex) {
                // Something not right with the schema from the wsdl.
View Full Code Here

        reader.parse(new InputSource(url.openStream()));

    }

    private static ValidatorHandler getValidationHandler() throws SAXException {
        SchemaFactory schemaFactory;
        try {
            schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (Error e) {
            // Some old JDKs don't support XMLSchema validation
            return null;
        } catch (Exception e) {
            // Some old JDKs don't support XMLSchema validation
            return null;
        }
        Schema schema = schemaFactory.newSchema(ReadDocumentTestCase.class.getClassLoader().getResource(TUSCANY_11_XSD));
        ValidatorHandler handler = schema.newValidatorHandler();
        return handler;
    }
View Full Code Here

    // Implementation methods
    // -----------------------------------------------------------------------

    protected SchemaFactory createSchemaFactory() {
        SchemaFactory factory = SchemaFactory.newInstance(schemaLanguage);
        if (getResourceResolver() != null) {
            factory.setResourceResolver(getResourceResolver());
        }
        return factory;
    }
View Full Code Here

    protected Source createSchemaSource() throws IOException {
        throw new IllegalArgumentException("You must specify either a schema, schemaFile, schemaSource or schemaUrl property");
    }

    protected Schema createSchema() throws SAXException, IOException {
        SchemaFactory factory = getSchemaFactory();

        URL url = getSchemaUrl();
        if (url != null) {
            return factory.newSchema(url);
        }
        File file = getSchemaFile();
        if (file != null) {
            return factory.newSchema(file);
        }
        return factory.newSchema(getSchemaSource());
    }
View Full Code Here

    }
   
    public static void validateFacesConfigFile(URL xmlFile,
            ExternalContext externalContext, String version) throws SAXException, IOException
    {
        SchemaFactory schemaFactory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        Source schemaFile = getFacesConfigSchemaFileAsSource(externalContext, version);
        if (schemaFile == null)
        {
            throw new IOException("Could not find schema file for validation.");
        }
       
        schemaFactory.setResourceResolver(JAVAEE_5_LS_RESOURCE_RESOLVER);
        Schema schema = schemaFactory.newSchema(schemaFile);

        Validator validator = schema.newValidator();
        URLConnection conn = xmlFile.openConnection();
        conn.setUseCaches(false);
        InputStream is = conn.getInputStream();
View Full Code Here

    }
   
    public static void validateFaceletTagLibFile(URL xmlFile, ExternalContext externalContext, String version)
        throws SAXException, IOException, ParserConfigurationException
    {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
       
        Source schemaFile = getFaceletSchemaFileAsSource(externalContext);
        if (schemaFile == null)
        {
            throw new IOException("Could not find schema file for validation.");
        }
        schemaFactory.setResourceResolver(ConfigFilesXmlValidationUtils.JAVAEE_5_LS_RESOURCE_RESOLVER);
        Schema schema = schemaFactory.newSchema(schemaFile);
   
        Validator validator = schema.newValidator();
        URLConnection conn = xmlFile.openConnection();
        conn.setUseCaches(false);
        InputStream is = conn.getInputStream();
View Full Code Here

TOP

Related Classes of javax.xml.validation.SchemaFactory

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.