Beispiel #1
0
def createHtmlElement(parentElement,
                      elementName,
                      attrMap={},
                      elementText=None):
    u"""createHtmlElement(Node, string, map, string) -> Node
    Creates a element given element name, attribute map and optional element node text.
    If the parentElement node is None, then element is under new document (zdom).
     """ #$NON-NLS-1$
    element = None
    elementName = getNoneString(elementName)
    elementText = getNoneString(elementText)
    if not elementName:
        return None
    dom = None
    if parentElement:
        dom = parentElement.ownerDocument
    else:
        dom = ZDom()
        dom.loadXML(ELEMENT_TEMPLATE)
        parentElement = dom.documentElement
    try:
        element = dom.createElement(elementName)
        parentElement.appendChild(element)
        for (n, v) in attrMap.iteritems():
            if n and v:
                element.setAttribute(n, v)
        if elementText:
            element.setText(elementText)
    except:
        pass
    return element
Beispiel #2
0
def createHtmlElement(parentElement, elementName, attrMap = {}, elementText = None):
    u"""createHtmlElement(Node, string, map, string) -> Node
    Creates a element given element name, attribute map and optional element node text.
    If the parentElement node is None, then element is under new document (zdom).
     """ #$NON-NLS-1$
    element = None
    elementName = getNoneString(elementName)
    elementText = getNoneString(elementText)
    if not elementName:
        return None
    dom = None
    if parentElement:
        dom = parentElement.ownerDocument
    else:
        dom = ZDom()
        dom.loadXML(ELEMENT_TEMPLATE)
        parentElement = dom.documentElement
    try:
        element = dom.createElement(elementName)
        parentElement.appendChild(element)
        for (n,v) in attrMap.iteritems():
            if n and v:                
                element.setAttribute(n,v)
        if elementText:
            element.setText(elementText)
    except:
        pass    
    return element
Beispiel #3
0
    def _save(self):
        if not self.registryFile:
            return

        mediaStorageDir = os.path.basename(self.registryFile)

        dom = ZDom()
        dom.loadXML(u"<registry/>")  #$NON-NLS-1$
        registryElem = dom.documentElement
        for fileName in self.fileMap:
            (size, timestamp, uploadResponse) = self.fileMap[fileName]
            relativeFileName = makeRelativePath(mediaStorageDir, fileName)
            url = uploadResponse.getUrl()
            embedFragment = uploadResponse.getEmbedFragment()
            metaData = uploadResponse.getMetaData()

            entryElem = dom.createElement(u"entry")  #$NON-NLS-1$
            entryElem.setAttribute(u"size", unicode(size))  #$NON-NLS-1$
            entryElem.setAttribute(u"timestamp",
                                   unicode(timestamp))  #$NON-NLS-1$

            fileElem = dom.createElement(u"file")  #$NON-NLS-1$
            urlElem = dom.createElement(u"url")  #$NON-NLS-1$
            embedElem = dom.createElement(u"embed")  #$NON-NLS-1$
            metaDataElem = dom.createElement(u"metaData")  #$NON-NLS-1$

            # When in portable mode, save the file paths as relative (which will
            # only happen when the image is on the same drive as the app).
            if isPortableEnabled():
                fileElem.setText(relativeFileName)
            else:
                fileElem.setText(fileName)

            entryElem.appendChild(fileElem)
            if url:
                urlElem.setText(unicode(url))
            entryElem.appendChild(urlElem)
            if embedFragment is not None:
                embedElem.appendChild(dom.importNode(embedFragment, True))
            entryElem.appendChild(embedElem)
            if metaData is not None:
                metaDataElem.appendChild(dom.importNode(metaData, True))
            entryElem.appendChild(metaDataElem)

            registryElem.appendChild(entryElem)

        dom.save(self.registryFile, True)
    def _save(self):
        if not self.registryFile:
            return

        mediaStorageDir = os.path.basename(self.registryFile)

        dom = ZDom()
        dom.loadXML(u"<registry/>") #$NON-NLS-1$
        registryElem = dom.documentElement
        for fileName in self.fileMap:
            (size, timestamp, uploadResponse) = self.fileMap[fileName]
            relativeFileName = makeRelativePath(mediaStorageDir, fileName)
            url = uploadResponse.getUrl()
            embedFragment = uploadResponse.getEmbedFragment()
            metaData = uploadResponse.getMetaData()

            entryElem = dom.createElement(u"entry") #$NON-NLS-1$
            entryElem.setAttribute(u"size", unicode(size)) #$NON-NLS-1$
            entryElem.setAttribute(u"timestamp", unicode(timestamp)) #$NON-NLS-1$

            fileElem = dom.createElement(u"file") #$NON-NLS-1$
            urlElem = dom.createElement(u"url") #$NON-NLS-1$
            embedElem = dom.createElement(u"embed") #$NON-NLS-1$
            metaDataElem = dom.createElement(u"metaData") #$NON-NLS-1$

            # When in portable mode, save the file paths as relative (which will
            # only happen when the image is on the same drive as the app).
            if isPortableEnabled():
                fileElem.setText(relativeFileName)
            else:
                fileElem.setText(fileName)

            entryElem.appendChild(fileElem)
            if url:
                urlElem.setText(unicode(url))
            entryElem.appendChild(urlElem)
            if embedFragment is not None:
                embedElem.appendChild(dom.importNode(embedFragment, True))
            entryElem.appendChild(embedElem)
            if metaData is not None:
                metaDataElem.appendChild(dom.importNode(metaData, True))
            entryElem.appendChild(metaDataElem)

            registryElem.appendChild(entryElem)

        dom.save(self.registryFile, True)
