Class: WirisPlugin::XmlWriter

Inherits:
Object
  • Object
show all
Includes:
Wiris
Defined in:
lib/com/wiris/util/xml/XmlWriter.rb

Constant Summary collapse

INDENT_STRING =
"   "
ECHO_FILTER =
2
AUTO_IGNORING_SPACE_FILTER =
1
PRETTY_PRINT_FILTER =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXmlWriter

Returns a new instance of XmlWriter.



30
31
32
33
34
35
36
37
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 30

def initialize()
  super()
  @filter = PRETTY_PRINT_FILTER
  @xmlDeclaration = false
  @inlineElements = Array.new()
  @firstLine = generateFirstLine("UTF-8")
  reset()
end

Instance Attribute Details

#autoIgnoringWhitespaceObject

Returns the value of attribute autoIgnoringWhitespace.



16
17
18
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 16

def autoIgnoringWhitespace
  @autoIgnoringWhitespace
end

#cdataSectionObject

Returns the value of attribute cdataSection.



24
25
26
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 24

def cdataSection
  @cdataSection
end

#currentPrefixesObject

Returns the value of attribute currentPrefixes.



25
26
27
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 25

def currentPrefixes
  @currentPrefixes
end

#depthObject

Returns the value of attribute depth.



23
24
25
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 23

def depth
  @depth
end

#filterObject

Returns the value of attribute filter.



18
19
20
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 18

def filter
  @filter
end

#firstLineObject

Returns the value of attribute firstLine.



20
21
22
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 20

def firstLine
  @firstLine
end

#hasWhiteSpaceObject

Returns the value of attribute hasWhiteSpace.



26
27
28
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 26

def hasWhiteSpace
  @hasWhiteSpace
end

#inlineObject

Returns the value of attribute inline.



21
22
23
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 21

def inline
  @inline
end

#inlineElementsObject

Returns the value of attribute inlineElements.



17
18
19
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 17

def inlineElements
  @inlineElements
end

#inlineMarkObject

Returns the value of attribute inlineMark.



22
23
24
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 22

def inlineMark
  @inlineMark
end

#nameSpaceObject

Returns the value of attribute nameSpace.



27
28
29
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 27

def nameSpace
  @nameSpace
end

#prettyPrintObject

Returns the value of attribute prettyPrint.



12
13
14
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 12

def prettyPrint
  @prettyPrint
end

#sbObject

Returns the value of attribute sb.



29
30
31
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 29

def sb
  @sb
end

#tagOpenObject

Returns the value of attribute tagOpen.



19
20
21
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 19

def tagOpen
  @tagOpen
end

#whiteSpaceObject

Returns the value of attribute whiteSpace.



28
29
30
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 28

def whiteSpace
  @whiteSpace
end

#xmlDeclarationObject

Returns the value of attribute xmlDeclaration.



14
15
16
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 14

def xmlDeclaration
  @xmlDeclaration
end

Class Method Details

.isWhiteSpace(chars, start, length) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 185

def self.isWhiteSpace(chars,start,length)
    for i in start..start+length-1
      c = chars[i]
      if !((((c==' ')||(c=="\n"))||(c=="\r"))||(c=="\t"))
        return false
      end
      i+=1
    end
  return true
end

Instance Method Details

#characters(ch, start, length) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 143

def characters(ch,start,length)
  if @cdataSection
    writeChars(ch,start,length)
  else 
    if !@inline
      if self.class.isWhiteSpace(ch,start,length)
        @hasWhiteSpace = true
          for i in start..start+length-1
            @whiteSpace::addChar(ch[i])
            i+=1
          end
        return 
      else 
        processWhiteSpace(true)
        @inlineMark = @depth-1
        @inline = true
      end
    end
    closeOpenTag(false)
    writeTextChars(ch,start,length)
  end
end

#closeOpenTag(endElement) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 195

def closeOpenTag(endElement)
  if @tagOpen
    if endElement
      write("/")
    end
    write(">")
    @tagOpen = false
  end
end

#endCDATAObject



139
140
141
142
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 139

def endCDATA()
  @cdataSection = false
  write("]]>")
end

#endDocumentObject



69
70
71
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 69

def endDocument()
  closeOpenTag(false)
end

#endElement(uri, localName, qName) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 113

def endElement(uri,localName,qName)
  name = qName
  if (name==nil)||(name::length()==0)
    name = localName
  end
  writeSpace = (@tagOpen||@inline)||!(@autoIgnoringWhitespace||@prettyPrint)
  processWhiteSpace(writeSpace)
  @depth-=1
  if @tagOpen
    closeOpenTag(true)
  else 
    if (!@inline&&@prettyPrint)&&!writeSpace
      writeIndent()
    end
    write(("</"+name)+">")
  end
  if @inline&&(@inlineMark==@depth)
    @inline = false
    @inlineMark = -1
  end
end

#endPrefixMapping(prefix) ⇒ Object



87
88
89
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 87

def endPrefixMapping(prefix)
  @currentPrefixes::remove(prefix)
end

#generateFirstLine(encoding) ⇒ Object



248
249
250
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 248

def generateFirstLine(encoding)
  return ("<?xml version=\"1.0\" encoding=\""+encoding)+"\"?>"
