Example usage for org.xml.sax.helpers XMLFilterImpl XMLFilterImpl

List of usage examples for org.xml.sax.helpers XMLFilterImpl XMLFilterImpl

Introduction

In this page you can find the example usage for org.xml.sax.helpers XMLFilterImpl XMLFilterImpl.

Prototype

public XMLFilterImpl(XMLReader parent) 

Source Link

Document

Construct an XML filter with the specified parent.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLReader xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
        String namespace = "http://www.java2s.com/schemas.xsd";
        String pref = "ns0:";

        @Override//  w ww  .  j a  va 2  s  . com
        public void startElement(String uri, String localName, String qName, Attributes atts)
                throws SAXException {
            super.startElement(namespace, localName, pref + qName, atts);
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(namespace, localName, pref + qName);
        }
    };
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    StringWriter s = new StringWriter();
    t.transform(new SAXSource(xmlReader, new InputSource("test.xml")), new StreamResult(s));
    System.out.println(s);
}

From source file:ddf.security.pdp.realm.xacml.processor.BalanaClient.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the Balana PDP. The
 * Balana PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response.//w ww  .ja v  a2  s . c  o m
 *
 * @param xacmlResponse The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(BalanaClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        domResult = new DOMResult();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    return domResult;
}

From source file:ddf.security.pdp.realm.xacml.processor.XacmlClient.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
 * XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response./* w  w  w  .j ava2s.c  o m*/
 *
 * @param xacmlResponse The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        domResult = new DOMResult();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    return domResult;
}

From source file:ddf.security.pdp.xacml.processor.BalanaPdp.java

/**
 * Adds namespaces and namespace prefixes to the XACML response returned by the Balana PDP. The
 * Balana PDP returns a response with no namespaces, so we need to add them to unmarshal the
 * response./*from   w  ww .j a v a  2 s . co  m*/
 * 
 * @param xacmlResponse
 *            The XACML response as a string.
 * @return DOM representation of the XACML response with namespaces and namespace prefixes.
 * @throws PdpException
 */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;

    try {
        xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    DOMResult domResult = new DOMResult();

    try {
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))),
                domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.error(message);
        throw new PdpException(message, e);
    }

    return domResult;
}

From source file:de.juwimm.cms.remote.EditionServiceSpringImpl.java

private Document getDocumentFromFile(File preparsedXMLfile, String editionFileName) throws Exception {
    if (log.isInfoEnabled())
        log.info("Finished writing Edition to File, starting to import it as GZIP-InputStream...");
    XMLFilter filter = new XMLFilterImpl(XMLReaderFactory.createXMLReader());
    preparsedXMLfile = File.createTempFile("edition_import_preparsed_", ".xml");
    if (log.isDebugEnabled())
        log.debug("preparsedXMLfile: " + preparsedXMLfile.getAbsolutePath());
    XMLWriter xmlWriter = new XMLWriter(new OutputStreamWriter(new FileOutputStream(preparsedXMLfile)));
    filter.setContentHandler(new EditionBlobContentHandler(xmlWriter, preparsedXMLfile));
    InputSource saxIn = null;//from   w w w. jav  a2  s .c o  m
    try {
        try {
            saxIn = new InputSource(new GZIPInputStream(new FileInputStream(editionFileName)));
        } catch (Exception exe) {
            saxIn = new InputSource(new BufferedReader(new FileReader(editionFileName)));
        }
    } catch (FileNotFoundException exe) {
        log.error("Edition file isnt available anymore. Edition needs to be deleted!");
    }
    filter.parse(saxIn);
    xmlWriter.flush();
    xmlWriter = null;
    filter = null;
    System.gc();
    if (log.isInfoEnabled())
        log.info("Finished cutting BLOBs, starting to open XML Document...");
    // BufferedReader br = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
    // InputSource in = new InputSource(br);
    InputSource domIn = new InputSource(new FileInputStream(preparsedXMLfile));
    return XercesHelper.inputSource2Dom(domIn);
}

From source file:ddf.services.schematron.SchematronValidationService.java

