Search in sources :

Example 36 with RubyArray

use of org.jruby.RubyArray in project gocd by gocd.

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);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 37 with RubyArray

use of org.jruby.RubyArray in project gocd by gocd.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.getRuntime(), errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 38 with RubyArray

use of org.jruby.RubyArray in project gocd by gocd.

the class XsltStylesheet method createDocumentFromString.

private IRubyObject createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult) {
    IRubyObject[] args = new IRubyObject[4];
    args[0] = stringOrBlank(runtime, stringResult);
    // url
    args[1] = runtime.getNil();
    // encoding
    args[2] = runtime.getNil();
    RubyClass parse_options = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
    if (htmlish) {
        args[3] = parse_options.getConstant("DEFAULT_HTML");
        RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        return RuntimeHelpers.invoke(context, htmlDocumentClass, "parse", args);
    } else {
        args[3] = parse_options.getConstant("DEFAULT_XML");
        RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        XmlDocument xmlDocument = (XmlDocument) RuntimeHelpers.invoke(context, xmlDocumentClass, "parse", args);
        if (((Document) xmlDocument.getNode()).getDocumentElement() == null) {
            RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
            RuntimeHelpers.invoke(context, errors, "<<", args[0]);
        }
        return xmlDocument;
    }
}
Also used : RubyArray(org.jruby.RubyArray) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 39 with RubyArray

use of org.jruby.RubyArray in project gocd by gocd.

the class XmlNode method findNamespaceHref.

private String findNamespaceHref(ThreadContext context, String prefix) {
    XmlNode currentNode = this;
    while (currentNode != document(context)) {
        RubyArray namespaces = (RubyArray) currentNode.namespace_scopes(context);
        Iterator iterator = namespaces.iterator();
        while (iterator.hasNext()) {
            XmlNamespace namespace = (XmlNamespace) iterator.next();
            if (namespace.getPrefix().equals(prefix)) {
                return namespace.getHref();
            }
        }
        if (currentNode.parent(context).isNil()) {
            break;
        } else {
            currentNode = (XmlNode) currentNode.parent(context);
        }
    }
    return null;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Iterator(java.util.Iterator)

Example 40 with RubyArray

use of org.jruby.RubyArray in project gocd by gocd.

the class XmlNode method attribute_nodes.

@JRubyMethod
public IRubyObject attribute_nodes(ThreadContext context) {
    NamedNodeMap nodeMap = this.node.getAttributes();
    Ruby ruby = context.getRuntime();
    if (nodeMap == null) {
        return ruby.newEmptyArray();
    }
    RubyArray attr = ruby.newArray();
    for (int i = 0; i < nodeMap.getLength(); i++) {
        if ((doc instanceof HtmlDocument) || !NokogiriHelpers.isNamespace(nodeMap.item(i))) {
            attr.append(getCachedNodeOrCreate(context.getRuntime(), nodeMap.item(i)));
        }
    }
    return attr;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Ruby(org.jruby.Ruby) 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