Example usage for javax.xml.transform.dom DOMResult DOMResult

List of usage examples for javax.xml.transform.dom DOMResult DOMResult

Introduction

In this page you can find the example usage for javax.xml.transform.dom DOMResult DOMResult.

Prototype

public DOMResult() 

Source Link

Document

Zero-argument default constructor.

Usage

From source file:org.wso2.carbon.identity.entitlement.EntitlementUtil.java

/**
 * Validates the given policy XML files against the standard XACML policies.
 *
 * @param policy Policy to validate//from  w  ww  . ja v a2 s. c o  m
 * @return return false, If validation failed or XML parsing failed or any IOException occurs
 */
public static boolean validatePolicy(PolicyDTO policy) {
    try {

        if (!"true".equalsIgnoreCase((String) EntitlementServiceComponent.getEntitlementConfig()
                .getEngineProperties().get(EntitlementExtensionBuilder.PDP_SCHEMA_VALIDATION))) {
            return true;
        }

        // there may be cases where you only updated the policy meta data in PolicyDTO not the
        // actual XACML policy String
        if (policy.getPolicy() == null || policy.getPolicy().trim().length() < 1) {
            return true;
        }

        //get policy version
        String policyXMLNS = getPolicyVersion(policy.getPolicy());

        Map<String, Schema> schemaMap = EntitlementServiceComponent.getEntitlementConfig().getPolicySchemaMap();
        //load correct schema by version
        Schema schema = schemaMap.get(policyXMLNS);

        if (schema != null) {
            //build XML document
            DocumentBuilder documentBuilder = getSecuredDocumentBuilder(false);
            InputStream stream = new ByteArrayInputStream(policy.getPolicy().getBytes());
            Document doc = documentBuilder.parse(stream);
            //Do the DOM validation
            DOMSource domSource = new DOMSource(doc);
            DOMResult domResult = new DOMResult();
            Validator validator = schema.newValidator();
            validator.validate(domSource, domResult);
            if (log.isDebugEnabled()) {
                log.debug("XACML Policy validation succeeded with the Schema");
            }
            return true;
        } else {
            log.error("Invalid Namespace in policy");
        }
    } catch (SAXException e) {
        log.error("XACML policy is not valid according to the schema :" + e.getMessage());
    } catch (IOException e) {
        //ignore
    } catch (ParserConfigurationException e) {
        //ignore
    }
    return false;
}

From source file:pl.nask.hsn2.workflow.parser.HWLParser.java

private void createSchema() throws IOException, SAXException {
    final DOMResult result = new DOMResult();

    SchemaOutputResolver outputResolver = new HwlSchemaOutputResolver(result);

    ctx.generateSchema(outputResolver);//from   w  ww.j a v a  2  s  . c om
    this.schemaNode = result.getNode();
    this.schemaSystemId = result.getSystemId();

    Source source = new DOMSource(schemaNode, schemaSystemId);

    this.schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);
}

From source file:sernet.gs.scraper.ZIPGSSource.java

private Node parseDocument(InputStream inputstream, String encoding)
        throws TransformerConfigurationException, IOException, SAXException {

    InputStreamReader reader = new InputStreamReader(inputstream, encoding);
    BufferedReader buffRead = new BufferedReader(reader);

    SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler th = stf.newTransformerHandler();
    DOMResult dr = new DOMResult();
    th.setResult(dr);/*  www  . j av  a2 s .c om*/
    Parser parser = new Parser();
    parser.setContentHandler(th);
    parser.parse(new InputSource(buffRead));
    Node domRootNode = dr.getNode();
    domRootNode.normalize();

    buffRead.close();
    reader.close();
    inputstream.close();

    return domRootNode;

}