Class: WirisPlugin::XmlWriter

Inherits:
Object
  • Object
show all
Extended by:
ContentHandlerInterface
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

Methods included from ContentHandlerInterface

ContentHandler

Constructor Details

#initializeXmlWriter

Returns a new instance of XmlWriter.



36
37
38
39
40
41
42
43
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 36

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.



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

def autoIgnoringWhitespace
  @autoIgnoringWhitespace
end

#cdataSectionObject

Returns the value of attribute cdataSection.



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

def cdataSection
  @cdataSection
end

#currentPrefixesObject

Returns the value of attribute currentPrefixes.



31
32
33
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 31

def currentPrefixes
  @currentPrefixes
end

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#firstLineObject

Returns the value of attribute firstLine.



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

def firstLine
  @firstLine
end

#hasWhiteSpaceObject

Returns the value of attribute hasWhiteSpace.



32
33
34
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 32

def hasWhiteSpace
  @hasWhiteSpace
end

#ignoreEntitiesObject

Returns the value of attribute ignoreEntities.



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

def ignoreEntities
  @ignoreEntities
end

#inlineElementsObject

Returns the value of attribute inlineElements.



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

def inlineElements
  @inlineElements
end

#inlineMarkObject

Returns the value of attribute inlineMark.



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

def inlineMark
  @inlineMark
end

#isInlineObject

Returns the value of attribute isInline.



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

def isInline
  @isInline
end

#nameSpaceObject

Returns the value of attribute nameSpace.



33
34
35
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 33

def nameSpace
  @nameSpace
end

#prettyPrintObject

Returns the value of attribute prettyPrint.



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

def prettyPrint
  @prettyPrint
end

#sbObject

Returns the value of attribute sb.



35
36
37
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 35

def sb
  @sb
end

#tagOpenObject

Returns the value of attribute tagOpen.



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

def tagOpen
  @tagOpen
end

#whiteSpaceObject

Returns the value of attribute whiteSpace.



34
35
36
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 34

def whiteSpace
  @whiteSpace
end

#xmlDeclarationObject

Returns the value of attribute xmlDeclaration.



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

def xmlDeclaration
  @xmlDeclaration
end

Class Method Details

.isWhiteSpace(text) ⇒ Object



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

def self.isWhiteSpace(text)
    it = Utf8::getIterator(text)
    while it::hasNext()
        c = it::next()
        if (((c != 32) && (c != 10)) && (c != 13)) && (c != 9)
            return false
        end
    end
    return true
end

Instance Method Details

#characters(ch) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 149

def characters(ch)
    if @cdataSection
        @sb::add(ch)
    else 
        if !@isInline
            if XmlWriter.isWhiteSpace(ch)
                @hasWhiteSpace = true
                @whiteSpace::add(ch)
                return 
            else 
                processWhiteSpace(true)
                @inlineMark = @depth - 1
                @isInline = true
            end
        end
        closeOpenTag(false)
        if @ignoreEntities
            @sb::add(ch)
        else 
            @sb::add(WXmlUtils::htmlEscape(ch))
        end
    end
end

#closeOpenTag(endElement) ⇒ Object



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

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

#endCDATAObject



145
146
147
148
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 145

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

#endDocumentObject



75
76
77
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 75

def endDocument()
    closeOpenTag(false)
end

#endElement(uri, localName, qName) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 119

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

#endPrefixMapping(prefix) ⇒ Object



93
94
95
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 93

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

#generateFirstLine(encoding) ⇒ Object



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

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

#getFilterObject



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

def getFilter()
    return @filter
end

#getInlineElementsObject



63
64
65
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 63

def getInlineElements()
    return @inlineElements
end

#isXmlDeclarationObject



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

def isXmlDeclaration()
    return @xmlDeclaration
end

#processWhiteSpace(write) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 226

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

#resetObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 244

def reset()
    @tagOpen = false
    @isInline = 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



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

def setFilter(filterType)
    self.filter = filterType
end

#setInlineElements(inlineElements) ⇒ Object



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

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

#setXmlDeclaration(xmlFragment) ⇒ Object



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

def setXmlDeclaration(xmlFragment)
    self.xmlDeclaration = xmlFragment
end

#startCDATAObject



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

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

#startDocumentObject



66
67
68
69
70
71
72
73
74
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 66

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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 96

def startElement(uri, localName, qName, atts)
    closeOpenTag(false)
    processWhiteSpace(@isInline || !(@autoIgnoringWhitespace || @prettyPrint))
    if @prettyPrint && !@isInline
        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 !@isInline && @inlineElements::contains_(name)
        @inlineMark = @depth
        @isInline = true
    end
    @depth+=1
end

#startPrefixMapping(prefix, uri) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 78

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



268
269
270
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 268

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

#write(s) ⇒ Object



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

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

#writeAttributes(atts) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/com/wiris/util/xml/XmlWriter.rb', line 197

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 Std::charCodeAt(name,6) == 54
                    prefix = Std::substr(name,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

#writeIndentObject



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

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

#writeText(s) ⇒ Object



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

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