"Fossies" - the Fresh Open Source Software Archive

Member "radialnet/core/XMLHandler.py" (3 Mar 2008, 6254 Bytes) of package /linux/privat/old/radialnet-0.44.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # vim: set fileencoding=utf-8 :
    2 
    3 # Copyright (C) 2007 Insecure.Com LLC.
    4 #
    5 # Author: João Paulo de Souza Medeiros <ignotus21@gmail.com>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
   10 # (at your option) any later version.
   11 #
   12 # This program is distributed in the hope that it will be useful,
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 # GNU General Public License for more details.
   16 #
   17 # You should have received a copy of the GNU General Public License
   18 # along with this program; if not, write to the Free Software
   19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 import xml.sax
   22 import xml.sax.saxutils
   23 from xml.sax.xmlreader import AttributesImpl as Attributes
   24 
   25 
   26 
   27 def convert_to_utf8(text):
   28     """
   29     """
   30     return text.encode('utf8', 'replace')
   31 
   32 
   33 
   34 class XMLNode:
   35     """
   36     """
   37     def __init__(self, name):
   38         """
   39         """
   40         self.__name = name
   41         self.__text = ""
   42         self.__attrs = dict()
   43         self.__children = []
   44 
   45 
   46     def set_text(self, text):
   47         """
   48         """
   49         self.__text = text
   50 
   51 
   52     def get_text(self):
   53         """
   54         """
   55         return self.__text
   56 
   57 
   58     def set_name(self, name):
   59         """
   60         """
   61         self.__name = name
   62 
   63 
   64     def get_name(self):
   65         """
   66         """
   67         return self.__name
   68 
   69 
   70     def add_attr(self, key, value):
   71         """
   72         """
   73         self.__attrs[key] = value
   74 
   75 
   76     def add_child(self, child):
   77         """
   78         """
   79         self.__children.append(child)
   80 
   81 
   82     def get_keys(self):
   83         """
   84         """
   85         return self.__attrs.keys()
   86 
   87 
   88     def get_attr(self, attr):
   89         """
   90         """
   91         if self.__attrs.has_key(attr):
   92             return self.__attrs[attr]
   93 
   94         return None
   95 
   96 
   97     def get_attrs(self):
   98         """
   99         """
  100         return self.__attrs
  101 
  102 
  103     def get_children(self):
  104         """
  105         """
  106         return self.__children
  107 
  108 
  109     def query_children(self, name, attr, value, first=False, deep=False):
  110         """
  111         """
  112         result = []
  113 
  114         for child in self.__children:
  115 
  116             if child.get_name() == name:
  117 
  118                 if child.get_attrs().has_key(attr):
  119 
  120                     c_value = child.get_attr(attr)
  121 
  122                     if c_value == value or c_value == str(value):
  123                         result.append(child)
  124 
  125             if deep:
  126 
  127                 c_result = child.query_children(name, attr, value, first, deep)
  128 
  129                 if c_result != None:
  130 
  131                     if first:
  132                         return c_result
  133 
  134                     else:
  135                         result.extend(c_result)
  136 
  137         if first and len(result) > 0:
  138             return result[0]
  139 
  140         if first:
  141             return None
  142 
  143         return result
  144 
  145 
  146     def search_children(self, name, first=False, deep=False):
  147         """
  148         """
  149         result = []
  150 
  151         for child in self.__children:
  152 
  153             if child.get_name() == name:
  154 
  155                 result.append(child)
  156 
  157                 if first:
  158                     return result[0]
  159 
  160             if deep:
  161 
  162                 c_result = child.search_children(name, first, deep)
  163 
  164                 if c_result != None and c_result != []:
  165 
  166                     if first:
  167                         return c_result
  168 
  169                     else:
  170                         result.extend(c_result)
  171 
  172         if first:
  173             return None
  174 
  175         return result
  176 
  177 
  178 
  179 class XMLWriter(xml.sax.saxutils.XMLGenerator):
  180     """
  181     """
  182     def __init__(self, file, root=None, encoding="utf-8"):
  183         """
  184         """
  185         xml.sax.saxutils.XMLGenerator.__init__(self, file, encoding)
  186 
  187         self.__root = root
  188 
  189 
  190     def set_root(self, root):
  191         """
  192         """
  193         self.__root = root
  194 
  195 
  196     def write(self):
  197         """
  198         """
  199         self.startDocument()
  200         self.write_xml_node([self.__root])
  201         self.endDocument()
  202 
  203 
  204     def write_xml_node(self, root):
  205         """
  206         """
  207         for child in root:
  208 
  209             self.startElement(child.get_name(), Attributes(child.get_attrs()))
  210 
  211             if child.get_text() != "":
  212                 self.characters(child.get_text())
  213 
  214             self.write_xml_node(child.get_children())
  215 
  216             self.endElement(child.get_name())
  217 
  218 
  219 
  220 class XMLReader(xml.sax.ContentHandler):
  221     """
  222     """
  223     def __init__(self, file=None):
  224         """
  225         """
  226         self.__text = ""
  227         self.__status = []
  228 
  229         self.__file = file
  230         self.__root = None
  231 
  232         self.__parser = xml.sax.make_parser();
  233         self.__parser.setContentHandler(self);
  234 
  235 
  236     def set_file(self, file, root):
  237         """
  238         """
  239         self.__file = file
  240 
  241 
  242     def get_file(self):
  243         """
  244         """
  245         return self.__file
  246 
  247 
  248     def get_root(self):
  249         """
  250         """
  251         return self.__root
  252 
  253 
  254     def parse(self):
  255         """
  256         """
  257         if self.__file != None:
  258             self.__parser.parse(self.__file)
  259 
  260 
  261     def startDocument(self):
  262         """
  263         """
  264         pass
  265 
  266 
  267     def startElement(self, name, attrs):
  268         """
  269         """
  270         # create new node
  271         node = XMLNode(name)
  272 
  273         # putting attributes and values in node
  274         for attr in attrs.getNames():
  275             node.add_attr(attr, convert_to_utf8(attrs.get(attr).strip()))
  276 
  277         # who is my father?
  278         if len(self.__status) > 0:
  279             self.__status[-1].add_child(node)
  280 
  281         if self.__root == None:
  282             self.__root = node
  283 
  284         self.__status.append(node)
  285 
  286 
  287     def endElement(self, name):
  288         """
  289         """
  290         self.__status[-1].set_text(convert_to_utf8(self.__text.strip()))
  291 
  292         self.__text = ""
  293         self.__status.pop()
  294 
  295 
  296     def endDocument(self):
  297         """
  298         """
  299         pass
  300 
  301 
  302     def characters(self, text):
  303         """
  304         """
  305         self.__text += text
  306 
  307 
  308 
  309 if __name__ == "__main__":
  310 
  311     import sys
  312 
  313     reader = XMLReader(sys.argv[1])
  314     reader.parse()
  315 
  316     root = reader.get_root()
  317 
  318     writer = XMLWriter(open("test.xml", 'w'), root)
  319     writer.write()