use of nokogiri.XmlNode 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.XmlNode in project gocd by gocd.
the class NokogiriHelpers method isWhitespaceText.
public static boolean isWhitespaceText(ThreadContext context, IRubyObject obj) {
if (obj == null || obj.isNil())
return false;
XmlNode node = (XmlNode) obj;
if (!(node instanceof XmlText))
return false;
String content = rubyStringToString(node.content(context));
return content.trim().length() == 0;
}
use of nokogiri.XmlNode in project gocd by gocd.
the class CanonicalFilter method includeNodes.
public boolean includeNodes(Node currentNode, Node parentNode) {
if (block == null || !block.isGiven())
return true;
IRubyObject current = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), currentNode);
IRubyObject parent = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), parentNode);
if (parent.isNil()) {
IRubyObject doc = ((XmlNode) current).document(context);
boolean returnValue = block.call(context, current, doc).isTrue();
block.call(context, doc, context.nil);
return returnValue;
}
return block.call(context, current, parent).isTrue();
}
use of nokogiri.XmlNode in project gocd by gocd.
the class NokogiriHelpers method constructNode.
/**
* Construct a new XmlNode wrapping <code>node</code>. The proper
* subclass of XmlNode is chosen based on the type of
* <code>node</code>.
*/
public static IRubyObject constructNode(Ruby runtime, Node node) {
if (node == null)
return runtime.getNil();
// this is slow; need a way to cache nokogiri classes/modules somewhere
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
XmlElement xmlElement = (XmlElement) NokogiriService.XML_ELEMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Element"));
xmlElement.setNode(runtime.getCurrentContext(), node);
return xmlElement;
case Node.ATTRIBUTE_NODE:
XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Attr"));
xmlAttr.setNode(runtime.getCurrentContext(), node);
return xmlAttr;
case Node.TEXT_NODE:
XmlText xmlText = (XmlText) NokogiriService.XML_TEXT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Text"));
xmlText.setNode(runtime.getCurrentContext(), node);
return xmlText;
case Node.COMMENT_NODE:
XmlComment xmlComment = (XmlComment) NokogiriService.XML_COMMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Comment"));
xmlComment.setNode(runtime.getCurrentContext(), node);
return xmlComment;
case Node.ENTITY_NODE:
return new XmlNode(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityDecl"), node);
case Node.ENTITY_REFERENCE_NODE:
XmlEntityReference xmlEntityRef = (XmlEntityReference) NokogiriService.XML_ENTITY_REFERENCE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityReference"));
xmlEntityRef.setNode(runtime.getCurrentContext(), node);
return xmlEntityRef;
case Node.PROCESSING_INSTRUCTION_NODE:
XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) NokogiriService.XML_PROCESSING_INSTRUCTION_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ProcessingInstruction"));
xmlProcessingInstruction.setNode(runtime.getCurrentContext(), node);
return xmlProcessingInstruction;
case Node.CDATA_SECTION_NODE:
XmlCdata xmlCdata = (XmlCdata) NokogiriService.XML_CDATA_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::CDATA"));
xmlCdata.setNode(runtime.getCurrentContext(), node);
return xmlCdata;
case Node.DOCUMENT_NODE:
XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
xmlDocument.setDocumentNode(runtime.getCurrentContext(), node);
return xmlDocument;
case Node.DOCUMENT_TYPE_NODE:
XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
xmlDtd.setNode(runtime, node);
return xmlDtd;
default:
XmlNode xmlNode = (XmlNode) NokogiriService.XML_NODE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
xmlNode.setNode(runtime.getCurrentContext(), node);
return xmlNode;
}
}
use of nokogiri.XmlNode in project nokogiri by sparklemotion.
the class CanonicalFilter method includeNodes.
public boolean includeNodes(Node currentNode, Node parentNode) {
if (block == null || !block.isGiven())
return true;
IRubyObject current = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), currentNode);
IRubyObject parent = NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), parentNode);
if (parent.isNil()) {
IRubyObject doc = ((XmlNode) current).document(context);
boolean returnValue = block.call(context, current, doc).isTrue();
block.call(context, doc, context.nil);
return returnValue;
}
return block.call(context, current, parent).isTrue();
}
Aggregations