use of ddf.catalog.transformer.xml.binding.StringxmlElement in project ddf by codice.
the class StringxmlAdapter method marshalFrom.
/**
* @param attribute
* @return JAXB representable attribute
* @throws CatalogTransformerException
*/
public static StringxmlElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
StringxmlElement element = new StringxmlElement();
element.setName(attribute.getName());
if (attribute.getValue() != null) {
for (Serializable value : attribute.getValues()) {
if (!(value instanceof String)) {
continue;
}
String xmlString = (String) value;
Element anyElement = null;
DocumentBuilder builder = null;
try {
synchronized (FACTORY) {
builder = FACTORY.newDocumentBuilder();
builder.setErrorHandler(null);
}
anyElement = builder.parse(new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))).getDocumentElement();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new CatalogTransformerException(TRANSFORMATION_FAILED_ERROR_MESSAGE, e);
}
Value anyValue = new StringxmlElement.Value();
anyValue.setAny(anyElement);
element.getValue().add(anyValue);
}
}
return element;
}
use of ddf.catalog.transformer.xml.binding.StringxmlElement in project ddf by codice.
the class StringxmlAdapter method unmarshalFrom.
public static Attribute unmarshalFrom(StringxmlElement element) throws CatalogTransformerException, TransformerException, JAXBException {
AttributeImpl attribute = null;
if (templates == null) {
throw new CatalogTransformerException("Could not transform XML due to internal configuration error.");
}
for (Value xmlValue : element.getValue()) {
String xmlString = "";
Element anyNode = xmlValue.getAny();
StringWriter buffer = new StringWriter();
Transformer transformer = templates.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(anyNode), new StreamResult(buffer));
xmlString = buffer.toString();
if (attribute == null) {
attribute = new AttributeImpl(element.getName(), xmlString);
} else {
attribute.addValue(xmlString);
}
}
return attribute;
}
Aggregations