Search in sources :

Example 46 with Validator

use of javax.xml.validation.Validator in project languagetool by languagetool-org.

the class XMLValidator method getValidator.

private Validator getValidator(URL xmlSchema) throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(xmlSchema);
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler());
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 47 with Validator

use of javax.xml.validation.Validator in project honeycomb by altamiracorp.

the class ConfigurationParser method checkValidConfig.

/**
     * Performs validation on the configuration content supplied by the
     * configuration supplier against the schema document provided by the
     * validation supplier.  Throws Runtime exception if validation fails.
     *
     * @param configSupplier The supplier that provides the configuration to inspect, not null
     * @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
     */
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier, final InputSupplier<? extends InputStream> schemaSupplier) {
    try {
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
        final Validator validator = schema.newValidator();
        validator.validate(new StreamSource(configSupplier.getInput()));
    } catch (Exception e) {
        logger.error("Unable to validate honeycomb configuration.", e);
        throw new RuntimeException("Exception while validating honeycomb configuration.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Validator(javax.xml.validation.Validator) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 48 with Validator

use of javax.xml.validation.Validator in project voldemort by voldemort.

the class ClusterMapper method readCluster.

@SuppressWarnings("unchecked")
public Cluster readCluster(Reader input, boolean verifySchema) {
    try {
        SAXBuilder builder = new SAXBuilder(false);
        Document doc = builder.build(input);
        if (verifySchema) {
            Validator validator = this.schema.newValidator();
            validator.validate(new JDOMSource(doc));
        }
        Element root = doc.getRootElement();
        if (!root.getName().equals(CLUSTER_ELMT))
            throw new MappingException("Invalid root element: " + doc.getRootElement().getName());
        String name = root.getChildText(CLUSTER_NAME_ELMT);
        List<Zone> zones = new ArrayList<Zone>();
        for (Element node : (List<Element>) root.getChildren(ZONE_ELMT)) zones.add(readZone(node));
        List<Node> servers = new ArrayList<Node>();
        for (Element node : (List<Element>) root.getChildren(SERVER_ELMT)) servers.add(readServer(node));
        return new Cluster(name, servers, zones);
    } catch (JDOMException e) {
        throw new MappingException(e);
    } catch (SAXException e) {
        throw new MappingException(e);
    } catch (IOException e) {
        throw new MappingException(e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Zone(voldemort.cluster.Zone) Element(org.jdom.Element) Node(voldemort.cluster.Node) ArrayList(java.util.ArrayList) JDOMSource(org.jdom.transform.JDOMSource) Cluster(voldemort.cluster.Cluster) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) ArrayList(java.util.ArrayList) List(java.util.List) Validator(javax.xml.validation.Validator)

Example 49 with Validator

use of javax.xml.validation.Validator in project voldemort by voldemort.

the class StoreDefinitionsMapper method readStoreList.

public List<StoreDefinition> readStoreList(Reader input, boolean verifySchema) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(input);
        if (verifySchema) {
            Validator validator = schema.newValidator();
            validator.validate(new JDOMSource(doc));
        }
        Element root = doc.getRootElement();
        if (!root.getName().equals(STORES_ELMT))
            throw new MappingException("Invalid root element: " + doc.getRootElement().getName());
        List<StoreDefinition> stores = new ArrayList<StoreDefinition>();
        for (Object store : root.getChildren(STORE_ELMT)) stores.add(readStore((Element) store));
        for (Object view : root.getChildren(VIEW_ELMT)) stores.add(readView((Element) view, stores));
        return stores;
    } catch (JDOMException e) {
        throw new MappingException(e);
    } catch (SAXException e) {
        throw new MappingException(e);
    } catch (IOException e) {
        throw new MappingException(e);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) ArrayList(java.util.ArrayList) JDOMSource(org.jdom.transform.JDOMSource) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) SAXException(org.xml.sax.SAXException) StoreDefinition(voldemort.store.StoreDefinition) Validator(javax.xml.validation.Validator)

Example 50 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2IdentifiersTest method validateSampleXML.

public void validateSampleXML(String name) throws SAXException, IOException {
    Source source = getInputStream("/record_2.0/samples/read_samples/" + name + "-2.0.xml");
    Validator validator = getValidator(name);
    validator.validate(source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) Validator(javax.xml.validation.Validator)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7