Search in sources :

Example 46 with RubyArray

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);
    }
}
Also used : RubyArray(org.jruby.RubyArray) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby)

Example 47 with RubyArray

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;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RaiseException(org.jruby.exceptions.RaiseException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 48 with RubyArray

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;
}
Also used : RubyModule(org.jruby.RubyModule) HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XmlDomParserContext(nokogiri.internals.XmlDomParserContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 49 with RubyArray

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;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) ArrayList(java.util.ArrayList) NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 50 with RubyArray

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;
}
Also used : RubyModule(org.jruby.RubyModule) RubyArray(org.jruby.RubyArray) IOException(java.io.IOException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyArray (org.jruby.RubyArray)65 JRubyMethod (org.jruby.anno.JRubyMethod)34 Ruby (org.jruby.Ruby)26 IRubyObject (org.jruby.runtime.builtin.IRubyObject)26 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)13 RubyString (org.jruby.RubyString)13 IOException (java.io.IOException)11 RaiseException (org.jruby.exceptions.RaiseException)10 ArrayList (java.util.ArrayList)8 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)8 RubyClass (org.jruby.RubyClass)6 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)5 RubyFixnum (org.jruby.RubyFixnum)5 ThreadContext (org.jruby.runtime.ThreadContext)5 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ASN1String (org.bouncycastle.asn1.ASN1String)4 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)4 RubyModule (org.jruby.RubyModule)4