use of org.jruby.RubyArray in project gocd by gocd.
the class XmlDtd method extractDecls.
/**
* Recursively extract various DTD declarations and store them in
* the various collections.
*/
protected void extractDecls(ThreadContext context) {
Ruby runtime = context.getRuntime();
// initialize data structures
allDecls = RubyArray.newArray(runtime);
attributes = RubyHash.newHash(runtime);
elements = RubyHash.newHash(runtime);
entities = RubyHash.newHash(runtime);
notations = RubyHash.newHash(runtime);
contentModels = RubyHash.newHash(runtime);
children = runtime.getNil();
// leave all the decl hash's empty
if (node == null)
return;
extractDecls(context, node.getFirstChild());
// convert allDecls to a NodeSet
children = XmlNodeSet.newXmlNodeSet(context, allDecls);
// add attribute decls as attributes to the matching element decl
RubyArray keys = attributes.keys();
for (int i = 0; i < keys.getLength(); ++i) {
IRubyObject akey = keys.entry(i);
IRubyObject val;
val = attributes.op_aref(context, akey);
if (val.isNil())
continue;
XmlAttributeDecl attrDecl = (XmlAttributeDecl) val;
IRubyObject ekey = attrDecl.element_name(context);
val = elements.op_aref(context, ekey);
if (val.isNil())
continue;
XmlElementDecl elemDecl = (XmlElementDecl) val;
elemDecl.appendAttrDecl(attrDecl);
}
// add content models to the matching element decl
keys = contentModels.keys();
for (int i = 0; i < keys.getLength(); ++i) {
IRubyObject key = keys.entry(i);
IRubyObject cm = contentModels.op_aref(context, key);
IRubyObject elem = elements.op_aref(context, key);
if (elem.isNil())
continue;
if (((XmlElementDecl) elem).isEmpty())
continue;
((XmlElementDecl) elem).setContentModel(cm);
}
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlNode method process_xincludes.
/**
* call-seq:
* process_xincludes(options)
*
* Loads and substitutes all xinclude elements below the node. The
* parser context will be initialized with +options+.
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject process_xincludes(ThreadContext context, IRubyObject options) {
XmlDocument xmlDocument = (XmlDocument) document(context);
RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
while (errors.getLength() > 0) {
XmlSyntaxError error = (XmlSyntaxError) errors.shift(context);
if (error.toString().contains("Include operation failed")) {
throw new RaiseException(error);
}
}
return this;
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlNode method in_context.
/**
* TODO: this is a stub implementation. It's not clear what
* 'in_context' is supposed to do. Also should take
* <code>options</code> into account.
*/
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject in_context(ThreadContext context, IRubyObject str, IRubyObject options) {
RubyModule klass;
XmlDomParserContext ctx;
InputStream istream;
XmlDocument document;
IRubyObject d = document(context);
Ruby runtime = context.getRuntime();
if (d != null && d instanceof XmlDocument) {
document = (XmlDocument) d;
} else {
return runtime.getNil();
}
if (document instanceof HtmlDocument) {
klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
ctx = new HtmlDomParserContext(runtime, options);
((HtmlDomParserContext) ctx).enableDocumentFragment();
istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
} else if (document instanceof XmlDocument) {
klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
ctx = new XmlDomParserContext(runtime, options);
String input = rubyStringToString(str);
istream = new ByteArrayInputStream(input.getBytes());
} else {
return runtime.getNil();
}
ctx.setInputSource(istream);
XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
RubyArray documentErrors = getErrorArray(document);
RubyArray docErrors = getErrorArray(doc);
if (isErrorIncreased(documentErrors, docErrors)) {
for (int i = 0; i < docErrors.getLength(); i++) {
documentErrors.add(docErrors.get(i));
}
document.setInstanceVariable("@errors", documentErrors);
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
return xmlNodeSet;
}
// The first child might be document type node (dtd declaration).
// XmlNodeSet to be return should not have dtd decl in its list.
Node first;
if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
first = doc.node.getFirstChild().getNextSibling();
} else {
first = doc.node.getFirstChild();
}
RubyArray nodeArray = RubyArray.newArray(runtime);
nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
return xmlNodeSet;
}
use of org.jruby.RubyArray in project gocd by gocd.
the class XmlNode method namespace_scopes.
/**
* Return an array of XmlNamespace nodes defined on this node and
* on any ancestor node.
*/
@JRubyMethod
public IRubyObject namespace_scopes(ThreadContext context) {
RubyArray scoped_namespaces = context.getRuntime().newArray();
if (doc == null)
return scoped_namespaces;
if (doc instanceof HtmlDocument)
return scoped_namespaces;
Node previousNode;
if (node.getNodeType() == Node.ELEMENT_NODE) {
previousNode = node;
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
previousNode = ((Attr) node).getOwnerElement();
} else {
previousNode = findPreviousElement(node);
}
if (previousNode == null)
return scoped_namespaces;
List<String> prefixes_in_scope = new ArrayList<String>();
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(previousNode);
for (Node previous = previousNode; previous != null; ) {
List<XmlNamespace> namespaces = nsCache.get(previous);
for (XmlNamespace namespace : namespaces) {
if (prefixes_in_scope.contains(namespace.getPrefix()))
continue;
scoped_namespaces.append(namespace);
prefixes_in_scope.add(namespace.getPrefix());
}
previous = findPreviousElement(previous);
}
return scoped_namespaces;
}
use of org.jruby.RubyArray in project jruby-openssl by jruby.
the class ASN1 method decode_all.
@JRubyMethod(meta = true, required = 1)
public static IRubyObject decode_all(final ThreadContext context, final IRubyObject self, IRubyObject obj) {
obj = to_der_if_possible(context, obj);
BytesInputStream in = new BytesInputStream(obj.asString().getByteList());
final RubyModule ASN1 = _ASN1(context.runtime);
final RubyArray arr = context.runtime.newArray();
while (in.available() > 0) {
try {
// set offset() before each object is read
in.mark(0);
arr.append(decodeImpl(context, ASN1, in));
} catch (IOException e) {
// throw context.runtime.newIOErrorFromException(e);
throw newASN1Error(context.runtime, e.getMessage());
} catch (IllegalArgumentException e) {
debugStackTrace(context.runtime, e);
throw context.runtime.newArgumentError(e.getMessage());
}
}
return arr;
}
Aggregations