Search in sources :

Example 31 with RubyArray

use of org.jruby.RubyArray in project nokogiri by sparklemotion.

the class XmlReader method setAndRaiseErrorsIfAny.

private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException {
    final ReaderNode currentNode = currentNode();
    if (currentNode == null) {
        return runtime.getNil();
    }
    if (currentNode.isError()) {
        RubyArray<?> errors = (RubyArray) getInstanceVariable("@errors");
        IRubyObject error = currentNode.toSyntaxError();
        errors.append(error);
        setInstanceVariable("@errors", errors);
        throw ex != null ? ex : ((XmlSyntaxError) error).toThrowable();
    }
    if (ex != null) {
        throw ex;
    }
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) ReaderNode(nokogiri.internals.ReaderNode) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 32 with RubyArray

use of org.jruby.RubyArray in project nokogiri by sparklemotion.

the class XmlSchema method from_document.

/*
   * call-seq:
   *  from_document(doc)
   *
   * Create a new Schema from the Nokogiri::XML::Document +doc+
   */
@JRubyMethod(meta = true, required = 1, optional = 1)
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    IRubyObject document = args[0];
    IRubyObject parseOptions = null;
    if (args.length > 1) {
        parseOptions = args[1];
    }
    XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
    RubyArray<?> errors = (RubyArray) doc.getInstanceVariable("@errors");
    if (!errors.isEmpty()) {
        throw ((XmlSyntaxError) errors.first()).toThrowable();
    }
    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, parseOptions);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) RubyArray(org.jruby.RubyArray) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 33 with RubyArray

use of org.jruby.RubyArray in project nokogiri by sparklemotion.

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

Example 34 with RubyArray

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

the class XmlDomParserContext method addErrorsIfNecessary.

public void addErrorsIfNecessary(ThreadContext context, XmlDocument doc) {
    Ruby ruby = context.getRuntime();
    RubyArray errors = ruby.newArray(errorHandler.getErrorsReadyForRuby(context));
    doc.setInstanceVariable("@errors", errors);
}
Also used : RubyArray(org.jruby.RubyArray) Ruby(org.jruby.Ruby)

Example 35 with RubyArray

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

the class NokogiriHandler method startElement.

/*
     * This has to call either "start_element" or
     * "start_element_namespace" depending on whether there are any
     * namespace attributes.
     *
     * Attributes that define namespaces are passed in a separate
     * array of of <code>[:prefix, :uri]</code> arrays and are not
     * passed with the other attributes.
     */
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    // for attributes other than namespace attrs
    RubyArray rubyAttr = RubyArray.newArray(ruby);
    // for namespace defining attributes
    RubyArray rubyNSAttr = RubyArray.newArray(ruby);
    ThreadContext context = ruby.getCurrentContext();
    // isFromFragmentHandler();
    boolean fromFragmentHandler = false;
    for (int i = 0; i < attrs.getLength(); i++) {
        String u = attrs.getURI(i);
        String qn = attrs.getQName(i);
        String ln = attrs.getLocalName(i);
        String val = attrs.getValue(i);
        String pre;
        pre = getPrefix(qn);
        if (ln == null || ln.equals(""))
            ln = getLocalPart(qn);
        if (isNamespace(qn) && !fromFragmentHandler) {
            // I haven't figured the reason out yet, but, in somewhere,
            // namespace is converted to array in array in array and cause
            // TypeError at line 45 in fragment_handler.rb
            RubyArray ns = RubyArray.newArray(ruby, 2);
            if (ln.equals("xmlns"))
                ln = null;
            ns.add(stringOrNil(ruby, ln));
            ns.add(ruby.newString(val));
            rubyNSAttr.add(ns);
        } else {
            IRubyObject[] args = null;
            if (needEmptyAttrCheck) {
                if (isEmptyAttr(ln)) {
                    args = new IRubyObject[3];
                    args[0] = stringOrNil(ruby, ln);
                    args[1] = stringOrNil(ruby, pre);
                    args[2] = stringOrNil(ruby, u);
                }
            }
            if (args == null) {
                args = new IRubyObject[4];
                args[0] = stringOrNil(ruby, ln);
                args[1] = stringOrNil(ruby, pre);
                args[2] = stringOrNil(ruby, u);
                args[3] = stringOrNil(ruby, val);
            }
            IRubyObject attr = RuntimeHelpers.invoke(context, attrClass, "new", args);
            rubyAttr.add(attr);
        }
    }
    if (localName == null || localName.equals(""))
        localName = getLocalPart(qName);
    call("start_element_namespace", stringOrNil(ruby, localName), rubyAttr, stringOrNil(ruby, getPrefix(qName)), stringOrNil(ruby, uri), rubyNSAttr);
}
Also used : RubyArray(org.jruby.RubyArray) ThreadContext(org.jruby.runtime.ThreadContext) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

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