use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.
the class AbstractGmdTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
StringWriter stringWriter = new StringWriter();
Boolean omitXmlDec = null;
if (MapUtils.isNotEmpty(arguments)) {
omitXmlDec = (Boolean) arguments.get(CswConstants.OMIT_XML_DECLARATION);
}
if (omitXmlDec == null || !omitXmlDec) {
stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
}
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
MarshallingContext context = new TreeMarshaller(writer, null, null);
copyArgumentsToContext(context, arguments);
converterSupplier.get().marshal(metacard, writer, context);
ByteArrayInputStream bais = new ByteArrayInputStream(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
return new BinaryContentImpl(bais, CswRecordConverter.XML_MIME_TYPE);
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project weixin-java-tools by chanjarster.
the class XStreamInitializer method getInstance.
public static XStream getInstance() {
XStream xstream = new XStream(new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out, getNameCoder()) {
protected String PREFIX_CDATA = "<![CDATA[";
protected String SUFFIX_CDATA = "]]>";
protected String PREFIX_MEDIA_ID = "<MediaId>";
protected String SUFFIX_MEDIA_ID = "</MediaId>";
@Override
protected void writeText(QuickWriter writer, String text) {
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
writer.write(text);
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
writer.write(text);
} else {
super.writeText(writer, text);
}
}
};
}
});
xstream.ignoreUnknownElements();
xstream.setMode(XStream.NO_REFERENCES);
xstream.addPermission(NullPermission.NULL);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
return xstream;
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project openhab1-addons by openhab.
the class ZWaveNodeSerializer method SerializeNode.
/**
* Serializes an XML tree of a {@link ZWaveNode}
*
* @param node
* the node to serialize
*/
public void SerializeNode(ZWaveNode node) {
synchronized (stream) {
// then when the binding starts it will have incomplete information!
if (node.getNodeInitializationStage().isStaticComplete() == false) {
logger.debug("NODE {}: Serialise aborted as static stages not complete", node.getNodeId());
return;
}
File file = new File(this.folderName, String.format("node%d.xml", node.getNodeId()));
BufferedWriter writer = null;
logger.debug("NODE {}: Serializing to file {}", node.getNodeId(), file.getPath());
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
stream.marshal(node, new PrettyPrintWriter(writer));
writer.flush();
} catch (IOException e) {
logger.error("NODE {}: Error serializing to file: {}", node.getNodeId(), e.getMessage());
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
}
}
}
}
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.
the class TestGmdConverter method convert.
private String convert(Object object, boolean writeNamespaces) {
GmdConverter converter = new GmdConverter();
StringWriter stringWriter = new StringWriter();
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter, new NoNameCoder());
MarshallingContext context = new TreeMarshaller(writer, null, null);
context.put(CswConstants.WRITE_NAMESPACES, writeNamespaces);
converter.marshal(object, writer, context);
return stringWriter.toString();
}
use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project ddf by codice.
the class TestCswRecordConverter method testMarshalBriefRecord.
@Test
public void testMarshalBriefRecord() throws IOException, JAXBException, SAXException, XpathException {
Metacard metacard = getTestMetacard();
StringWriter stringWriter = new StringWriter();
PrettyPrintWriter writer = new PrettyPrintWriter(stringWriter);
MarshallingContext context = new TreeMarshaller(writer, null, null);
context.put(CswConstants.ELEMENT_SET_TYPE, ElementSetType.BRIEF);
converter.marshal(metacard, writer, context);
String xml = stringWriter.toString();
assertThat(xml, containsString(CswConstants.CSW_BRIEF_RECORD));
assertRecordXml(xml, metacard, BRIEF);
}
Aggregations