private SchematronReport generateReport(String metadata, Templates validator)
        throws SchematronValidationException {

    XMLReader xmlReader = null;/*ww  w. j a  v  a2s.c o m*/
    try {
        XMLReader xmlParser = XML_UTILS.getSecureXmlParser();
        xmlReader = new XMLFilterImpl(xmlParser);
    } catch (SAXException e) {
        throw new SchematronValidationException(e);
    }

    SchematronReport report;
    try {
        Transformer transformer = validator.newTransformer();
        DOMResult schematronResult = new DOMResult();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(metadata))),
                schematronResult);
        report = new SvrlReport(schematronResult);
    } catch (TransformerException e) {
        throw new SchematronValidationException("Could not setup validator to perform validation.", e);
    }
    return report;
}

From source file:de.juwimm.cms.remote.ViewServiceSpringImpl.java

@Override
protected ViewComponentValue handleImportViewComponent(Integer parentId, InputStream xmlFile, boolean withMedia,
        boolean withChildren, Integer unitId, boolean useNewIds, Integer siteId, Integer fulldeploy)
        throws Exception {
    String tmpFileName = "";
    try {//w w  w.  j  av  a 2  s  . c  om
        tmpFileName = this.storeSiteFile(xmlFile);
        if (log.isInfoEnabled())
            log.info("-------------->importFile saved.");
    } catch (IOException e) {
        log.warn("Unable to copy received inputstream: " + e.getMessage(), e);
    }
    ViewComponentHbm parent = getViewComponentHbmDao().load(parentId);
    ViewComponentHbm firstChild = parent.getFirstChild();
    File preparsedXMLfile = null;
    XMLFilter filter = new XMLFilterImpl(XMLReaderFactory.createXMLReader());
    preparsedXMLfile = File.createTempFile("edition_import_preparsed_", ".xml");
    if (log.isDebugEnabled())
        log.debug("preparsedXMLfile: " + preparsedXMLfile.getAbsolutePath());
    XMLWriter xmlWriter = new XMLWriter(new OutputStreamWriter(new FileOutputStream(preparsedXMLfile)));
    filter.setContentHandler(new de.juwimm.cms.util.EditionBlobContentHandler(xmlWriter, preparsedXMLfile));
    InputSource saxIn = null;
    try {
        try {
            saxIn = new InputSource(new GZIPInputStream(new FileInputStream(tmpFileName)));
        } catch (Exception exe) {
            saxIn = new InputSource(new BufferedReader(new FileReader(tmpFileName)));
        }
    } catch (FileNotFoundException exe) {
        if (log.isDebugEnabled())
            log.error("Error at creating InputSource in paste");
    }
    filter.parse(saxIn);
    xmlWriter.flush();
    xmlWriter = null;
    filter = null;
    System.gc();
    InputSource domIn = new InputSource(new BufferedInputStream(new FileInputStream(preparsedXMLfile)));
    org.w3c.dom.Document doc = XercesHelper.inputSource2Dom(domIn);
    Hashtable<Integer, Integer> picIds = null;
    Hashtable<Integer, Integer> docIds = null;
    if (withMedia) {
        picIds = importPictures(unitId, doc, preparsedXMLfile, useNewIds);
        docIds = importDocuments(unitId, doc, preparsedXMLfile, useNewIds);
    }
    /**import persons */
    mappingPersons = new Hashtable<Long, Long>();

    Iterator it = XercesHelper.findNodes(doc, "//viewcomponent");
    while (it.hasNext()) {
        Node nodeViewComponent = (Node) it.next();
        Integer oldunitId = new Integer(((Element) nodeViewComponent).getAttribute("unitId"));
        if (oldunitId.intValue() != unitId.intValue()) {
            importPersons(unitId, doc, useNewIds, picIds);
            /**import only if not in the same unit*/
        }
    }
    ViewComponentHbm viewComponent = createViewComponentFromXml(parentId, doc, withChildren, picIds, docIds,
            useNewIds, siteId, fulldeploy, unitId);
    //importRealmsForViewComponent(doc, viewComponent, 1);
    /**import realms*/

    parent.addChild(viewComponent);
    viewComponent.setParent(parent);

    if (firstChild != null) {
        viewComponent.setNextNode(firstChild);
        firstChild.setPrevNode(viewComponent);
        parent.setFirstChild(viewComponent);
    } else {
        parent.setFirstChild(viewComponent);
    }

    return viewComponent.getDao();

}