private static String asString(Node node) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); XmlNodePrinter nodePrinter = new XmlNodePrinter(pw); nodePrinter.setPreserveWhitespace(true); nodePrinter.print(node); return sw.toString(); }
if (isEmptyElement(node)) { printLineBegin(); out.print("<"); out.print(getName(node)); if (ctx != null) { printNamespace(node, ctx); printNameAttributes(node.attributes(), ctx); if (expandEmptyElements) { out.print("></"); out.print(getName(node)); out.print(">"); } else { out.print("/>"); printLineEnd(); out.flush(); return; if (printSpecialNode(node)) { out.flush(); return; printName(node, ctx, true, isListOfSimple((List) value)); printList((List) value, ctx); printName(node, ctx, false, isListOfSimple((List) value)); out.flush(); return;
protected void printSimpleItem(Object value) { if (!preserveWhitespace) printLineBegin(); printEscaped(InvokerHelper.toString(value), false); if (!preserveWhitespace) printLineEnd(); }
protected void printName(Node node, NamespaceContext ctx, boolean begin, boolean preserve) { if (node == null) { throw new NullPointerException("Node must not be null."); } Object name = node.name(); if (name == null) { throw new NullPointerException("Name must not be null."); } if (!preserve || begin) printLineBegin(); out.print("<"); if (!begin) { out.print("/"); } out.print(getName(node)); if (ctx != null) { printNamespace(node, ctx); } if (begin) { printNameAttributes(node.attributes(), ctx); } out.print(">"); if (!preserve || !begin) printLineEnd(); }
protected void printList(List list, NamespaceContext ctx) { out.incrementIndent(); for (Object value : list) { NamespaceContext context = new NamespaceContext(ctx); /* * If the current value is a node, recurse into that node. */ if (value instanceof Node) { print((Node) value, context); continue; } printSimpleItem(value); } out.decrementIndent(); }
protected void printNameAttributes(Map attributes, NamespaceContext ctx) { if (attributes == null || attributes.isEmpty()) { return; } for (Object p : attributes.entrySet()) { Map.Entry entry = (Map.Entry) p; out.print(" "); out.print(getName(entry.getKey())); out.print("="); Object value = entry.getValue(); out.print(quote); if (value instanceof String) { printEscaped((String) value, true); } else { printEscaped(InvokerHelper.toString(value), true); } out.print(quote); printNamespace(entry.getKey(), ctx); } }
public void print(Node node) { print(node, new NamespaceContext()); }
public String realToString(Object object) throws TranslationException { StringWriter writer = null; try { Node node = (Node)object; writer = new StringWriter(); new XmlNodePrinter(new PrintWriter(writer)).print(node); return writer.toString(); } finally { if (writer != null) { try { writer.close(); } catch (IOException ex) { } } } }
def xml = "<tag><nested>hello</nested></tag>" def stringWriter = new StringWriter() def node = new XmlParser().parseText(xml); new XmlNodePrinter(new PrintWriter(stringWriter)).print(node) println stringWriter.toString()
private XmlNodePrinter printer(OutputStream stream) { XmlNodePrinter xmlNodePrinter = new XmlNodePrinter(new PrintWriter(stream)); xmlNodePrinter.setNamespaceAware(namespaceAware); return xmlNodePrinter; }
protected void printLineEnd() { printLineEnd(null); }
private String getName(Object object) { if (object instanceof String) { return (String) object; } else if (object instanceof QName) { QName qname = (QName) object; if (!namespaceAware) { return qname.getLocalPart(); } return qname.getQualifiedName(); } else if (object instanceof Node) { Object name = ((Node) object).name(); return getName(name); } return object.toString(); }
protected void printName(Node node, NamespaceContext ctx, boolean begin, boolean preserve) { if (node == null) { throw new NullPointerException("Node must not be null."); } Object name = node.name(); if (name == null) { throw new NullPointerException("Name must not be null."); } if (!preserve || begin) printLineBegin(); out.print("<"); if (!begin) { out.print("/"); } out.print(getName(node)); if (ctx != null) { printNamespace(node, ctx); } if (begin) { printNameAttributes(node.attributes(), ctx); } out.print(">"); if (!preserve || !begin) printLineEnd(); }
protected void printList(List list, NamespaceContext ctx) { out.incrementIndent(); for (Iterator iter = list.iterator(); iter.hasNext();) { NamespaceContext context = new NamespaceContext(ctx); Object value = iter.next(); /* * If the current value is a node, recurse into that node. */ if (value instanceof Node) { print((Node) value, context); continue; } printSimpleItem(value); } out.decrementIndent(); }
protected void printNameAttributes(Map attributes, NamespaceContext ctx) { if (attributes == null || attributes.isEmpty()) { return; } for (Object p : attributes.entrySet()) { Map.Entry entry = (Map.Entry) p; out.print(" "); out.print(getName(entry.getKey())); out.print("="); Object value = entry.getValue(); out.print(quote); if (value instanceof String) { printEscaped((String) value, true); } else { printEscaped(InvokerHelper.toString(value), true); } out.print(quote); printNamespace(entry.getKey(), ctx); } }
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { printer(stream).print((Node) graph); }
private void setGPathParamValue(String varName, String varType, Object value) throws ActivityException { if (isOutputDocument(varName)) { com.centurylink.mdw.variable.VariableTranslator translator = VariableTranslator.getTranslator(getPackage(), varType); DocumentReferenceTranslator docRefTranslator = (DocumentReferenceTranslator) translator; try { if (value instanceof Node) new XmlNodePrinter(new PrintWriter(outputDocumentWriter)).print((Node)value); Object doc = docRefTranslator.realToObject(outputDocumentWriter.toString()); super.setVariableValue(varName, varType, doc); } catch (Exception ex) { logger.severeException(ex.getMessage(), ex); throw new ActivityException(ex.getMessage(), ex); } } else { super.setVariableValue(varName, varType, value); } }
String xml = "<tag><nested>hello</nested></tag>"; StringWriter stringWriter = new StringWriter(); Node node = new XmlParser().parseText(xml); new XmlNodePrinter(new PrintWriter(stringWriter)).print(node); System.out.println(stringWriter.toString());
private XmlNodePrinter newPrinter(OutputStream stream) { XmlNodePrinter xmlNodePrinter = new XmlNodePrinter(new PrintWriter(stream)); xmlNodePrinter.setNamespaceAware(isNamespaceAware()); return xmlNodePrinter; }
protected void printLineEnd() { printLineEnd(null); }