end

#getFilterObject



41
42
43
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 41

def getFilter()
  return @filter
end

#getInlineElementsObject



57
58
59
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 57

def getInlineElements()
  return @inlineElements
end

#isXmlDeclarationObject



47
48
49
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 47

def isXmlDeclaration()
  return @xmlDeclaration
end

#processWhiteSpace(write) ⇒ Object



233
234
235
236
237
238
239
240
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 233

def processWhiteSpace(write)
  if @hasWhiteSpace&&write
    closeOpenTag(false)
    writeText(@whiteSpace::toString())
  end
  @whiteSpace = StringBuf.new()
  @hasWhiteSpace = false
end

#resetObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 251

def reset()
  @tagOpen = false
  @inline = false
  @inlineMark = -1
  @depth = 0
  @cdataSection = false
  @hasWhiteSpace = false
  @currentPrefixes = Hash.new()
  @whiteSpace = StringBuf.new()
  @nameSpace = StringBuf.new()
  @sb = StringBuf.new()
  if @filter==PRETTY_PRINT_FILTER
    @autoIgnoringWhitespace = true
    @prettyPrint = true
  else 
    if @filter==AUTO_IGNORING_SPACE_FILTER
      @autoIgnoringWhitespace = true
      @prettyPrint = false
    else 
      @autoIgnoringWhitespace = false
      @prettyPrint = false
    end
  end
end

#setFilter(filterType) ⇒ Object



38
39
40
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 38

def setFilter(filterType)
  @filter = filterType
end

#setInlineElements(inlineElements) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 50

def setInlineElements(inlineElements)
  if inlineElements!=nil
    @inlineElements = inlineElements
  else 
    @inlineElements = Array.new()
  end
end

#setXmlDeclaration(xmlFragment) ⇒ Object



44
45
46
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 44

def setXmlDeclaration(xmlFragment)
  @xmlDeclaration = xmlFragment
end

#startCDATAObject



134
135
136
137
138
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 134

def startCDATA()
  closeOpenTag(false)
  write("<![CDATA[")
  @cdataSection = true
end

#startDocumentObject



60
61
62
63
64
65
66
67
68
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 60

def startDocument()
  reset()
  if @xmlDeclaration
    write(@firstLine)
  end
  if !@prettyPrint
    write("\n")
  end
end

#startElement(uri, localName, qName, atts) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 90

def startElement(uri,localName,qName,atts)
  closeOpenTag(false)
  processWhiteSpace(@inline||!(@autoIgnoringWhitespace||@prettyPrint))
  if @prettyPrint&&!@inline
    writeIndent()
  end
  name = qName
  if (name==nil)||(name::length()==0)
    name = localName
  end
  write("<"+name)
  writeAttributes(atts)
  if @nameSpace!=nil
    write(@nameSpace::toString())
    @nameSpace = nil
  end
  @tagOpen = true
  if !@inline&&@inlineElements::contains_(name)
    @inlineMark = @depth
    @inline = true
  end
  @depth+=1
end

#startPrefixMapping(prefix, uri) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 72

def startPrefixMapping(prefix,uri)
  if (uri==@currentPrefixes::get(prefix))
    return 
  end
  if uri::length()==0
    return 
  end
  pref = prefix
  if prefix::length()>0
    pref = ":"+prefix
  end
  ns = (((" xmlns"+pref)+"=\"")+uri)+"\""
  @nameSpace::add(ns)
  @currentPrefixes::set(prefix,uri)
end

#toStringObject



275
276
277
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 275

def toString()
  return sb::toString()
end

#write(s) ⇒ Object



171
172
173
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 171

def write(s)
  @sb::add(s)
end

#writeAttributes(atts) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 204

def writeAttributes(atts)
  if atts==nil
    return 
  end
    for i in 0..atts::getLength()-1
      name = atts::getName(i)
      value = atts::getValue(i)
      if name::startsWith("xmlns")
        prefix = nil
        uri = value
        if name::length()>5
          if name::charAt(6)==':'
            prefix = name::substring(6)
          end
        else 
          prefix = ""
        end
        if (prefix!=nil)&&(uri==@currentPrefixes::get(prefix))
            next
        end
      end
      write(" ")
      write(name)
      write("=\"")
      writeText(value)
      write("\"")
      i+=1
    end
end

#writeChars(ch, start, length) ⇒ Object



165
166
167
168
169
170
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 165

def writeChars(ch,start,length)
    for i in start..start+length-1
      @sb::addChar(ch[i])
      i+=1
    end
end

#writeIndentObject



241
242
243
244
245
246
247
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 241

def writeIndent()
  write("\n")
    for i in 0..@depth-1
      write(INDENT_STRING)
      i+=1
    end
end

#writeText(s) ⇒ Object



174
175
176
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 174

def writeText(s)
  @sb::add(WXmlUtils::htmlEscape(s))
end

#writeTextChars(ch, start, length) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 177

def writeTextChars(ch,start,length)
  s = StringBuf.new()
    for i in start..start+length-1
      s::addChar(ch[i])
      i+=1
    end
  @sb::add(WXmlUtils::htmlEscape(s::toString()))
end