File: input.py

package info (click to toggle)
xmldiff 0.6.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 464 kB
  • ctags: 272
  • sloc: python: 1,811; ansic: 135; sh: 125; xml: 124; makefile: 93
file content (101 lines) | stat: -rw-r--r-- 4,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
""" Provides functions for converting DOM tree or xml file in order to process
it with xmldiff functions. """ 
# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program 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.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
__revision__ = '$Id: input.py,v 1.14 2003/09/27 14:46:29 nico Exp $'

def tree_from_stream(stream, 
                     norm_sp=1, ext_ges=0, ext_pes=0, include_comment=1,
                     encoding='UTF-8', html=0):
    """
    create internal tree from xml stream (open file or IOString)
    if norm_sp = 1, normalize space and new line
    """
    from xml.sax import make_parser, SAXNotRecognizedException
    from xml.sax.handler import feature_namespaces, feature_external_ges, \
         feature_external_pes, property_lexical_handler
    from logilab.xmldiff.parser import SaxHandler
    handler = SaxHandler(norm_sp, include_comment, encoding)
    if html:
        parser = make_parser(["xml.sax.drivers2.drv_sgmlop_html"])
    else:
        parser = make_parser()
        # do not perform Namespace processing 
        parser.setFeature(feature_namespaces, 0)
    # do not include any external entities
    try:
        parser.setFeature(feature_external_ges, ext_ges)
        #xml.sax._exceptions.
    except SAXNotRecognizedException:
        print 'Unable to set feature external ges'
    try:
        parser.setFeature(feature_external_pes, ext_pes)
        #xml.sax._exceptions.
    except SAXNotRecognizedException:
        print 'Unable to set feature external pes'
    # add lexical handler for comments,  entities, dtd and cdata
    parser.setProperty(property_lexical_handler, handler)
    parser.setContentHandler(handler)
    parser.parse(stream)
    return handler.get_tree()


def tree_from_dom(root, ezs=0):
    """ create internal tree from DOM subtree """
    from xml.dom.ext.Dom2Sax import Dom2SaxParser 
    from xml.sax.handler import feature_namespaces, property_lexical_handler
    #from parser import DomParser
    parser = Dom2SaxParser()
    from logilab.xmldiff.parser import SaxHandler
    handler = SaxHandler(normalize_space=0, include_comment=1)
    # do not perform Namespace processing 
    parser.setFeature(feature_namespaces, 0)
    # add lexical handler for comments,  entities, dtd and cdata
    parser.setProperty(property_lexical_handler, handler)
    parser.setContentHandler(handler)
    parser.parse(root)
    return handler.get_tree()


if __name__ == '__main__':
    from xml.dom.ext import StripXml, PrettyPrint
    from xml.dom.ext.reader.Sax2 import Reader
    import sys
    reader = Reader()
    file = open(sys.argv[1],'r')
    fragment = reader.fromStream(file)
    d = StripXml(fragment)
    file.close()
    tree = tree_from_dom(d)
    file = open(sys.argv[2],'r')
    fragment = reader.fromStream(file)
    d = StripXml(fragment)
    file.close()
    tree2 = tree_from_dom(d)
    from logilab.xmldiff.objects import repr
    print 'Source tree', repr(tree)
    print 'Destination tree', repr(tree2)
    #from ezs import EzsCorrector
    #strategy = EzsCorrector()
    from logilab.xmldiff.fmes import FmesCorrector
    strategy = FmesCorrector(0.59, 0.5)
    #from ezs import process
    actions = strategy.process_trees(tree, tree2)
    from logilab.xmldiff.format import xupdate_dom
    PrettyPrint(
        xupdate_dom(
        reader.fromString('<root/>'),
        actions))