Search in sources :

Example 91 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

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 92 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

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)

Example 93 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlNode method init.

/**
     * Initialize the object from Ruby arguments.  Should be
     * overridden by subclasses.  Should check for a minimum number of
     * args but not for an exact number.  Any extra args will then be
     * passed to 'initialize'.  The way 'new' and this 'init' function
     * interact means that subclasses cannot arbitrarily change the
     * require aruments by defining an 'initialize' method.  This is
     * how the C libxml wrapper works also.
     *
     * As written it performs initialization for a new Element with
     * the given <code>name</code> within the document
     * <code>doc</code>.  So XmlElement need not override this.  This
     * implementation cannot be moved to XmlElement however, because
     * subclassing XmlNode must result in something that behaves much
     * like XmlElement.
     */
protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2)
        throw context.getRuntime().newArgumentError(args.length, 2);
    IRubyObject name = args[0];
    IRubyObject doc = args[1];
    Document document = asXmlNode(context, doc).getOwnerDocument();
    if (document == null) {
        throw getRuntime().newArgumentError("node must have owner document");
    }
    Element element;
    String node_name = rubyStringToString(name);
    String prefix = NokogiriHelpers.getPrefix(node_name);
    String namespace_uri = null;
    if (document.getDocumentElement() != null) {
        namespace_uri = document.getDocumentElement().lookupNamespaceURI(prefix);
    }
    element = document.createElementNS(namespace_uri, node_name);
    setNode(context, element);
}
Also used : Element(org.w3c.dom.Element) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 94 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlNode method namespace.

@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    if (doc instanceof HtmlDocument)
        return runtime.getNil();
    NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
    String namespaceURI = node.getNamespaceURI();
    if (namespaceURI == null || namespaceURI == "") {
        return runtime.getNil();
    }
    String prefix = node.getPrefix();
    XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, namespaceURI);
    if (namespace == null || namespace.isEmpty()) {
        // if it's not in the cache, create an unowned, uncached namespace and
        // return that. XmlReader can't insert namespaces into the cache, so
        // this is necessary for XmlReader to work correctly.
        namespace = new XmlNamespace(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
        IRubyObject rubyPrefix = NokogiriHelpers.stringOrNil(runtime, prefix);
        IRubyObject rubyUri = NokogiriHelpers.stringOrNil(runtime, namespaceURI);
        namespace.init(null, rubyPrefix, rubyUri, doc);
    }
    return namespace;
}
Also used : NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 95 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlNode method native_write_to.

/**
     * @param args {IRubyObject io,
     *              IRubyObject encoding,
     *              IRubyObject indentString,
     *              IRubyObject options}
     */
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
    IRubyObject io = args[0];
    IRubyObject encoding = args[1];
    IRubyObject indentString = args[2];
    IRubyObject options = args[3];
    String encString = encoding.isNil() ? null : rubyStringToString(encoding);
    SaveContextVisitor visitor = new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
    accept(context, visitor);
    final IRubyObject rubyString;
    if (NokogiriHelpers.isUTF8(encString)) {
        rubyString = convertString(context.getRuntime(), visitor.getInternalBuffer());
    } else {
        ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
        ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
        rubyString = RubyString.newString(context.getRuntime(), str);
    }
    RuntimeHelpers.invoke(context, io, "write", rubyString);
    return io;
}
Also used : ByteList(org.jruby.util.ByteList) SaveContextVisitor(nokogiri.internals.SaveContextVisitor) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ByteBuffer(java.nio.ByteBuffer) JRubyMethod(org.jruby.anno.JRubyMethod)

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