Diff for /ECHO_content/ECHO_xslt.py between versions 1.9 and 1.14

version 1.9, 2006/09/10 22:57:38 version 1.14, 2006/10/18 14:42:33
Line 19  import transaction Line 19  import transaction
   
 import os.path  import os.path
   
 import urllib  import urllib,cgi
   
 try:  try:
     from Ft.Xml.Xslt.Processor import Processor      from Ft.Xml.Xslt.Processor import Processor
     from Ft.Xml import InputSource, EMPTY_NAMESPACE,Parse      from Ft.Xml import InputSource, EMPTY_NAMESPACE,Parse
       from Ft.Xml.Domlette import NonvalidatingReader
 except:  except:
     print "4suite has to be installed"      print "4suite has to be installed"
   
Line 76  class getXML(Implicit): Line 77  class getXML(Implicit):
                           
                   
             #print self.xsl              #print self.xsl
             #print xsltproc.run(document)              #< xsltproc.run(document)
             tmp=xsltproc.run(document)              tmp=xsltproc.run(document)
                           
             self.result=tmp[0:]              self.result=tmp[0:]
Line 136  class ECHO_cache: Line 137  class ECHO_cache:
         if not fileStore:          if not fileStore:
             return None              return None
         else:          else:
              
             return self.root[name].get(pn,None)              return self.root[name].get(pn,None)
                   
   
