use of org.jruby.RubyModule in project gocd by gocd.
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 if (document instanceof XmlDocument) {
klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
ctx = new XmlDomParserContext(runtime, options);
String input = rubyStringToString(str);
istream = new ByteArrayInputStream(input.getBytes());
} else {
return runtime.getNil();
}
ctx.setInputSource(istream);
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;
}
use of org.jruby.RubyModule in project gocd by gocd.
the class NokogiriService method createXmlModule.
private RubyClass createXmlModule(Ruby ruby, RubyModule xmlModule) {
RubyClass node = xmlModule.defineClassUnder("Node", ruby.getObject(), XML_NODE_ALLOCATOR);
node.defineAnnotatedMethods(XmlNode.class);
RubyClass attr = xmlModule.defineClassUnder("Attr", node, XML_ATTR_ALLOCATOR);
attr.defineAnnotatedMethods(XmlAttr.class);
RubyClass attrDecl = xmlModule.defineClassUnder("AttributeDecl", node, XML_ATTRIBUTE_DECL_ALLOCATOR);
attrDecl.defineAnnotatedMethods(XmlAttributeDecl.class);
RubyClass characterData = xmlModule.defineClassUnder("CharacterData", node, null);
RubyClass comment = xmlModule.defineClassUnder("Comment", characterData, XML_COMMENT_ALLOCATOR);
comment.defineAnnotatedMethods(XmlComment.class);
RubyClass text = xmlModule.defineClassUnder("Text", characterData, XML_TEXT_ALLOCATOR);
text.defineAnnotatedMethods(XmlText.class);
RubyModule cdata = xmlModule.defineClassUnder("CDATA", text, XML_CDATA_ALLOCATOR);
cdata.defineAnnotatedMethods(XmlCdata.class);
RubyClass dtd = xmlModule.defineClassUnder("DTD", node, XML_DTD_ALLOCATOR);
dtd.defineAnnotatedMethods(XmlDtd.class);
RubyClass documentFragment = xmlModule.defineClassUnder("DocumentFragment", node, XML_DOCUMENT_FRAGMENT_ALLOCATOR);
documentFragment.defineAnnotatedMethods(XmlDocumentFragment.class);
RubyClass element = xmlModule.defineClassUnder("Element", node, XML_ELEMENT_ALLOCATOR);
element.defineAnnotatedMethods(XmlElement.class);
RubyClass elementContent = xmlModule.defineClassUnder("ElementContent", ruby.getObject(), XML_ELEMENT_CONTENT_ALLOCATOR);
elementContent.defineAnnotatedMethods(XmlElementContent.class);
RubyClass elementDecl = xmlModule.defineClassUnder("ElementDecl", node, XML_ELEMENT_DECL_ALLOCATOR);
elementDecl.defineAnnotatedMethods(XmlElementDecl.class);
RubyClass entityDecl = xmlModule.defineClassUnder("EntityDecl", node, XML_ENTITY_DECL_ALLOCATOR);
entityDecl.defineAnnotatedMethods(XmlEntityDecl.class);
entityDecl.defineConstant("INTERNAL_GENERAL", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_GENERAL));
entityDecl.defineConstant("EXTERNAL_GENERAL_PARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_PARSED));
entityDecl.defineConstant("EXTERNAL_GENERAL_UNPARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_UNPARSED));
entityDecl.defineConstant("INTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PARAMETER));
entityDecl.defineConstant("EXTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_PARAMETER));
entityDecl.defineConstant("INTERNAL_PREDEFINED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PREDEFINED));
RubyClass entref = xmlModule.defineClassUnder("EntityReference", node, XML_ENTITY_REFERENCE_ALLOCATOR);
entref.defineAnnotatedMethods(XmlEntityReference.class);
RubyClass namespace = xmlModule.defineClassUnder("Namespace", ruby.getObject(), XML_NAMESPACE_ALLOCATOR);
namespace.defineAnnotatedMethods(XmlNamespace.class);
RubyClass nodeSet = xmlModule.defineClassUnder("NodeSet", ruby.getObject(), XML_NODESET_ALLOCATOR);
nodeSet.defineAnnotatedMethods(XmlNodeSet.class);
RubyClass pi = xmlModule.defineClassUnder("ProcessingInstruction", node, XML_PROCESSING_INSTRUCTION_ALLOCATOR);
pi.defineAnnotatedMethods(XmlProcessingInstruction.class);
RubyClass reader = xmlModule.defineClassUnder("Reader", ruby.getObject(), XML_READER_ALLOCATOR);
reader.defineAnnotatedMethods(XmlReader.class);
RubyClass schema = xmlModule.defineClassUnder("Schema", ruby.getObject(), XML_SCHEMA_ALLOCATOR);
schema.defineAnnotatedMethods(XmlSchema.class);
RubyClass relaxng = xmlModule.defineClassUnder("RelaxNG", schema, XML_RELAXNG_ALLOCATOR);
relaxng.defineAnnotatedMethods(XmlRelaxng.class);
RubyClass xpathContext = xmlModule.defineClassUnder("XPathContext", ruby.getObject(), XML_XPATHCONTEXT_ALLOCATOR);
xpathContext.defineAnnotatedMethods(XmlXpathContext.class);
return node;
}
use of org.jruby.RubyModule in project jo by headius.
the class JoLibrary method load.
public void load(Ruby runtime, boolean wrap) throws IOException {
RubyModule jo = runtime.defineModule("Jo");
final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
jo.setInternalVariable("executor", executor);
RubyClass joFuture = jo.defineClassUnder("Future", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
RubyClass joChannel = jo.defineClassUnder("Channel", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
jo.defineAnnotatedMethods(JoMethods.class);
joFuture.defineAnnotatedMethods(JoFuture.class);
joChannel.defineAnnotatedMethods(JoChannel.class);
runtime.addFinalizer(new Finalizable() {
public void finalize() {
executor.shutdown();
}
});
}
use of org.jruby.RubyModule in project nokogiri by sparklemotion.
the class NokogiriService method createDocuments.
private void createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node) {
RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
xmlDocument.defineAnnotatedMethods(XmlDocument.class);
//RubyModule htmlDoc = html.defineOrGetClassUnder("Document", document);
RubyModule htmlDocument = htmlModule.defineClassUnder("Document", xmlDocument, HTML_DOCUMENT_ALLOCATOR);
htmlDocument.defineAnnotatedMethods(HtmlDocument.class);
}
use of org.jruby.RubyModule in project nokogiri by sparklemotion.
the class NokogiriService method createXmlModule.
private RubyClass createXmlModule(Ruby ruby, RubyModule xmlModule) {
RubyClass node = xmlModule.defineClassUnder("Node", ruby.getObject(), XML_NODE_ALLOCATOR);
node.defineAnnotatedMethods(XmlNode.class);
RubyClass attr = xmlModule.defineClassUnder("Attr", node, XML_ATTR_ALLOCATOR);
attr.defineAnnotatedMethods(XmlAttr.class);
RubyClass attrDecl = xmlModule.defineClassUnder("AttributeDecl", node, XML_ATTRIBUTE_DECL_ALLOCATOR);
attrDecl.defineAnnotatedMethods(XmlAttributeDecl.class);
RubyClass characterData = xmlModule.defineClassUnder("CharacterData", node, null);
RubyClass comment = xmlModule.defineClassUnder("Comment", characterData, XML_COMMENT_ALLOCATOR);
comment.defineAnnotatedMethods(XmlComment.class);
RubyClass text = xmlModule.defineClassUnder("Text", characterData, XML_TEXT_ALLOCATOR);
text.defineAnnotatedMethods(XmlText.class);
RubyModule cdata = xmlModule.defineClassUnder("CDATA", text, XML_CDATA_ALLOCATOR);
cdata.defineAnnotatedMethods(XmlCdata.class);
RubyClass dtd = xmlModule.defineClassUnder("DTD", node, XML_DTD_ALLOCATOR);
dtd.defineAnnotatedMethods(XmlDtd.class);
RubyClass documentFragment = xmlModule.defineClassUnder("DocumentFragment", node, XML_DOCUMENT_FRAGMENT_ALLOCATOR);
documentFragment.defineAnnotatedMethods(XmlDocumentFragment.class);
RubyClass element = xmlModule.defineClassUnder("Element", node, XML_ELEMENT_ALLOCATOR);
element.defineAnnotatedMethods(XmlElement.class);
RubyClass elementContent = xmlModule.defineClassUnder("ElementContent", ruby.getObject(), XML_ELEMENT_CONTENT_ALLOCATOR);
elementContent.defineAnnotatedMethods(XmlElementContent.class);
RubyClass elementDecl = xmlModule.defineClassUnder("ElementDecl", node, XML_ELEMENT_DECL_ALLOCATOR);
elementDecl.defineAnnotatedMethods(XmlElementDecl.class);
RubyClass entityDecl = xmlModule.defineClassUnder("EntityDecl", node, XML_ENTITY_DECL_ALLOCATOR);
entityDecl.defineAnnotatedMethods(XmlEntityDecl.class);
entityDecl.defineConstant("INTERNAL_GENERAL", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_GENERAL));
entityDecl.defineConstant("EXTERNAL_GENERAL_PARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_PARSED));
entityDecl.defineConstant("EXTERNAL_GENERAL_UNPARSED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_GENERAL_UNPARSED));
entityDecl.defineConstant("INTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PARAMETER));
entityDecl.defineConstant("EXTERNAL_PARAMETER", RubyFixnum.newFixnum(ruby, XmlEntityDecl.EXTERNAL_PARAMETER));
entityDecl.defineConstant("INTERNAL_PREDEFINED", RubyFixnum.newFixnum(ruby, XmlEntityDecl.INTERNAL_PREDEFINED));
RubyClass entref = xmlModule.defineClassUnder("EntityReference", node, XML_ENTITY_REFERENCE_ALLOCATOR);
entref.defineAnnotatedMethods(XmlEntityReference.class);
RubyClass namespace = xmlModule.defineClassUnder("Namespace", ruby.getObject(), XML_NAMESPACE_ALLOCATOR);
namespace.defineAnnotatedMethods(XmlNamespace.class);
RubyClass nodeSet = xmlModule.defineClassUnder("NodeSet", ruby.getObject(), XML_NODESET_ALLOCATOR);
nodeSet.defineAnnotatedMethods(XmlNodeSet.class);
RubyClass pi = xmlModule.defineClassUnder("ProcessingInstruction", node, XML_PROCESSING_INSTRUCTION_ALLOCATOR);
pi.defineAnnotatedMethods(XmlProcessingInstruction.class);
RubyClass reader = xmlModule.defineClassUnder("Reader", ruby.getObject(), XML_READER_ALLOCATOR);
reader.defineAnnotatedMethods(XmlReader.class);
RubyClass schema = xmlModule.defineClassUnder("Schema", ruby.getObject(), XML_SCHEMA_ALLOCATOR);
schema.defineAnnotatedMethods(XmlSchema.class);
RubyClass relaxng = xmlModule.defineClassUnder("RelaxNG", schema, XML_RELAXNG_ALLOCATOR);
relaxng.defineAnnotatedMethods(XmlRelaxng.class);
RubyClass xpathContext = xmlModule.defineClassUnder("XPathContext", ruby.getObject(), XML_XPATHCONTEXT_ALLOCATOR);
xpathContext.defineAnnotatedMethods(XmlXpathContext.class);
return node;
}
Aggregations