Example #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
Example #3
0
def rendercnxml(source, prestyles=()):

    stylesheets = list(prestyles)
    stylesheets.append(CNXML_RENDER_XSL)

    params = {}
    params['wrapper'] = 0

    doc = XMLService.parseString(source)
    result = XMLService.xsltPipeline(doc, stylesheets, **params)
    return result
Example #4
0
 def abstract_text(self):
     """The rendered abstract/summary text of content, by itself, stripped of markup.
     """
     context = self.context
     source = getattr(context, 'getRawAbstract', None)
     source = source and source() or context.abstract
     if source:
         source = """<md:abstract xmlns="http://cnx.rice.edu/cnxml"
                                 xmlns:bib="http://bibtexml.sf.net/"
                                 xmlns:m="http://www.w3.org/1998/Math/MathML"
                                 xmlns:md="http://cnx.rice.edu/mdml"
                                 xmlns:q="http://cnx.rice.edu/qml/1.0">%s</md:abstract>""" % source
         doc = XMLService.parseString(source)
         result = XMLService.xsltPipeline(doc, [CNXML_SEARCHABLE_XSL])
         return result
     return ''
Example #5
0
if __name__ == "__main__":
    strZipFile = sys.argv[1]
    strInputOOoXmlFile = sys.argv[2]
    strOutputMassageOOoXmlFileBase = sys.argv[3]
    strOutputCnxmlFile = sys.argv[4]
    #print 'arg1 is \n' + sys.argv[1]
    #print 'arg2 is \n' + sys.argv[2]
    #print 'arg3 is \n' + sys.argv[3]
    #print 'arg4 is \n' + sys.argv[4]

    fileInputXml = open(strInputOOoXmlFile)
    strInputOOoXml = fileInputXml.read()
    fileInputXml.close()

    strOOoXml = strInputOOoXml
    doc = XMLService.parseString(strOOoXml)

    objZipFile = zipfile.ZipFile(strZipFile,
                                 'r')  # no 'rb' since 'b' => binary?

    #
    # Pass #1 - OOo Xml to OOo Xml xform - change one entry table & remove empty <text:p>
    #
    try:
        styles_xml = objZipFile.read('styles.xml')
        (tmpsfile, tmpsname) = tempfile.mkstemp('.OOo')
        os.write(tmpsfile, styles_xml)
        os.close(tmpsfile)
        stylesPath = tmpsname

        strOutputMassageOOoXml = XMLService.transform(strOOoXml,
Example #6
0
#CNXML_XSL = 'http://cnx.rice.edu/technology/cnxml/stylesheet/unibrowser.xsl'
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'])
#CNXML_XSL = 'http://cnx.rice.edu/technology/cnxml/stylesheet/unibrowser.xsl'
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'])
if __name__ == "__main__":
     strZipFile                     = sys.argv[1]
     strInputOOoXmlFile             = sys.argv[2]
     strOutputMassageOOoXmlFileBase = sys.argv[3]
     strOutputCnxmlFile             = sys.argv[4]
     #print 'arg1 is \n' + sys.argv[1]
     #print 'arg2 is \n' + sys.argv[2]
     #print 'arg3 is \n' + sys.argv[3]
     #print 'arg4 is \n' + sys.argv[4]

     fileInputXml = open(strInputOOoXmlFile)
     strInputOOoXml = fileInputXml.read()
     fileInputXml.close()

     strOOoXml = strInputOOoXml
     doc = XMLService.parseString(strOOoXml)

     objZipFile = zipfile.ZipFile(strZipFile, 'r') # no 'rb' since 'b' => binary?

     #
     # Pass #1 - OOo Xml to OOo Xml xform - change one entry table & remove empty <text:p>
     #
     try:
         styles_xml = objZipFile.read('styles.xml')
         (tmpsfile, tmpsname) = tempfile.mkstemp('.OOo')
         os.write(tmpsfile, styles_xml)
         os.close(tmpsfile)
         stylesPath=tmpsname

         strOutputMassageOOoXml = XMLService.transform(strOOoXml, OO2OO_XSL, stylesPath=stylesPath)
         if len(strOutputMassageOOoXml) > 0: