qsos-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qsos-commits] qsos/libs/ruby/qsos/lib/transformation XMLreade...


From: Romain PELISSE
Subject: [Qsos-commits] qsos/libs/ruby/qsos/lib/transformation XMLreade...
Date: Wed, 09 Aug 2006 14:52:50 +0000

CVSROOT:        /sources/qsos
Module name:    qsos
Changes by:     Romain PELISSE <rpelisse>       06/08/09 14:52:50

Added files:
        libs/ruby/qsos/lib/transformation: XMLreader.rb XMLwriter.rb 

Log message:
        XML serialization and deserialization object.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qsos/libs/ruby/qsos/lib/transformation/XMLreader.rb?cvsroot=qsos&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/qsos/libs/ruby/qsos/lib/transformation/XMLwriter.rb?cvsroot=qsos&rev=1.1

Patches:
Index: XMLreader.rb
===================================================================
RCS file: XMLreader.rb
diff -N XMLreader.rb
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ XMLreader.rb        9 Aug 2006 14:52:50 -0000       1.1
@@ -0,0 +1,92 @@
+require 'rexml/document'
+require 'model/element.rb'
+
+module QSOS
+class XMLreader
+
+       attr    :properties
+       
+       def initialize()
+               nbProps = 0
+               @properties = Array.new
+               @properties.push('comment')
+        @properties.push('score')
+        @properties.push('desc0')
+        @properties.push('desc1')        
+               @properties.push('desc2')
+       end
+       
+       def extractValue(node)
+               value = ""
+               node.texts.each do |text|
+                       value += text.to_s
+               end
+               return value
+#              set = node.find(node.name)
+#              if set != nil && set.length > 0
+#                      return set.to_a.first.child
+#              end
+#              return ""
+       end
+       
+       def extractAttribut(attribut)
+               if attribut != nil 
+                       if attribut.class != REXML::Attribute
+                               # TODO : throw an exception
+                               puts "error"
+                       else
+                               return attribut.value
+                       end
+               end
+               return nil
+       end
+       
+       #private:extractAttribut
+       
+    def makeElement(node)
+       if node.nil?
+               # TODO: throw an exception
+               return nil
+       end
+        # creating an empty element
+        item = QSOS::Element.new
+        # element meta should be the name of the tag
+        if node.name != nil && node.name != ""
+               item.meta = node.name
+        else
+               item.meta = 'unkown'    # this is an error, should be 
checked... TODO:exception ?
+        end
+        # getting the text value
+        value = ""
+        node.texts.each do |text|
+               value += text.to_s
+        end
+        item.text = value
+               # setting data from the node's attributes        
+               item.title = extractAttribut(node.attribute('title'))
+               item.name = extractAttribut(node.attribute('name'))
+               # setting data form childs nodes        
+               if node.has_elements?
+                       node.elements.each do |child|
+                               # the node is a property of this element
+                               if self.properties.include?(child.name)
+                                       
item.setMySelf(self.extractValue(child),child.name)
+                               else # the node is a children of this node
+                                       item.childs.push(makeElement(child))
+                               end
+                       end
+               end
+               return item
+       end
+       
+       def transformFrom(file_or_url)
+               if file_or_url.nil? 
+                       return nil # TODO : throw an exception
+               end
+               # TODO : Check for URL  
+               file = File.new(file_or_url)
+               doc = REXML::Document.new(file)
+       return self.makeElement(doc.root)
+       end
+end
+end

Index: XMLwriter.rb
===================================================================
RCS file: XMLwriter.rb
diff -N XMLwriter.rb
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ XMLwriter.rb        9 Aug 2006 14:52:50 -0000       1.1
@@ -0,0 +1,90 @@
+#!/usr/bin/ruby -w
+require 'rexml/document'
+require 'model/element.rb'
+require 'model/sheet.rb'
+
+module QSOS
+class XMLWriter
+
+       def initialize()
+       end
+  
+       def xmlize(sheet)
+               @doc = REXML::Document.new()
+               @doc.add_element( self.transformAnElement(sheet.root) )
+               return @doc
+       end
+   
+       def transformAnElement(item)
+       # Creating the node itself
+       if item == nil
+               return nil
+       end
+       # 
+       if item.meta != nil
+               node = REXML::Element.new(item.meta)
+           else
+               return nil      # TODO: this is an error, throw an exception ? 
+           end
+               # setting proper attribut for this element
+       if  item.name != nil && item.name != ""
+                       node.add_attribute('name',item.name)
+       end
+               
+       if  item.title != nil
+               node.add_attribute('title',item.title)
+               end
+               # Adding the element text 
+               if item.text != nil && item.text != ""
+               node.add_text(item.text)
+       end 
+               # TODO: use getMySelf ( to rename in getProperty) to improve 
the next operation
+       if item.desc != nil  
+               elchild = REXML::Element.new("desc")
+               elchild.add_text(item.desc())
+               node.add_element(elchild)
+       end
+               
+               if item.desc0 != nil
+               elchild = REXML::Element.new("desc0")
+                       elchild.add_text(item.desc0)
+                       node.add_element(elchild)
+               end
+       
+       if item.desc1 != nil
+               elchild = REXML::Element.new("desc1")
+               elchild.add_text(item.desc1)
+               node.add_element(elchild)
+               
+       end
+       
+       if item.desc2 != nil
+               elchild = REXML::Element.new("desc2")
+               elchild.add_text(item.desc2)
+                       node.add_element(elchild)
+       end
+       
+       if      item.comment != nil && item.comment != ""
+               elchild = REXML::Element.new("comment")
+               elchild.add_text(item.comment)
+               node.add_element(elchild)
+       end
+       
+       if item.score != nil && item.score != ""
+               elchild = REXML::Element.new("score")
+               elchild.add_text(item.score)
+                       node.add_element(elchild)
+       end     
+       
+       # Adding every child to the node
+       nbChild = -1
+       while nbChild < item.childs.length
+               child = self.transformAnElement( item.childs[nbChild += 1] )
+               if ! child.nil?         
+                       node.add_element(child )
+               end
+       end             
+       return node
+       end
+end
+end
\ No newline at end of file




reply via email to

[Prev in Thread] Current Thread [Next in Thread]