class CTioga2::Graphics::Styles::StyleSheet::XPathElement

An element in a XPath

Attributes

direct_parent[RW]

If this flag is on, the object has to be the direct parent of the child below.

obj_class[RW]

The class – or nil if not selecting on class

obj_id[RW]

The ID, or nil if not selecting on id

obj_type[RW]

The type – or nil if not selecting on type

Public Class Methods

from_text(txt) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 73
def self.from_text(txt)
  a = XPathElement.new
  a.parse_string(txt)
  return a
end

Public Instance Methods

matches?(obj) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 79
def matches?(obj)
  if @obj_type && (obj.style_name != @obj_type)
    return false
  end
  if @obj_class && !obj.object_classes.include?(@obj_class)
    return false
  end
  if @obj_id && (obj.object_id != @obj_id)
    return false
  end
  return true
end
parse_string(txt) click to toggle source

A XPathElement is a series of elements (w and - allowed), optionnally followed by a > sign. No space allowed, excepted before the > sign

# File lib/ctioga2/graphics/styles/stylesheet.rb, line 49
def parse_string(txt)


  rest = txt.gsub(/([.#]?)([\w-]+)/) do |x|
    if $1 == "."
      @obj_class = $2
    elsif $1 == "#"
      @obj_id = $2
    else
      @obj_type = $2
    end
    ""
  end

  if rest =~ /^\s*\*?\s*(>)?$/
    if $1 == ">"
      @direct_parent = true
    end
  else
    raise "Incorrect XPath element: #{txt}"
  end

end
to_s() click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 92
def to_s
  a = @obj_type || ""
  if @obj_id
    a += "##{@obj_id}"
  end
  if @obj_class
    a += ".#{@obj_class}"
  end
  if a.size == 0
    a = "*"
  end
  if @direct_parent
    a += " >"
  end
  return a
  
end