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