use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlEntityReference method init.
protected void init(ThreadContext context, IRubyObject[] args) {
if (args.length < 2) {
throw getRuntime().newArgumentError(args.length, 2);
}
IRubyObject doc = args[0];
IRubyObject name = args[1];
Document document = ((XmlNode) doc).getOwnerDocument();
// FIXME: disable error checking as a workaround for #719. this depends on the internals of Xerces.
CoreDocumentImpl internalDocument = (CoreDocumentImpl) document;
boolean oldErrorChecking = internalDocument.getErrorChecking();
internalDocument.setErrorChecking(false);
Node node = document.createEntityReference(rubyStringToString(name));
internalDocument.setErrorChecking(oldErrorChecking);
setNode(context, node);
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNamespace method createDefaultNamespace.
// owner should be an Attr node
public static XmlNamespace createDefaultNamespace(Ruby runtime, Node owner) {
String prefixValue = owner.getPrefix();
String hrefValue = owner.getNamespaceURI();
Document document = owner.getOwnerDocument();
// check namespace cache
XmlDocument xmlDocument = (XmlDocument) getCachedNodeOrCreate(runtime, document);
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"));
IRubyObject prefix = stringOrNil(runtime, prefixValue);
IRubyObject href = stringOrNil(runtime, hrefValue);
// initialize XmlNamespace object
namespace.init((Attr) owner, prefix, href, prefixValue, hrefValue, xmlDocument);
// updating namespace cache
xmlDocument.getNamespaceCache().put(namespace, owner);
return namespace;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNode method init.
/**
* Initialize the object from Ruby arguments. Should be
* overridden by subclasses. Should check for a minimum number of
* args but not for an exact number. Any extra args will then be
* passed to 'initialize'. The way 'new' and this 'init' function
* interact means that subclasses cannot arbitrarily change the
* require aruments by defining an 'initialize' method. This is
* how the C libxml wrapper works also.
*
* As written it performs initialization for a new Element with
* the given <code>name</code> within the document
* <code>doc</code>. So XmlElement need not override this. This
* implementation cannot be moved to XmlElement however, because
* subclassing XmlNode must result in something that behaves much
* like XmlElement.
*/
protected void init(ThreadContext context, IRubyObject[] args) {
if (args.length < 2)
throw context.getRuntime().newArgumentError(args.length, 2);
IRubyObject name = args[0];
IRubyObject doc = args[1];
Document document = asXmlNode(context, doc).getOwnerDocument();
if (document == null) {
throw getRuntime().newArgumentError("node must have owner document");
}
Element element;
String node_name = rubyStringToString(name);
String prefix = NokogiriHelpers.getPrefix(node_name);
String namespace_uri = null;
if (document.getDocumentElement() != null) {
namespace_uri = document.getDocumentElement().lookupNamespaceURI(prefix);
}
element = document.createElementNS(namespace_uri, node_name);
setNode(context, element);
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNode method namespace.
@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
Ruby runtime = context.getRuntime();
if (doc instanceof HtmlDocument)
return runtime.getNil();
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
String namespaceURI = node.getNamespaceURI();
if (namespaceURI == null || namespaceURI == "") {
return runtime.getNil();
}
String prefix = node.getPrefix();
XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, namespaceURI);
if (namespace == null || namespace.isEmpty()) {
// if it's not in the cache, create an unowned, uncached namespace and
// return that. XmlReader can't insert namespaces into the cache, so
// this is necessary for XmlReader to work correctly.
namespace = new XmlNamespace(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
IRubyObject rubyPrefix = NokogiriHelpers.stringOrNil(runtime, prefix);
IRubyObject rubyUri = NokogiriHelpers.stringOrNil(runtime, namespaceURI);
namespace.init(null, rubyPrefix, rubyUri, doc);
}
return namespace;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlNode method native_write_to.
/**
* @param args {IRubyObject io,
* IRubyObject encoding,
* IRubyObject indentString,
* IRubyObject options}
*/
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
IRubyObject io = args[0];
IRubyObject encoding = args[1];
IRubyObject indentString = args[2];
IRubyObject options = args[3];
String encString = encoding.isNil() ? null : rubyStringToString(encoding);
SaveContextVisitor visitor = new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
accept(context, visitor);
final IRubyObject rubyString;
if (NokogiriHelpers.isUTF8(encString)) {
rubyString = convertString(context.getRuntime(), visitor.getInternalBuffer());
} else {
ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
rubyString = RubyString.newString(context.getRuntime(), str);
}
RuntimeHelpers.invoke(context, io, "write", rubyString);
return io;
}
Aggregations