Beispiel #5
0
    def _serializeResponseInfo(self, metaFile, connectionRespInfo,
                               dataFilename):
        dom = ZDom()
        dom.loadXML(u"""<http-connection-response />""")  #$NON-NLS-1$
        rootElem = dom.documentElement
        urlElem = dom.createElement(u"url")  #$NON-NLS-1$
        urlElem.setText(connectionRespInfo.getURL())
        rootElem.appendChild(urlElem)

        dataFileElem = dom.createElement(u"data-file")  #$NON-NLS-1$
        dataFileElem.setText(dataFilename)
        rootElem.appendChild(dataFileElem)

        respCodeElem = dom.createElement(u"response-code")  #$NON-NLS-1$
        respCodeElem.setText(unicode(connectionRespInfo.getCode()))
        rootElem.appendChild(respCodeElem)

        respMsgElem = dom.createElement(u"response-message")  #$NON-NLS-1$
        respMsgElem.setText(unicode(connectionRespInfo.getMessage()))
        rootElem.appendChild(respMsgElem)

        headersElem = dom.createElement(u"response-headers")  #$NON-NLS-1$
        rootElem.appendChild(headersElem)

        for headerName in connectionRespInfo.getHeaders():
            headerVal = connectionRespInfo.getHeader(headerName)
            headerElem = dom.createElement(u"header")  #$NON-NLS-1$
            headerElem.setAttribute(u"name", headerName)  #$NON-NLS-1$
            headerElem.setText(headerVal)
            headersElem.appendChild(headerElem)

        dom.save(metaFile, True)
Beispiel #6
0
    def _serializeResponseInfo(self, metaFile, connectionRespInfo, dataFilename):
        dom = ZDom()
        dom.loadXML(u"""<http-connection-response />""") #$NON-NLS-1$
        rootElem = dom.documentElement
        urlElem = dom.createElement(u"url") #$NON-NLS-1$
        urlElem.setText(connectionRespInfo.getURL())
        rootElem.appendChild(urlElem)

        dataFileElem = dom.createElement(u"data-file") #$NON-NLS-1$
        dataFileElem.setText(dataFilename)
        rootElem.appendChild(dataFileElem)

        respCodeElem = dom.createElement(u"response-code") #$NON-NLS-1$
        respCodeElem.setText(unicode(connectionRespInfo.getCode()))
        rootElem.appendChild(respCodeElem)

        respMsgElem = dom.createElement(u"response-message") #$NON-NLS-1$
        respMsgElem.setText(unicode(connectionRespInfo.getMessage()))
        rootElem.appendChild(respMsgElem)

        headersElem = dom.createElement(u"response-headers") #$NON-NLS-1$
        rootElem.appendChild(headersElem)

        for headerName in connectionRespInfo.getHeaders():
            headerVal = connectionRespInfo.getHeader(headerName)
            headerElem = dom.createElement(u"header") #$NON-NLS-1$
            headerElem.setAttribute(u"name", headerName) #$NON-NLS-1$
            headerElem.setText(headerVal)
            headersElem.appendChild(headerElem)

        dom.save(metaFile, True)
