Class: WirisPlugin::WXmlUtils

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

Constant Summary collapse

@@WHITESPACE_COLLAPSE_REGEX =
EReg.new("[ \\t\\n\\r]{2,}","g")
@@entities =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWXmlUtils

Returns a new instance of WXmlUtils.



8
9
10
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 8

def initialize()
    super()
end

Class Method Details

.copyChildren(from, to) ⇒ Object



446
447
448
449
450
451
452
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 446

def self.copyChildren(from, to)
    children = from::iterator()
    while children::hasNext()
        child = children::next()
        to::addChild(WXmlUtils::importXml(child,to))
    end
end

.copyElements(from, to) ⇒ Object



453
454
455
456
457
458
459
460
461
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 453

def self.copyElements(from, to)
    it = from::iterator()
    while it::hasNext()
        child = it::next()
        if child::nodeType == Xml::Element
            to::addChild(WXmlUtils::importXml(child,to))
        end
    end
end

.copyXml(elem) ⇒ Object



443
444
445
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 443

def self.copyXml(elem)
    return WXmlUtils.importXml(elem,elem)
end

.copyXmlNamespace(elem, customNamespace, prefixAttributes) ⇒ Object



557
558
559
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 557

def self.copyXmlNamespace(elem, customNamespace, prefixAttributes)
    return WXmlUtils.importXmlNamespace(elem,elem,customNamespace,prefixAttributes)
end

.createPCData(node, text) ⇒ Object



117
118
119
120
121
122
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 117

def self.createPCData(node, text)
    if PlatformSettings::PARSE_XML_ENTITIES
        text = WXmlUtils::htmlEscape(text)
    end
    return node::createPCData_(text)
end

.entitiesObject



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

def self.entities
    @@entities
end

.entities=(entities) ⇒ Object



178
179
180
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 178

def self.entities=(entities)
    @@entities = entities
end

.escapeXmlEntities(s) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 123

def self.escapeXmlEntities(s)
    s = StringTools::replace(s,"&","&")
    s = StringTools::replace(s,"<","&lt;")
    s = StringTools::replace(s,">","&gt;")
    s = StringTools::replace(s,"\"","&quot;")
    s = StringTools::replace(s,"\'","&apos;")
    return s
end

.filterMathMLEntities(text) ⇒ Object



288
289
290
291
292
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 288

def self.filterMathMLEntities(text)
    text = WXmlUtils.resolveEntities(text)
    text = WXmlUtils.nonAsciiToEntities(text)
    return text
end

.getAttribute(node, attributeName) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 91

def self.getAttribute(node, attributeName)
    value = node::get(attributeName)
    if value == nil
        return nil
    end
    if PlatformSettings::PARSE_XML_ENTITIES
        return WXmlUtils::htmlUnescape(value)
    end
    return value
end

.getChildElementCount(parent) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 479

def self.getChildElementCount(parent)
    if (parent::nodeType != Xml::Element) && (parent::nodeType != Xml::Document)
        return 0
    end
    it = parent::elements()
    count = 0
    while it::hasNext()
        it::next()
        count+=1
    end
    return count
end

.getChildPosition(parent, node) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 467

def self.getChildPosition(parent, node)
    childIndex = 0
    it = parent::iterator()
    while it::hasNext()
        child = it::next()
        if (child == node)
            return childIndex
        end
        childIndex+=1
    end
    return -1
end

.getDocumentElement(doc) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 81

def self.getDocumentElement(doc)
    nodeList = doc::iterator()
    while nodeList::hasNext()
        node = nodeList::next()
        if node::nodeType == Xml::Element
            return node
        end
    end
    return nil
end

.getElementContent(element) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 18

def self.getElementContent(element)
    sb = StringBuf.new()
    if (element::nodeType == Xml::Document) || (element::nodeType == Xml::Element)
        i = element::iterator()
        while i::hasNext()
            sb::add(i::next()::toString())
        end
    end
    return sb::toString()
end

