Example usage for javax.xml.validation Validator validate

List of usage examples for javax.xml.validation Validator validate

Introduction

In this page you can find the example usage for javax.xml.validation Validator validate.

Prototype

public void validate(Source source) throws SAXException, IOException 

Source Link

Document

Validates the specified input.

Usage

From source file:voldemort.xml.ClusterMapper.java

@SuppressWarnings("unchecked")
public Cluster readCluster(Reader input, boolean verifySchema) {
    try {/*from   w  ww .j a v a2  s . c om*/
        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);
    }
}

From source file:voldemort.xml.StoreDefinitionsMapper.java

public List<StoreDefinition> readStoreList(Reader input, boolean verifySchema) {
    try {/*www  . ja va 2  s .co  m*/

        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);
    }
}