Ejemplo n.º 1
0
def get_default_cache():
    global default_cache

    if default_cache == None:
        default_cache = cacheRepository()
        # now we can activate the entity loader
        libxml2.setEntityLoader(libxml2_entity_loader)

    return default_cache
def _render(self, source_xml, content_type):
    """ Render document using libxslt.

    """
    global xslt_cache
    # handle URI's as if they were in the ZODB
    resolver = UriResolver(self).resolve
    libxml2.setEntityLoader(resolver)

    stylesheet = self()
    key = self.absolute_url(1)
    if cache_on and xslt_cache.has_key(key):
        style = xslt_cache[key]
    else:
        styledoc = libxml2.parseDoc(stylesheet)
        style = libxslt.parseStylesheetDoc(styledoc)
        if cache_on:
            xslt_cache[key] = style

    doc = libxml2.parseDoc(source_xml)
    result = style.applyStylesheet(doc, None)

    result_string = ''
    try:
        if not RENDERVIAFILE:
            result_string = style.saveResultToString(result)
        else:
            # this is how we have to do things prior to the
            # 1.0.21 release of libxslt, a pretty horrible hack
            fn = tempfile.mktemp()
            style.saveResultToFilename(fn, result, 0)

            f = file(fn)
            result_string = f.read()
            f.close()
            os.remove(fn)
    finally:
        if not cache_on:
            style.freeStylesheet()
        doc.freeDoc()
        result.freeDoc()

    return result_string
#
libxml2.substituteEntitiesDefault(1)
testsuite = libxml2.parseFile(CONF)


#
# Error and warnng callbacks
#
def callback(ctx, str):
    global log
    log.write("%s%s" % (ctx, str))


libxml2.registerErrorHandler(callback, "")

libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
    print "%s doesn't start with a testSuite element, aborting" % (CONF)
    sys.exit(1)
if quiet == 0:
    print "Running Relax NG testsuite"
handle_testSuite(root)

if quiet == 0 or nb_schemas_failed != 0:
    print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
        nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
if quiet == 0 or nb_instances_failed != 0:
    print "found %d test instances: %d success %d failures" % (
        nb_instances_tests, nb_instances_success, nb_instances_failed)