Beispiel #7
0
 def serialize(self, packet):
     dom = ZDom()
     dom.loadXML(u"<zup:usage-packet xmlns:zup='%s' />" % IZBlogAppNamespaces.RAVEN_USAGE_STATS_NAMESPACE) #$NON-NLS-1$
     
     rootElem = dom.documentElement
     rootElem.setAttribute(u"packet-id", packet.getId()) #$NON-NLS-1$
     
     for key in packet.getAttributes():
         value = packet.getAttributes()[key]
         attrElem = dom.createElement(u"zup:attribute", IZBlogAppNamespaces.RAVEN_USAGE_STATS_NAMESPACE) #$NON-NLS-1$
         rootElem.appendChild(attrElem)
         attrElem.setAttribute(u"name", key) #$NON-NLS-1$
         attrElem.setText(value)
     
     return dom
Beispiel #8
0
    def importPersonalDictionary(self):
        joeyConfigDom = self._getJoeyUserConfigDom()
        if not joeyConfigDom:
            return
        try:
            node = joeyConfigDom.selectSingleNode(u"/joey/user-config/spell-check/language") #$NON-NLS-1$
            if not node:
                return
            spellcheckLang = getNoneString( node.getText())
            if not spellcheckLang:
                return

            # FIXME (EPW) we could support other languages...
            if not spellcheckLang == u"en_US": #$NON-NLS-1$
                return

            # 1) read all words from ZBW personal-dictionary.xml file
            # 2) create new spellchecker.xml DOM
            # 3) save new Raven spellchecker file to 'PROFILE\LANG\spellchecker.xml'

            joeyDictFile = os.path.join(self.pathToJoeyProfile, u"spelling/personal-dictionary.xml") #$NON-NLS-1$
            dom = ZDom()
            dom.load(joeyDictFile)
            dom.setNamespaceMap(ZBW_PERSONAL_DICTIONARY_NSS_MAP)
            wordNodeList = dom.selectNodes(u"/pd:personal-dictionary/pd:word") #$NON-NLS-1$
            
            newDom = ZDom()
            newDom.loadXML(ZBlogWriterDictionaryImporter.SPELLCHECK_TEMPLATE)
            newDom.setNamespaceMap(RAVEN_SPELLCHECK_NSS_MAP)
            personalDictElem = newDom.selectSingleNode(u"/spl:spellchecker/spl:personal-dictionary") #$NON-NLS-1$
            
            for wordNode in wordNodeList:
                word = wordNode.getText()
                newWordElem = newDom.createElement(u"word", IZBlogAppNamespaces.RAVEN_SPELLCHECKER_NAMESPACE) #$NON-NLS-1$
                newWordElem.setText(word)
                personalDictElem.appendChild(newWordElem)
            
            outputDir = os.path.join(self.pathToRavenProfile, u"spellcheck/en_US") #$NON-NLS-1$
            os.makedirs(outputDir)
            outputFile = os.path.join(outputDir, u"spellchecker.xml") #$NON-NLS-1$
            newDom.save(outputFile, True)
        except ZException, ze:
            # FIXME (EPW) need to report errors in some way
            ze.printStackTrace()
Beispiel #9
0
    def serialize(self, packet):
        dom = ZDom()
        dom.loadXML(
            u"<zup:usage-packet xmlns:zup='%s' />" %
            IZBlogAppNamespaces.RAVEN_USAGE_STATS_NAMESPACE)  #$NON-NLS-1$

        rootElem = dom.documentElement
        rootElem.setAttribute(u"packet-id", packet.getId())  #$NON-NLS-1$

        for key in packet.getAttributes():
            value = packet.getAttributes()[key]
            attrElem = dom.createElement(
                u"zup:attribute",
                IZBlogAppNamespaces.RAVEN_USAGE_STATS_NAMESPACE)  #$NON-NLS-1$
            rootElem.appendChild(attrElem)
            attrElem.setAttribute(u"name", key)  #$NON-NLS-1$
            attrElem.setText(value)

        return dom
