zip_resolver_example.py :  » Language-Interface » Pyana » Pyana-0.9.2 » Examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Pyana 
Pyana » Pyana 0.9.2 » Examples » zip_resolver_example.py
"""This module demonstates an entity resolver that makes it possible
to process XML files stored in zip archives.

File types other than zip archives are handled normally. Zip files
must end in a '.zip' extension. For archives containing more than
one file, the exact file in the zip archive must be specified.
This is done by appending a '?' and then the name of the file e.g.

<xsl:value-of select="document('arhive.zip?data.xml')"/>

This example would be better if the Python ZipFile object presented
a real stream-oriented interface.
"""

import Pyana
import sys
from StringIO import StringIO
from urlparse import urlparse
from urllib import urlopen
from zipfile import ZipFile
from re import match

class StringSource:
    def __init__(self, string):
        self.string = string
        
    def makeStream(self):
        return StringIO(self.string)

class ZipEntityResolver:
    def __init__(self, reportExceptions = 1):
        # Failed document() calls are not reported by Xalan,
        # so we'll use reportExceptions for debugging
        # purposes
        self.reportExceptions = reportExceptions
        
    def resolveEntity(self, public, system):
        try:
            # Xalan presents file URIs as file:///C:/file.txt
            # Python expects file URIs as file:///C|/file.txt
            filematch = match('file:///(\w):(.*)', system)
            if filematch:
                system = 'file:///%s|%s' % (
                            filematch.group(1),
                            filematch.group(2)
                        )

            # Lot of code duplication here. If I weren't the laziest
            # person in the world, I'd fix that.
            if system.lower().endswith('.zip'):
                zipRead = StringIO(urlopen(system).read())
                zipfile = ZipFile(zipRead)
                if len(zipfile.namelist()) != 1:
                    raise ValueError('Zip archive must contain a single '\
                                     'file or the query notation must '\
                                     'be used.')
                else:
                    return StringSource(
                            zipfile.read(zipfile.namelist()[0])
                        )
            else:
                ziploc = system.lower().find('.zip?')
                if ziploc != -1:
                    zipRead = StringIO(urlopen(system[:ziploc + 4]).read())
                    zipfile = ZipFile(zipRead)
                
                    return StringSource(
                            zipfile.read(system[ziploc + 5:])
                        )
                else:
                    return None
        except:
            if self.reportExceptions:
                import traceback
                traceback.print_exc()
            raise
        
def main(xml, xsl):
    t = Pyana.Transformer()
    resolver = ZipEntityResolver()
    xmlSource = resolver.resolveEntity('', xml) or Pyana.URI(xml)
    xslSource = resolver.resolveEntity('', xsl) or Pyana.URI(xsl)
    t.setEntityResolver(resolver)
    print t.transform2String(xmlSource, xslSource)

if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.