class WirisPlugin::XmlWriter

Constants

AUTO_IGNORING_SPACE_FILTER
ECHO_FILTER
INDENT_STRING
PRETTY_PRINT_FILTER

Attributes

autoIgnoringWhitespace[RW]
cdataSection[RW]
currentPrefixes[RW]
depth[RW]
filter[RW]
firstLine[RW]
hasWhiteSpace[RW]
ignoreEntities[RW]
inlineElements[RW]
inlineMark[RW]
isInline[RW]
nameSpace[RW]
prettyPrint[RW]
sb[RW]
tagOpen[RW]
whiteSpace[RW]
xmlDeclaration[RW]

Public Class Methods

isWhiteSpace(text) click to toggle source
# 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
new() click to toggle source
Calls superclass method
# 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

Public Instance Methods

characters(ch) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 188
def closeOpenTag(endElement)
    if @tagOpen
        if endElement
            write("/")
        end
        write(">")
        @tagOpen = false
    end
end
endCDATA() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 145
def endCDATA()
    @cdataSection = false
    write("]]>")
end
endDocument() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 75
def endDocument()
    closeOpenTag(false)
end
endElement(uri, localName, qName) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 93
def endPrefixMapping(prefix)
    @currentPrefixes::remove(prefix)
end
generateFirstLine(encoding) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 241
def generateFirstLine(encoding)
    return ("<?xml version=\"1.0\" encoding=\"" + encoding) + "\"?>"
end
getFilter() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 47
def getFilter()
    return @filter
end
getInlineElements() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 63
def getInlineElements()
    return @inlineElements
end
isXmlDeclaration() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 53
def isXmlDeclaration()
    return @xmlDeclaration
end
processWhiteSpace(write) click to toggle source
# 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
reset() click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 44
def setFilter(filterType)
    self.filter = filterType
end
setInlineElements(inlineElements) click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 50
def setXmlDeclaration(xmlFragment)
    self.xmlDeclaration = xmlFragment
end
startCDATA() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 140
def startCDATA()
    closeOpenTag(false)
    write("<![CDATA[")
    @cdataSection = true
end
startDocument() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
toString() click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 268
def toString()
    return self.sb::toString()
end
write(s) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 172
def write(s)
    @sb::add(s)
end
writeAttributes(atts) click to toggle source
# 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
writeIndent() click to toggle source
# 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) click to toggle source
# File lib/com/wiris/util/xml/XmlWriter.rb, line 175
def writeText(s)
    @sb::add(WXmlUtils::htmlEscape(s))
end