Beispiel #10
0
def applyTemplateToDocument(template, document, mode = APPLY_TEMPLATE_MODE_FULL):
    u"""applyTemplateToDocument(IZTemplate, IZDocument) -> ZXHtmlDocument""" #$NON-NLS-1$

    logger = getLoggerService()

    if template is None or document is None:
        logger.warning(u"applyTemplateToDocument:: Either template or document were None - skipping.") #$NON-NLS-1$
        return None

    title = getSafeString(document.getTitle())
    xhtmlDoc = None
    content = document.getContent()
    if content is not None:
        xhtmlDoc = content.getXhtmlDocument()

    if xhtmlDoc is None:
        logger.warning(u"applyTemplateToDocument:: XHtml document was None, skipping.") #$NON-NLS-1$
        return None

    analyzer = ZXhtmlBodyFindingAnalyser()
    xhtmlDoc.analyse(analyzer)
    xhtmlBody = analyzer.getBody()

    # At this point, we have the document's title and body.
    logger.debug(u"applyTemplateToDocument:: title: %s" % title) #$NON-NLS-1$

    # Get the path to the template file we want to use.
    templateDir = template.getTemplateDirectory()
    templateFileName = template.getAllFileName()
    if mode == APPLY_TEMPLATE_MODE_TITLE_AND_BODY:
        templateFileName = template.getTitleAndBodyFileName()
    elif mode == APPLY_TEMPLATE_MODE_BODY_ONLY:
        templateFileName = template.getBodyOnlyFileName()
    templateFilePath = os.path.join(templateDir, templateFileName)

    logger.debug(u"applyTemplateToDocument:: templateFileName: %s" % templateFileName) #$NON-NLS-1$
    logger.debug(u"applyTemplateToDocument:: templateFilePath: %s" % templateFilePath) #$NON-NLS-1$

    if os.path.isfile(templateFilePath) and xhtmlBody is not None:
        templateContent = getFileContents(templateFilePath)
        templateContent = _preprocessTemplateContent(templateContent, templateDir)
        
        templateDom = ZDom()
        templateDom.loadXML(templateContent)
        templateXHtml = ZXhtmlDocument(templateDom)

        # Get the DOCTYPE value from the root file (if any)
        rootFilePath = template.getResolvedRootFile()
        rootFileContent = None
        if os.path.isfile(rootFilePath):
            rootFile = open(rootFilePath, u"r") #$NON-NLS-1$
            try:
                rootFileContent = rootFile.read()
            finally:
                rootFile.close()
        if rootFileContent:
            results = DOCTYPE_RE.findall(rootFileContent)
            if results:
                docTypeString = results[0]
                templateXHtml.setDocTypeString(docTypeString)

        # Find the template body elements.
        visitor = ZTemplateBodyFindingVisitor()
        visitor.visit(templateDom)
        templateBodyElements = visitor.getBodyElements()

        if not templateBodyElements:
            logger.warning(u"applyTemplateToDocument:: failed to find any template body elements") #$NON-NLS-1$

        # Find the template title elements.
        visitor = ZTemplateTitleFindingVisitor()
        visitor.visit(templateDom)
        templateTitleElements = visitor.getTitleElements()

        if not templateTitleElements:
            logger.warning(u"applyTemplateToDocument:: failed to find any template title elements") #$NON-NLS-1$

        # Do title stuff - replace the ravenTitle elem with a span containing the title
        for templateTitleElem in templateTitleElements:
            titleParentElem = templateTitleElem.parentNode
            newTitleElem = templateDom.createElement(u"span") #$NON-NLS-1$
            newTitleElem.setText(title)
            titleParentElem.replaceChild(newTitleElem, templateTitleElem)

        # Do body stuff - insert all children of xhtmlBody, then remove the ravenBody elem
        for templateBodyElem in templateBodyElements:
            bodyParentElem = templateBodyElem.parentNode
            for node in xhtmlBody.getChildren():
                bodyParentElem.insertBefore(templateDom.importNode(node, True), templateBodyElem)
            bodyParentElem.removeChild(templateBodyElem)

        # IE hates empty DIVs and %20's in attributes.  Dunno why.
        _fixupIECrappiness(templateDom)

        return templateXHtml
    else:
        logger.warning(u"applyTemplateToDocument:: template file not found") #$NON-NLS-1$

    return xhtmlDoc