Commit a714b001 authored by Nicolas Delaby's avatar Nicolas Delaby

Initial release of xupdate_processor implemented in SAX with help of lxml.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils/xupdate_processor@31007 20353a03-c40f-0410-a6d1-a30d3c3de9de
parents
[buildout]
installed_develop_eggs = /home/nicolas/my_libs/xupdate_processor/develop-eggs/xupdate-processor.egg-link
parts = test
[test]
__buildout_installed__ = /home/nicolas/my_libs/xupdate_processor/parts/test
/home/nicolas/my_libs/xupdate_processor/bin/test
__buildout_signature__ = zc.recipe.testrunner-1.2.0-py2.4.egg zc.recipe.egg-1.2.2-py2.4.egg setuptools-0.6c9-py2.4.egg zope.testing-3.8.3-py2.4.egg zc.buildout-1.2.1-py2.4.egg zope.interface-3.5.2-py2.4-linux-i686.egg zope.exceptions-3.5.2-py2.4.egg
_b = /home/nicolas/my_libs/xupdate_processor/bin
_d = /home/nicolas/my_libs/xupdate_processor/develop-eggs
_e = /home/nicolas/.eggs
bin-directory = /home/nicolas/my_libs/xupdate_processor/bin
develop-eggs-directory = /home/nicolas/my_libs/xupdate_processor/develop-eggs
eggs = xupdate_processor
eggs-directory = /home/nicolas/.eggs
executable = /usr/bin/python2.4
initialization = from shutil import copy2; from glob import glob; [copy2(f, '../../parts/test/%s' % f.split('/')[-1]) for f in glob('../../*.xml')]
location = /home/nicolas/my_libs/xupdate_processor/parts/test
recipe = zc.recipe.testrunner
script = /home/nicolas/my_libs/xupdate_processor/bin/test
[buildout]
installed_develop_eggs = /home/nicolas/my_libs/xupdate_processor/develop-eggs/xupdate-processor.egg-link
[buildout]
parts = test
This diff is collapsed.
[buildout]
develop = .
parts = test
[test]
recipe = zc.recipe.testrunner
eggs = xupdate_processor
initialization = from shutil import copy2; from glob import glob; [copy2(f, '../../parts/test/%s' % f.split('/')[-1]) for f in glob('../../*.xml')]
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" office:version="1.2"><office:scripts/><office:font-face-decls><style:font-face style:name="Liberation Serif" svg:font-family="&apos;Liberation Serif&apos;" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Liberation Sans" svg:font-family="&apos;Liberation Sans&apos;" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="DejaVu Sans" svg:font-family="&apos;DejaVu Sans&apos;" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles><style:style style:name="fr1" style:family="graphic" style:parent-style-name="Graphics"><style:graphic-properties style:horizontal-pos="center" style:horizontal-rel="paragraph" style:mirror="none" fo:clip="rect(0cm, 0cm, 0cm, 0cm)" draw:luminance="0%" draw:contrast="0%" draw:red="0%" draw:green="0%" draw:blue="0%" draw:gamma="100%" draw:color-inversion="false" draw:image-opacity="100%" draw:color-mode="standard"/></style:style></office:automatic-styles><office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls><text:p text:style-name="Standard">Hello World !</text:p><text:p text:style-name="Standard"/><text:p text:style-name="Standard">XUProc is a wonderfull Tool</text:p><text:p text:style-name="Standard"><draw:frame draw:style-name="fr1" draw:name="images1" text:anchor-type="paragraph" svg:width="6.011cm" svg:height="5.306cm" draw:z-index="0"><draw:image xlink:href="Pictures/10000201000000D5000000BCBE47EB65.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame></text:p></office:text></office:body></office:document-content>
\ No newline at end of file
This diff is collapsed.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(name="xupdate_processor",
version=0.1,
description="XUpdate Processor",
author="Nicolas DELABY",
author_email="nicolas@nexedi.com",
url="http://nexedi.com",
license="GPL",
packages=find_packages(),
package_data={'doc_test': ['*.xml',],
'': ['DOCTEST',]},
scripts=["xuproc"],
install_requires=['zope.interface', 'lxml', 'erp5diff >= 0.7'],
classifiers=['License :: OSI Approved :: GNU General Public License (GPL)',
'Operating System :: OS Independent',
'Topic :: Text Processing :: Markup :: XML',
'Topic :: Utilities'],
include_package_data=True,
zip_safe=False,
)
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" office:version="1.2"><office:scripts/><office:font-face-decls><style:font-face style:name="Liberation Serif" svg:font-family="&apos;Liberation Serif&apos;" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Liberation Sans" svg:font-family="&apos;Liberation Sans&apos;" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="DejaVu Sans" svg:font-family="&apos;DejaVu Sans&apos;" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles/><office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls><text:p text:style-name="Standard">Hello World !</text:p></office:text></office:body></office:document-content>
\ No newline at end of file
This diff is collapsed.
# -*- coding: utf-8 -*-
from zope import interface
import zope.testing
import unittest
OPTIONFLAGS = (zope.testing.doctest.ELLIPSIS |
zope.testing.doctest.NORMALIZE_WHITESPACE)
def test_suite():
doctests = ('DOCTEST',)
globs = dict(interface=interface)
return unittest.TestSuite((
zope.testing.doctest.DocFileSuite(doctest,
optionflags=OPTIONFLAGS,
globs=globs,
) for doctest in doctests
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
\ No newline at end of file
<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0">
<xupdate:update xmlns:rpt="http://openoffice.org/2005/report" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:oooc="http://openoffice.org/2004/calc" select="/office:document-content/office:automatic-styles">
<style:style style:name="fr1" style:family="graphic" style:parent-style-name="Graphics">
<style:graphic-properties style:horizontal-pos="center" style:horizontal-rel="paragraph" style:mirror="none" fo:clip="rect(0cm, 0cm, 0cm, 0cm)" draw:luminance="0%" draw:contrast="0%" draw:red="0%" draw:green="0%" draw:blue="0%" draw:gamma="100%" draw:color-inversion="false" draw:image-opacity="100%" draw:color-mode="standard"/>
</style:style>
</xupdate:update>
<xupdate:insert-after select="/office:document-content/office:body/office:text/text:p[1]">
<xupdate:element name="text:p" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
<xupdate:attribute name="text:style-name" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Standard</xupdate:attribute>
</xupdate:element>
<xupdate:element name="text:p" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0"><xupdate:attribute name="text:style-name" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Standard</xupdate:attribute>XUProc is a wonderfull Tool</xupdate:element>
<xupdate:element name="text:p" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
<xupdate:attribute name="text:style-name" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Standard</xupdate:attribute>
<draw:frame xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" draw:style-name="fr1" draw:name="images1" text:anchor-type="paragraph" svg:width="6.011cm" svg:height="5.306cm" draw:z-index="0">
<draw:image xlink:href="Pictures/10000201000000D5000000BCBE47EB65.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>
</draw:frame>
</xupdate:element>
</xupdate:insert-after>
</xupdate:modifications>
<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0">
<xupdate:remove xmlns:rpt="http://openoffice.org/2005/report" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:oooc="http://openoffice.org/2004/calc" select="/office:document-content/office:body/office:text/text:list/text:list-item[1]"/>
<xupdate:remove select="/office:document-content/office:body/office:text/text:list/text:list-item[4]"/>
<xupdate:remove select="/office:document-content/office:body/office:text/text:list/text:list-item[5]"/>
<xupdate:insert-after select="/office:document-content/office:body/office:text/text:list/text:list-item[8]">
<xupdate:element name="text:list-item" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
<text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" text:style-name="P2">Item Four</text:p>
</xupdate:element>
<xupdate:element name="text:list-item" namespace="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
<text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" text:style-name="P2">Item five</text:p>
</xupdate:element>
</xupdate:insert-after>
</xupdate:modifications>
# -*- coding: utf-8 -*-
from xuproc import applyXUpdate
from lxml import etree
def main():
import sys
doc_xml_name = sys.argv[1]
xu_xml_name = sys.argv[2]
print etree.tostring(applyXUpdate(xml_xu_filename=xu_xml_name,
xml_doc_filename=doc_xml_name),
pretty_print=True)
\ No newline at end of file
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.sax
from lxml import etree
from cStringIO import StringIO
from content_handler import XUpdateHandler
def applyXUpdate(xml_xu_file=None,
xml_doc_file=None,
xml_xu_string=None,
xml_doc_string=None,
xml_xu_filename=None,
xml_doc_filename=None):
etree_parser = etree.XMLParser(remove_blank_text=True)
if xml_xu_file:
xml_xu = xml_xu_file
if xml_xu_string:
xml_xu = StringIO(xml_xu_string)
if xml_xu_filename:
xml_xu = xml_xu_filename
if xml_doc_file:
xml_doc = xml_doc_file
if xml_doc_string:
xml_doc = StringIO(xml_doc_string)
if xml_doc_filename:
xml_doc = xml_doc_filename
original_tree = etree.parse(xml_doc, etree_parser)
parser = xml.sax.make_parser()
parser.setFeature('http://xml.org/sax/features/namespaces', True)
parser.setFeature('http://xml.org/sax/features/namespace-prefixes', True)
parser.setContentHandler(XUpdateHandler(original_tree=original_tree))
parser.parse(xml_xu)
content_handler = parser.getContentHandler()
return content_handler.result_tree
if __name__ == '__main__':
import sys
doc_xml_name = sys.argv[1]
xu_xml_name = sys.argv[2]
print etree.tostring(applyXUpdate(xml_xu_filename=xu_xml_name, xml_doc_filename=doc_xml_name), pretty_print=True)
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
#
# Nicolas Delaby <nicolas@nexedi.com>
#
# Copyright (C) 2009 Nexedi SA
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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.
#
##############################################################################
from xupdate_processor import main
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment