| Class | REXML::Entity |
| In: |
lib/rexml/entity.rb
|
| Parent: | Child |
God, I hate DTDs. I really do. Why this idiot standard still plagues us is beyond me.
| PUBIDCHAR | = | "\x20\x0D\x0Aa-zA-Z0-9\\-()+,./:=?;!*@$_%#" |
| SYSTEMLITERAL | = | %Q{((?:"[^"]*")|(?:'[^']*'))} |
| PUBIDLITERAL | = | %Q{("[#{PUBIDCHAR}']*"|'[#{PUBIDCHAR}]*')} |
| EXTERNALID | = | "(?:(?:(SYSTEM)\\s+#{SYSTEMLITERAL})|(?:(PUBLIC)\\s+#{PUBIDLITERAL}\\s+#{SYSTEMLITERAL}))" |
| NDATADECL | = | "\\s+NDATA\\s+#{NAME}" |
| PEREFERENCE | = | "%#{NAME};" |
| ENTITYVALUE | = | %Q{((?:"(?:[^%&"]|#{PEREFERENCE}|#{REFERENCE})*")|(?:'([^%&']|#{PEREFERENCE}|#{REFERENCE})*'))} |
| PEDEF | = | "(?:#{ENTITYVALUE}|#{EXTERNALID})" |
| ENTITYDEF | = | "(?:#{ENTITYVALUE}|(?:#{EXTERNALID}(#{NDATADECL})?))" |
| PEDECL | = | "<!ENTITY\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>" |
| GEDECL | = | "<!ENTITY\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>" |
| ENTITYDECL | = | /\s*(?:#{GEDECL})|(?:#{PEDECL})/um |
| PEREFERENCE_RE | = | /#{PEREFERENCE}/um |
| external | [R] | |
| name | [R] | |
| ndata | [R] | |
| pubid | [R] | |
| ref | [R] |
Evaluates whether the given string matchs an entity definition, returning true if so, and false otherwise.
# File lib/rexml/entity.rb, line 68
68: def Entity::matches? string
69: (ENTITYDECL =~ string) == 0
70: end
Create a new entity. Simple entities can be constructed by passing a name, value to the constructor; this creates a generic, plain entity reference. For anything more complicated, you have to pass a Source to the constructor with the entity definiton, or use the accessor methods. WARNING: There is no validation of entity state except when the entity is read from a stream. If you start poking around with the accessors, you can easily create a non-conformant Entity. The best thing to do is dump the stupid DTDs and use XMLSchema instead.
e = Entity.new( 'amp', '&' )
# File lib/rexml/entity.rb, line 35
35: def initialize stream, value=nil, parent=nil, reference=false
36: super(parent)
37: @ndata = @pubid = @value = @external = nil
38: if stream.kind_of? Array
39: @name = stream[1]
40: if stream[-1] == '%'
41: @reference = true
42: stream.pop
43: else
44: @reference = false
45: end
46: if stream[2] =~ /SYSTEM|PUBLIC/
47: @external = stream[2]
48: if @external == 'SYSTEM'
49: @ref = stream[3]
50: @ndata = stream[4] if stream.size == 5
51: else
52: @pubid = stream[3]
53: @ref = stream[4]
54: end
55: else
56: @value = stream[2]
57: end
58: else
59: @reference = reference
60: @external = nil
61: @name = stream
62: @value = value
63: end
64: end
Returns the value of this entity unprocessed — raw. This is the normalized value; that is, with all %ent; and &ent; entities intact
# File lib/rexml/entity.rb, line 87
87: def normalized
88: @value
89: end
Evaluates to the unnormalized value of this entity; that is, replacing all entities — both %ent; and &ent; entities. This differs from +value()+ in that value only replaces %ent; entities.
# File lib/rexml/entity.rb, line 75
75: def unnormalized
76: document.record_entity_expansion unless document.nil?
77: v = value()
78: return nil if v.nil?
79: @unnormalized = Text::unnormalize(v, parent)
80: @unnormalized
81: end
Returns the value of this entity. At the moment, only internal entities are processed. If the value contains internal references (IE, %blah;), those are replaced with their values. IE, if the doctype contains:
<!ENTITY % foo "bar"> <!ENTITY yada "nanoo %foo; nanoo>
then:
doctype.entity('yada').value #-> "nanoo bar nanoo"
# File lib/rexml/entity.rb, line 136
136: def value
137: if @value
138: matches = @value.scan(PEREFERENCE_RE)
139: rv = @value.clone
140: if @parent
141: matches.each do |entity_reference|
142: entity_value = @parent.entity( entity_reference[0] )
143: rv.gsub!( /%#{entity_reference};/um, entity_value )
144: end
145: end
146: return rv
147: end
148: nil
149: end
Write out a fully formed, correct entity definition (assuming the Entity object itself is valid.)
| out: | An object implementing <TT><<<TT> to which the entity will be output |
| indent: | DEPRECATED and ignored |
# File lib/rexml/entity.rb, line 99
99: def write out, indent=-1
100: out << '<!ENTITY '
101: out << '% ' if @reference
102: out << @name
103: out << ' '
104: if @external
105: out << @external << ' '
106: if @pubid
107: q = @pubid.include?('"')?"'":'"'
108: out << q << @pubid << q << ' '
109: end
110: q = @ref.include?('"')?"'":'"'
111: out << q << @ref << q
112: out << ' NDATA ' << @ndata if @ndata
113: else
114: q = @value.include?('"')?"'":'"'
115: out << q << @value << q
116: end
117: out << '>'
118: end