Class: JEXML::Document
Overview
Wraps an underlying org.w3c.dom.Document (java.sun.com/javase/6/docs/api/org/w3c/dom/Document.html).
Instance Attribute Summary
Attributes inherited from Node
Class Method Summary collapse
-
.new(source) ⇒ Object
Create a new Document from either a file or a String containing the XML content.
-
.new_from_string(str) ⇒ Object
Create a new Document from a String containing the XML content.
-
.validate(str) ⇒ Object
Validate the XML in the provided string.
Instance Method Summary collapse
-
#create_cdata_node(contents) ⇒ Object
Create a CDATA node with the specified contents.
-
#create_element(tag_name, attrs = {}, text = nil) ⇒ Object
Create an element with the specified tag name, attributes, and text content.
-
#format(omit_xml_decl = true) ⇒ Object
Return the document in nice format (as a String).
-
#import_node(src_node) ⇒ Object
Import the specified node into this document.
-
#initialize(java_node = nil) ⇒ Document
constructor
Initialize the Ruby Document object with the corresponding Java Document object.
-
#root ⇒ Object
Get the root element for this document.
-
#root=(element) ⇒ Object
Set the provided element as the root of the document.
Methods inherited from Node
#<<, #add_child_node, #child_elements, #child_nodes, create, #document, #element, #elements, #first_child_element, #first_child_node, #name, #node, #nodes, #normalize, #remove_child_nodes, #replace_child_node, #text, #text=, #type_code, #value
Constructor Details
#initialize(java_node = nil) ⇒ Document
Initialize the Ruby Document object with the corresponding Java Document object.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jexml/document.rb', line 42 def initialize(java_node = nil) # If a java_node is not specified, create a new one. if java_node == nil factory = javax.xml.parsers.DocumentBuilderFactory.newInstance builder = factory.newDocumentBuilder java_node = builder.newDocument end # Create the node as normal. super(java_node) end |
Class Method Details
.new(source) ⇒ Object
Create a new Document from either a file or a String containing the XML content.
5 6 7 8 9 10 11 12 13 |
# File 'lib/jexml/document.rb', line 5 def self.new(source) if source.kind_of?(File) new_from_string(source.read) elsif source.kind_of?(String) new_from_string(source) else super end end |
.new_from_string(str) ⇒ Object
Create a new Document from a String containing the XML content. This uses a non-validating parser.
17 18 19 20 21 22 23 24 25 |
# File 'lib/jexml/document.rb', line 17 def self.new_from_string(str) is = java.io.ByteArrayInputStream.new(java.lang.String.new(str).getBytes) factory = javax.xml.parsers.DocumentBuilderFactory.newInstance factory.setNamespaceAware(true) builder = factory.newDocumentBuilder doc = builder.parse(is) # Create a new object where the document is the node. Node.create(doc) end |
.validate(str) ⇒ Object
Validate the XML in the provided string. Will raise an exception with the validation error if not valid. This uses a validating parser.
30 31 32 33 34 35 36 37 38 |
# File 'lib/jexml/document.rb', line 30 def self.validate(str) is = java.io.ByteArrayInputStream.new(java.lang.String.new(str).getBytes) factory = javax.xml.parsers.DocumentBuilderFactory.newInstance factory.setValidating(true) factory.setNamespaceAware(true) builder = factory.newDocumentBuilder builder.setErrorHandler(MyErrorHandler.new) builder.parse(is) end |
Instance Method Details
#create_cdata_node(contents) ⇒ Object
Create a CDATA node with the specified contents.
90 91 92 |
# File 'lib/jexml/document.rb', line 90 def create_cdata_node(contents) Node.create(@java_node.createCDATASection(contents)) end |
#create_element(tag_name, attrs = {}, text = nil) ⇒ Object
Create an element with the specified tag name, attributes, and text content.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jexml/document.rb', line 78 def create_element(tag_name, attrs = {}, text = nil) element = Node.create(@java_node.createElement(tag_name)) if attrs != nil attrs.each do |name, value| element.set_attribute(name.to_s, value.to_s) end end element.text = text if text != nil element end |
#format(omit_xml_decl = true) ⇒ Object
Return the document in nice format (as a String). Because the Java JDK cannot properly format the XML, we will give it to JDOM and have it do it. Set the optional parameter omit_xml_decl to false to generate the XML declaration for the document.
57 58 59 60 61 62 63 64 65 |
# File 'lib/jexml/document.rb', line 57 def format(omit_xml_decl = true) builder = org.jdom.input.DOMBuilder.new doc = builder.build(@java_node) out = org.jdom.output.XMLOutputter.new format = org.jdom.output.Format.getPrettyFormat format.setOmitDeclaration(omit_xml_decl) out.setFormat(format) out.outputString(doc) end |
#import_node(src_node) ⇒ Object
Import the specified node into this document.
73 74 75 |
# File 'lib/jexml/document.rb', line 73 def import_node(src_node) Node.create(@java_node.importNode(src_node.java_node, true)) end |
#root ⇒ Object
Get the root element for this document.
68 69 70 |
# File 'lib/jexml/document.rb', line 68 def root Node.create(@java_node.getDocumentElement) end |
#root=(element) ⇒ Object
Set the provided element as the root of the document. This assumes that the element is an element, and that the document does not yet have any children.
97 98 99 |
# File 'lib/jexml/document.rb', line 97 def root=(element) @java_node.appendChild(element.java_node) end |