Class: WirisPlugin::WXmlUtils

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

Constant Summary collapse

@@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

.copyXml(elem) ⇒ Object



377
378
379
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 377

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

.copyXmlNamespace(elem, customNamespace) ⇒ Object



410
411
412
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 410

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

.createPCData(node, text) ⇒ Object



88
89
90
91
92
93
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 88

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

.entitiesObject



138
139
140
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 138

def self.entities
  @@entities
end

.entities=(entities) ⇒ Object



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

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

.filterMathMLEntities(text) ⇒ Object



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

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

.getAttribute(node, attributeName) ⇒ Object



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

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

.getDocumentElement(doc) ⇒ Object



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

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



11
12
13
14
15
16
17
18
19
20
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 11

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 41

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 21

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 31

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

.getNodeValue(node) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 78

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



366
367
368
369
370
371
372
373
374
375
376
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 366

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

.getUtf8Char(text, i) ⇒ Object



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
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 251

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

.htmlEscape(input) ⇒ Object



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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 102

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



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 380

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(importXml(children::next(),model))
    end
  else 
    if elem::nodeType==Xml::Document
      n = 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 
          raise Exception,"Unsupported node type: "+elem::nodeType.to_s
        end
      end
    end
  end
  return n
end

.importXmlNamespace(elem, model, customNamespace) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 413

def self.importXmlNamespace(elem,model,customNamespace)
  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 key::indexOf(":")==-1
        keyNamespaced = (customNamespace+":")+key
      end
      n::set(keyNamespaced,elem::get(key))
    end
    children = elem::iterator()
    while children::hasNext()
      n::addChild(importXmlNamespace(children::next(),model,customNamespace))
    end
  else 
    if elem::nodeType==Xml::Document
      n = importXmlNamespace(elem::firstElement(),model,customNamespace)
    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

.indentXml(xml, space) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 447

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



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 347

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



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 327

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



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 315

def self.isNameChar(c)
  if 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



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 303

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



502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 502

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 278

def self.nonAsciiToEntities(s)
  sb = StringBuf.new()
  i = 0
  n = s::length()
  while i<n
    c = 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

.parseXML(xml) ⇒ Object



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

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

.resolveEntities(text) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 154

def self.resolveEntities(text)
  initEntities()
  sb = StringBuf.new()
  i = 0
  n = text::length()
  while i<n
    c = 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 isNameStart(c)
          name = StringBuf.new()
          name::addChar(c)
          i+=1
          c = Std::charCodeAt(text,i)
          while isNameChar(c)
            name::addChar(c)
            i+=1
            c = Std::charCodeAt(text,i)
          end
          ent = name::toString()
          if ((c==59)&&@@entities::exists(ent))&&!(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 isHexDigit(c)
                hex::addChar(c)
                i+=1
                c = Std::charCodeAt(text,i)
              end
              hent = hex::toString()
              if (c==59)&&!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)&&!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



339
340
341
342
343
344
345
346
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 339

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

.serializeXML(xml) ⇒ Object



149
150
151
152
153
# File 'lib/com/wiris/util/xml/WXmlUtils.rb', line 149

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

.setAttribute(node, name, value) ⇒ Object



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

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