use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNamespace method createFromAttr.
public static XmlNamespace createFromAttr(Ruby runtime, Attr attr) {
String prefixValue = getLocalNameForNamespace(attr.getName());
IRubyObject prefix_value;
if (prefixValue == null) {
prefix_value = runtime.getNil();
prefixValue = "";
} else {
prefix_value = RubyString.newString(runtime, prefixValue);
}
String hrefValue = attr.getValue();
IRubyObject href_value = RubyString.newString(runtime, hrefValue);
// check namespace cache
XmlDocument xmlDocument = (XmlDocument) getCachedNodeOrCreate(runtime, attr.getOwnerDocument());
xmlDocument.initializeNamespaceCacheIfNecessary();
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"));
namespace.init(attr, prefix_value, href_value, prefixValue, hrefValue, xmlDocument);
// updateing namespace cache
xmlDocument.getNamespaceCache().put(namespace, attr.getOwnerElement());
return namespace;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNode method adoptAs.
/**
* Adopt XmlNode <code>other</code> into the document of
* <code>this</code> using the specified scheme.
*/
protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme, IRubyObject other_) {
XmlNode other = asXmlNode(context, other_);
// this.doc might be null since this node can be empty node.
if (this.doc != null) {
other.setDocument(context, this.doc);
}
IRubyObject nodeOrTags = other;
Node thisNode = node;
Node otherNode = other.node;
try {
Document prev = otherNode.getOwnerDocument();
Document doc = thisNode.getOwnerDocument();
clearXpathContext(prev);
clearXpathContext(doc);
if (doc != null && doc != otherNode.getOwnerDocument()) {
Node ret = doc.adoptNode(otherNode);
// FIXME: this is really a hack, see documentation of fixUserData() for more details.
fixUserData(prev, ret);
if (ret == null) {
throw context.getRuntime().newRuntimeError("Failed to take ownership of node");
}
otherNode = ret;
}
Node parent = thisNode.getParentNode();
switch(scheme) {
case CHILD:
Node[] children = adoptAsChild(context, thisNode, otherNode);
if (children.length == 1 && otherNode == children[0]) {
break;
} else {
nodeOrTags = nodeArrayToRubyArray(context.getRuntime(), children);
}
break;
case PREV_SIBLING:
adoptAsPrevSibling(context, parent, thisNode, otherNode);
break;
case NEXT_SIBLING:
adoptAsNextSibling(context, parent, thisNode, otherNode);
break;
case REPLACEMENT:
adoptAsReplacement(context, parent, thisNode, otherNode);
break;
}
} catch (Exception e) {
throw context.getRuntime().newRuntimeError(e.toString());
}
if (otherNode.getNodeType() == Node.TEXT_NODE) {
coalesceTextNodes(context, other, scheme);
}
if (this instanceof XmlDocument) {
((XmlDocument) this).resetNamespaceCache(context);
}
other.relink_namespace(context);
return nodeOrTags;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlDocument method getInternalSubset.
public IRubyObject getInternalSubset(ThreadContext context) {
IRubyObject dtd = (IRubyObject) node.getUserData(DTD_INTERNAL_SUBSET);
if (dtd == null) {
Document document = getDocument();
if (document.getUserData(XmlDocument.DTD_RAW_DOCUMENT) != null) {
dtd = XmlDtd.newFromInternalSubset(context.getRuntime(), document);
} else if (document.getDoctype() != null) {
DocumentType docType = document.getDoctype();
IRubyObject name, publicId, systemId;
name = publicId = systemId = context.getRuntime().getNil();
if (docType.getName() != null) {
name = context.getRuntime().newString(docType.getName());
}
if (docType.getPublicId() != null) {
publicId = context.getRuntime().newString(docType.getPublicId());
}
if (docType.getSystemId() != null) {
systemId = context.getRuntime().newString(docType.getSystemId());
}
dtd = XmlDtd.newEmpty(context.getRuntime(), document, name, publicId, systemId);
} else {
dtd = context.getRuntime().getNil();
}
setInternalSubset(dtd);
}
return dtd;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlDtd method extractDecls.
/**
* Recursively extract various DTD declarations and store them in
* the various collections.
*/
protected void extractDecls(ThreadContext context) {
Ruby runtime = context.getRuntime();
// initialize data structures
allDecls = RubyArray.newArray(runtime);
attributes = RubyHash.newHash(runtime);
elements = RubyHash.newHash(runtime);
entities = RubyHash.newHash(runtime);
notations = RubyHash.newHash(runtime);
contentModels = RubyHash.newHash(runtime);
children = runtime.getNil();
// leave all the decl hash's empty
if (node == null)
return;
extractDecls(context, node.getFirstChild());
// convert allDecls to a NodeSet
children = XmlNodeSet.newXmlNodeSet(context, allDecls);
// add attribute decls as attributes to the matching element decl
RubyArray keys = attributes.keys();
for (int i = 0; i < keys.getLength(); ++i) {
IRubyObject akey = keys.entry(i);
IRubyObject val;
val = attributes.op_aref(context, akey);
if (val.isNil())
continue;
XmlAttributeDecl attrDecl = (XmlAttributeDecl) val;
IRubyObject ekey = attrDecl.element_name(context);
val = elements.op_aref(context, ekey);
if (val.isNil())
continue;
XmlElementDecl elemDecl = (XmlElementDecl) val;
elemDecl.appendAttrDecl(attrDecl);
}
// add content models to the matching element decl
keys = contentModels.keys();
for (int i = 0; i < keys.getLength(); ++i) {
IRubyObject key = keys.entry(i);
IRubyObject cm = contentModels.op_aref(context, key);
IRubyObject elem = elements.op_aref(context, key);
if (elem.isNil())
continue;
if (((XmlElementDecl) elem).isEmpty())
continue;
((XmlElementDecl) elem).setContentModel(cm);
}
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlDtd method extractDecls.
/**
* The <code>node</code> is either the first child of the root dtd
* node (as returned by getInternalSubset()) or the first child of
* the external subset node (as returned by getExternalSubset()).
*
* This recursive function will not descend into an
* 'externalSubset' node, thus for an internal subset it only
* extracts nodes in the internal subset, and for an external
* subset it extracts everything and assumess <code>node</code>
* and all children are part of the external subset.
*/
protected void extractDecls(ThreadContext context, Node node) {
while (node != null) {
if (isExternalSubset(node)) {
return;
} else if (isAttributeDecl(node)) {
XmlAttributeDecl decl = (XmlAttributeDecl) XmlAttributeDecl.create(context, node);
attributes.op_aset(context, decl.attribute_name(context), decl);
allDecls.append(decl);
} else if (isElementDecl(node)) {
XmlElementDecl decl = (XmlElementDecl) XmlElementDecl.create(context, node);
elements.op_aset(context, decl.element_name(context), decl);
allDecls.append(decl);
} else if (isEntityDecl(node)) {
XmlEntityDecl decl = (XmlEntityDecl) XmlEntityDecl.create(context, node);
entities.op_aset(context, decl.node_name(context), decl);
allDecls.append(decl);
} else if (isNotationDecl(node)) {
XmlNode tmp = (XmlNode) NokogiriHelpers.constructNode(context.getRuntime(), node);
IRubyObject decl = invoke(context, notationClass, "new", tmp.getAttribute(context, "name"), tmp.getAttribute(context, "pubid"), tmp.getAttribute(context, "sysid"));
notations.op_aset(context, tmp.getAttribute(context, "name"), decl);
allDecls.append(decl);
} else if (isContentModel(node)) {
XmlElementContent cm = new XmlElementContent(context.getRuntime(), (XmlDocument) document(context), node);
contentModels.op_aset(context, cm.element_name(context), cm);
} else {
// recurse
extractDecls(context, node.getFirstChild());
}
node = node.getNextSibling();
}
}
Aggregations