use of nokogiri.XmlDocument in project nokogiri by sparklemotion.
the class XmlDomParserContext method parse.
/**
* Must call setInputSource() before this method.
*/
public XmlDocument parse(ThreadContext context, IRubyObject klazz, IRubyObject url) {
XmlDocument xmlDoc;
try {
Document doc = do_parse();
xmlDoc = wrapDocument(context, (RubyClass) klazz, doc);
xmlDoc.setUrl(url);
addErrorsIfNecessary(context, xmlDoc);
return xmlDoc;
} catch (SAXException e) {
return getDocumentWithErrorsOrRaiseException(context, (RubyClass) klazz, e);
} catch (IOException e) {
return getDocumentWithErrorsOrRaiseException(context, (RubyClass) klazz, e);
}
}
use of nokogiri.XmlDocument in project gocd by gocd.
the class XmlDomParserContext method getDocumentWithErrorsOrRaiseException.
public XmlDocument getDocumentWithErrorsOrRaiseException(ThreadContext context, RubyClass klazz, Exception ex) {
if (options.recover) {
XmlDocument xmlDocument = getInterruptedOrNewXmlDocument(context, klazz);
this.addErrorsIfNecessary(context, xmlDocument);
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
xmlSyntaxError.setException(ex);
((RubyArray) xmlDocument.getInstanceVariable("@errors")).append(xmlSyntaxError);
return xmlDocument;
} else {
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
xmlSyntaxError.setException(ex);
throw new RaiseException(xmlSyntaxError);
}
}
use of nokogiri.XmlDocument in project gocd by gocd.
the class ReaderNode method getAttributesNodes.
public IRubyObject getAttributesNodes() {
RubyArray array = RubyArray.newArray(ruby);
if (attributeList != null && attributeList.length > 0) {
if (document == null) {
XmlDocument doc = (XmlDocument) XmlDocument.rbNew(ruby.getCurrentContext(), getNokogiriClass(ruby, "Nokogiri::XML::Document"), new IRubyObject[0]);
document = doc.getDocument();
}
for (int i = 0; i < attributeList.length; i++) {
if (!isNamespace(attributeList.names.get(i))) {
Attr attr = document.createAttributeNS(attributeList.namespaces.get(i), attributeList.names.get(i));
attr.setValue(attributeList.values.get(i));
XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Attr"));
xmlAttr.setNode(ruby.getCurrentContext(), attr);
array.append(xmlAttr);
}
}
}
return array;
}
use of nokogiri.XmlDocument in project gocd by gocd.
the class NokogiriHelpers method getCachedNodeOrCreate.
/**
* Get the XmlNode associated with the underlying
* <code>node</code>. Creates a new XmlNode (or appropriate subclass)
* or XmlNamespace wrapping <code>node</code> if there is no cached
* value.
*/
public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
if (node == null)
return ruby.getNil();
if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
if (!(xmlDocument instanceof HtmlDocument)) {
String prefix = getLocalNameForNamespace(((Attr) node).getName());
prefix = prefix != null ? prefix : "";
String href = ((Attr) node).getValue();
XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
if (xmlNamespace != null)
return xmlNamespace;
else
return XmlNamespace.createFromAttr(ruby, (Attr) node);
}
}
XmlNode xmlNode = getCachedNode(node);
if (xmlNode == null) {
xmlNode = (XmlNode) constructNode(ruby, node);
node.setUserData(CACHED_NODE, xmlNode, null);
}
return xmlNode;
}
use of nokogiri.XmlDocument in project gocd by gocd.
the class XmlDomParserContext method wrapDocument.
/**
* This method is broken out so that HtmlDomParserContext can
* override it.
*/
protected XmlDocument wrapDocument(ThreadContext context, RubyClass klazz, Document doc) {
XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), klazz);
xmlDocument.setDocumentNode(context, doc);
xmlDocument.setEncoding(ruby_encoding);
if (options.dtdLoad) {
IRubyObject xmlDtdOrNil = XmlDtd.newFromExternalSubset(context.getRuntime(), doc);
if (!xmlDtdOrNil.isNil()) {
XmlDtd xmlDtd = (XmlDtd) xmlDtdOrNil;
doc.setUserData(XmlDocument.DTD_EXTERNAL_SUBSET, xmlDtd, null);
}
}
return xmlDocument;
}
Aggregations