Search in sources :

Example 61 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlText method init.

@Override
protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    content = args[0];
    IRubyObject xNode = args[1];
    XmlNode xmlNode = asXmlNode(context, xNode);
    XmlDocument xmlDoc = (XmlDocument) xmlNode.document(context);
    doc = xmlDoc;
    Document document = xmlDoc.getDocument();
    // text node content should not be encoded when it is created by Text node.
    // while content should be encoded when it is created by Element node.
    Node node = document.createTextNode(rubyStringToString(content));
    setNode(context, node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 62 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlDtd method newFromExternalSubset.

public static IRubyObject newFromExternalSubset(Ruby runtime, Document doc) {
    Object dtdTree_ = doc.getUserData(XmlDocument.DTD_RAW_DOCUMENT);
    if (dtdTree_ == null) {
        return runtime.getNil();
    }
    Node dtdTree = (Node) dtdTree_;
    Node dtd = getExternalSubset(dtdTree);
    if (dtd == null) {
        return runtime.getNil();
    } else if (!dtd.hasChildNodes()) {
        return runtime.getNil();
    } else {
        // Import the node into doc so it has the correct owner document.
        dtd = doc.importNode(dtd, true);
        XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
        xmlDtd.setNode(runtime, dtd);
        return xmlDtd;
    }
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 63 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlEntityReference method init.

protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    IRubyObject doc = args[0];
    IRubyObject name = args[1];
    Document document = ((XmlNode) doc).getOwnerDocument();
    // FIXME: disable error checking as a workaround for #719. this depends on the internals of Xerces.
    CoreDocumentImpl internalDocument = (CoreDocumentImpl) document;
    boolean oldErrorChecking = internalDocument.getErrorChecking();
    internalDocument.setErrorChecking(false);
    Node node = document.createEntityReference(rubyStringToString(name));
    internalDocument.setErrorChecking(oldErrorChecking);
    setNode(context, node);
}
Also used : CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 64 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlEntityReference method accept.

@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
    visitor.enter(node);
    Node child = node.getFirstChild();
    while (child != null) {
        IRubyObject nokoNode = getCachedNodeOrCreate(context.getRuntime(), child);
        if (nokoNode instanceof XmlNode) {
            XmlNode cur = (XmlNode) nokoNode;
            cur.accept(context, visitor);
        } else if (nokoNode instanceof XmlNamespace) {
            XmlNamespace cur = (XmlNamespace) nokoNode;
            cur.accept(context, visitor);
        }
        child = child.getNextSibling();
    }
    visitor.leave(node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 65 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlNamespace method createDefaultNamespace.

// owner should be an Attr node
public static XmlNamespace createDefaultNamespace(Ruby runtime, Node owner) {
    String prefixValue = owner.getPrefix();
    String hrefValue = owner.getNamespaceURI();
    Document document = owner.getOwnerDocument();
    // check namespace cache
    XmlDocument xmlDocument = (XmlDocument) getCachedNodeOrCreate(runtime, document);
    XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefixValue, hrefValue);
    if (xmlNamespace != null)
        return xmlNamespace;
    // creating XmlNamespace instance
    XmlNamespace namespace = (XmlNamespace) NokogiriService.XML_NAMESPACE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
    IRubyObject prefix = stringOrNil(runtime, prefixValue);
    IRubyObject href = stringOrNil(runtime, hrefValue);
    // initialize XmlNamespace object
    namespace.init((Attr) owner, prefix, href, prefixValue, hrefValue, xmlDocument);
    // updating namespace cache
    xmlDocument.getNamespaceCache().put(namespace, owner);
    return namespace;
}
Also used : RubyString(org.jruby.RubyString) Document(org.w3c.dom.Document) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

IRubyObject (org.jruby.runtime.builtin.IRubyObject)105 JRubyMethod (org.jruby.anno.JRubyMethod)32 Document (org.w3c.dom.Document)27 Ruby (org.jruby.Ruby)25 Node (org.w3c.dom.Node)24 RubyString (org.jruby.RubyString)16 RubyArray (org.jruby.RubyArray)13 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)11 RaiseException (org.jruby.exceptions.RaiseException)10 InputStream (java.io.InputStream)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)6 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)6 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)6 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)6 RubyProc (org.jruby.RubyProc)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)4 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4