Module: XMLUtil
- Defined in:
- lib/xml_util.rb
Overview
This module provides utility methods for working with XML.
Copyright 2008 R. Mark Volkmann
This file is part of WAX.
WAX is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with WAX. If not, see www.gnu.org/licenses.
-
Mark Volkmann, Object Computing, Inc.
Constant Summary collapse
- DEFAULT_ENCODING =
The default encoding used in XML declarations.
"UTF-8"
- NMTOKEN_PATTERN =
/^[A-Za-z][A-Za-z0-9\-_\.]*$/
- XMLSCHEMA_INSTANCE_NS =
"http://www.w3.org/1999/XMLSchema-instance"
Class Method Summary collapse
-
.escape(text) ⇒ Object
Escapes special characters in XML text.
-
.is_comment(text) ⇒ Object
Determines whether given text is a valid comment.
-
.is_nmtoken(text) ⇒ Object
Determines whether given text is a name token.
-
.is_uri(text) ⇒ Object
Determines whether given text is a URI.
-
.is_version(text) ⇒ Object
Determines whether given text is a valid XML version.
-
.verify_comment(text) ⇒ Object
Verifies that the given text is a valid comment and raises an ArgumentError if it isn’t.
-
.verify_nmtoken(text) ⇒ Object
Verifies that the given text is a valid name token and raises an ArgumentError if it isn’t.
-
.verify_uri(text) ⇒ Object
Verifies that the given text is a valid URI and raises an ArgumentError if it isn’t.
-
.verify_version(text) ⇒ Object
Verifies that the given text is a valid XML version and raises an ArgumentError if it isn’t.
Class Method Details
.escape(text) ⇒ Object
Escapes special characters in XML text.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/xml_util.rb', line 30 def self.escape(text) return text unless text.kind_of?(String) result = "" text.each_byte do |c| # TODO: Is using [0] the best way to do these comparisons? if c == '<'[0] result << "<" elsif c == '>'[0] result << ">" elsif c == "'"[0] result << "'" elsif c == '"'[0] result << """ elsif c == '&'[0] result << "&" else result << c end end result end |
.is_comment(text) ⇒ Object
Determines whether given text is a valid comment.
55 56 57 |
# File 'lib/xml_util.rb', line 55 def self.is_comment(text) /--/ !~ text end |
.is_nmtoken(text) ⇒ Object
Determines whether given text is a name token.
60 61 62 63 |
# File 'lib/xml_util.rb', line 60 def self.is_nmtoken(text) return false if text == nil (NMTOKEN_PATTERN =~ text) != nil end |
.is_uri(text) ⇒ Object
Determines whether given text is a URI.
66 67 68 69 |
# File 'lib/xml_util.rb', line 66 def self.is_uri(text) # TODO: Finish this true end |
.is_version(text) ⇒ Object
Determines whether given text is a valid XML version.
72 73 74 |
# File 'lib/xml_util.rb', line 72 def self.is_version(text) ["1.0", "1.1", "1.2"].include?(text) end |
.verify_comment(text) ⇒ Object
Verifies that the given text is a valid comment and raises an ArgumentError if it isn’t.
78 79 80 81 82 |
# File 'lib/xml_util.rb', line 78 def self.verify_comment(text) unless is_comment(text) raise ArgumentError, "\"#{text}\" is an invalid comment" end end |
.verify_nmtoken(text) ⇒ Object
Verifies that the given text is a valid name token and raises an ArgumentError if it isn’t.
86 87 88 89 90 |
# File 'lib/xml_util.rb', line 86 def self.verify_nmtoken(text) unless is_nmtoken(text) raise ArgumentError, "\"#{text}\" is an invalid NMTOKEN" end end |
.verify_uri(text) ⇒ Object
Verifies that the given text is a valid URI and raises an ArgumentError if it isn’t.
94 95 96 97 98 |
# File 'lib/xml_util.rb', line 94 def self.verify_uri(text) unless is_uri(text) raise ArgumentError, "\"#{text}\" is an invalid URI" end end |
.verify_version(text) ⇒ Object
Verifies that the given text is a valid XML version and raises an ArgumentError if it isn’t.
102 103 104 105 106 |
# File 'lib/xml_util.rb', line 102 def self.verify_version(text) unless is_version(text) raise ArgumentError, "\"#{text}\" is an invalid XML version" end end |