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);
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations