use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XsltStylesheet method transform.
@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
argumentTypeCheck(runtime, args[0]);
NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
final DOMResult result;
String stringResult = null;
try {
// DOMResult
result = tryXsltTransformation(context, args, domSource, elistener);
if (result.getNode().getFirstChild() == null) {
// StreamResult
stringResult = retryXsltTransformation(context, args, domSource, elistener);
}
} catch (TransformerConfigurationException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (TransformerException ex) {
throw runtime.newRuntimeError(ex.getMessage());
} catch (IOException ex) {
throw runtime.newRuntimeError(ex.getMessage());
}
switch(elistener.getErrorType()) {
case ERROR:
case FATAL:
throw runtime.newRuntimeError(elistener.getErrorMessage());
case WARNING:
default:
}
if (stringResult == null) {
return createDocumentFromDomResult(context, runtime, result);
} else {
return createDocumentFromString(context, runtime, stringResult);
}
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlSchema method from_document.
/*
* call-seq:
* from_document(doc)
*
* Create a new Schema from the Nokogiri::XML::Document +doc+
*/
@JRubyMethod(meta = true)
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject document) {
XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
if (!errors.isEmpty()) {
throw new RaiseException((XmlSyntaxError) errors.first());
}
DOMSource source = new DOMSource(doc.getDocument());
IRubyObject uri = doc.url(context);
if (!uri.isNil()) {
source.setSystemId(uri.convertToString().asJavaString());
}
return getSchema(context, (RubyClass) klazz, source);
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlXpathContext method register_variable.
@JRubyMethod
public IRubyObject register_variable(IRubyObject name, IRubyObject value) {
NokogiriXPathVariableResolver variableResolver = this.variableResolver;
if (variableResolver == null) {
variableResolver = NokogiriXPathVariableResolver.create();
this.variableResolver = variableResolver;
}
variableResolver.registerVariable(name.asJavaString(), value.asJavaString());
return this;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XsltStylesheet method serialize.
@JRubyMethod
public IRubyObject serialize(ThreadContext context, IRubyObject doc) throws IOException, TransformerException {
XmlDocument xmlDoc = (XmlDocument) doc;
TransformerImpl transformer = (TransformerImpl) this.sheet.newTransformer();
ByteArrayOutputStream writer = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(writer);
SerializationHandler serializationHandler = transformer.createSerializationHandler(streamResult);
serializationHandler.serialize(xmlDoc.getNode());
return context.getRuntime().newString(writer.toString());
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlProcessingInstruction method rbNew.
@JRubyMethod(name = "new", meta = true, rest = true, required = 3)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
IRubyObject doc = args[0];
IRubyObject target = args[1];
IRubyObject data = args[2];
Document document = ((XmlNode) doc).getOwnerDocument();
Node node = document.createProcessingInstruction(rubyStringToString(target), rubyStringToString(data));
XmlProcessingInstruction self = new XmlProcessingInstruction(context.getRuntime(), (RubyClass) klazz, node);
RuntimeHelpers.invoke(context, self, "initialize", args);
return self;
}
Aggregations