Class: WirisPlugin::XmlSerializer
- Inherits:
-
Object
- Object
- WirisPlugin::XmlSerializer
- Includes:
- Wiris
- Defined in:
- lib/com/wiris/util/xml/XmlSerializer.rb
Constant Summary collapse
- MODE_READ =
0- MODE_WRITE =
1- MODE_REGISTER =
2- MODE_CACHE =
3
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#cacheTagStackCount ⇒ Object
Returns the value of attribute cacheTagStackCount.
-
#child ⇒ Object
Returns the value of attribute child.
-
#children ⇒ Object
Returns the value of attribute children.
-
#childrenStack ⇒ Object
Returns the value of attribute childrenStack.
-
#childStack ⇒ Object
Returns the value of attribute childStack.
-
#currentTag ⇒ Object
Returns the value of attribute currentTag.
-
#element ⇒ Object
Returns the value of attribute element.
-
#elementStack ⇒ Object
Returns the value of attribute elementStack.
-
#ignore ⇒ Object
Returns the value of attribute ignore.
-
#ignoreTagStackCount ⇒ Object
Returns the value of attribute ignoreTagStackCount.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#rawxmls ⇒ Object
Returns the value of attribute rawxmls.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
- .booleanToString(b) ⇒ Object
- .compareStrings(a, b) ⇒ Object
- .getXmlTextContent(element) ⇒ Object
- .parseBoolean(s) ⇒ Object
Instance Method Summary collapse
- #arrayToString(a) ⇒ Object
- #attributeBoolean(name, value, def_) ⇒ Object
- #attributeFloat(name, value, def_) ⇒ Object
- #attributeInt(name, value, def_) ⇒ Object
- #attributeIntArray(name, value, def_) ⇒ Object
- #attributeString(name, value, def_) ⇒ Object
- #base64Content(data) ⇒ Object
- #beginCache ⇒ Object
- #beginTag(tag) ⇒ Object
- #beginTagIf(tag, current, desired) ⇒ Object
- #beginTagIfBool(tag, current, desired) ⇒ Object
- #booleanContent(content) ⇒ Object
- #cacheAttribute(name, value, def_) ⇒ Object
- #childInt(name, value, def_) ⇒ Object
- #childString(name, value, def_) ⇒ Object
- #currentChild ⇒ Object
- #endCache ⇒ Object
- #endTag ⇒ Object
- #floatContent(d) ⇒ Object
- #getMainTag(xml) ⇒ Object
- #getMode ⇒ Object
- #getTagName(elem) ⇒ Object
-
#initialize ⇒ XmlSerializer
constructor
A new instance of XmlSerializer.
- #isIgnoreTag(s) ⇒ Object
- #nextChild ⇒ Object
- #popState ⇒ Object
- #pushState ⇒ Object
- #rawXml(xml) ⇒ Object
- #read(xml) ⇒ Object
- #readNode ⇒ Object
- #readNodeModel(model) ⇒ Object
- #register(elem) ⇒ Object
- #serializeArray(array, tagName) ⇒ Object
- #serializeArrayName(array, tagName) ⇒ Object
- #serializeChild(s) ⇒ Object
- #serializeChildName(s, tagName) ⇒ Object
- #serializeXml(tag, elem) ⇒ Object
- #setCached(cache) ⇒ Object
- #setCurrentElement(element) ⇒ Object
- #setIgnoreTags(ignore) ⇒ Object
- #stringToArray(s) ⇒ Object
- #textContent(content) ⇒ Object
- #write(s) ⇒ Object
Constructor Details
#initialize ⇒ XmlSerializer
Returns a new instance of XmlSerializer.
25 26 27 28 29 30 31 32 33 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 25 def initialize() super() = Hash.new() @elementStack = Array.new() @childrenStack = Array.new() @childStack = Array.new() @cacheTagStackCount = 0 @ignoreTagStackCount = 0 end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
21 22 23 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 21 def cache @cache end |
#cacheTagStackCount ⇒ Object
Returns the value of attribute cacheTagStackCount.
22 23 24 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 22 def cacheTagStackCount @cacheTagStackCount end |
#child ⇒ Object
Returns the value of attribute child.
14 15 16 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 14 def child @child end |
#children ⇒ Object
Returns the value of attribute children.
13 14 15 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 13 def children @children end |
#childrenStack ⇒ Object
Returns the value of attribute childrenStack.
16 17 18 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 16 def childrenStack @childrenStack end |
#childStack ⇒ Object
Returns the value of attribute childStack.
17 18 19 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 17 def childStack @childStack end |
#currentTag ⇒ Object
Returns the value of attribute currentTag.
20 21 22 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 20 def currentTag @currentTag end |
#element ⇒ Object
Returns the value of attribute element.
12 13 14 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 12 def element @element end |
#elementStack ⇒ Object
Returns the value of attribute elementStack.
15 16 17 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 15 def elementStack @elementStack end |
#ignore ⇒ Object
Returns the value of attribute ignore.
23 24 25 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 23 def ignore @ignore end |
#ignoreTagStackCount ⇒ Object
Returns the value of attribute ignoreTagStackCount.
24 25 26 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 24 def ignoreTagStackCount @ignoreTagStackCount end |
#mode ⇒ Object
Returns the value of attribute mode.
11 12 13 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 11 def mode @mode end |
#rawxmls ⇒ Object
Returns the value of attribute rawxmls.
19 20 21 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 19 def rawxmls @rawxmls end |
#tags ⇒ Object
Returns the value of attribute tags.
18 19 20 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 18 def end |
Class Method Details
.booleanToString(b) ⇒ Object
390 391 392 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 390 def self.booleanToString(b) return b ? "true" : "false" end |
.compareStrings(a, b) ⇒ Object
471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 471 def self.compareStrings(a,b) an = a::length() bn = b::length() n = (an>bn) ? bn : an for i in 0..n-1 c = Std::charCodeAt(a,i)-Std::charCodeAt(b,i) if c!=0 return c end i+=1 end return a::length()-b::length() end |
.getXmlTextContent(element) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 251 def self.getXmlTextContent(element) if (element::nodeType==Xml::CData)||(element::nodeType==Xml::PCData) return WXmlUtils::getNodeValue(element) else if (element::nodeType==Xml::Document)||(element::nodeType==Xml::Element) sb = StringBuf.new() children = element::iterator() while children::hasNext() sb::add(getXmlTextContent(children::next())) end return sb::toString() else return "" end end end |
.parseBoolean(s) ⇒ Object
387 388 389 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 387 def self.parseBoolean(s) return (s::toLowerCase()=="true")||(s=="1") end |
Instance Method Details
#arrayToString(a) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 172 def arrayToString(a) if a==nil return nil end sb = StringBuf.new() for i in 0..a::length-1 if i!=0 sb::add(",") end sb::add(a[i].to_s+"") i+=1 end return sb::toString() end |
#attributeBoolean(name, value, def_) ⇒ Object
163 164 165 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 163 def attributeBoolean(name,value,def_) return XmlSerializer::parseBoolean(attributeString(name,XmlSerializer::booleanToString(value),XmlSerializer::booleanToString(def_))) end |
#attributeFloat(name, value, def_) ⇒ Object
198 199 200 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 198 def attributeFloat(name,value,def_) return Std::parseFloat(attributeString(name,""+value.to_s,""+def_.to_s)) end |
#attributeInt(name, value, def_) ⇒ Object
166 167 168 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 166 def attributeInt(name,value,def_) return Std::parseInt(attributeString(name,""+value.to_s,""+def_.to_s)) end |
#attributeIntArray(name, value, def_) ⇒ Object
169 170 171 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 169 def attributeIntArray(name,value,def_) return stringToArray(attributeString(name,arrayToString(value),arrayToString(def_))) end |
#attributeString(name, value, def_) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 136 def attributeString(name,value,def_) if @mode==XmlSerializer::MODE_READ value = WXmlUtils::getAttribute(@element,name) if value==nil value = def_ end else if @mode==XmlSerializer::MODE_WRITE if ((value!=nil)&&!((value==def_)))&&(@ignoreTagStackCount==0) WXmlUtils::setAttribute(@element,name,value) end end end return value end |
#base64Content(data) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 216 def base64Content(data) b64 = BaseCode.new(Bytes::ofString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")) if @mode==MODE_READ content = textContent(nil) data = b64::decodeBytes(Bytes::ofString(content)) else if @mode==MODE_WRITE textContent(b64::encodeBytes(data)::toString()) end end return data end |
#beginCache ⇒ Object
408 409 410 411 412 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 408 def beginCache() if @cache&&(@mode==XmlSerializer::MODE_WRITE) @mode = XmlSerializer::MODE_CACHE end end |
#beginTag(tag) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 83 def beginTag(tag) if @mode==MODE_READ if ((currentChild()!=nil)&&(currentChild()::nodeType==Xml::Element))&&(tag==currentChild()::nodeName) pushState() setCurrentElement(currentChild()) else return false end else if @mode==MODE_WRITE if isIgnoreTag(tag)||(@ignoreTagStackCount>0) @ignoreTagStackCount+=1 else child = @element::createElement_(tag) @element::addChild(child) @element = child end else if (@mode==MODE_REGISTER)&&(@currentTag==nil) @currentTag = tag else if @mode==MODE_CACHE @cacheTagStackCount+=1 end end end end return true end |
#beginTagIf(tag, current, desired) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 59 def beginTagIf(tag,current,desired) if @mode==MODE_READ if beginTag(tag) return desired end else if current==desired beginTag(tag) end end return current end |
#beginTagIfBool(tag, current, desired) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 71 def beginTagIfBool(tag,current,desired) if @mode==MODE_READ if beginTag(tag) return desired end else if current==desired beginTag(tag) end end return current end |
#booleanContent(content) ⇒ Object
245 246 247 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 245 def booleanContent(content) return XmlSerializer::parseBoolean(textContent(XmlSerializer::booleanToString(content))) end |
#cacheAttribute(name, value, def_) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 151 def cacheAttribute(name,value,def_) if @mode==XmlSerializer::MODE_WRITE if @cache value = attributeString(name,value,def_) @mode = XmlSerializer::MODE_CACHE @cacheTagStackCount = 0 end else value = attributeString(name,value,def_) end return value end |
#childInt(name, value, def_) ⇒ Object
402 403 404 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 402 def childInt(name,value,def_) return Std::parseInt(childString(name,""+value.to_s,""+def_.to_s)) end |
#childString(name, value, def_) ⇒ Object
393 394 395 396 397 398 399 400 401 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 393 def childString(name,value,def_) if !((@mode==MODE_WRITE)&&(((value==nil)&&(def_==nil))||((value!=nil)&&(value==def_)))) if beginTag(name) value = textContent(value) endTag() end end return value end |
#currentChild ⇒ Object
371 372 373 374 375 376 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 371 def currentChild() if (@child==nil)&&@children::hasNext() @child = @children::next() end return @child end |
#endCache ⇒ Object
413 414 415 416 417 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 413 def endCache() if @mode==XmlSerializer::MODE_CACHE @mode = XmlSerializer::MODE_WRITE end end |
#endTag ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 112 def endTag() if @mode==MODE_READ @element = @element::parent_() popState() nextChild() else if @mode==MODE_WRITE if @ignoreTagStackCount>0 @ignoreTagStackCount-=1 else @element = @element::parent_() end else if @mode==MODE_CACHE if @cacheTagStackCount>0 @cacheTagStackCount-=1 else @mode = MODE_WRITE @element = @element::parent_() end end end end end |
#floatContent(d) ⇒ Object
248 249 250 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 248 def floatContent(d) return Std::parseFloat(textContent(d.to_s+"")) end |
#getMainTag(xml) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 418 def getMainTag(xml) i = 0 loop do i = xml::indexOf("<",i) i+=1 c = Std::charCodeAt(xml,i) break if not (c!=33)&&(c!=63) end _end = [">", " ", "/"] min = 0 for j in 0.._end::length-1 n = xml::indexOf(_end[j]) if (n!=-1)&&(n<min) n = min end j+=1 end return Std::substr(xml,i,min) end |
#getMode ⇒ Object
34 35 36 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 34 def getMode() return @mode end |
#getTagName(elem) ⇒ Object
338 339 340 341 342 343 344 345 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 338 def getTagName(elem) mode = @mode @mode = XmlSerializer::MODE_REGISTER @currentTag = nil elem::onSerialize(self) @mode = mode return @currentTag end |
#isIgnoreTag(s) ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 460 def isIgnoreTag(s) if @ignore!=nil i = ignore::iterator() while i::hasNext() if (i::next()==s) return true end end end return false end |
#nextChild ⇒ Object
363 364 365 366 367 368 369 370 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 363 def nextChild() if @children::hasNext() @child = @children::next() else @child = nil end return @child end |
#popState ⇒ Object
382 383 384 385 386 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 382 def popState() @element = elementStack::pop() @children = childrenStack::pop() @child = childStack::pop() end |
#pushState ⇒ Object
377 378 379 380 381 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 377 def pushState() elementStack::push(@element) childrenStack::push(@children) childStack::push(@child) end |
#rawXml(xml) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 228 def rawXml(xml) if @mode==MODE_READ raise Exception,"Should not use rawXml() function on read operation!" else if @mode==MODE_WRITE raw = @element::createElement_("rawXml") raw::set("id",""+rawxmls::length().to_s) rawxmls::push(xml) element::addChild(raw) else if @mode==MODE_REGISTER @currentTag = getMainTag(xml) end end end return xml end |
#read(xml) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 37 def read(xml) document = Xml::parse(xml) setCurrentElement(document) @mode = XmlSerializer::MODE_READ return readNode() end |
#readNode ⇒ Object
346 347 348 349 350 351 352 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 346 def readNode() if !tags::exists(currentChild()::nodeName) raise Exception,("Tag "+currentChild()::nodeName.to_s)+" not registered." end model = tags::get(currentChild()::nodeName) return readNodeModel(model) end |
#readNodeModel(model) ⇒ Object
353 354 355 356 357 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 353 def readNodeModel(model) node = model::newInstance() node::onSerialize(self) return node end |
#register(elem) ⇒ Object
335 336 337 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 335 def register(elem) tags::set(getTagName(elem),elem) end |
#serializeArray(array, tagName) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 282 def serializeArray(array,tagName) if @mode==XmlSerializer::MODE_READ array = Array.new() child = currentChild() while (child!=nil)&&((tagName==nil)||(tagName==child::nodeName)) elem = (readNode()) array::push(elem) child = currentChild() end else if ((@mode==XmlSerializer::MODE_WRITE)&&(array!=nil))&&(array::length()>0) items = array::iterator() while items::hasNext() (items::next())::onSerialize(self) end end end return array end |
#serializeArrayName(array, tagName) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 314 def serializeArrayName(array,tagName) if @mode==MODE_READ if beginTag(tagName) array = serializeArray(array,nil) endTag() end else if ((@mode==MODE_WRITE)&&(array!=nil))&&(array::length()>0) element = @element @element = element::createElement_(tagName) element::addChild(@element) array = serializeArray(array,nil) @element = element else if @mode==MODE_REGISTER beginTag(tagName) end end end return array end |
#serializeChild(s) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 267 def serializeChild(s) if @mode==XmlSerializer::MODE_READ child = currentChild() if child!=nil s = (readNode()) else s = nil end else if (@mode==XmlSerializer::MODE_WRITE)&&(s!=nil) (s)::onSerialize(self) end end return s end |
#serializeChildName(s, tagName) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 301 def serializeChildName(s,tagName) if @mode==MODE_READ child = currentChild() if (child!=nil)&&(child::nodeName==tagName) s = serializeChild(s) end else if @mode==MODE_WRITE s = serializeChild(s) end end return s end |
#serializeXml(tag, elem) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 437 def serializeXml(tag,elem) if @mode==MODE_READ if (tag==nil)||((currentChild()!=nil)&&(currentChild()::nodeName==tag)) elem = currentChild() nextChild() end else if @mode==MODE_WRITE if (elem!=nil)&&(@ignoreTagStackCount==0) imported = WXmlUtils::importXml(elem,@element) element::addChild(imported) end else if @mode==MODE_REGISTER beginTag(tag) end end end return elem end |
#setCached(cache) ⇒ Object
405 406 407 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 405 def setCached(cache) @cache = cache end |
#setCurrentElement(element) ⇒ Object
358 359 360 361 362 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 358 def setCurrentElement(element) @element = element @children = element::elements() @child = nil end |
#setIgnoreTags(ignore) ⇒ Object
457 458 459 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 457 def setIgnoreTags(ignore) @ignore = ignore end |
#stringToArray(s) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 186 def stringToArray(s) if s==nil return nil end ss = s::split(",") a = [] for i in 0..ss::length-1 a[i] = Std::parseInt(ss[i]) i+=1 end return a end |
#textContent(content) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 201 def textContent(content) if @mode==MODE_READ content = XmlSerializer::getXmlTextContent(@element) else if ((@mode==MODE_WRITE)&&(content!=nil))&&(@ignoreTagStackCount==0) if (content::length()>100)||(StringTools::startsWith(content,"<")&&StringTools::endsWith(content,">")) textNode = @element::createCData_(content) else textNode = WXmlUtils::createPCData(@element,content) end element::addChild(textNode) end end return content end |
#write(s) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/com/wiris/util/xml/XmlSerializer.rb', line 43 def write(s) @mode = XmlSerializer::MODE_WRITE @element = Xml::createDocument() @rawxmls = Array.new() s::onSerialize(self) res = element::toString() for i in 0..rawxmls::length()-1 start = res::indexOf(("<rawXml id=\""+i.to_s)+"\"") if start!=-1 _end = res::indexOf(">",start) res = (Std::substr(res,0,start).to_s+rawxmls::_(i).to_s)+Std::substr(res,_end+1).to_s end i+=1 end return res end |