.getElements(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 70

def self.getElements(node)
    nodes = Array.new()
    nodeList = node::iterator()
    while nodeList::hasNext()
        item = nodeList::next()
        if item::nodeType == Xml::Element
            nodes::push(item)
        end
    end
    return nodes
end

.getElementsByAttributeValue(nodeList, attributeName, attributeValue) ⇒ Object



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

def self.getElementsByAttributeValue(nodeList, attributeName, attributeValue)
    nodes = Array.new()
    while nodeList::hasNext()
        node = nodeList::next()
        if (node::nodeType == Xml::Element) && (attributeValue == WXmlUtils::getAttribute(node,attributeName))
            nodes::push(node)
        end
    end
    return nodes
end

.getElementsByTagName(nodeList, tagName) ⇒ Object



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

def self.getElementsByTagName(nodeList, tagName)
    nodes = Array.new()
    while nodeList::hasNext()
        node = nodeList::next()
        if (node::nodeType == Xml::Element) && (node::nodeName == tagName)
            nodes::push(node)
        end
    end
    return nodes
end

.getInnerText(xml) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 419

def self.getInnerText(xml)
    if (xml::nodeType == Xml::PCData) || (xml::nodeType == Xml::CData)
        return WXmlUtils::getNodeValue(xml)
    end
    r = ""
    iter = xml::iterator()
    while iter::hasNext()
        r += WXmlUtils.getInnerText(iter::next())
    end
    return r
end

.getNamespace(element, prefix) ⇒ Object



661
662
663
664
665
666
667
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 661

def self.getNamespace(element, prefix)
    if (element != nil) && (element::nodeType == Xml::Document)
        element = element::firstElement()
    end
    prefixAttr = (prefix == nil) ? "xmlns" : ("xmlns:" + prefix)
    return WXmlUtils.getNamespaceSearch(element,prefixAttr)
end

.getNamespaceSearch(element, attribute) ⇒ Object



668
669
670
671
672
673
674
675
676
677
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 668

def self.getNamespaceSearch(element, attribute)
    while (element != nil) && (element::nodeType == Xml::Element)
        attributeValue = element::get(attribute)
        if attributeValue != nil
            return attributeValue
        end
        element = element::parent_()
    end
    return nil
end

.getNodeValue(node) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 107

def self.getNodeValue(node)
    value = node::getNodeValue_()
    if value == nil
        return nil
    end
    if PlatformSettings::PARSE_XML_ENTITIES && (node::nodeType == Xml::PCData)
        return WXmlUtils::htmlUnescape(value)
    end
    return value
end

.getText(xml) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 408

def self.getText(xml)
    if xml::nodeType == Xml::PCData
        return xml::getNodeValue_()
    end
    r = ""
    iter = xml::iterator()
    while iter::hasNext()
        r += WXmlUtils.getText(iter::next())
    end
    return r
end

.getUtf8Char(text, i) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 293

def self.getUtf8Char(text, i)
    c = Std::charCodeAt(text,i)
    d = c
    if PlatformSettings::UTF8_CONVERSION
        if d > 127
            j = 0
            c = 128
            loop do
                c = c >> 1
                j+=1
            break if not (d&c) != 0
            end
            d = (c - 1)&d
            while j-=1 > 0
                i+=1
                c = Std::charCodeAt(text,i)
                d = (d << 6) + (c&63)
            end
        end
    else 
        if (d >= 55296) && (d <= 56319)
            c = Std::charCodeAt(text,i + 1)
            d = (((d - 55296) << 10) + ((c - 56320))) + 65536
        end
    end
    return d
end

.hasSameAttributes(a, b) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 28

def self.hasSameAttributes(a, b)
    if (a == nil) && (b == nil)
        return true
    else 
        if (a == nil) || (b == nil)
            return false
        end
    end
    iteratorA = a::attributes()
    iteratorB = b::attributes()
    while iteratorA::hasNext()
        if !iteratorB::hasNext()
            return false
        end
        iteratorB::next()
        attr = iteratorA::next()
        if !(WXmlUtils::getAttribute(a,attr) == WXmlUtils::getAttribute(b,attr))
            return false
        end
    end
    return !iteratorB::hasNext()
end

.htmlEscape(input) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 131

def self.htmlEscape(input)
    output = StringTools::replace(input,"&","&amp;")
    output = StringTools::replace(output,"<","&lt;")
    output = StringTools::replace(output,">","&gt;")
    output = StringTools::replace(output,"\"","&quot;")
    output = StringTools::replace(output,"&apos;","\'")
    return output
end

.htmlUnescape(input) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 139

def self.htmlUnescape(input)
    output = ""
    start = 0
    position = input::indexOf('&',start)
    while position != -1
        output += Std::substr(input,start,position - start)
        if input::charAt(position + 1) == '#'
            startPosition = position + 2
            endPosition = input::indexOf(';',startPosition)
            if endPosition != -1
                number = Std::substr(input,startPosition,endPosition - startPosition)
                if StringTools::startsWith(number,"x")
                    number = "0" + number
                end
                charCode = Std::parseInt(number)
                output += Utf8::uchr(charCode)
                start = endPosition + 1
            else 
                output += '&'
                start = position + 1
            end
        else 
            output += '&'
            start = position + 1
        end
        position = input::indexOf('&',start)
    end
    output += Std::substr(input,start,input::length() - start)
    output = StringTools::replace(output,"&lt;","<")
    output = StringTools::replace(output,"&gt;",">")
    output = StringTools::replace(output,"&quot;","\"")
    output = StringTools::replace(output,"&apos;","\'")
    output = StringTools::replace(output,"&amp;","&")
    return output
end

.importXml(elem, model) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 501

def self.importXml(elem, model)
    n = nil
    if elem::nodeType == Xml::Element
        n = model::createElement_(elem::nodeName)
        keys = elem::attributes()
        while keys::hasNext()
            key = keys::next()
            n::set(key,elem::get(key))
        end
        children = elem::iterator()
        while children::hasNext()
            n::addChild(WXmlUtils.importXml(children::next(),model))
        end
    else 
        if elem::nodeType == Xml::Document
            n = WXmlUtils.importXml(elem::firstElement(),model)
        else 
            if elem::nodeType == Xml::CData
                n = model::createCData_(elem::getNodeValue_())
            else 
                if elem::nodeType == Xml::PCData
                    n = model::createPCData_(elem::getNodeValue_())
                else 
                    if elem::nodeType == Xml::Comment
                        n = model::createComment_(elem::getNodeValue_())
                    else 
                        raise Exception,"Unsupported node type: " + elem::nodeType.to_s
                    end
                end
            end
        end
    end
    return n
end

.importXmlNamespace(elem, model, customNamespace, prefixAttributes) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 560

def self.importXmlNamespace(elem, model, customNamespace, prefixAttributes)
    n = nil
    if elem::nodeType == Xml::Element
        n = model::createElement_((customNamespace + ":") + elem::nodeName.to_s)
        keys = elem::attributes()
        while keys::hasNext()
            key = keys::next()
            keyNamespaced = key
            if (prefixAttributes && (key::indexOf(":") == -1)) && (key::indexOf("xmlns") == -1)
                keyNamespaced = (customNamespace + ":") + key
            end
            n::set(keyNamespaced,elem::get(key))
        end
        children = elem::iterator()
        while children::hasNext()
            n::addChild(WXmlUtils.importXmlNamespace(children::next(),model,customNamespace,prefixAttributes))
        end
    else 
        if elem::nodeType == Xml::Document
            n = WXmlUtils.importXmlNamespace(elem::firstElement(),model,customNamespace,prefixAttributes)
        else 
            if elem::nodeType == Xml::CData
                n = model::createCData_(elem::getNodeValue_())
            else 
                if elem::nodeType == Xml::PCData
                    n = model::createPCData_(elem::getNodeValue_())
                else 
                    raise Exception,"Unsupported node type: " + elem::nodeType.to_s
                end
            end
        end
    end
    return n
end

.importXmlWithoutChildren(elem, model) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 535

def self.importXmlWithoutChildren(elem, model)
    n = nil
    if elem::nodeType == Xml::Element
        n = model::createElement_(elem::nodeName)
        keys = elem::attributes()
        while keys::hasNext()
            key = keys::next()
            n::set(key,elem::get(key))
        end
    else 
        if elem::nodeType == Xml::CData
            n = model::createCData_(elem::getNodeValue_())
        else 
            if elem::nodeType == Xml::PCData
                n = model::createPCData_(elem::getNodeValue_())
            else 
                raise Exception,"Unsupported node type: " + elem::nodeType.to_s
            end
        end
    end
    return n
end

.indentXml(xml, space) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 594

def self.indentXml(xml, space)
    depth = 0
    opentag = EReg.new("^<([\\w-_]+)[^>]*>$","")
    autotag = EReg.new("^<([\\w-_]+)[^>]*/>$","")
    closetag = EReg.new("^</([\\w-_]+)>$","")
    cdata = EReg.new("^<!\\[CDATA\\[[^\\]]*\\]\\]>$","")
    res = StringBuf.new()
    _end = 0
    while (_end < xml::length()) && ((start = xml::indexOf("<",_end)) != -1)
        text = start > _end
        if text
            res::add(Std::substr(xml,_end,start - _end))
        end
        _end = xml::indexOf(">",start) + 1
        aux = Std::substr(xml,start,_end - start)
        if autotag::match(aux)
            res::add("\n")
            for i in 0..depth - 1
                res::add(space)
                i+=1
            end
            res::add(aux)
        else 
            if opentag::match(aux)
                res::add("\n")
                for i in 0..depth - 1
                    res::add(space)
                    i+=1
                end
                res::add(aux)
                depth+=1
            else 
                if closetag::match(aux)
                    depth-=1
                    if !text
                        res::add("\n")
                        for i in 0..depth - 1
                            res::add(space)
                            i+=1
                        end
                    end
                    res::add(aux)
                else 
                    if cdata::match(aux)
                        res::add(aux)
                    else 
                        Std::trace((("WARNING! malformed XML at character " + _end.to_s) + ":") + xml)
                        res::add(aux)
                    end
                end
            end
        end
    end
    return StringTools::trim(res::toString())
end

.initEntitiesObject



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 389

def self.initEntities()
    if @@entities == nil
        e = WEntities::MATHML_ENTITIES
        @@entities = Hash.new()
        start = 0
        while (mid = e::indexOf("@",start)) != -1
            name = Std::substr(e,start,mid - start)
            mid+=1
            start = e::indexOf("@",mid)
            if start == -1
                break
            end
            value = Std::substr(e,mid,start - mid)
            num = Std::parseInt("0x" + value)
            @@entities::set(name,"" + num.to_s)
            start+=1
        end
    end
end

.isHexDigit(c) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 369

def self.isHexDigit(c)
    if (c >= 48) && (c <= 57)
        return true
    end
    if (c >= 65) && (c <= 70)
        return true
    end
    if (c >= 97) && (c <= 102)
        return true
    end
    return false
end

.isNameChar(c) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 357

def self.isNameChar(c)
    if WXmlUtils.isNameStart(c)
        return true
    end
    if (48 <= c) && (c <= 57)
        return true
    end
    if (c == 46) || (c == 45)
        return true
    end
    return false
end

.isNameStart(c) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 345

def self.isNameStart(c)
    if (65 <= c) && (c <= 90)
        return true
    end
    if (97 <= c) && (c <= 122)
        return true
    end
    if (c == 95) || (c == 58)
        return true
    end
    return false
end

.isXmlEntity(ent) ⇒ Object



649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 649

def self.isXmlEntity(ent)
    if Std::charCodeAt(ent,0) == 35
        if Std::charCodeAt(ent,1) == 120
            c = Std::parseInt("0x" + Std::substr(ent,2).to_s)
        else 
            c = Std::parseInt(Std::substr(ent,1))
        end
        return (((((c == 34) || (c == 38)) || (c == 39)) || (c == 60)) || (c == 62))
    else 
        return (((((ent == "amp") || (ent == "lt")) || (ent == "gt")) || (ent == "quot")) || (ent == "apos"))
    end
end

.nonAsciiToEntities(s) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 320

def self.nonAsciiToEntities(s)
    sb = StringBuf.new()
    i = 0
    n = s::length()
    while i < n
        c = WXmlUtils.getUtf8Char(s,i)
        if c > 127
            hex = WInteger::toHex(c,5)
            j = 0
            while j < hex::length()
                if !((Std::substr(hex,j,1) == "0"))
                    hex = Std::substr(hex,j)
                    break
                end
                j+=1
            end
            sb::add(("&#x" + hex) + ";")
            i += (Utf8::uchr(c))::length()
        else 
            sb::addChar(c)
            i+=1
        end
    end
    return sb::toString()
end

.normalizeWhitespace(s) ⇒ Object



678
679
680
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 678

def self.normalizeWhitespace(s)
    return (s != nil) ? @@WHITESPACE_COLLAPSE_REGEX::replace(StringTools::trim(s)," ") : nil
end

.parseXML(xml) ⇒ Object



181
182
183
184
185
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 181

def self.parseXML(xml)
    xml = WXmlUtils.filterMathMLEntities(xml)
    x = Xml::parse(xml)
    return x
end

.removeChildren(element) ⇒ Object



462
463
464
465
466
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 462

def self.removeChildren(element)
    while element::firstChild() != nil
        element::removeChild(element::firstChild())
    end
end

.replaceChild(parent, childToReplace, replacement) ⇒ Object



491
492
493
494
495
496
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 491

def self.replaceChild(parent, childToReplace, replacement)
    childIndex = WXmlUtils.getChildPosition(parent,childToReplace)
    if childIndex != -1
        WXmlUtils.replaceIndexSub(parent,childIndex,childToReplace,replacement)
    end
end

.replaceIndexSub(parent, index, childToReplace, replacement) ⇒ Object



497
498
499
500
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 497

def self.replaceIndexSub(parent, index, childToReplace, replacement)
    parent::insertChild(replacement,index)
    parent::removeChild(childToReplace)
end

.resolveEntities(text) ⇒ Object



196
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 196

def self.resolveEntities(text)
    WXmlUtils.initEntities()
    sb = StringBuf.new()
    i = 0
    n = text::length()
    while i < n
        c = WXmlUtils.getUtf8Char(text,i)
        if ((c == 60) && ((i + 12) < n)) && (Std::charCodeAt(text,i + 1) == 33)
            if (Std::substr(text,i,9) == "<![CDATA[")
                e = text::indexOf("]]>",i)
                if e != -1
                    sb::add(Std::substr(text,i,(e - i) + 3))
                    i = e + 3
                        next
                end
            end
        end
        if c > 127
            special = Utf8::uchr(c)
            sb::add(special)
            i += special::length() - 1
        else 
            if c == 38
                i+=1
                c = Std::charCodeAt(text,i)
                if WXmlUtils.isNameStart(c)
                    name = StringBuf.new()
                    name::addChar(c)
                    i+=1
                    c = Std::charCodeAt(text,i)
                    while WXmlUtils.isNameChar(c)
                        name::addChar(c)
                        i+=1
                        c = Std::charCodeAt(text,i)
                    end
                    ent = name::toString()
                    if ((c == 59) && @@entities::exists(ent)) && !(WXmlUtils.isXmlEntity(ent))
                        val = @@entities::get(ent)
                        sb::add(Utf8::uchr(Std::parseInt(val)))
                    else 
                        sb::add("&")
                        sb::add(name)
                        sb::addChar(c)
                    end
                else 
                    if c == 35
                        i+=1
                        c = Std::charCodeAt(text,i)
                        if c == 120
                            hex = StringBuf.new()
                            i+=1
                            c = Std::charCodeAt(text,i)
                            while WXmlUtils.isHexDigit(c)
                                hex::addChar(c)
                                i+=1
                                c = Std::charCodeAt(text,i)
                            end
                            hent = hex::toString()
                            if (c == 59) && !WXmlUtils.isXmlEntity("#x" + hent)
                                dec = Std::parseInt("0x" + hent)
                                sb::add(Utf8::uchr(dec))
                            else 
                                sb::add("&#x")
                                sb::add(hent)
                                sb::addChar(c)
                            end
                        else 
                            if ((48 <= c) && (c <= 57))
                                dec = StringBuf.new()
                                while ((48 <= c) && (c <= 57))
                                    dec::addChar(c)
                                    i+=1
                                    c = Std::charCodeAt(text,i)
                                end
                                if (c == 59) && !WXmlUtils.isXmlEntity("#" + dec.to_s)
                                    sb::add(Utf8::uchr(Std::parseInt(dec::toString())))
                                else 
                                    sb::add("&#" + dec::toString().to_s)
                                    sb::addChar(c)
                                end
                            end
                        end
                    end
                end
            else 
                sb::addChar(c)
            end
        end
        i+=1
    end
    return sb::toString()
end

.resolveMathMLEntity(name) ⇒ Object



381
382
383
384
385
386
387
388
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 381

def self.resolveMathMLEntity(name)
    WXmlUtils.initEntities()
    if @@entities::exists(name)
        code = @@entities::get(name)
        return Std::parseInt(code)
    end
    return -1
end

.safeParseXML(xml) ⇒ Object



186
187
188
189
190
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 186

def self.safeParseXML(xml)
    begin
    return WXmlUtils.parseXML(xml)
    end
end

.serializeXML(xml) ⇒ Object



191
192
193
194
195
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 191

def self.serializeXML(xml)
    s = xml::toString()
    s = WXmlUtils.filterMathMLEntities(s)
    return s
end

.setAttribute(node, name, value) ⇒ Object



101
102
103
104
105
106
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 101

def self.setAttribute(node, name, value)
    if (value != nil) && PlatformSettings::PARSE_XML_ENTITIES
        value = WXmlUtils::htmlEscape(value)
    end
    node::set(name,value)
end

.setText(xml, text) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 430

def self.setText(xml, text)
    if xml::nodeType != Xml::Element
        return 
    end
    it = xml::iterator()
    if it::hasNext()
        child = it::next()
        if child::nodeType == Xml::PCData
            xml::removeChild(child)
        end
    end
    xml::addChild(xml::createPCData_(text))
end

.WHITESPACE_COLLAPSE_REGEXObject



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

def self.WHITESPACE_COLLAPSE_REGEX
    @@WHITESPACE_COLLAPSE_REGEX
end

.WHITESPACE_COLLAPSE_REGEX=(WHITESPACE_COLLAPSE_REGEX) ⇒ Object



15
16
17
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 15

def self.WHITESPACE_COLLAPSE_REGEX=(WHITESPACE_COLLAPSE_REGEX)
    @@WHITESPACE_COLLAPSE_REGEX = WHITESPACE_COLLAPSE_REGEX
end