use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method children.
@JRubyMethod
public IRubyObject children(ThreadContext context) {
XmlNodeSet xmlNodeSet = XmlNodeSet.create(context.runtime);
NodeList nodeList = node.getChildNodes();
if (nodeList.getLength() > 0) {
// initializes @document from first node
xmlNodeSet.setNodeList(nodeList);
} else {
// TODO this is very ripe for refactoring
setDocumentAndDecorate(context, xmlNodeSet, doc);
}
return xmlNodeSet;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method namespace_definitions.
/**
* Return an array of XmlNamespace nodes based on the attributes
* of this node.
*/
@JRubyMethod
public IRubyObject namespace_definitions(ThreadContext context) {
// don't use namespace_definitions cache anymore since
// namespaces might be deleted. Reflecting the result of
// namesapce removals is complicated, so the cache might not be
// updated.
Ruby ruby = context.getRuntime();
RubyArray namespace_definitions = ruby.newArray();
if (doc == null)
return namespace_definitions;
if (doc instanceof HtmlDocument)
return namespace_definitions;
List<XmlNamespace> namespaces = ((XmlDocument) doc).getNamespaceCache().get(node);
for (XmlNamespace namespace : namespaces) {
namespace_definitions.append(namespace);
}
return namespace_definitions;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XsltStylesheet method parse_stylesheet_doc.
@JRubyMethod(meta = true, rest = true)
public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
Ruby runtime = context.getRuntime();
ensureFirstArgIsDocument(runtime, args[0]);
XmlDocument xmlDoc = (XmlDocument) args[0];
ensureDocumentHasNoError(context, xmlDoc);
Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
XsltStylesheet xslt = (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass) klazz);
try {
xslt.init(args[1], doc);
} catch (TransformerConfigurationException ex) {
throw runtime.newRuntimeError("could not parse xslt stylesheet");
}
return xslt;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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());
DOMResult result = null;
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 gocd by gocd.
the class XmlDocument method canonicalize.
/* call-seq:
* doc.canonicalize(mode=XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
* doc.canonicalize { |obj, parent| ... }
*
* Canonicalize a document and return the results. Takes an optional block
* that takes two parameters: the +obj+ and that node's +parent+.
* The +obj+ will be either a Nokogiri::XML::Node, or a Nokogiri::XML::Namespace
* The block must return a non-nil, non-false value if the +obj+ passed in
* should be included in the canonicalized document.
*/
@JRubyMethod(optional = 3)
public IRubyObject canonicalize(ThreadContext context, IRubyObject[] args, Block block) {
Integer mode = 0;
String inclusive_namespace = null;
Boolean with_comments = false;
if (args.length > 0 && !(args[0].isNil())) {
mode = RubyFixnum.fix2int(args[0]);
}
if (args.length > 1) {
if (!args[1].isNil() && !(args[1] instanceof List)) {
throw context.getRuntime().newTypeError("Expected array");
}
if (!args[1].isNil()) {
inclusive_namespace = (String) ((RubyArray) args[1]).join(context, context.getRuntime().newString(" ")).asString().asJavaString();
}
}
if (args.length > 2) {
with_comments = args[2].isTrue();
}
String algorithmURI = null;
switch(mode) {
case // XML_C14N_1_0
0:
if (with_comments)
algorithmURI = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
else
algorithmURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
break;
case // XML_C14N_EXCLUSIVE_1_0
1:
if (with_comments)
algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
else
algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
break;
case // XML_C14N_1_1 = 2
2:
if (with_comments)
algorithmURI = Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS;
else
algorithmURI = Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS;
}
try {
Canonicalizer canonicalizer = Canonicalizer.getInstance(algorithmURI);
XmlNode startingNode = getStartingNode(block);
byte[] result;
CanonicalFilter filter = new CanonicalFilter(context, block);
if (inclusive_namespace == null) {
result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), filter);
} else {
result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), inclusive_namespace, filter);
}
String resultString = new String(result, "UTF-8");
return stringOrNil(context.getRuntime(), resultString);
} catch (CanonicalizationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return context.getRuntime().getNil();
}
Aggregations