Search in sources :

Example 1 with XMLFilterImpl

use of org.xml.sax.helpers.XMLFilterImpl in project ddf by codice.

the class XacmlClient method addNamespaceAndPrefixes.

/**
     * 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.
     *
     * @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 xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser) {

            @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.info(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.info(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    return domResult;
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 2 with XMLFilterImpl

use of org.xml.sax.helpers.XMLFilterImpl in project robovm by robovm.

the class XMLFilterImplTest method testXMLFilterImplXMLReader.

public void testXMLFilterImplXMLReader() {
    // Ordinary case
    assertEquals(null, parent.getParent());
    // null case
    XMLFilterImpl filter = new XMLFilterImpl(null);
    assertEquals(null, filter.getParent());
}
Also used : XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl)

Example 3 with XMLFilterImpl

use of org.xml.sax.helpers.XMLFilterImpl in project ddf by codice.

the class XsltMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.debug("Entering metacard xslt transform.");
    Transformer transformer;
    Map<String, Object> mergedMap = new HashMap<String, Object>(localMap);
    if (arguments != null) {
        mergedMap.putAll(arguments);
    }
    // adding metacard data not in document
    mergedMap.put("id", getValueOrEmptyString(metacard.getId()));
    mergedMap.put("siteName", getValueOrEmptyString(metacard.getSourceId()));
    mergedMap.put("title", getValueOrEmptyString(metacard.getTitle()));
    mergedMap.put("type", getValueOrEmptyString(metacard.getMetacardType()));
    mergedMap.put("date", getValueOrEmptyString(metacard.getCreatedDate()));
    mergedMap.put("product", getValueOrEmptyString(metacard.getResourceURI()));
    mergedMap.put("thumbnail", getValueOrEmptyString(metacard.getThumbnail()));
    mergedMap.put("geometry", getValueOrEmptyString(metacard.getLocation()));
    ServiceReference[] refs = null;
    try {
        LOGGER.debug("Searching for other Metacard Transformers.");
        // TODO INJECT THESE!!!
        refs = context.getServiceReferences(MetacardTransformer.class.getName(), null);
    } catch (InvalidSyntaxException e) {
    // can't happen because filter is null
    }
    if (refs != null) {
        List<String> serviceList = new ArrayList<String>();
        LOGGER.debug("Found other Metacard transformers, adding them to a service reference list.");
        for (ServiceReference ref : refs) {
            if (ref != null) {
                String title = null;
                String shortName = (String) ref.getProperty(Constants.SERVICE_SHORTNAME);
                if ((title = (String) ref.getProperty(Constants.SERVICE_TITLE)) == null) {
                    title = "View as " + shortName.toUpperCase();
                }
                String url = "/services/catalog/" + metacard.getId() + "?transform=" + shortName;
                // define the services
                serviceList.add(title);
                serviceList.add(url);
            }
        }
        mergedMap.put("services", serviceList);
    } else {
        LOGGER.debug("No other Metacard transformers were found.");
    }
    // TODO: maybe add updated, type, and uuid here?
    // map.put("updated", fmt.print(result.getPostedDate().getTime()));
    // map.put("type", card.getSingleType().getValue());
    BinaryContent resultContent;
    StreamResult resultOutput = null;
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser);
    } catch (SAXException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    Source source = new SAXSource(xmlReader, new InputSource(new StringReader(metacard.getMetadata())));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resultOutput = new StreamResult(baos);
    try {
        transformer = templates.newTransformer();
    } catch (TransformerConfigurationException tce) {
        throw new CatalogTransformerException("Could not perform Xslt transform: " + tce.getException(), tce.getCause());
    }
    if (!mergedMap.isEmpty()) {
        for (Map.Entry<String, Object> entry : mergedMap.entrySet()) {
            LOGGER.debug("Adding parameter to transform {}:{}", entry.getKey(), entry.getValue());
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
    }
    try {
        transformer.transform(source, resultOutput);
        byte[] bytes = baos.toByteArray();
        IOUtils.closeQuietly(baos);
        LOGGER.debug("Transform complete.");
        resultContent = new XsltTransformedContent(bytes, mimeType);
    } catch (TransformerException te) {
        throw new CatalogTransformerException("Could not perform Xslt transform: " + te.getMessage(), te.getCause());
    } finally {
    // TODO: if we ever start to reuse transformers, we should add this
    // code back in
    // transformer.reset();
    }
    return resultContent;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXException(org.xml.sax.SAXException) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceReference(org.osgi.framework.ServiceReference) SAXSource(javax.xml.transform.sax.SAXSource) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with XMLFilterImpl

use of org.xml.sax.helpers.XMLFilterImpl in project ddf by codice.

the class TikaInputTransformer method transformToXml.

private String transformToXml(String xhtml) {
    LOGGER.debug("Transforming xhtml to xml.");
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser);
    } catch (SAXException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    if (xmlReader != null) {
        try {
            Writer xml = new StringWriter();
            Transformer transformer = templates.newTransformer();
            transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xhtml))), new StreamResult(xml));
            return xml.toString();
        } catch (TransformerException e) {
            LOGGER.debug("Unable to transform metadata from XHTML to XML.", e);
        }
    }
    return xhtml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) InputTransformer(ddf.catalog.transform.InputTransformer) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) Writer(java.io.Writer) StringWriter(java.io.StringWriter) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 5 with XMLFilterImpl

use of org.xml.sax.helpers.XMLFilterImpl in project ddf by codice.

the class VideoInputTransformer method transformToXml.

private String transformToXml(String xhtml) {
    LOGGER.debug("Transforming xhtml to xml.");
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser);
    } catch (SAXException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    if (xmlReader != null) {
        try {
            Writer xml = new StringWriter();
            Transformer transformer = templates.newTransformer();
            transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xhtml))), new StreamResult(xml));
            return xml.toString();
        } catch (TransformerException e) {
            LOGGER.debug("Unable to transform metadata from XHTML to XML.", e);
        }
    }
    return xhtml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) InputTransformer(ddf.catalog.transform.InputTransformer) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

XMLFilterImpl (org.xml.sax.helpers.XMLFilterImpl)6 StringReader (java.io.StringReader)5 Transformer (javax.xml.transform.Transformer)5 TransformerException (javax.xml.transform.TransformerException)5 SAXSource (javax.xml.transform.sax.SAXSource)5 InputSource (org.xml.sax.InputSource)5 SAXException (org.xml.sax.SAXException)5 XMLReader (org.xml.sax.XMLReader)5 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 StreamResult (javax.xml.transform.stream.StreamResult)3 InputTransformer (ddf.catalog.transform.InputTransformer)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 DOMResult (javax.xml.transform.dom.DOMResult)2 BinaryContent (ddf.catalog.data.BinaryContent)1 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1