Example usage for javax.xml.validation SchemaFactory newInstance

List of usage examples for javax.xml.validation SchemaFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.validation SchemaFactory newInstance.

Prototype

public static SchemaFactory newInstance(String schemaLanguage) 

Source Link

Document

Lookup an implementation of the SchemaFactory that supports the specified schema language and return it.

Usage

From source file:i5.las2peer.services.mobsos.surveys.SurveyService.java

/**
 * Initialize XML parser and validator for questionnaire forms and answers
 * /*from  w w  w .  j av a2  s .  co  m*/
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private void initXMLInfrastructure() throws SAXException, ParserConfigurationException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File(questionnaireSchemaPath));
    validator = schema.newValidator();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setSchema(schema);
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);

    parser = dbf.newDocumentBuilder();
}

From source file:com.evolveum.midpoint.prism.schema.SchemaRegistry.java

private void parseJavaxSchema() throws SAXException, IOException {
    schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] sources = new Source[schemaDescriptions.size()];
    int i = 0;//w  w  w  .ja va2 s . c  o  m
    for (SchemaDescription schemaDescription : schemaDescriptions) {
        Source source = schemaDescription.getSource();
        sources[i] = source;
        i++;
    }
    schemaFactory.setResourceResolver(this);
    javaxSchema = schemaFactory.newSchema(sources);
}

From source file:com.maxl.java.aips2sqlite.Aips2Sqlite.java

static List<MedicalInformations.MedicalInformation> readAipsFile() {
    List<MedicalInformations.MedicalInformation> med_list = null;
    try {//from w w  w.  java  2s.  c  o  m
        JAXBContext context = JAXBContext.newInstance(MedicalInformations.class);

        // Validation
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(Constants.FILE_MEDICAL_INFOS_XSD));
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler());

        // Marshaller
        /*
         * Marshaller ma = context.createMarshaller();
         * ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
         * MedicalInformations medi_infos = new MedicalInformations();
         * ma.marshal(medi_infos, System.out);
         */
        // Unmarshaller
        long startTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.print("- Unmarshalling Swissmedic xml... ");

        FileInputStream fis = new FileInputStream(new File(Constants.FILE_MEDICAL_INFOS_XML));
        Unmarshaller um = context.createUnmarshaller();
        MedicalInformations med_infos = (MedicalInformations) um.unmarshal(fis);
        med_list = med_infos.getMedicalInformation();

        long stopTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println(med_list.size() + " medis in " + (stopTime - startTime) / 1000.0f + " sec");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    return med_list;
}

From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.PluginConfigurationServiceDefault.java

protected void initializeXmlDigesterLoader() {
    if (PluginConfigurationServiceDefault.logger.isDebugEnabled())
        PluginConfigurationServiceDefault.logger.debug("Initializing the XML digester.");

    Schema schema;/*  w ww .  java2  s .  c  o  m*/
    FileInputStream stream = null;
    try {
        stream = FileUtils.openInputStream(this.xsdFile);
        schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(stream));
    } catch (SAXException e) {
        throw new FatalBeanException("Could not parse plugin configuration XSD", e);
    } catch (IOException e) {
        throw new FatalBeanException("Could not parse plugin configuration XSD", e);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
            PluginConfigurationServiceDefault.logger.warn("Failed to close XSD file stream.", e);
        }
    }

    this.digesterLoader.setNamespaceAware(true);
    this.digesterLoader.setSchema(schema);
    this.digesterLoader.setErrorHandler(new ConfigurationErrorHandler());
    this.digesterLoader.setUseContextClassLoader(false);
    this.digesterLoader.setClassLoader(Digester.class.getClassLoader());

    ConvertUtils.register(new JodaXML8601DateTimeConverter(), DateTime.class);
}

From source file:com.thoughtworks.go.config.MagicalGoConfigXmlWriterTest.java

@Test
public void shouldBeAValidXSD() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    factory.newSchema(new StreamSource(getClass().getResourceAsStream("/cruise-config.xsd")));
}

From source file:com.evolveum.midpoint.prism.schema.SchemaRegistryImpl.java

private void parseJavaxSchema() throws SAXException, IOException {
    schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] sources = new Source[schemaDescriptions.size()];
    int i = 0;/*  ww  w  .j av a  2  s .c o  m*/
    for (SchemaDescription schemaDescription : schemaDescriptions) {
        Source source = schemaDescription.getSource();
        sources[i] = source;
        i++;
    }
    schemaFactory.setResourceResolver(entityResolver);
    javaxSchema = schemaFactory.newSchema(sources);
}

From source file:info.novatec.ita.check.config.StereotypeCheckReader.java

/**
 * Read and validate the given file to a configuration for stereotype check.
 * /*  w  w w  .j a v a 2  s .c  o  m*/
 * @param file
 *            The file to read.
 * @param additionalCheckCfg
 *            a previously read configuration which may override parts of
 *            the configuration read by the file.
 * @param readingAdditionalCfg
 *            Are we reading the additionalCfg.
 * @return the configuration.
 * @throws XMLStreamException
 * @throws IllegalArgumentException
 * @throws SAXException
 * @throws IOException
 */
private static StereotypeCheckConfiguration read(File file, String checkstyleStereotypeXsd,
        StereotypeCheckConfiguration additionalCheckCfg, boolean readingAdditionalCfg)
        throws XMLStreamException, IllegalArgumentException, SAXException, IOException {

    // Validate with StreamSource because Stax Validation is not provided
    // with every implementation of JAXP
    SchemaFactory schemafactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemafactory
            .newSchema(StereotypeCheckReader.class.getClassLoader().getResource(checkstyleStereotypeXsd));

    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(file));

    // Parse with Stax
    XMLStreamReader reader = XMLInputFactory.newInstance()
            .createXMLStreamReader(new BufferedInputStream(new FileInputStream(file)));
    StereotypeCheckConfigurationReader delegate = new StereotypeCheckConfigurationReader(reader,
            additionalCheckCfg, readingAdditionalCfg);

    while (delegate.hasNext()) {
        delegate.next();
    }

    return delegate.getConfig();
}

From source file:com.graphhopper.util.InstructionListTest.java

public void verifyGPX(String gpx) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;/*  w  w  w.  j  a  va 2s. com*/
    try {
        Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd"));
        schema = schemaFactory.newSchema(schemaFile);

        // using more schemas: http://stackoverflow.com/q/1094893/194609
    } catch (SAXException e1) {
        throw new IllegalStateException(
                "There was a problem with the schema supplied for validation. Message:" + e1.getMessage());
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(gpx)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:hydrograph.ui.propertywindow.widgets.customwidgets.schema.GridRowLoader.java

private boolean validateXML(InputStream xml, InputStream xsd) {
    try {//www . j  a  v  a  2s.  c  om
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();

        validator.validate(new StreamSource(xml));
        return true;
    } catch (SAXException | IOException ex) {
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error",
                Messages.IMPORT_XML_FORMAT_ERROR + "-\n" + ex.getMessage());
        logger.error(Messages.IMPORT_XML_FORMAT_ERROR);
        return false;
    }
}

From source file:org.rivetlogic.export.components.AbstractXMLProcessor.java

public void setXsdPath(String xsdPath) throws SAXException {
    this.xsdPath = xsdPath;
    if (xsdPath != null) {

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = null;//  w  ww.  j a  va2  s  .  c  o  m
        try {
            schema = factory.newSchema(this.getClass().getClassLoader().getResource(xsdPath));
        } catch (SAXException e) {
            logger.error(e);
            throw e;
        }
        validator = schema.newValidator();
    }
}