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();
}
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class HtmlElementDescription method sub_elements.
@JRubyMethod()
public IRubyObject sub_elements(ThreadContext context) {
Ruby ruby = context.getRuntime();
List<String> subs = findSubElements(element);
IRubyObject[] ary = new IRubyObject[subs.size()];
for (int i = 0; i < subs.size(); ++i) {
ary[i] = ruby.newString(subs.get(i));
}
return ruby.newArray(ary);
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class HtmlSaxPushParser method native_write.
@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
try {
initialize_task(context);
} catch (IOException e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
if (data == null) {
terminateTask(context);
// Nokogiri::HTML::SyntaxError
throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
}
int errorCount0 = parserTask.getErrorCount();
if (isLast.isTrue()) {
IRubyObject document = invoke(context, this, "document");
invoke(context, document, "end_document");
terminateTask(context);
} else {
try {
Future<Void> task = stream.addChunk(data);
task.get();
} catch (ClosedStreamException ex) {
// this means the stream is closed, ignore this exception
} catch (Exception e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
}
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
terminateTask(context);
throw new RaiseException(parserTask.getLastError(), true);
}
return this;
}
Aggregations