Exemplo n.º 1
0
    def test_transform(self):

        tester.startTest("Checking for BIG5 codec")
        try:
            import codecs

            big5_decoder = codecs.getdecoder("big5")
        except LookupError:
            try:
                from encodings import big5
            except ImportError:
                tester.warning(
                    "No BIG5 encoding support for case 1.  You can install \n"
                    "BIG5 by downloading and installing ChineseCodes from\n"
                    "ftp://python-codecs.sourceforge.net/pub/python-codecs/"
                )
                tester.testDone()
                return
            else:
                big5_decode = big5.decode
        else:
            big5_decode = lambda s: big5_decoder(s)[0]
        tester.testDone()

        b5 = big5_decode(self.source)
        utf8 = b5.encode("utf-8")

        from amara.xslt import transform

        io = cStringIO.StringIO()
        result = transform(utf8, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Exemplo n.º 2
0
    def test_transform(self):
        SIZE = 10

        import sys
        from amara.xslt import transform

        #Create the matrix to be transposed
        from Ft.Xml.Domlette import implementation
        doc = implementation.createDocument(None, 'table', None)
        counter = 1
        for row in range(SIZE):
            row_elem = doc.createElementNS(None, 'row')
            doc.documentElement.appendChild(row_elem)
            for col in range(SIZE):
                col_elem = doc.createElementNS(None, 'column')
                row_elem.appendChild(col_elem)
                content = doc.createTextNode(str(counter))
                col_elem.appendChild(content)
                counter = counter + 1

        stream = cStringIO.StringIO()
        from Ft.Xml.Domlette import Print
        Print(doc, stream)

        self.source = stringsource(stream.getvalue())
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Exemplo n.º 3
0
    def test_transform(self):

        tester.startTest("Checking for BIG5 codec")
        try:
            import codecs
            big5_decoder = codecs.getdecoder('big5')
        except LookupError:
            try:
                from encodings import big5
            except ImportError:
                tester.warning(
                    "No BIG5 encoding support for case 1.  You can install \n"
                    "BIG5 by downloading and installing ChineseCodes from\n"
                    "ftp://python-codecs.sourceforge.net/pub/python-codecs/")
                tester.testDone()
                return
            else:
                big5_decode = big5.decode
        else:
            big5_decode = lambda s: big5_decoder(s)[0]
        tester.testDone()

        b5 = big5_decode(self.source)
        utf8 = b5.encode("utf-8")

        from amara.xslt import transform
        io = cStringIO.StringIO()
        result = transform(utf8, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Exemplo n.º 4
0
    def test_transform(self):
        SIZE=10
        
        import sys
        from amara.xslt import transform

        #Create the matrix to be transposed
        from Ft.Xml.Domlette import implementation
        doc = implementation.createDocument(None, 'table', None)
        counter = 1
        for row in range(SIZE):
            row_elem = doc.createElementNS(None, 'row')
            doc.documentElement.appendChild(row_elem)
            for col in range(SIZE):
                col_elem = doc.createElementNS(None, 'column')
                row_elem.appendChild(col_elem)
                content = doc.createTextNode(str(counter))
                col_elem.appendChild(content)
                counter = counter + 1

        stream = cStringIO.StringIO()
        from Ft.Xml.Domlette import Print
        Print(doc,stream)

        self.source = stringsource(stream.getvalue())
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Exemplo n.º 5
0
 def test_transform(self):
     # Preliminary, populate file with unindented HTML
     from amara.xslt import transform
     io = cStringIO.StringIO()
     self.transform = stringsource(commontransform%(furi, "no"))
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
     open(fname, 'w').write(io.getvalue())
Exemplo n.º 6
0
 def test_transform(self):
     # Preliminary, populate file with unindented HTML
     from amara.xslt import transform
     io = cStringIO.StringIO()
     self.transform = stringsource(commontransform % (furi, "no"))
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
     open(fname, 'w').write(io.getvalue())
Exemplo n.º 7
0
 def test_transform(self):
     # Subsequent, read file and compare
     from amara.xslt import transform
     self.assert_(os.path.exists(fname))
     file = open(fname, 'r')
     fcontent = file.read()
     file.close()
     self.assert_(treecompare.html_compare(self.expected, fcontent))
     os.unlink(fname)
     
     # Re-populate file, with indented HTML
     io = cStringIO.StringIO()
     self.transform = commontransform%(furi, "yes")
     result = transform(stringsource(self.source), stringsource(self.transform), output=io)
     open(fname, 'w').write(io.getvalue())
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
Exemplo n.º 8
0
    def __init__(self,
                 location=None,
                 graph=None,
                 debug=False,
                 nsBindings = None,
                 owlEmbeddings = False):
        self.owlEmbeddings = owlEmbeddings
        self.nsBindings = nsBindings if nsBindings else {}
        self.location = location
        self.rules = {}
        self.debug = debug
        if graph:
            assert location is None,"Must supply one of graph or location"
            self.graph = graph
            if debug:
                print "RIF in RDF graph was provided"
        else:
            assert graph is None,"Must supply one of graph or location"
            if debug:
                print "RIF document URL provided ", location
            if self.location.find('http:')+1:
                req = urllib2.Request(self.location)

                ##From: http://www.diveintopython.org/http_web_services/redirects.html
                #points an 'opener' to the address to 'sniff' out final Location header
                opener = urllib2.build_opener(SmartRedirectHandler())
                f = opener.open(req)
                self.content = f.read()
            else:
                try:
                    self.content = urllib2.urlopen(self.location).read()
                except ValueError:
                    self.content = urllib2.urlopen(iri.os_path_to_uri(self.location)).read()
#                self.content = open(self.location).read()
            try:
                rdfContent = transform(self.content,inputsource(TRANSFORM_URI))
                self.graph = Graph().parse(StringIO(rdfContent))
                if debug:
                    print "Extracted rules from RIF XML format"
            except ValueError:
                try:
                    self.graph = Graph().parse(StringIO(self.content),format='xml')
                except:
                    self.graph = Graph().parse(StringIO(self.content),format='n3')
                if debug:
                    print "Extracted rules from RIF in RDF document"
        self.nsBindings.update(dict(self.graph.namespaces()))
Exemplo n.º 9
0
    def test_transform(self):
        # Subsequent, read file and compare
        from amara.xslt import transform
        self.assert_(os.path.exists(fname))
        file = open(fname, 'r')
        fcontent = file.read()
        file.close()
        self.assert_(treecompare.html_compare(self.expected, fcontent))
        os.unlink(fname)

        # Re-populate file, with indented HTML
        io = cStringIO.StringIO()
        self.transform = commontransform % (furi, "yes")
        result = transform(stringsource(self.source),
                           stringsource(self.transform),
                           output=io)
        open(fname, 'w').write(io.getvalue())
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
Exemplo n.º 10
0
Arquivo: xslt.py Projeto: dpla/akara
def akara_xslt(body, ctype, **params):
    '''
    @xslt - URL to the XSLT transform to be applied
    all other query parameters are passed ot the XSLT processor as top-level params
    
    Sample request:
    curl --request POST --data-binary "@foo.xml" --header "Content-Type: application/xml" "http://*****:*****@xslt=http://hg.akara.info/amara/trunk/raw-file/tip/demo/data/identity.xslt"
    '''
    if "@xslt" in params:
        akaraxslttransform = params["@xslt"]
    else:
        if not DEFAULT_TRANSFORM:
            raise ValueError('XSLT transform required')
        akaraxslttransform = DEFAULT_TRANSFORM
    restricted_resolver = irihelpers.resolver(authorizations=ALLOWED)
    #Using restricted_resolver should forbid Any URI access outside the specified "jails"
    #Including access through imports and includes
    body = inputsource(body, resolver=restricted_resolver)
    akaraxslttransform = inputsource(akaraxslttransform, resolver=restricted_resolver)
    result = transform(body, akaraxslttransform)

    response.add_header("Content-Type", result.parameters.media_type)
    return result 
Exemplo n.º 11
0
def akara_xslt(body, ctype, **params):
    '''
    @xslt - URL to the XSLT transform to be applied
    all other query parameters are passed ot the XSLT processor as top-level params
    
    Sample request:
    curl --request POST --data-binary "@foo.xml" --header "Content-Type: application/xml" "http://*****:*****@xslt=http://hg.akara.info/amara/trunk/raw-file/tip/demo/data/identity.xslt"
    '''
    if "@xslt" in params:
        akaraxslttransform = params["@xslt"]
    else:
        if not DEFAULT_TRANSFORM:
            raise ValueError('XSLT transform required')
        akaraxslttransform = DEFAULT_TRANSFORM
    restricted_resolver = irihelpers.resolver(authorizations=ALLOWED)
    #Using restricted_resolver should forbid Any URI access outside the specified "jails"
    #Including access through imports and includes
    body = inputsource(body, resolver=restricted_resolver)
    akaraxslttransform = inputsource(akaraxslttransform,
                                     resolver=restricted_resolver)
    result = transform(body, akaraxslttransform)

    response.add_header("Content-Type", result.parameters.media_type)
    return result
Exemplo n.º 12
0
 def test_transform_output(self):
     from amara.xslt import transform
     io = cStringIO.StringIO()
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.xml_compare(self.expected, io.getvalue()))
     return
Exemplo n.º 13
0
import sys
import os
import uuid
from amara.xslt import transform

start_pos = 1
params = {}
while (sys.argv[start_pos].find("-D") == 0):
    macro = sys.argv[start_pos][2:]
    key = macro.split("=")[0]
    value = ""
    if len(macro.split("=")) == 2:
        value = macro.split("=")[1]
    start_pos = start_pos + 1
    params[key] = value

outputFile = sys.argv[start_pos]
sourceFile = sys.argv[start_pos + 1]
xslFile = sys.argv[start_pos + 2]

result = transform(sourceFile, xslFile, params)

o_flag = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
try:
    o_flag |= os.O_BINARY
except AttributeError:
    pass

os.write(os.open(outputFile, o_flag), result)
Exemplo n.º 14
0
 def test_transform_output(self):
     from amara.xslt import transform
     io = cStringIO.StringIO()
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.xml_compare(self.expected, io.getvalue()))
     return
Exemplo n.º 15
0
import sys
import os
import uuid
from amara.xslt import transform

start_pos = 1
params = {}
while( sys.argv[start_pos].find("-D")==0 ):
	macro = sys.argv[start_pos][2:]
	key = macro.split("=")[0]
	value = ""
	if len(macro.split("="))==2:
		value = macro.split("=")[1]
	start_pos=start_pos+1
	params[key] = value

outputFile = sys.argv[start_pos]
sourceFile = sys.argv[start_pos+1]
xslFile = sys.argv[start_pos+2]

result = transform(sourceFile, xslFile, params)

o_flag = os.O_WRONLY|os.O_CREAT|os.O_TRUNC
try:
	o_flag |= os.O_BINARY
except AttributeError:
	pass
	
os.write(os.open(outputFile,o_flag),result)