Ejemplo n.º 4
0
<struct>&simplestruct2.ent;</struct>
"""
expect="""10 struct 0 0
1 struct 0 0
1 descr 1 1
15 struct 0 0
"""
res=""
simplestruct2_ent="""<descr/>"""

def myResolver(URL, ID, ctxt):
    if URL == "simplestruct2.ent":
        return(str_io(simplestruct2_ent))
    return None

libxml2.setEntityLoader(myResolver)

input = libxml2.inputBuffer(str_io(s))
reader = input.newTextReader("test3")
reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
while reader.Read() == 1:
    res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
                                   reader.Depth(),reader.IsEmptyElement())

if res != expect:
    print("test3 failed: unexpected output")
    print(res)
    sys.exit(1)

#
# cleanup
Ejemplo n.º 5
0
#
# Parse the conf file
#
libxml2.substituteEntitiesDefault(1);
testsuite = libxml2.parseFile(CONF)

#
# Error and warnng callbacks
#
def callback(ctx, str):
    global log
    log.write("%s%s" % (ctx, str))

libxml2.registerErrorHandler(callback, "")

libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
    print "%s doesn't start with a testSuite element, aborting" % (CONF)
    sys.exit(1)
if quiet == 0:
    print "Running Relax NG testsuite"
handle_testSuite(root)

if quiet == 0 or nb_schemas_failed != 0:
    print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
      nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
if quiet == 0 or nb_instances_failed != 0:
    print "found %d test instances: %d success %d failures" % (
      nb_instances_tests, nb_instances_success, nb_instances_failed)
Ejemplo n.º 6
0
try:
    import StringIO
    str_io = StringIO.StringIO
except:
    import io
    str_io = io.StringIO

# Memory debug specific
libxml2.debugMemory(1)


def myResolver(URL, ID, ctxt):
    return (str_io("<foo/>"))


libxml2.setEntityLoader(myResolver)

doc = libxml2.parseFile("doesnotexist.xml")
root = doc.children
if root.name != "foo":
    print("root element name error")
    sys.exit(1)
doc.freeDoc()

i = 0
while i < 5000:
    doc = libxml2.parseFile("doesnotexist.xml")
    root = doc.children
    if root.name != "foo":
        print("root element name error")
        sys.exit(1)
Ejemplo n.º 7
0
def initialize():
    get_default_cache()
    libxml2.setEntityLoader(libxml2_entity_loader)
Ejemplo n.º 8
0
# logging init
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
log = logging.getLogger(__name__)


# libxml2 init
def _libxml2_error_handler(ctxt, err):
    log.error('libxml2: %s %s', ctxt, err)


def _libxml2_entity_loader(url, id, ctxt):
    return None


libxml2.registerErrorHandler(_libxml2_error_handler, None)
libxml2.setEntityLoader(_libxml2_entity_loader)


# custom exceptions
class MyXmlParserBaseExc(Exception):
    """Base parser exception"""
    pass


class MyXmlParserXSDInitExc(MyXmlParserBaseExc):
    """XSD initialization exception"""
    pass


class MyXmlParserXmlParseExc(MyXmlParserBaseExc):
    """XML file parse exception"""
Ejemplo n.º 9
0
def toString(
        xslPath, xslParams=None,
        xslBasePath=None,
        rootTagName='pyxslt',
        encoding='ASCII', prettyPrintXml=False,
        **elements):
    """
    Serialize a dictionary of Python objects to an XML document,
    transform that document using an XSL template, then return the
    textual version of the document.

    @param xslPath: The path to the XSL file that should be used to
        transform the serialized Python objects.
    @type xslPath: C{str} containing the path to an XSL stylesheet.
    @param xslParams: Parameters to pass to the XSL stylesheet.
    @type xslParams: C{dict}
    @param xslBasePath: The path to prepend to absolute stylesheet
        references.
    @type xslBasePath: C{str}
    @param rootTagName: The name of the XML tag that will enclose the
        serialized Python objects.
    @type rootTagName: C{str}
    @param encoding: The character encoding to use when outputting the
        final XML document.  Values such as C{UTF-8}, C{ISO-8859-1},
        C{ASCII}, etc. are appropriate.
    @type encoding: C{str}
    @param prettyPrintXml: C{True} to indent the final XML output,
        C{False} to return the bare XML without any extraneous spaces or
        linefeeds.
    @type prettyPrintXml: C{bool}
    @return: The given Python objects as a transformed XML document.
    @rtype: C{str}
    """

    # Initialize the entity loader if we were given a base path.
    if xslBasePath:
        entityLoader = EntityLoader(xslBasePath, os.getcwd())
        libxml2.setEntityLoader(entityLoader)

    # Load the stylesheet.
    try:
        # Parse the stylesheet.
        xslDoc = libxml2.parseFile(xslPath)
        stylesheet = libxslt.parseStylesheetDoc(xslDoc)
    except:
        # Free the XSL document.
        try: xslDoc.freeDoc()
        except: pass

        # Re-raise the exception.
        raise

    # Render the arguments with the stylesheet.
    try:
        # Create the Serializer.
        ser = serialize.Serializer(rootTagName=rootTagName)

        # Serialize the elements.
        ser.serialize(**elements)

        # Apply the stylesheet to the serialized arguments.
        out = stylesheet.applyStylesheet(ser.xmlDoc, xslParams)

        # Return the rendered content.
        return out.serialize(encoding=encoding, format=prettyPrintXml)
    finally:
        try: out.freeDoc()
        except: pass
        try: stylesheet.freeStylesheet()
        except: pass
        try: doc.freeDoc()
        except: pass
Ejemplo n.º 10
0
    # Turn the reStructuredText document tree into a reStructuredText
    # XML document.
    restXml = restxmldoc.RestXmlDocument(restDoc, smartPunctuation)


    # Use the document's stylesheet if the caller did not give us an
    # override stylesheet.
    if not xslPath:
        xslPath = restXml.xslTemplate

    # Initialize the entity loader if we were given a base path.
    if xslBasePath:
        entityLoader = loader.EntityLoader(
            xslBasePath, os.path.dirname(restPath))
        libxml2.setEntityLoader(entityLoader)

    # Load the XSL file.
    stylesheet = xslt.Stylesheet(xslPath)


    # Is this a multiple-instance document (multidoc)?  If so, we need
    # to process the file multiple times, one for each document
    # instance.  If not, just process the current document.
    resultDocuments = []
    if restDoc.settings.restxsl_multidoc:
        # Generate a document for each of the multidoc's children.
        for mdChildIndex, mdChild in enumerate(
                restXml.doc.xpathEval('//pyxslt[@multidoc="true"]/*')):
            # Get the name of the document using the multidoc XPATH
            # expression.