def toCnxml(self, strXml, objZipFile):
        # stow styles.xml in a tempfile
        styles_xml = objZipFile.read('styles.xml')
        (tmpsfile, tmpsname) = tempfile.mkstemp('.OOo')
        os.write(tmpsfile, styles_xml)
        os.close(tmpsfile)
        stylesPath=tmpsname

        #
        # not strictly required.  this xform removes empty paragraphs.
        # makes other oo 2 cnxml xforms possible.
        #
        try:
            strOOoXml = XMLService.transform(strXml, OO2OO_XSL, stylesPath=stylesPath)
            if len(strOOoXml) == 0:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to OOo XSL transform failed.");
                strOOoXml = strXml
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to OOo XSL transform failed.");
            strOOoXml = strXml

        # Clean up styles.xml tempfile
        os.remove(tmpsname)

        #
        # addSectionTags() calls the SAX parser.parse() which expects a file argument
        # thus we force the xml string into being a file object
        #
        try:
            strSectionedXml = addSectionTags(StringIO(strOOoXml))
            if len(strSectionedXml) > 0:
                bAddedSections = True
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add sections.");
                strSectionedXml = strOOoXml
                bAddedSections = False
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add sections.");
            strSectionedXml = strOOoXml
            bAddedSections = False

        #
        # add external MathML as child of <draw:object> via SAX parser.
        #
        try:
            strMathedXml = addMathML(StringIO(strSectionedXml), objZipFile)
            if len(strMathedXml) > 0:
                bAddedMath = True
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add MathML.");
                strMathedXml = strSectionedXml
                bAddedMath = False
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add MathML.");
            strMathedXml = strSectionedXml
            bAddedMath = False

        #
        # oo 2 cnxml via xsl transform.
        #
        try:
            strCnxml = XMLService.transform(strMathedXml, OO2CNXML_XSL)
            bTransformed = True
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to CNXML XSL transform failed.");
            # set strCnxml to invalid CNXML ...
            strCnxml = '<>'
            bTransformed = False

        #
        # Replace Word Symbol Font with correct entity
        #
        strCnxml = symbolReplace(strCnxml, UNICODE_DICTIONARY)

        #
        # Global id generation
        #
        strCnxml = autoIds(strCnxml, prefix='oo-')

        #
        # Error handling
        #
        errors = XMLService.validate(strCnxml)
        if errors:

            if bAddedSections or bAddedMath:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Invalid CNXML generated. Trying w/o sections and MathML. Errors were \n" + str([str(e) for e in errors]))

                try:
                    strCnxml = XMLService.transform(strXml, OO2CNXML_XSL)
                    strCnxml = autoIds(strCnxml, prefix='oo-')
                except:
                    zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to CNXML XSL transform failed again with the undoctored OOo Xml.");
                    strCnxml = '<>'

                errors = XMLService.validate(strCnxml)
                if errors:
                    zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Still...invalid CNXML. errors were \n" + str(errors))
                    raise OOoImportError, "Generated CNXML is invalid"
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Invalid CNXML generated. errors were \n" + str(errors))
                raise OOoImportError, "Generated CNXML is invalid"

        #
        # Tidy up the CNXML
        #
        docCnxmlClean = XMLService.transform(strCnxml, CNXMLTIDY_XSL)

        return str(docCnxmlClean)
示例#2
0
        strCnxml = strOutputCnxml
        #raise

    fileMassagedOutputXml = open(strOutputMassageOOoXmlFileBase + '.ided.xml',
                                 "w")
    fileMassagedOutputXml.write(str(strCnxml))
    fileMassagedOutputXml.close()

    fileMassagedOutputXml = open(
        strOutputMassageOOoXmlFileBase + '.ided.tidy.xml', "w")
    options = dict(input_xml=1, char_encoding='utf8', indent=1)
    strTidy = tidy.parseString(strCnxml, **options)
    fileMassagedOutputXml.write(str(strTidy))
    fileMassagedOutputXml.close()

    errors = XMLService.validate(strCnxml)
    if errors:
        print "validation errors:\n" + str(errors)
    else:
        print "no validation errors."

    #raise Heck

    #
    # special carve out from OOoImport.py to facilitate
    #
    strCnxml = oo_to_cnxml().toCnxml(strInputOOoXml, objZipFile)
    errors = XMLService.validate(strCnxml)
    if errors:
        print "**** oo_to_cnxml().toCnxml() failed. does not return a valid string. Errors were \n" + str(
            [str(e) for e in errors])
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=content=None
##title=
from Products.RhaptosModuleEditor import MODULE_EIP_XSL
from Products.CNXMLDocument.XMLService import XMLError
from Products.CNXMLDocument import XMLService

request = context.REQUEST

if content is None:
    try:
        content = request['BODY']
    except KeyError:
        raise TypeError, "No content provided"

results = XMLService.validate(
    content,
    url="http://cnx.rice.edu/technology/cnxml/schema/rng/0.7/cnxml-fragment.rng"
)

if results:
    request.RESPONSE.setStatus(400, "Invalid CNXML")
    return results

else:
    return context.cnxml_transform(content, stylesheet=MODULE_EIP_XSL)
## Script (Python) "eip_transform"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=content=None
##title=
from Products.RhaptosModuleEditor import MODULE_EIP_XSL
from Products.CNXMLDocument.XMLService import XMLError
from Products.CNXMLDocument import XMLService

