use of org.jruby.RubyArray in project gocd by gocd.
the class XsltStylesheet method ensureDocumentHasNoError.
private static void ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc) {
Ruby runtime = context.getRuntime();
RubyArray errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
if (!errors_of_xmlDoc.isEmpty()) {
throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
}
}
use of org.jruby.RubyArray 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();
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlNode method element_children.
@JRubyMethod(name = { "element_children", "elements" })
public IRubyObject element_children(ThreadContext context) {
List<Node> elementNodes = new ArrayList<Node>();
addElements(node, elementNodes, false);
if (elementNodes.size() == 0)
return XmlNodeSet.newEmptyNodeSet(context);
RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
return xmlNodeSet;
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlElement method accept.
@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
visitor.enter((Element) node);
XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
if (xmlNodeSet.length() > 0) {
RubyArray array = (RubyArray) xmlNodeSet.to_a(context);
for (int i = 0; i < array.getLength(); i++) {
Object item = array.get(i);
if (item instanceof XmlNode) {
XmlNode cur = (XmlNode) item;
cur.accept(context, visitor);
} else if (item instanceof XmlNamespace) {
XmlNamespace cur = (XmlNamespace) item;
cur.accept(context, visitor);
}
}
}
visitor.leave((Element) node);
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlNode method accept.
// Users might extend XmlNode. This method works for such a case.
public void accept(ThreadContext context, SaveContextVisitor visitor) {
visitor.enter(node);
XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
if (xmlNodeSet.length() > 0) {
RubyArray array = (RubyArray) xmlNodeSet.to_a(context);
for (int i = 0; i < array.getLength(); i++) {
Object item = array.get(i);
if (item instanceof XmlNode) {
XmlNode cur = (XmlNode) item;
cur.accept(context, visitor);
} else if (item instanceof XmlNamespace) {
XmlNamespace cur = (XmlNamespace) item;
cur.accept(context, visitor);
}
}
}
visitor.leave(node);
}
Aggregations