Gathers utility methods.
Work-around a bug in NetBeans (netbeans.org/bugzilla/show_bug.cgi?id=188653) $:[0] = File.join(File.dirname(FILE),’../../lib’) if ENV[“NB_EXEC_EXTEXECUTION_PROCESS_UUID“]
Methods
public class
Classes and Modules
Class SDL4R::ParserClass SDL4R::ParserTest
Class SDL4R::SDL4RTest
Class SDL4R::SDLTest
Class SDL4R::SdlBinary
Class SDL4R::SdlParseError
Class SDL4R::SdlTimeSpan
Class SDL4R::Tag
Class SDL4R::TagTest
Constants
MAX_INTEGER_32 | = | 2**31 - 1 |
MIN_INTEGER_32 | = | -(2**31) |
MAX_INTEGER_64 | = | 2**63 - 1 |
MIN_INTEGER_64 | = | -(2**63) |
BASE64_WRAP_LINE_LENGTH | = | 72 |
ANONYMOUS_TAG_NAME | = | "content" |
ROOT_TAG_NAME | = | "root" |
ESCAPED_QUOTES | = | { "\"" => "\\\"", "'" => "\\'", "`" => "\\`", } |
ESCAPED_CHARS | = | { "\\" => "\\\\", "\t" => "\\t", "\r" => "\\r", "\n" => "\\n", } |
Public class methods
Try to coerce ‘o’ into a SdlBinary. Raise an ArgumentError if it fails.
# File lib/sdl4r/sdl_binary.rb, line 72 72: def self.SdlBinary(o) 73: if o.kind_of? SdlBinary 74: return o 75: elsif o.kind_of? String 76: return SdlBinary.new(o) 77: else 78: raise ArgumentError, "can't coerce argument" 79: end 80: end
Coerce the type to a standard SDL type or raises an ArgumentError.
Returns o if of the following classes: NilClass, String, Numeric, Float, TrueClass, FalseClass, Date, DateTime, Time, SdlTimeSpan, SdlBinary,
Rationals are turned into Floats using Rational#to_f.
# File lib/sdl4r/sdl4r.rb, line 156 156: def self.coerce_or_fail(o) 157: case o 158: 159: when Rational 160: return o.to_f 161: 162: when NilClass, 163: String, 164: Numeric, 165: Float, 166: TrueClass, 167: FalseClass, 168: Date, 169: DateTime, 170: Time, 171: SdlTimeSpan, 172: SdlBinary 173: return o 174: 175: end 176: 177: raise ArgumentError, "#{o.class.name} is not coercible to an SDL type" 178: end
Creates an SDL string representation for a given object and returns it.
o: | the object to format |
add_quotes: | indicates whether quotes will be added to Strings and characters (true by default) |
line_prefix: | the line prefix to use (“” by default) |
indent: | the indent string to use (“t“ by default) |
# File lib/sdl4r/sdl4r.rb, line 47 47: def self.format(o, add_quotes = true, line_prefix = "", indent = "\t") 48: if o.is_a?(String) 49: if add_quotes 50: o_length = 0 51: o.scan(/./m) { o_length += 1 } # counts the number of chars (as opposed of bytes) 52: if o_length == 1 53: return "'" + escape(o, "'") + "'" 54: else 55: return '"' + escape(o, '"') + '"' 56: end 57: else 58: return escape(o) 59: end 60: 61: elsif o.is_a?(Bignum) 62: return o.to_s + "BD" 63: 64: elsif o.is_a?(Integer) 65: if MIN_INTEGER_32 <= o and o <= MAX_INTEGER_32 66: return o.to_s 67: elsif MIN_INTEGER_64 <= o and o <= MAX_INTEGER_64 68: return o.to_s + "L" 69: else 70: return o.to_s + "BD" 71: end 72: 73: elsif o.is_a?(Float) 74: return (o.to_s + "F") 75: 76: elsif o.is_a?(Rational) 77: return o.to_f.to_s + "F" 78: 79: elsif o.is_a?(BigDecimal) 80: s = o.to_s('F') 81: s.sub!(/\.0$/, "") 82: return "#{s}BD" 83: 84: elsif o.nil? 85: return "null" 86: 87: elsif o.is_a?(SdlBinary) 88: encoded_o = Base64.encode64(o.bytes) 89: encoded_o.gsub!(/[\r\n]/m, "") # Remove the EOL inserted every 60 chars 90: 91: if add_quotes 92: if encoded_o.length > BASE64_WRAP_LINE_LENGTH 93: # FIXME: we should a constant or some parameter instead of hardcoded spaces 94: wrap_lines_in_ascii(encoded_o, BASE64_WRAP_LINE_LENGTH, "#{line_prefix}#{indent}") 95: encoded_o.insert(0, "[#{$/}") 96: encoded_o << "#{$/}#{line_prefix}]" 97: else 98: encoded_o.insert(0, "[") 99: encoded_o << "]" 100: end 101: end 102: 103: return encoded_o 104: 105: # Below, we use "#{o.year}" instead of "%Y" because "%Y" always emit 4 chars at least even if 106: # the date is before 1000. 107: elsif o.is_a?(DateTime) || o.is_a?(Time) 108: milliseconds = get_datetime_milliseconds(o) 109: 110: if milliseconds == 0 111: zone_part = o.strftime("%:z") 112: if zone_part and zone_part != "+00:00" 113: return o.strftime("#{o.year}/%m/%d %H:%M:%S#{zone_part}") 114: else 115: return o.strftime("#{o.year}/%m/%d %H:%M:%S") 116: end 117: else 118: ms_part = milliseconds.to_s.ljust(3, '0') 119: if zone_part and zone_part != "+00:00" 120: return o.strftime("#{o.year}/%m/%d %H:%M:%S." + ms_part + zone_part) 121: else 122: return o.strftime("#{o.year}/%m/%d %H:%M:%S." + ms_part) 123: end 124: end 125: 126: elsif o.is_a?(Date) 127: return o.strftime("#{o.year}/%m/%d") 128: 129: else 130: return o.to_s 131: end 132: end
Creates and returns the object representing a datetime (DateTime in the default implementation). This method is, by default, called by the Parser class. It could be overriden as follows in order to get Time instances from all the SDL4R parsers.
module SDL4R def self.new_date_time(year, month, day, hour, min, sec, time_zone_offset) Time.utc(year, month, day, hour, min, sec) end end
# File lib/sdl4r/sdl4r.rb, line 144 144: def self.new_date_time(year, month, day, hour, min, sec, time_zone_offset) 145: DateTime.civil(year, month, day, hour, min, sec, time_zone_offset) 146: end
Creates and returns a tag named “root” and add all the tags specified in the given input.
input: | String, IO, Pathname or URI. |
root = SDL4R::read(<<EOF planets { earth area_km2=510900000 mars } EOF ) root = SDL4R::read(Pathname.new("my_dir/my_file.sdl")) IO.open("my_dir/my_file.sdl", "r") { |io| root = SDL4R::read(io) } root = SDL4R::read(URI.new("http://my_site/my_file.sdl"))
# File lib/sdl4r/sdl4r.rb, line 237 237: def self.read(input) 238: Tag.new(ROOT_TAG_NAME).read(input) 239: end
Parse a string representing the attributes portion of an SDL tag and return the results as a map.
Example
hash = SDL4R.to_attribute_hash("value=1 debugging=on time=12:24:01"); # { "value" => 1, "debugging" => true, "time" => SdlTimeSpan.new(12, 24, 01) }
# File lib/sdl4r/sdl4r.rb, line 275 275: def self.to_attribute_map(s) 276: raise ArgumentError, "'s' cannot be null" if s.nil? 277: return read("atts " + s).child.attributes 278: end
Parses and returns the value corresponding with the specified SDL literal.
SDL4R.to_value("\"abcd\"") # => "abcd" SDL4R.to_value("1") # => 1 SDL4R.to_value("null") # => nil
# File lib/sdl4r/sdl4r.rb, line 247 247: def self.to_value(s) 248: raise ArgumentError, "'s' cannot be null" if s.nil? 249: return read(s).child.value 250: end
Parse the string of values and return a list. The string is handled as if it is the values portion of an SDL tag.
Example
array = SDL4R.to_value_array("1 true 12:24:01")
Will return an int, a boolean, and a time span.
# File lib/sdl4r/sdl4r.rb, line 261 261: def self.to_value_array(s) 262: raise ArgumentError, "'s' cannot be null" if s.nil? 263: return read(s).child.values 264: end
Validates an SDL identifier String. SDL Identifiers must start with a Unicode letter or underscore (_) and contain only unicode letters, digits, underscores (_), dashes(-), periods (.) and dollar signs ($).
Raises
ArgumentError if the identifier is not legal
TODO: support UTF-8 identifiers
# File lib/sdl4r/sdl4r.rb, line 189 189: def self.validate_identifier(identifier) 190: if identifier.nil? or identifier.empty? 191: raise ArgumentError, "SDL identifiers cannot be null or empty." 192: end 193: 194: # in Java, was if(!Character.isJavaIdentifierStart(identifier.charAt(0))) 195: unless identifier =~ /^[a-zA-Z_]/ 196: raise ArgumentError, 197: "'" + identifier[0..0] + 198: "' is not a legal first character for an SDL identifier. " + 199: "SDL Identifiers must start with a unicode letter or " + 200: "an underscore (_)." 201: end 202: 203: unless identifier.length == 1 or identifier =~ /^[a-zA-Z_][a-zA-Z_0-9\-\.\$]*$/ 204: for i in 1..identifier.length 205: unless identifier[i..i] =~ /^[a-zA-Z_0-9\-\.\$]$/ 206: raise ArgumentError, 207: "'" + identifier[i..i] + 208: "' is not a legal character for an SDL identifier. " + 209: "SDL Identifiers must start with a unicode letter or " + 210: "underscore (_) followed by 0 or more unicode " + 211: "letters, digits, underscores (_), dashes (-), periodss (.) and dollar signs ($)" 212: end 213: end 214: end 215: end