Class: XML::Util::XmlCanonicalizer
- Inherits:
-
Object
- Object
- XML::Util::XmlCanonicalizer
- Defined in:
- lib/xml/util/xmlcanonicalizer.rb
Constant Summary collapse
- BEFORE_DOC_ELEMENT =
0
- INSIDE_DOC_ELEMENT =
1
- AFTER_DOC_ELEMENT =
2
- NODE_TYPE_ATTRIBUTE =
3
- NODE_TYPE_WHITESPACE =
4
- NODE_TYPE_COMMENT =
5
- NODE_TYPE_PI =
6
- NODE_TYPE_TEXT =
7
Instance Attribute Summary collapse
-
#inclusive_namespaces ⇒ Object
Returns the value of attribute inclusive_namespaces.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#prefix_list ⇒ Object
Returns the value of attribute prefix_list.
Instance Method Summary collapse
- #add_inclusive_namespaces(prefix_list, element, visible_namespaces) ⇒ Object
- #canonicalize(document) ⇒ Object
- #canonicalize_element(element, logging = true) ⇒ Object
-
#initialize(with_comments, excl_c14n) ⇒ XmlCanonicalizer
constructor
A new instance of XmlCanonicalizer.
- #is_namespace_decl(attribute) ⇒ Object
- #is_namespace_node(namespace_uri) ⇒ Object
- #is_namespace_rendered(prefix, uri) ⇒ Object
- #is_node_visible(node) ⇒ Object
- #is_text_node(type) ⇒ Object
- #node_namespaces(node) ⇒ Object
- #normalize_string(input, type) ⇒ Object
- #remove_whitespace(string) ⇒ Object
- #white_text?(text) ⇒ Boolean
- #write_attribute_axis(node) ⇒ Object
- #write_document_node(document) ⇒ Object
- #write_element_node(node, visible) ⇒ Object
- #write_namespace_axis(node, visible) ⇒ Object
- #write_node(node) ⇒ Object
- #write_text_node(node, visible) ⇒ Object
Constructor Details
#initialize(with_comments, excl_c14n) ⇒ XmlCanonicalizer
Returns a new instance of XmlCanonicalizer.
36 37 38 39 40 41 42 43 44 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 36 def initialize(with_comments, excl_c14n) @with_comments = with_comments @exclusive = excl_c14n @state = BEFORE_DOC_ELEMENT @xnl = Array.new() @prevVisibleNamespacesStart = 0 @prevVisibleNamespacesEnd = 0 @visibleNamespaces = Array.new() end |
Instance Attribute Details
#inclusive_namespaces ⇒ Object
Returns the value of attribute inclusive_namespaces.
24 25 26 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 24 def inclusive_namespaces @inclusive_namespaces end |
#logger ⇒ Object
Returns the value of attribute logger.
24 25 26 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 24 def logger @logger end |
#prefix_list ⇒ Object
Returns the value of attribute prefix_list.
24 25 26 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 24 def prefix_list @prefix_list end |
Instance Method Details
#add_inclusive_namespaces(prefix_list, element, visible_namespaces) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 46 def add_inclusive_namespaces(prefix_list, element, visible_namespaces) namespaces = element.attributes() namespaces.each_attribute{|ns| if (ns.prefix=="xmlns") if (prefix_list.include?(ns.local_name())) visible_namespaces.push(NamespaceNode.new("xmlns:"+ns.local_name(), ns.value())) end end } parent = element.parent() add_inclusive_namespaces(prefix_list, parent, visible_namespaces) if (parent) visible_namespaces end |
#canonicalize(document) ⇒ Object
60 61 62 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 60 def canonicalize(document) write_document_node(document) end |
#canonicalize_element(element, logging = true) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 64 def canonicalize_element(element, logging = true) @preserve_document = element.document() tmp_parent = element.parent() body_string = remove_whitespace(element.to_s().gsub("\n","").gsub("\t","").gsub("\r","")) document = Document.new(body_string) tmp_parent.delete_element(element) element = tmp_parent.add_element(document.root()) @preserve_element = element document = Document.new(element.to_s()) ns = element.namespace(element.prefix()) document.root().add_namespace(element.prefix(), ns) write_document_node(document) end |
#is_namespace_decl(attribute) ⇒ Object
307 308 309 310 311 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 307 def is_namespace_decl(attribute) # return true if (attribute.name() == "xmlns") return true if (attribute.prefix().index("xmlns") == 0) return false end |
#is_namespace_node(namespace_uri) ⇒ Object
235 236 237 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 235 def is_namespace_node(namespace_uri) return (namespace_uri == "http://www.w3.org/2000/xmlns/") end |
#is_namespace_rendered(prefix, uri) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 239 def is_namespace_rendered(prefix, uri) is_empty_ns = prefix == nil && uri == nil if (is_empty_ns) start = 0 else start = @prevVisibleNamespacesStart end @visibleNamespaces.each{|ns| if (ns.prefix() == "xmlns:"+prefix.to_s() && ns.uri() == uri) return true end } return is_empty_ns # (@visibleNamespaces.size()-1).downto(start) {|i| # ns = @visibleNamespaces[i] # return true if (ns.prefix() == "xmlns:"+prefix.to_s() && ns.uri() == uri) # # p = ns.prefix() if (ns.prefix().index("xmlns") == 0) # # return ns.uri() == uri if (p == prefix) # } # return is_empty_ns end |
#is_node_visible(node) ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 261 def is_node_visible(node) return true if (@xnl.size() == 0) @xnl.each{|element| return true if (element == node) } return false end |
#is_text_node(type) ⇒ Object
313 314 315 316 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 313 def is_text_node(type) return true if (type == NODE_TYPE_TEXT || type == NODE_TYPE_CDATA || type == NODE_TYPE_WHITESPACE) return false end |
#node_namespaces(node) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 184 def node_namespaces(node) ns = Array.new() ns.push(node.prefix()) node.attributes().each_attribute{|a| if (a.prefix() != nil) ns.push(a.prefix()) end if (a.prefix() == "" && a.local_name() == "xmlns") ns.push("xmlns") end } ns end |
#normalize_string(input, type) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 269 def normalize_string(input, type) sb = "" return input # input.each_byte{|b| # if (b == 60 && (type == NODE_TYPE_ATTRIBUTE || is_text_node(type))) # sb = sb + "<" # elsif (b == 62 && is_text_node(type)) # sb = sb + ">" # elsif (b == 38 && (is_text_node(type) || is_text_node(type))) #Ampersand # sb = sb + "&" # elsif (b == 34 && is_text_node(type)) #Quote # sb = sb + """ # elsif (b == 9 && is_text_node(type)) #Tabulator # sb = sb + "	" # elsif (b == 11 && is_text_node(type)) #CR # sb = sb + "
" # elsif (b == 13 && (type == NODE_TYPE_ATTRIBUTE || (is_text_node(type) && type != NODE_TYPE_WHITESPACE) || type == NODE_TYPE_COMMENT || type == NODE_TYPE_PI)) # sb = sb + "
" # elsif (b == 13) # next # else # sb = sb.concat(b) # end # } # sb end |
#remove_whitespace(string) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 318 def remove_whitespace(string) new_string = "" in_white = false string.each_byte{|b| # if (in_white && b == 32) # else if !(in_white && b == 32) new_string = new_string + b.chr() end if (b == 62) #> in_white = true end if (b == 60) #< in_white = false end } new_string end |
#white_text?(text) ⇒ Boolean
302 303 304 305 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 302 def white_text?(text) return true if ((text.strip() == "") || (text.strip() == nil)) return false end |
#write_attribute_axis(node) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 198 def write_attribute_axis(node) list = Array.new() node.attributes.keys.sort.each{|key| attr = node.attributes.get_attribute(key) list.push(attr) if (!is_namespace_node(attr.value()) && !is_namespace_decl(attr)) } if (!@exclusive && node.parent() != nil && node.parent().parent() != nil) cur = node.parent() while (cur != nil) # next if (cur.attributes() == nil) cur.each_attribute{|attribute| next if (attribute.prefix() != "xml") next if (attribute.prefix().index("xmlns") == 0) next if (node.namespace(attribute.prefix()) == attribute.value()) found = true list.each{|n| if (n.prefix() == "xml" && n.value() == attritbute.value()) found = true break end } next if (found) list.push(attribute) } end end list.each{|attribute| if (attribute != nil) if (attribute.name() != "xmlns") @res = @res + " " + normalize_string(attribute.to_string(), NODE_TYPE_ATTRIBUTE).gsub("'",'"') # else # @res = @res + " " + normalize_string(attribute.name()+'="'+attribute.to_s()+'"', NODE_TYPE_ATTRIBUTE).gsub("'",'"') end end } end |
#write_document_node(document) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 78 def write_document_node(document) @res = "" @state = BEFORE_DOC_ELEMENT if (document.class().to_s() == "REXML::Element") write_node(document) else document.each_child{|child| write_node(child) } end @res end |
#write_element_node(node, visible) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 115 def write_element_node(node, visible) savedPrevVisibleNamespacesStart = @prevVisibleNamespacesStart savedPrevVisibleNamespacesEnd = @prevVisibleNamespacesEnd savedVisibleNamespacesSize = @visibleNamespaces.size() state = @state state = INSIDE_DOC_ELEMENT if (visible && state == BEFORE_DOC_ELEMENT) @res = @res + "<" + node.() if (visible) write_namespace_axis(node, visible) write_attribute_axis(node) @res = @res + ">" if (visible) node.each_child{|child| write_node(child) } @res = @res + "</" +node.() + ">" if (visible) @state = AFTER_DOC_ELEMENT if (visible && state == BEFORE_DOC_ELEMENT) @prevVisibleNamespacesStart = savedPrevVisibleNamespacesStart @prevVisibleNamespacesEnd = savedPrevVisibleNamespacesEnd @visibleNamespaces.slice!(savedVisibleNamespacesSize, @visibleNamespaces.size() - savedVisibleNamespacesSize) if (@visibleNamespaces.size() > savedVisibleNamespacesSize) end |
#write_namespace_axis(node, visible) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 135 def write_namespace_axis(node, visible) doc = node.document() has_empty_namespace = false list = Array.new() cur = node # while ((cur != nil) && (cur != doc) && (cur.node_type() != :document)) node_namespaces(cur).each{|prefix| next if ((prefix == "xmlns") && (node.namespace(prefix) == "")) namespace = cur.namespace(prefix) next if (is_namespace_node(namespace)) next if (node.namespace(prefix) != cur.namespace(prefix)) next if (prefix == "xml" && namespace == "http://www.w3.org/XML/1998/namespace") next if (!is_node_visible(cur)) rendered = is_namespace_rendered(prefix, namespace) @visibleNamespaces.push(NamespaceNode.new("xmlns:"+prefix,namespace)) if (visible) if ((!rendered) && !list.include?(prefix)) list.push(prefix) end has_empty_namespace = true if (prefix == nil) } if (visible && !has_empty_namespace && !is_namespace_rendered(nil, nil)) @res = @res + ' xmlns=""' end #: ns of inclusive_list if self.inclusive_namespaces && !self.inclusive_namespaces.empty? self.inclusive_namespaces.each{|prefix| list.push(prefix) if (!list.include?(prefix) && (node.attributes.prefixes.include?(prefix))) } end if list.delete('xmlns') list = ["xmlns"] + list.sort else list.sort! end list.each{|prefix| next if (prefix == "") ns = node.namespace(prefix) ns = @preserve_element.namespace(prefix) if (ns == nil) @res = @res + normalize_string(" " + prefix + '="' + ns + '"', NODE_TYPE_TEXT) if (prefix == "xmlns") @res = @res + normalize_string(" xmlns:" + prefix + '="' + ns + '"', NODE_TYPE_TEXT) if (prefix != nil && prefix != "xmlns") } if (visible) @prevVisibleNamespacesStart = @prevVisibleNamespacesEnd @prevVisibleNamespacesEnd = @visibleNamespaces.size() end end |
#write_node(node) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 91 def write_node(node) visible = is_node_visible(node) if ((node.node_type() == :text) && white_text?(node.value())) res = node.value() res.gsub("\r\n","\n") # res = res.delete(" ").delete("\t") res.delete("\r") @res = @res + res # write_text_node(node,visible) if (@state == INSIDE_DOC_ELEMENT) return end if (node.node_type() == :text) write_text_node(node, visible) return end if (node.node_type() == :element) write_element_node(node, visible) end if (node.node_type() == :processing_instruction) end if (node.node_type() == :comment) end end |
#write_text_node(node, visible) ⇒ Object
296 297 298 299 300 |
# File 'lib/xml/util/xmlcanonicalizer.rb', line 296 def write_text_node(node, visible) if (visible) @res = @res + normalize_string(node.to_s, node.node_type()) end end |