Module SDL4R
In: lib/sdl4r/sdl_time_span.rb
lib/sdl4r/sdl_parse_error.rb
lib/sdl4r/tag.rb
lib/sdl4r/parser.rb
lib/sdl4r/sdl_binary.rb
lib/sdl4r/parser/reader.rb
lib/sdl4r/parser/tokenizer.rb
lib/sdl4r/parser/token.rb
lib/sdl4r/parser/time_span_with_zone.rb
lib/sdl4r/sdl4r.rb

Gathers utility methods.

Methods

Classes and Modules

Class SDL4R::Parser
Class SDL4R::SdlBinary
Class SDL4R::SdlParseError
Class SDL4R::SdlTimeSpan
Class SDL4R::Tag

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
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.

[Source]

# File lib/sdl4r/sdl_binary.rb, line 69
  def self.SdlBinary(o)
    if o.kind_of? SdlBinary
      return o
    elsif o.kind_of? String
      return SdlBinary.new(o)
    else
      raise ArgumentError, "can't coerce argument"
    end
  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.

[Source]

# File lib/sdl4r/sdl4r.rb, line 149
  def self.coerce_or_fail(o)
    case o

    when Rational
      return o.to_f

    when NilClass,
        String,
        Numeric,
        Float,
        TrueClass,
        FalseClass,
        Date,
        DateTime,
        Time,
        SdlTimeSpan,
        SdlBinary
      return o

    end

    raise ArgumentError, "#{o.class.name} is not coercible to an SDL type"
  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)

[Source]

# File lib/sdl4r/sdl4r.rb, line 42
  def self.format(o, add_quotes = true, line_prefix = "", indent = "\t")
    if o.is_a?(String)
      if add_quotes
        o_length = 0
        o.scan(/./m) { o_length += 1 } # counts the number of chars (as opposed of bytes)
        if o_length == 1
          return "'" + escape(o, "'") + "'"
        else
          return '"' + escape(o, '"') + '"'
        end
      else
        return escape(o)
      end
      
    elsif o.is_a?(Bignum)
      return o.to_s + "BD"
      
    elsif o.is_a?(Integer)
      if MIN_INTEGER_32 <= o and o <= MAX_INTEGER_32
        return o.to_s
      elsif MIN_INTEGER_64 <= o and o <= MAX_INTEGER_64
        return o.to_s + "L"
      else
        return o.to_s + "BD"
      end
      
    elsif o.is_a?(Float)
      return (o.to_s + "F")
      
    elsif o.is_a?(Rational)
      return o.to_f.to_s + "F"

    elsif o.is_a?(BigDecimal)
      s = o.to_s('F')
      s.sub!(/\.0$/, "")
      return "#{s}BD"

    elsif o.nil?
      return "null"

    elsif o.is_a?(SdlBinary)
      encoded_o = Base64.encode64(o.bytes)
      encoded_o.gsub!(/[\r\n]/m, "") # Remove the EOL inserted every 60 chars

      if add_quotes
        if encoded_o.length > BASE64_WRAP_LINE_LENGTH
          # FIXME: we should a constant or some parameter instead of hardcoded spaces
          wrap_lines_in_ascii(encoded_o, BASE64_WRAP_LINE_LENGTH, "#{line_prefix}#{indent}")
          encoded_o.insert(0, "[#{$/}")
          encoded_o << "#{$/}#{line_prefix}]"
        else
          encoded_o.insert(0, "[")
          encoded_o << "]"
        end
      end

      return encoded_o
      
    # Below, we use "#{o.year}" instead of "%Y" because "%Y" always emit 4 chars at least even if
    # the date is before 1000.
    elsif o.is_a?(DateTime) || o.is_a?(Time)
      milliseconds = o.is_a?(DateTime) ? get_date_milliseconds(o) : (o.usec / 10).to_i

      if milliseconds == 0
        if o.zone
          return o.strftime("#{o.year}/%m/%d %H:%M:%S%Z")
        else
          return o.strftime("#{o.year}/%m/%d %H:%M:%S")
        end
      else
        if o.zone
          return o.strftime("#{o.year}/%m/%d %H:%M:%S." + milliseconds.to_s.ljust(3, '0') + "%Z")
        else
          return o.strftime("#{o.year}/%m/%d %H:%M:%S." + milliseconds.to_s.ljust(3, '0'))
        end
      end

    elsif o.is_a?(Date)
      return o.strftime("#{o.year}/%m/%d")
      
    else
      return o.to_s
    end
  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

[Source]

# File lib/sdl4r/sdl4r.rb, line 137
  def self.new_date_time(year, month, day, hour, min, sec, time_zone_offset)
    DateTime.civil(year, month, day, hour, min, sec, time_zone_offset)
  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"))

[Source]

# File lib/sdl4r/sdl4r.rb, line 230
  def self.read(input)
    Tag.new("root").read(input)
  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) }

[Source]

# File lib/sdl4r/sdl4r.rb, line 268
  def self.to_attribute_map(s)
    raise ArgumentError, "'s' cannot be null" if s.nil?
    return read("atts " + s).child.attributes
  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

[Source]

# File lib/sdl4r/sdl4r.rb, line 240
  def self.to_value(s)
    raise ArgumentError, "'s' cannot be null" if s.nil?
    return read(s).child.value
  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.

[Source]

# File lib/sdl4r/sdl4r.rb, line 254
  def self.to_value_array(s)
    raise ArgumentError, "'s' cannot be null" if s.nil?
    return read(s).child.values
  end

Validates an SDL identifier String. SDL Identifiers must start with a Unicode letter or underscore (_) and contain only unicode letters, digits, underscores (_), dashes(-) and periods (.).

Raises

ArgumentError if the identifier is not legal

TODO: support UTF-8 identifiers

[Source]

# File lib/sdl4r/sdl4r.rb, line 182
  def self.validate_identifier(identifier)
    if identifier.nil? or identifier.empty?
      raise ArgumentError, "SDL identifiers cannot be null or empty."
    end

    # in Java, was if(!Character.isJavaIdentifierStart(identifier.charAt(0)))
    unless identifier =~ /^[a-zA-Z_]/
      raise ArgumentError,
        "'" + identifier[0..0] +
        "' is not a legal first character for an SDL identifier. " +
        "SDL Identifiers must start with a unicode letter or " +
        "an underscore (_)."
    end
    
    unless identifier.length == 1 or identifier =~ /^[a-zA-Z_][a-zA-Z_0-9\-\.]*$/
      for i in 1..identifier.length
        unless identifier[i..i] =~ /^[a-zA-Z_0-9\-]$/
          raise ArgumentError,
            "'" + identifier[i..i] + 
            "' is not a legal character for an SDL identifier. " +
            "SDL Identifiers must start with a unicode letter or " +
            "underscore (_) followed by 0 or more unicode " +
            "letters, digits, underscores (_), or dashes (-)"
        end
      end
    end
  end

[Validate]