Class: HTree::XMLDecl

Inherits:
Object
  • Object
show all
Includes:
Leaf, Trav
Defined in:
lib/htree/loc.rb,
lib/htree/leaf.rb,
lib/htree/rexml.rb,
lib/htree/output.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/modules.rb,
lib/htree/equality.rb,
lib/htree/raw_string.rb

Defined Under Namespace

Modules: Trav Classes: Loc

Constant Summary

Constants included from HTree

DefaultContext, ElementContent, ElementExclusions, ElementInclusions, EmptyBindingObject, HTMLContext, NamedCharacters, NamedCharactersPattern, OmittedAttrName

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Leaf::Trav

#traverse_all_element, #traverse_some_element, #traverse_text_internal

Methods included from Traverse

#bogusetag?, #comment?, #doc?, #doctype?, #elem?, #get_subnode, #procins?, #text?, #traverse_text, #xmldecl?

Methods included from Leaf

#extract_text, #init_raw_string, #pretty_print, #raw_string=, #raw_string_internal

Methods included from Node

#display_html, #display_xml, #extract_text, #generate_xml_output_code, #make_loc, #raw_string, #subst, #subst_internal, #to_node, #to_rexml

Methods included from HTree

#==, build_node, #check_equality, compile_template, #exact_equal?, #exact_equal_object, expand_template, fix_element, fix_structure_list, frozen_string, #hash, parse_as, parse_pairs, parse_xml, scan, #usual_equal_object, with_frozen_string_hash

Constructor Details

#initialize(version, encoding = nil, standalone = nil) ⇒ XMLDecl

Returns a new instance of XMLDecl.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/htree/leaf.rb', line 6

def initialize(version, encoding=nil, standalone=nil)
  init_raw_string
  if /\A[a-zA-Z0-9_.:-]+\z/ !~ version
    raise HTree::Error, "invalid version in XML declaration: #{version.inspect}"
  end
  if encoding && /\A[A-Za-z][A-Za-z0-9._-]*\z/ !~ encoding
    raise HTree::Error, "invalid encoding in XML declaration: #{encoding.inspect}"
  end
  unless standalone == nil || standalone == true || standalone == false
    raise HTree::Error, "invalid standalone document declaration in XML declaration: #{standalone.inspect}"
  end
  @version = version
  @encoding = encoding
  @standalone = standalone
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



21
22
23
# File 'lib/htree/leaf.rb', line 21

def encoding
  @encoding
end

#standaloneObject (readonly)

Returns the value of attribute standalone.



21
22
23
# File 'lib/htree/leaf.rb', line 21

def standalone
  @standalone
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/htree/leaf.rb', line 21

def version
  @version
end

Class Method Details

.parse(raw_string) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/htree/parse.rb', line 347

def XMLDecl.parse(raw_string)
  unless /\A#{Pat::XmlDecl_C}\z/o =~ raw_string
    raise HTree::Error, "cannot recognize as XML declaration: #{raw_string.inspect}"
  end

  version = $1 || $2
  encoding = $3 || $4
  case $5 || $6
  when 'yes'
    standalone = true
  when 'no'
    standalone = false
  else
    standalone = nil
  end

  result = XMLDecl.new(version, encoding, standalone)
  result.raw_string = raw_string
  result
end

Instance Method Details

#eliminate_raw_stringObject



104
105
106
# File 'lib/htree/raw_string.rb', line 104

def eliminate_raw_string
  XMLDecl.new(@version, @encoding, @standalone)
end

#make_exact_equal_objectObject



165
166
167
# File 'lib/htree/equality.rb', line 165

def make_exact_equal_object
  [@raw_string, @version, @encoding, @standalone]
end

#make_usual_equal_objectObject



169
170
171
# File 'lib/htree/equality.rb', line 169

def make_usual_equal_object
  [@version, @encoding, @standalone]
end

#node_test_stringObject



94
# File 'lib/htree/loc.rb', line 94

def node_test_string() 'xml-declaration()' end

#output(out, context) ⇒ Object

don’t output anything.



157
158
# File 'lib/htree/output.rb', line 157

def output(out, context)
end

#output_prolog_xmldecl(out, context) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/htree/output.rb', line 160

def output_prolog_xmldecl(out, context)
  out.output_string "<?xml version=\"#{@version}\""
  if @encoding
    out.output_string " encoding=\"#{@encoding}\""
  end
  if @standalone != nil
    out.output_string " standalone=\"#{@standalone ? 'yes' : 'no'}\""
  end
  out.output_string "?>"
end

#to_rexml_internal(parent, context) ⇒ Object



97
98
99
100
101
# File 'lib/htree/rexml.rb', line 97

def to_rexml_internal(parent, context)
  r = REXML::XMLDecl.new(self.version, self.encoding, self.standalone)
  parent << r if parent
  r
end