Class REXML::DocType
In: lib/rexml/doctype.rb
Parent: Parent

Represents an XML DOCTYPE declaration; that is, the contents of <!DOCTYPE … >. DOCTYPES can be used to declare the DTD of a document, as well as being used to declare entities used in the document.

Methods

add   attribute_of   attributes_of   clone   context   entity   new   node_type   notation   notations   public   strip_quotes   system   write  

Included Modules

XMLTokens

Constants

START = "<!DOCTYPE"
STOP = ">"
SYSTEM = "SYSTEM"
PUBLIC = "PUBLIC"
DEFAULT_ENTITIES = { 'gt'=>EntityConst::GT, 'lt'=>EntityConst::LT, 'quot'=>EntityConst::QUOT, "apos"=>EntityConst::APOS

Attributes

entities  [R]  name is the name of the doctype external_id is the referenced DTD, if given
external_id  [R]  name is the name of the doctype external_id is the referenced DTD, if given
name  [R]  name is the name of the doctype external_id is the referenced DTD, if given
namespaces  [R]  name is the name of the doctype external_id is the referenced DTD, if given

Public Class methods

Constructor

  dt = DocType.new( 'foo', '-//I/Hate/External/IDs' )
  # <!DOCTYPE foo '-//I/Hate/External/IDs'>
  dt = DocType.new( doctype_to_clone )
  # Incomplete.  Shallow clone of doctype

Note that the constructor:

 Doctype.new( Source.new( "<!DOCTYPE foo 'bar'>" ) )

is deprecated. Do not use it. It will probably disappear.

[Source]

    # File lib/rexml/doctype.rb, line 41
41:     def initialize( first, parent=nil )
42:       @entities = DEFAULT_ENTITIES
43:       @long_name = @uri = nil
44:       if first.kind_of? String
45:         super()
46:         @name = first
47:         @external_id = parent
48:       elsif first.kind_of? DocType
49:         super( parent )
50:         @name = first.name
51:         @external_id = first.external_id
52:       elsif first.kind_of? Array
53:         super( parent )
54:         @name = first[0]
55:         @external_id = first[1]
56:         @long_name = first[2]
57:         @uri = first[3]
58:       elsif first.kind_of? Source
59:         super( parent )
60:         parser = Parsers::BaseParser.new( first )
61:         event = parser.pull
62:         if event[0] == :start_doctype
63:           @name, @external_id, @long_name, @uri, = event[1..-1]
64:         end
65:       else
66:         super()
67:       end
68:     end

Public Instance methods

[Source]

     # File lib/rexml/doctype.rb, line 138
138:     def add child
139:       super(child)
140:       @entities = DEFAULT_ENTITIES.clone if @entities == DEFAULT_ENTITIES
141:       @entities[ child.name ] = child if child.kind_of? Entity
142:     end

[Source]

    # File lib/rexml/doctype.rb, line 84
84:     def attribute_of element, attribute
85:       att_decl = find do |child|
86:         child.kind_of? AttlistDecl and
87:         child.element_name == element and
88:         child.include? attribute
89:       end
90:       return nil unless att_decl
91:       att_decl[attribute]
92:     end

[Source]

    # File lib/rexml/doctype.rb, line 74
74:     def attributes_of element
75:       rv = []
76:       each do |child|
77:         child.each do |key,val|
78:           rv << Attribute.new(key,val)
79:         end if child.kind_of? AttlistDecl and child.element_name == element
80:       end
81:       rv
82:     end

[Source]

    # File lib/rexml/doctype.rb, line 94
94:     def clone
95:       DocType.new self
96:     end

[Source]

     # File lib/rexml/doctype.rb, line 130
130:     def context
131:       @parent.context
132:     end

[Source]

     # File lib/rexml/doctype.rb, line 134
134:     def entity( name )
135:       @entities[name].unnormalized if @entities[name]
136:     end

[Source]

    # File lib/rexml/doctype.rb, line 70
70:     def node_type
71:       :doctype
72:     end

Retrieves a named notation. Only notations declared in the internal DTD subset can be retrieved.

Method contributed by Henrik Martensson

[Source]

     # File lib/rexml/doctype.rb, line 182
182:     def notation(name)
183:       notations.find { |notation_decl|
184:         notation_decl.name == name
185:       }
186:     end

This method returns a list of notations that have been declared in the internal DTD subset. Notations in the external DTD subset are not listed.

Method contributed by Henrik Martensson

[Source]

     # File lib/rexml/doctype.rb, line 174
174:     def notations
175:       children().select {|node| node.kind_of?(REXML::NotationDecl)}
176:     end

This method retrieves the public identifier identifying the document‘s DTD.

Method contributed by Henrik Martensson

[Source]

     # File lib/rexml/doctype.rb, line 148
148:     def public
149:       case @external_id
150:       when "SYSTEM"
151:         nil
152:       when "PUBLIC"
153:         strip_quotes(@long_name)
154:       end
155:     end

This method retrieves the system identifier identifying the document‘s DTD

Method contributed by Henrik Martensson

[Source]

     # File lib/rexml/doctype.rb, line 160
160:     def system
161:       case @external_id
162:       when "SYSTEM"
163:         strip_quotes(@long_name)
164:       when "PUBLIC"
165:         @uri.kind_of?(String) ? strip_quotes(@uri) : nil
166:       end
167:     end
output:Where to write the string
indent:An integer. If -1, no indentation will be used; otherwise, the indentation will be this number of spaces, and children will be indented an additional amount.
transitive:Ignored
ie_hack:Ignored

[Source]

     # File lib/rexml/doctype.rb, line 108
108:     def write( output, indent=0, transitive=false, ie_hack=false )
109:       f = REXML::Formatters::Default.new
110:       indent( output, indent )
111:       output << START
112:       output << ' '
113:       output << @name
114:       output << " #@external_id" if @external_id
115:       output << " #{@long_name.inspect}" if @long_name
116:       output << " #{@uri.inspect}" if @uri
117:       unless @children.empty?
118:         next_indent = indent + 1
119:         output << ' ['
120:         child = nil    # speed
121:         @children.each { |child|
122:           output << "\n"
123:           f.write( child, output )
124:         }
125:         output << "\n]"
126:       end
127:       output << STOP
128:     end

Private Instance methods

Method contributed by Henrik Martensson

[Source]

     # File lib/rexml/doctype.rb, line 191
191:     def strip_quotes(quoted_string)
192:       quoted_string =~ /^[\'\"].*[\ยด\"]$/ ?
193:         quoted_string[1, quoted_string.length-2] :
194:         quoted_string
195:     end

[Validate]