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