use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlReader method empty_element_p.
@JRubyMethod(name = { "empty_element?", "self_closing?" })
public IRubyObject empty_element_p(ThreadContext context) {
ReaderNode readerNode = currentNode();
ensureNodeClosed(context);
if (readerNode == null)
return context.getRuntime().getNil();
if (!(readerNode instanceof ElementNode))
context.getRuntime().getFalse();
return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlReader method from_memory.
@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
// args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
Ruby runtime = context.getRuntime();
// Not nil allowed!
if (args[0].isNil())
throw runtime.newArgumentError("string cannot be nil");
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
reader.init(runtime);
reader.setInstanceVariable("@source", args[0]);
reader.setInstanceVariable("@errors", runtime.newArray());
IRubyObject url = context.nil;
if (args.length > 1)
url = args[1];
if (args.length > 2)
reader.setInstanceVariable("@encoding", args[2]);
Options options;
if (args.length > 3) {
options = new ParserContext.Options((Long) args[3].toJava(Long.class));
} else {
// use the default options RECOVER | NONET
options = new ParserContext.Options(2048 | 1);
}
IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
InputStream in = new UncloseableInputStream(new IOInputStream(stringIO));
reader.setInput(context, in, url, options);
return reader;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlAttributeDecl method enumeration.
/**
* FIXME: will enumerations all be of the simple (val1|val2|val3)
* type string?
*/
@JRubyMethod
public IRubyObject enumeration(ThreadContext context) {
RubyArray enumVals = RubyArray.newArray(context.getRuntime());
String atype = ((Element) node).getAttribute("atype");
if (atype != null && atype.length() != 0 && atype.charAt(0) == '(') {
// removed enclosing parens
String valueStr = atype.substring(1, atype.length() - 1);
String[] values = valueStr.split("\\|");
for (int i = 0; i < values.length; i++) {
enumVals.append(context.getRuntime().newString(values[i]));
}
}
return enumVals;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlDocument method rbNew.
/*
* call-seq:
* new(version = default)
*
* Create a new document with +version+ (defaults to "1.0")
*/
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
XmlDocument xmlDocument;
try {
Document docNode = createNewDocument();
if ("Nokogiri::HTML::Document".equals(((RubyClass) klazz).getName())) {
xmlDocument = (XmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
xmlDocument.setDocumentNode(context, docNode);
} else {
// XML::Document and sublass
xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
xmlDocument.setDocumentNode(context, docNode);
}
} catch (Exception ex) {
throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
}
RuntimeHelpers.invoke(context, xmlDocument, "initialize", args);
return xmlDocument;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method in_context.
/**
* TODO: this is a stub implementation. It's not clear what
* 'in_context' is supposed to do. Also should take
* <code>options</code> into account.
*/
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject in_context(ThreadContext context, IRubyObject str, IRubyObject options) {
RubyModule klass;
XmlDomParserContext ctx;
InputStream istream;
XmlDocument document;
IRubyObject d = document(context);
Ruby runtime = context.getRuntime();
if (d != null && d instanceof XmlDocument) {
document = (XmlDocument) d;
} else {
return runtime.getNil();
}
if (document instanceof HtmlDocument) {
klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
ctx = new HtmlDomParserContext(runtime, options);
((HtmlDomParserContext) ctx).enableDocumentFragment();
istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
} else {
klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
ctx = new XmlDomParserContext(runtime, options);
String input = rubyStringToString(str);
istream = new ByteArrayInputStream(input.getBytes());
}
ctx.setInputSource(istream);
// run `test_parse_with_unparented_html_text_context_node' few times to see this happen
if (document instanceof HtmlDocument && !(document.getEncoding() == null || document.getEncoding().isNil())) {
HtmlDomParserContext htmlCtx = (HtmlDomParserContext) ctx;
htmlCtx.setEncoding(document.getEncoding().asJavaString());
}
XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
RubyArray documentErrors = getErrorArray(document);
RubyArray docErrors = getErrorArray(doc);
if (isErrorIncreased(documentErrors, docErrors)) {
for (int i = 0; i < docErrors.getLength(); i++) {
documentErrors.add(docErrors.get(i));
}
document.setInstanceVariable("@errors", documentErrors);
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
return xmlNodeSet;
}
// The first child might be document type node (dtd declaration).
// XmlNodeSet to be return should not have dtd decl in its list.
Node first;
if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
first = doc.node.getFirstChild().getNextSibling();
} else {
first = doc.node.getFirstChild();
}
RubyArray nodeArray = RubyArray.newArray(runtime);
nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
return xmlNodeSet;
}
Aggregations