示例#1
0
    def importFile(self, text):
        """Convert from XMLSpy format"""

        # Fixup invalid PI prefix. Naughty Altova, PIs can't start with 'xml'
        text = text.replace('<?xmlspy', '<?authentic')
        try:
            doc = XMLService.parseString(text)
        except XMLService.XMLError:
            raise ValueError, "Could not perform XMLSpy/Authentic import: unable to parse file"

        # Get rid of Authentic goop:
        xslt = doc.children
        if xslt.type != 'pi':
            doc.freeDoc()
            raise ValueError, "Could not perform XMLSpy/Authentic import: missing stylesheet PI"
        xslt.unlinkNode()
        xslt.freeNode()

        pi = doc.children
        if pi.type != 'pi':
            doc.freeDoc()
            raise ValueError, "Could not perform XMLSpy/Authentic import: missing xmlspysps PI"
        pi.unlinkNode()
        pi.freeNode()

        rootNode = doc.children
        attr = rootNode.properties
        while attr:
            if attr.name == 'schemaLocation':
                attr.removeProp()
                break
            attr = attr.next

        schema_ns = rootNode.removeNsDef(
            "http://www.w3.org/2001/XMLSchema-instance")
        schema_ns.freeNode()

        ns = XMLService.listDocNamespaces(doc)
        # Get rid of namespaces we don't care about
        try:
            ns.remove('http://bibtexml.sf.net/')
            ns.remove('http://cnx.rice.edu/mdml/0.4')
        except ValueError:
            pass

        ns.sort()
        try:
            doctype = DTD[tuple(ns)]
        except KeyError:
            raise ValueError, "Cannot determine CNXML version from provided file"

        #return ns, doctype

        body = rootNode.serialize(encoding='utf-8')
        result = '\n'.join(
            ['<?xml version="1.0" encoding="utf-8"?>', doctype, body])

        doc.freeDoc()

        return result
    def importFile(self, text):
        """Convert from XMLSpy format"""

        # Fixup invalid PI prefix. Naughty Altova, PIs can't start with 'xml'
        text = text.replace('<?xmlspy', '<?authentic')
        try:
            doc = XMLService.parseString(text)
        except XMLService.XMLError:
            raise ValueError, "Could not perform XMLSpy/Authentic import: unable to parse file"

        # Get rid of Authentic goop:
        xslt = doc.children
        if xslt.type != 'pi':
            doc.freeDoc()
            raise ValueError, "Could not perform XMLSpy/Authentic import: missing stylesheet PI"
        xslt.unlinkNode()
        xslt.freeNode()

        pi = doc.children
        if pi.type != 'pi':
            doc.freeDoc()
            raise ValueError, "Could not perform XMLSpy/Authentic import: missing xmlspysps PI"
        pi.unlinkNode()
        pi.freeNode()

        rootNode = doc.children
        attr = rootNode.properties
        while attr:
            if attr.name == 'schemaLocation':
                attr.removeProp()
                break
            attr = attr.next
        
        schema_ns = rootNode.removeNsDef("http://www.w3.org/2001/XMLSchema-instance")
        schema_ns.freeNode()

        ns = XMLService.listDocNamespaces(doc)
        # Get rid of namespaces we don't care about
        try:
            ns.remove('http://bibtexml.sf.net/')
            ns.remove('http://cnx.rice.edu/mdml/0.4')
        except ValueError:
            pass

        ns.sort()
        try:
            doctype = DTD[tuple(ns)]
        except KeyError:
            raise ValueError, "Cannot determine CNXML version from provided file"

        #return ns, doctype
    

        body = rootNode.serialize(encoding='utf-8')
        result = '\n'.join(['<?xml version="1.0" encoding="utf-8"?>', doctype , body])

        doc.freeDoc()

        return result
示例#3
0
CNXML_XSL = CNXML_RENDER_XSL

if not stylesheet:
    stylesheet = CNXML_XSL
stylesheets = [stylesheet]

### for old CNXML (< 0.5) ###
doctype = getattr(context, 'doctype', None)
if doctype and doctype.find('0.5') == -1:
    from Products.CNXMLDocument import CNXML_UPGRADE_XSL
    stylesheets.insert(0, CNXML_UPGRADE_XSL)
### /upgrade ###

# Parse the source and grab the namespaces
doc = XMLService.parseString(source)
sourceNs = XMLService.listDocNamespaces(doc)

# Figure out our content types
has_math = MATHML_NS in sourceNs
params['doctype'], params['mimetype'], ns = context.content_type_decide(
    has_math=has_math)

# Transform source

result = XMLService.xsltPipeline(doc, stylesheets, **params)

# Set content-type
context.REQUEST.RESPONSE.setHeader('Content-Type',
                                   "%s; charset=utf-8" % params['mimetype'])

# Prepend doctype
CNXML_XSL = CNXML_RENDER_XSL

if not stylesheet:
    stylesheet = CNXML_XSL
stylesheets = [stylesheet]

### for old CNXML (< 0.5) ###
doctype = getattr(context, 'doctype', None)
if doctype and doctype.find('0.5') == -1:
    from Products.CNXMLDocument import CNXML_UPGRADE_XSL
    stylesheets.insert(0, CNXML_UPGRADE_XSL)
### /upgrade ###

# Parse the source and grab the namespaces
doc = XMLService.parseString(source)
sourceNs = XMLService.listDocNamespaces(doc)

# Figure out our content types
has_math = MATHML_NS in sourceNs
params['doctype'], params['mimetype'], ns = context.content_type_decide(has_math=has_math)

# Transform source

result = XMLService.xsltPipeline(doc, stylesheets, **params)

# Set content-type
context.REQUEST.RESPONSE.setHeader('Content-Type', "%s; charset=utf-8" % params['mimetype'])

# Prepend doctype
header = context.xmlheader(params['doctype'])