Skip to content

Instantly share code, notes, and snippets.

@Serabe
Created June 24, 2009 22:11
Show Gist options
  • Select an option

  • Save Serabe/135547 to your computer and use it in GitHub Desktop.

Select an option

Save Serabe/135547 to your computer and use it in GitHub Desktop.
@JRubyMethod
public IRubyObject native_write_to(ThreadContext context, IRubyObject io, IRubyObject encoding, IRubyObject indentString, IRubyObject options) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(this.node), new StreamResult(sw));
} catch (TransformerException te) {
throw context.getRuntime().newRuntimeError("couldn't transform the node back to string");
}
RuntimeHelpers.invoke(context, io, "write", context.getRuntime().newString(sw.toString()));
return io;
}
/*
* call-seq:
* native_write_to(io, encoding, options)
*
* Write this Node to +io+ with +encoding+ and +options+
*/
static VALUE native_write_to(
VALUE self,
VALUE io,
VALUE encoding,
VALUE indent_string,
VALUE options
) {
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlIndentTreeOutput = 1;
xmlTreeIndentString = StringValuePtr(indent_string);
xmlSaveCtxtPtr savectx = xmlSaveToIO(
(xmlOutputWriteCallback)io_write_callback,
(xmlOutputCloseCallback)io_close_callback,
(void *)io,
RTEST(encoding) ? StringValuePtr(encoding) : NULL,
NUM2INT(options)
);
xmlSaveTree(savectx, node);
xmlSaveClose(savectx);
return io;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment