# -*- coding: iso-8859-1 -*- # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) # This file is part of MEDITE. # # MEDITE is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # MEDITE is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Foobar; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #---------------------------------------------------------------------------- # Nom: codageXml.py # Objet: fichier qui contient des methodes qui aide a la constructions # des fichiers xml #---------------------------------------------------------------------------- import Ft.Xml, Ft.Xml.Domlette # ligne ci-dessous nécessaire car sinon lors du freeze avec cxfreeze il a besoin # de cet import qui n'est pas ilmporté normalement import Ft.Xml.XInclude import Ft.Xml.XPath, Ft.Xml.XPath.ParsedPredicateList, Ft.Xml.XPath.ParsedAbbreviatedAbsoluteLocationPath import Ft.Xml.XPath.ParsedAbbreviatedRelativeLocationPath from xml.dom.minidom import * #from xml.dom.pulldom import * import sys ## astuce pour l'encoding du fichier xml def encodingXml(pChemin): f = open(pChemin) debut = f.read(23) debut = debut[0:20] + ' encoding="iso-8859-1" ' +debut[20:] # iso-8859-1 reste = f.read() ts = debut + reste f.close() f = open(pChemin,'w') f.write(ts) f.close() ## pass ## astuce pour l'encodage des noeuds des balises def encodingNode(pNode): if not isinstance(pNode,unicode): code = unicode(pNode, 'iso-8859-1') else: code = pNode return code ## Methode pour parser un fichier xml accentue def lireDocXml(pChemin): f = open(pChemin) text = f.read() f.close() # print text #textDoc = unicode(text,'iso-8859-1') ficXML = xml.dom.minidom.parseString(text) return ficXML # return xml.dom.pulldom.parse(pChemin) ## Methode pour parser un fichier xml accentue avec Ft.Xml def lireDocXmlFt(pChemin): f = open(pChemin) text = f.read() f.close() # print text #textDoc = unicode(text,'iso-8859-1') ficXML = Ft.Xml.Parse(text) return ficXML # return xml.dom.pulldom.parse(pChemin) ## Methode pour parser un fichier xml en ne lisant que l'arbre des versions def lireDocXmlVersionTree(pChemin): f = open(pChemin) text = f.read() f.close() # on coupe après arbre pour ne garder que les infos de l'arbre des versions pos = text.find('') text = text[:pos+8] + '' ficXML = xml.dom.minidom.parseString(text) return ficXML ## Methode pour ecrire un doc xml dans un fichier def ecrireDocXml(pChemin,pDocument): f = open(pChemin,'w') content = pDocument.toxml() #d = SAX2DOM(pDocument) #content = d.toxml() # if isinstance(pDocument,xml.dom.minidom.Document): # content = pDocument.toxml() # else: #pullDom # sys.stderr.write(str(type(pDocument))) # sys.stderr.write(str(pDocument)) # if pDocument == None: # sys.stderr.write("none") # sys.stderr.flush() # for event, node in pDocument: # sys.stderr.write("d\n") # sys.stderr.write(str(event)+" "+str(node.nodeName)+" d \n") # sys.stderr.flush() # if (event=='START_DOCUMENT'): # pDocument.expandNode(node) # content = node.toxml() # break # elif (event!='START_DOCUMENT' and node.nodeName!='root'): # pDocument.expandNode(node) # sys.stderr.write("d"+node.toxml()+"\n") # sys.stderr.flush() f.write(content.encode('iso-8859-1')) f.close() encodingXml(pChemin) ## Methode pour ecrire un doc xml dans un fichier avec Ft.Xml def ecrireDocXmlFt(pChemin,pDocument): f = open(pChemin,'w') #content = pDocument.toxml() #f.write(content.encode('iso-8859-1')) Ft.Xml.Domlette.Print(pDocument, stream=f, encoding='iso-8859-1') f.close() #encodingXml(pChemin)