Search in sources :

Example 1 with HtmlDocument

use of nokogiri.HtmlDocument in project nokogiri by sparklemotion.

the class NokogiriHelpers method convertEncodingByNKFIfNecessary.

public static CharSequence convertEncodingByNKFIfNecessary(ThreadContext context, XmlDocument doc, CharSequence str) {
    if (!(doc instanceof HtmlDocument))
        return str;
    String parsed_encoding = ((HtmlDocument) doc).getPraedEncoding();
    if (parsed_encoding == null)
        return str;
    String ruby_encoding = rubyStringToString(doc.getEncoding());
    if (ruby_encoding == null)
        return str;
    Charset encoding = Charset.forName(ruby_encoding);
    if (Charset.forName(parsed_encoding).compareTo(encoding) == 0)
        return str;
    // no need to convert
    if (str.length() == 0)
        return str;
    return NokogiriHelpers.nkf(context, encoding, str);
}
Also used : HtmlDocument(nokogiri.HtmlDocument) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString)

Example 2 with HtmlDocument

use of nokogiri.HtmlDocument in project gocd by gocd.

the class HtmlDomParserContext method wrapDocument.

@Override
protected XmlDocument wrapDocument(ThreadContext context, RubyClass klazz, Document document) {
    HtmlDocument htmlDocument = (HtmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), klazz);
    htmlDocument.setDocumentNode(context, document);
    if (ruby_encoding.isNil()) {
        // ruby_encoding might have detected by HtmlDocument::EncodingReader
        if (detected_encoding != null && !detected_encoding.isNil()) {
            ruby_encoding = detected_encoding;
        } else {
            // no encoding given & no encoding detected, then try to get it
            String charset = tryGetCharsetFromHtml5MetaTag(document);
            ruby_encoding = stringOrNil(context.getRuntime(), charset);
        }
    }
    htmlDocument.setEncoding(ruby_encoding);
    htmlDocument.setParsedEncoding(java_encoding);
    return htmlDocument;
}
Also used : HtmlDocument(nokogiri.HtmlDocument)

Example 3 with HtmlDocument

use of nokogiri.HtmlDocument in project gocd by gocd.

the class NokogiriHelpers method getCachedNodeOrCreate.

/**
     * Get the XmlNode associated with the underlying
     * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
     * or XmlNamespace wrapping <code>node</code> if there is no cached
     * value.
     */
public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
    if (node == null)
        return ruby.getNil();
    if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
        XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
        if (!(xmlDocument instanceof HtmlDocument)) {
            String prefix = getLocalNameForNamespace(((Attr) node).getName());
            prefix = prefix != null ? prefix : "";
            String href = ((Attr) node).getValue();
            XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
            if (xmlNamespace != null)
                return xmlNamespace;
            else
                return XmlNamespace.createFromAttr(ruby, (Attr) node);
        }
    }
    XmlNode xmlNode = getCachedNode(node);
    if (xmlNode == null) {
        xmlNode = (XmlNode) constructNode(ruby, node);
        node.setUserData(CACHED_NODE, xmlNode, null);
    }
    return xmlNode;
}
Also used : XmlNode(nokogiri.XmlNode) HtmlDocument(nokogiri.HtmlDocument) XmlNamespace(nokogiri.XmlNamespace) XmlDocument(nokogiri.XmlDocument) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Example 4 with HtmlDocument

use of nokogiri.HtmlDocument in project gocd by gocd.

the class NokogiriHelpers method convertEncodingByNKFIfNecessary.

public static String convertEncodingByNKFIfNecessary(Ruby runtime, XmlDocument doc, String thing) {
    if (!(doc instanceof HtmlDocument))
        return thing;
    String parsed_encoding = ((HtmlDocument) doc).getPraedEncoding();
    if (parsed_encoding == null)
        return thing;
    String ruby_encoding = rubyStringToString(doc.getEncoding());
    if (ruby_encoding == null)
        return thing;
    if (Charset.forName(parsed_encoding).compareTo(Charset.forName(ruby_encoding)) == 0) {
        return thing;
    } else {
        return NokogiriHelpers.nkf(runtime, ruby_encoding, thing);
    }
}
Also used : HtmlDocument(nokogiri.HtmlDocument) RubyString(org.jruby.RubyString)

Example 5 with HtmlDocument

use of nokogiri.HtmlDocument in project nokogiri by sparklemotion.

the class NokogiriHelpers method getCachedNodeOrCreate.

/**
     * Get the XmlNode associated with the underlying
     * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
     * or XmlNamespace wrapping <code>node</code> if there is no cached
     * value.
     */
public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
    if (node == null)
        return ruby.getNil();
    if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
        XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
        if (!(xmlDocument instanceof HtmlDocument)) {
            String prefix = getLocalNameForNamespace(((Attr) node).getName());
            prefix = prefix != null ? prefix : "";
            String href = ((Attr) node).getValue();
            XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
            if (xmlNamespace != null)
                return xmlNamespace;
            else
                return XmlNamespace.createFromAttr(ruby, (Attr) node);
        }
    }
    XmlNode xmlNode = getCachedNode(node);
    if (xmlNode == null) {
        xmlNode = (XmlNode) constructNode(ruby, node);
        node.setUserData(CACHED_NODE, xmlNode, null);
    }
    return xmlNode;
}
Also used : XmlNode(nokogiri.XmlNode) HtmlDocument(nokogiri.HtmlDocument) XmlNamespace(nokogiri.XmlNamespace) XmlDocument(nokogiri.XmlDocument) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Aggregations

HtmlDocument (nokogiri.HtmlDocument)6 RubyString (org.jruby.RubyString)4 XmlAttr (nokogiri.XmlAttr)2 XmlDocument (nokogiri.XmlDocument)2 XmlNamespace (nokogiri.XmlNamespace)2 XmlNode (nokogiri.XmlNode)2 Attr (org.w3c.dom.Attr)2 Charset (java.nio.charset.Charset)1