Line 144  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 146  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
   
     meta_type="ECHO_xslt"      meta_type="ECHO_xslt"
           
     cache=ECHO_cache()      cache=ECHO_cache() # cache for analysed pages
       caching="yes"
       
       appendQueryString=True # add query string to the cgiUrl can be changed with addChanges
       
       passURL=False #use url from querystring parameter fn to retrieve the text and not the url in cgi-url can be changed with addChanges
           
           
     results={}      results={}
     manage_options=ECHO_pageTemplate.manage_options+(      manage_options=ECHO_pageTemplate.manage_options+(
Line 164  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 172  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         pt=zptFile(self, 'zpt/ChangeECHO_xsltForm.zpt')          pt=zptFile(self, 'zpt/ChangeECHO_xsltForm.zpt')
         return pt()          return pt()
   
     def addChanges(self,cgiUrl,RESPONSE=None):      def addChanges(self,cgiUrl,appendQueryString=False,passURL=False,caching=False,RESPONSE=None):
         """change the xslt, ueberschriebt addChanges in ECHO_PageTemplate"""          """change the xslt, ueberschriebt addChanges in ECHO_PageTemplate"""
       
         if urlparse.urlparse(cgiUrl)[0]=="":#relative url in absolute          if urlparse.urlparse(cgiUrl)[0]=="":#relative url in absolute
             self.cgiUrl=urlparse.urljoin(self.absolute_url(), cgiUrl)              self.cgiUrl=urlparse.urljoin(self.absolute_url(), cgiUrl)
         else:          else:
             self.cgiUrl=cgiUrl              self.cgiUrl=cgiUrl
                           
           if appendQueryString: 
               self.appendQueryString=True
           else:
               self.appendQueryString=False
           
           if passURL:
               self.passURL=True
           else:
               self.passURL=False
          
           if caching:
               self.caching="yes"
           else:
               self.caching="No"
           
   
         if RESPONSE:          if RESPONSE:
             RESPONSE.redirect("manage_main")              RESPONSE.redirect("manage_main")
                   
Line 218  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 243  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
                 return self._v_xmltrans.getResult()                  return self._v_xmltrans.getResult()
   
           
       def getText(self):
           """print nur den text"""
           qs,baseUri=self.getTextInput()
           self.REQUEST.RESPONSE.redirect(qs)
   
     def deleteCache(self):      def deleteCache(self):
         """deletefrom cache"""          """deletefrom cache"""
         fn=self.REQUEST['fn']          fn=self.REQUEST['fn']
         self.cache.deleteObject(fn)          self.cache.deleteObject(fn)
                   
     def getPageLex(self,_pn="1",_caching="yes"):      
       def createLinkNode(self,url,dom):
           """createa a link node"""
           txt=dom.createTextNode("<XMLLink>")
           node=dom.createElementNS("http://test.de","a")
           node.setAttributeNS("http://test.de","href",url)
           node.appendChild(txt)
           return node
         
       def forwardLink(self,linkid,url,type="target",RESPONSE=None):
           """forward to link"""
           if RESPONSE:
               RESPONSE.redirect(self.getLink(linkid,url,type=type))
               
           else:
               return self.getLink(linkid,url,type=type)
       def getLink(self,linkid,url,type="target"):
           """get target for linkid"""
           dom=NonvalidatingReader.parseUri(url)
           
           masterurl=dom.xpath("//mpiwg:masterurl/@ref",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
           slaveurl=dom.xpath("//mpiwg:slaveurl/@ref",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
           
           #check now if there are in the link file
        
           xp="//mpiwg:link[@id='%s']"%linkid
           
           if type=="target":
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   fn=link.xpath("mpiwg:target/@filename",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   
                   if urlparse.urlparse(urllib.unquote(fn))[0]=="http": # fn ist eine url
                       return urllib.unquote(fn)  # dann gibt diese zurueck 
                   
                   ref=link.xpath("mpiwg:target/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   
                   ref2=link.xpath("mpiwg:target/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   selectionNodeIndex=link.xpath("mpiwg:target/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                     
                 
                   
                   lstr=slaveurl+'fn='+fn+'&_id='+ref+'&_pagelink=%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,'target')
                   lstr+="&_links="+urllib.quote(url)
                   
           else:
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   fn=link.xpath("mpiwg:source/@filename",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   if urlparse.urlparse(urllib.unquote(fn))[0]=="http": # fn ist eine url
                       return urllib.unquote(fn)  # dann gibt diese zurueck 
                   
                   ref=link.xpath("mpiwg:source/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   
                   ref2=link.xpath("mpiwg:source/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   selectionNodeIndex=link.xpath("mpiwg:source/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                                
                   lstr=masterurl+'fn='+fn+'&_id='+ref+'&_pagelink=%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,'source')
                   lstr+="&_links="+urllib.quote(url)
           return lstr
      
       def addLinksUrl(self,txt,url):
           """add reference to links to  url"""
           ret=[]
           dom=NonvalidatingReader.parseUri(url)
           textDom=NonvalidatingReader.parseString(txt)
   
           #find ids in txt
           ids=textDom.xpath("//*[@id]")
           
           for textid in ids:
               xp="//mpiwg:link[mpiwg:source/@refid='%s']"%textid.xpath("@id")[0].value
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   ref2=link.xpath("mpiwg:source/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   selectionNodeIndex=link.xpath("mpiwg:source/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   linkid=link.xpath("@id")[0].value         
                   ret.append('%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,'source'))
              
               xp="//mpiwg:link[mpiwg:target/@refid='%s']"%textid.xpath("@id")[0].value
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   ref2=link.xpath("mpiwg:target/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   selectionNodeIndex=link.xpath("mpiwg:target/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   linkid=link.xpath("@id")[0].value         
                   ret.append('%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,'target'))
              
              
           return ret
             
       def addLinks(self,txt,url="http://127.0.0.1:8080/HFQP/linkCreator/getCollectionXML?collection=commentary2"):
           """add links to a page from xml linkfile"""
           
           dom=NonvalidatingReader.parseUri(url)
           textDom=NonvalidatingReader.parseString(txt)
   
           #find ids in txt
           ids=textDom.xpath("//*[@id]")
           masterurl=dom.xpath("//mpiwg:masterurl/@ref",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
           slaveurl=dom.xpath("//mpiwg:slaveurl/@ref",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
           
           #check now if there are in the link file
           for textid in ids:
               xp="//mpiwg:link[mpiwg:source/@refid='%s']"%textid.xpath("@id")[0].value
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   fn=link.xpath("mpiwg:target/@filename",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   print fn
                   if urlparse.urlparse(urllib.unquote(fn))[0]=="http": # fn ist eine url
                       lstr=urllib.unquote(fn)  # dann gibt diese zurueck 
                   else:
                       try:
                           ref=link.xpath("mpiwg:target/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                       
                           ref2=link.xpath("mpiwg:target/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                           selectionNodeIndex=link.xpath("mpiwg:target/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                           linkid=link.xpath("@id")[0].value         
                           lstr=slaveurl+'fn='+fn+'&_id='+ref+'&_pagelink=%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,'target')
                           lstr+="&_links="+urllib.quote(url)
                       except:
                           lstr=""
                   node=self.createLinkNode(lstr,textDom)
                   textid.parentNode.insertBefore(node,textid)
                 
             
               xp="//mpiwg:link[mpiwg:target/@refid='%s']"%textid.xpath("@id")[0].value
               for link in dom.xpath(xp,explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                   fn=link.xpath("mpiwg:source/@filename",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                   if urlparse.urlparse(urllib.unquote(fn))[0]=="http": # fn ist eine url
                       lstr=urllib.unquote(fn)  # dann gibt diese zurueck 
                   else:
                   
                       ref=link.xpath("mpiwg:source/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                       
                       ref2=link.xpath("mpiwg:source/mpiwg:pagelink/@refid",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                       selectionNodeIndex=link.xpath("mpiwg:source/mpiwg:pagelink/@selectionNodeIndex",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})[0].value
                       linkid=link.xpath("@id")[0].value                    
                       lstr=masterurl+'fn='+fn+'&_id='+ref+'&_pagelink=%s///%s/%s/%s'%(ref2,selectionNodeIndex,linkid,"source")
                       lstr+="&_links="+urllib.quote(url)
                       
                   node=self.createLinkNode(lstr,textDom)
                   textid.parentNode.insertBefore(node,textid)
                 
               
           
           strio = StringIO()
           PrettyPrint(textDom,strio) 
           xmlstr = strio.getvalue()
           
           return xmlstr
   
               
           
       def getPageLex(self,_pn="1",_id=None,_caching=None,_links=None,_showall="no"):
         """getpage mit lexikalischer analyse und xslt transform          """getpage mit lexikalischer analyse und xslt transform
         if _caching=yes dann wird die lwxikalisch analysierte seite in einem cache abgespeichert          if _caching=yes dann wird die lwxikalisch analysierte seite in einem cache abgespeichert
         """          """
           def encode(hash):
               ret=[]
               for x in hash.keys():
                   value=hash[x]
                   
                   if type(value) is ListType:
                       for z in value:
                           ret.append("%s=%s"%(x,z))
                   else:
                       ret.append("%s=%s"%(x,value))
               return "&".join(ret)
                       
                           
               
           if not _caching:
               _caching=self.caching
                 
         fn=self.REQUEST['fn']          fn=self.REQUEST['fn']
   
           if not _id:
                   
         fromCache=self.cache.retrieveObject(fn,_pn)          fromCache=self.cache.retrieveObject(fn,_pn)
   
         if fromCache and _caching=="yes":          if fromCache and _caching=="yes":
             print "retrieve",fn,_pn                 
             txt = fromCache              txt = fromCache
         else:          else:
             txt=self.tagLex(nr=_pn)                 txt=self.tagLex(nr=_pn)   
             print "store",fn,_pn                 
             self.cache.storeObject(fn,_pn,txt[0:])              self.cache.storeObject(fn,_pn,txt[0:])
                   
           else:
              txt=self.tagLex(id=_id)
         
           if _showall=="yes":
              params=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
              
              params['_pagelink']=self.addLinksUrl(txt,url=_links)
              params['_showall']='no'
             
              print self.absolute_url()+"?"+encode(params)
              self.REQUEST.RESPONSE.redirect(self.absolute_url()+"/getPageLex?"+encode(params))
              
              
         xsl=self.xslt()          xsl=self.xslt()
                   
         xsltproc=Processor()          xsltproc=Processor()
Line 253  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 460  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         xsltproc.appendStylesheet(stylesheet)          xsltproc.appendStylesheet(stylesheet)
         tmp=xsltproc.run(document)          tmp=xsltproc.run(document)
                   
           if _links:
               _links=urllib.unquote(_links)
               tmp=self.addLinks(tmp,url=_links)
               
           #bugfix for digilib images which doesn't accept &amp;
           tmp=tmp.replace("&amp;","&")
           
   
         return tmp[0:]          return tmp[0:]
                           
       def getTextInput(self):
           """get the text
           wie der text geholt wird liegt an der konfiguration,
           is appendQueryString gesetzt, dann wir jeweils der Querystring an vorgebenen url gesetzt, erwartet wird fn=
           fźr den Pfad, is passURL gesetzt, dann wird falls fn= eine vollstŠndige url enthŠlt, diese anstelle der in cgiurl definierten genommen.
           """
   
     def getPage(self,_pn,REQUEST=None):          if getattr(self,'passURL',False) and self.REQUEST.has_key('fn') and (urlparse.urlparse(self.REQUEST['fn'])[0]=='http'):
               qs=self.REQUEST['fn']
               baseUri=qs
           elif getattr(self,'pappendQueryString',True):
               qs="%s%s"%(self.cgiUrl,self.REQUEST['QUERY_STRING'])
               baseUri=self.cgiUrl
           else:
               qs="%s"%(self.cgiUrl)
               baseUri=self.cgiUrl
           
           #fact= InputSource.DefaultFactory.fromUri(qs)
           return qs,baseUri
           #return InputSource.InputSource(fact)
           #xmlt=urllib.urlopen(qs).read()
           
       def getPage(self,_pn="-1",_id=None,REQUEST=None,_caching=None):
         """get a page from an xml"""          """get a page from an xml"""
           
           if not _caching:
               _caching=self.caching
               
         pn=int(_pn)-1          pn=int(_pn)-1
         if pn<0:          if pn<0 and (not _id):
             if REQUEST:              if REQUEST:
                 return "Sorry, pagenumbers have to be greater than 0"                  return "Sorry, pagenumbers have to be greater than 0"
             else:              else:
                 return None                  return None
         impl=xml.dom.getDOMImplementation()  
         newDoc=impl.createDocument(EMPTY_NAMESPACE,None,None  
                                    )  
         root=newDoc.createElement('page')  
         newDoc.appendChild(root)  
                   
           xmlt,self.baseUri=self.getTextInput()
                   
         qs="%s%s"%(self.cgiUrl,self.REQUEST['QUERY_STRING'])          #get the text from cache, if existing
           fromCache=self.cache.retrieveObject(self.baseUri,"-1")
           if fromCache and _caching=="yes":
             
               txt = fromCache
           else:
                   
               txt=urllib.urlopen(xmlt).read()
                   
               self.cache.storeObject(self.baseUri,"-1",txt)
             
         xmlt=urllib.urlopen(qs).read()  
         dom=Parse(xmlt)  
                 
           dom=NonvalidatingReader.parseString(txt,self.baseUri)
           
           #pb should have a namespache
   
           pbs=dom.xpath("//mpiwg:pb",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})
           
           if len(pbs)==0: # versuche nochmal ohne
         pbs=dom.xpath("//pb")          pbs=dom.xpath("//pb")
   
           if _id:
               #suche wieviele pb for der id
               
               
               idpb=dom.xpath("//*[@id='%s']/preceding::node()/mpiwg:pb"%_id,explicitNss={'html':'http://test.de','mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})
               if len(idpb)==0:
                   idpb=dom.xpath("//*[@id='%s']/preceding::node()/pb"%_id)
            
               if len(idpb)==0:
                           k=0
                           for node in dom.xpath("//*[@id='%s']//preceding::node()"%_id,explicitNss={'html':'http://test.de','mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'}):
                               if getattr(node,'tagName',"")=="mpiwg:pb":
                                   k+=1
               else:
                   k=len(idpb)
               pn=k-1 #-1 wegen Seitenzahlzaehlung startet mit 0
               
         if pn > len(pbs):          if pn > len(pbs):
             if REQUEST:              if REQUEST:
                 return "Sorry, pagenumber %s does not exit"%pn+1                  return "Sorry, pagenumber %s does not exit"%(pn+1)
             else:              else:
                 return None                  return None
                           
Line 292  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 558  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         else:          else:
             endNode=None              endNode=None
                   
              
 #        follower=beginNode.xpath('following::node()')  
 #        #root.appendChild(beginNode)  
 #        found=[]  
 #         
 #        for node in endNode.xpath('preceding::node()'):  
 #            if node in follower:  
 #                found.append(node)  
 #      
           
      
         #rempove all children of parents which are also in the list  
 #        for x in found:  
 #            for child in x.xpath(".//*"):  
 #                try:  
 #                    
 #                    nr=found.index(child)  
 #                 
 #                    del(found[nr])  
 #                except:  
 #                    pass  
 #  
 #        found.append(endNode)  
 #         
         deleteNodes=beginNode.xpath('preceding::node()')          deleteNodes=beginNode.xpath('preceding::node()')
         if endNode:          if endNode:
             deleteNodes+=endNode.xpath('following::node()')              deleteNodes+=endNode.xpath('following::node()')

Removed from v.1.9  
changed lines
  Added in v.1.14


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>