request = context.REQUEST

versioninfo = context.rmeVersionInfo()
cnxmlvers = versioninfo['cnxmlvers'] or '0.7'
if content is None:
    try:
        content = request['BODY']
    except KeyError:
        raise TypeError, "No content provided"

results = XMLService.validate(content, url="http://cnx.rice.edu/technology/cnxml/schema/rng/%s/cnxml-fragment.rng" % cnxmlvers)

if results:
    request.RESPONSE.setStatus(400, "Invalid CNXML")
    return results

else:
    return context.cnxml_transform(content, stylesheet=MODULE_EIP_XSL)
    def toCnxml(self, strXml, objZipFile):
        # stow styles.xml in a tempfile
        styles_xml = objZipFile.read('styles.xml')
        (tmpsfile, tmpsname) = tempfile.mkstemp('.OOo')
        os.write(tmpsfile, styles_xml)
        os.close(tmpsfile)
        stylesPath=tmpsname

        #
        # not strictly required.  this xform removes empty paragraphs.
        # makes other oo 2 cnxml xforms possible.
        #
        try:
            strOOoXml = XMLService.transform(strXml, OO2OO_XSL, stylesPath=stylesPath)
            if len(strOOoXml) == 0:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to OOo XSL transform failed.");
                strOOoXml = strXml
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to OOo XSL transform failed.");
            strOOoXml = strXml

        # Clean up styles.xml tempfile
        os.remove(tmpsname)

        #
        # addSectionTags() calls the SAX parser.parse() which expects a file argument
        # thus we force the xml string into being a file object
        #
        try:
            strSectionedXml = addSectionTags(StringIO(strOOoXml))
            if len(strSectionedXml) > 0:
                bAddedSections = True
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add sections.");
                strSectionedXml = strOOoXml
                bAddedSections = False
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add sections.");
            strSectionedXml = strOOoXml
            bAddedSections = False

        #
        # add external MathML as child of <draw:object> via SAX parser.
        #
        try:
            strMathedXml = addMathML(StringIO(strSectionedXml), objZipFile)
            if len(strMathedXml) > 0:
                bAddedMath = True
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add MathML.");
                strMathedXml = strSectionedXml
                bAddedMath = False
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Failed to add MathML.");
            strMathedXml = strSectionedXml
            bAddedMath = False

        #
        # oo 2 cnxml via xsl transform.
        #
        try:
            strCnxml = XMLService.transform(strMathedXml, OO2CNXML_XSL)
            bTransformed = True
        except:
            zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to CNXML XSL transform failed.");
            # set strCnxml to invalid CNXML ...
            strCnxml = '<>'
            bTransformed = False

        #
        # Replace Word Symbol Font with correct entity
        #
        strCnxml = symbolReplace(strCnxml, UNICODE_DICTIONARY)

        #
        # Global id generation
        #
        strCnxml = autoIds(strCnxml, prefix='oo-')

        #
        # Error handling
        #
        errors = XMLService.validate(strCnxml)
        if errors:

            if bAddedSections or bAddedMath:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Invalid CNXML generated. Trying w/o sections and MathML. Errors were \n" + str([str(e) for e in errors]))

                try:
                    strCnxml = XMLService.transform(strXml, OO2CNXML_XSL)
                    strCnxml = autoIds(strCnxml, prefix='oo-')
                except:
                    zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "OOo to CNXML XSL transform failed again with the undoctored OOo Xml.");
                    strCnxml = '<>'

                errors = XMLService.validate(strCnxml)
                if errors:
                    zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Still...invalid CNXML. errors were \n" + str(errors))
                    raise OOoImportError, "Generated CNXML is invalid"
            else:
                zLOG.LOG("OOo2CNXML Transform", zLOG.INFO, "Invalid CNXML generated. errors were \n" + str(errors))
                raise OOoImportError, "Generated CNXML is invalid"

        #
        # Tidy up the CNXML
        #
        docCnxmlClean = XMLService.transform(strCnxml, CNXMLTIDY_XSL)

        return str(docCnxmlClean)
     except:
         print "**** failed in adding @id XSL transform. raised exception. ignore and continue."
         strCnxml = strOutputCnxml
         #raise

     fileMassagedOutputXml = open(strOutputMassageOOoXmlFileBase + '.ided.xml', "w")
     fileMassagedOutputXml.write(str(strCnxml))
     fileMassagedOutputXml.close()

     fileMassagedOutputXml = open(strOutputMassageOOoXmlFileBase + '.ided.tidy.xml', "w")
     options = dict(input_xml=1, char_encoding='utf8', indent=1)
     strTidy = tidy.parseString(strCnxml,**options)
     fileMassagedOutputXml.write(str(strTidy))
     fileMassagedOutputXml.close()

     errors = XMLService.validate(strCnxml)
     if errors:
         print "validation errors:\n" + str(errors)
     else:
         print "no validation errors."

     #raise Heck

     #
     # special carve out from OOoImport.py to facilitate
     #
     strCnxml = oo_to_cnxml().toCnxml(strInputOOoXml, objZipFile)
     errors = XMLService.validate(strCnxml)
     if errors:
         print "**** oo_to_cnxml().toCnxml() failed. does not return a valid string. Errors were \n" + str([str(e) for e in errors])
     else: