Diff for /ECHO_content/ECHO_xslt.py between versions 1.10 and 1.20

version 1.10, 2006/09/11 14:43:23 version 1.20, 2008/11/13 14:35:53
Line 7  from ECHO_Nav import ECHO_pageTemplate Line 7  from ECHO_Nav import ECHO_pageTemplate
 from threading import Thread,Timer  from threading import Thread,Timer
 import threading  import threading
 from ECHO_helpers import *  from ECHO_helpers import *
 import ECHO_language  try:
       from ECHO_language import *
   except:
       print "no echo language"
       class ECHO_language:
           """leere Klasse"""
           pass
 import sys  import sys
 import urllib  import urllib
 import urlparse  import urlparse
Line 19  import transaction Line 25  import transaction
   
 import os.path  import os.path
   
 import urllib  import urllib,cgi
   import logging
 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
Line 29  except: Line 35  except:
     print "4suite has to be installed"      print "4suite has to be installed"
   
   
 class getXML(Implicit):  class getXML(Thread):
     """get XML thread"""      """get XML thread"""
   
     def set(self,qs,xsl,result):      def set(self,qs,xsl,result):
Line 57  class getXML(Implicit): Line 63  class getXML(Implicit):
       
     def __call__(self):      def __call__(self):
         """wait"""          """wait"""
           self.run()
         return True          return True
           
     def run(self):      def run(self):
Line 65  class getXML(Implicit): Line 72  class getXML(Implicit):
   
         try:          try:
       
             urlH=urllib.urlopen(self._v_qs)              #urlH=urllib.urlopen(self._v_qs)
             xml=urlH.read()              #xml=urlH.read()
             urlH.close()              #urlH.close()
             xsltproc=Processor()              xsltproc=Processor()
             document = InputSource.DefaultFactory.fromString(xml)              logging.debug("start XML")
               document = InputSource.DefaultFactory.fromUri(self._v_qs)
                           
             stylesheet = InputSource.DefaultFactory.fromUri(self.xsl)              stylesheet = InputSource.DefaultFactory.fromUri(self.xsl)
                           logging.debug("got all files XML")
             xsltproc.appendStylesheet(stylesheet)              xsltproc.appendStylesheet(stylesheet)
                           logging.debug("got all files do the transform")
                   
             #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 100  from ZODB.FileStorage import FileStorage Line 108  from ZODB.FileStorage import FileStorage
 class ECHO_cache:  class ECHO_cache:
     def __init__(self):      def __init__(self):
         """init the storage"""          """init the storage"""
         self.storage=FileStorage("/var/tmp/echo_cache.fs")          self.storage=FileStorage(os.path.join(INSTANCE_HOME,"var/echo_cache.fs"))       
         self.db=DB(self.storage)              self.db=DB(self.storage)    
         self.connection=self.db.open()          self.connection=self.db.open()
         self.root=self.connection.root()          self.root=self.connection.root()
Line 137  class ECHO_cache: Line 145  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)
                   
   
 class ECHO_xslt(ECHO_pageTemplate,ECHO_language.ECHO_language):  class ECHO_xslt(ECHO_pageTemplate,ECHO_language):
     """ECHO_xslt classe"""      """ECHO_xslt classe"""
   
     meta_type="ECHO_xslt"      meta_type="ECHO_xslt"
Line 212  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 221  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
                                   
             qs="%s%s"%(self.cgiUrl,self.REQUEST['QUERY_STRING'])              qs="%s%s"%(self.cgiUrl,self.REQUEST['QUERY_STRING'])
             xsl=self.absolute_url()+"/xslt"              xsl=self.absolute_url()+"/xslt"
             self._v_xmltrans=getXML().__of__(self)              #self._v_xmltrans=getXML().__of__(self)
               self._v_xmltrans=getXML()
             #self._xmltrans.start()              #self._xmltrans.start()
             thread=Thread(target=self._v_xmltrans)              #thread=Thread(target=self._v_xmltrans)
             thread.start()              #thread.start()
               logging.debug("Thread prepared")
             self._v_xmltrans.set(qs,xsl,None)              self._v_xmltrans.set(qs,xsl,None)
             self._v_xmltrans.run()              self._v_xmltrans.start()
                           logging.debug("Thread started")
                           
             self.threadName=thread.getName()[0:]              #self.threadName=thread.getName()[0:]
               self.threadName=self._v_xmltrans.getName()[0:]
             wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])              wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
             if wait_template:              if wait_template:
                 return wait_template[0][1]()                  return wait_template[0][1]()
Line 252  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 264  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         fn=self.REQUEST['fn']          fn=self.REQUEST['fn']
         self.cache.deleteObject(fn)          self.cache.deleteObject(fn)
                   
     def getPageLex(self,_pn="1",_caching=None):      
       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",_displaylinks="yes"):
         """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:          if not _caching:
             _caching=self.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)
     
Line 273  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 447  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
                     
             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()
                   
Line 285  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 471  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         xsltproc.appendStylesheet(stylesheet)          xsltproc.appendStylesheet(stylesheet)
         tmp=xsltproc.run(document)          tmp=xsltproc.run(document)
                   
           if _links and (_displaylinks=='yes'):
               _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):      def getTextInput(self):
Line 309  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 503  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         #return InputSource.InputSource(fact)          #return InputSource.InputSource(fact)
         #xmlt=urllib.urlopen(qs).read()          #xmlt=urllib.urlopen(qs).read()
                   
     def getPage(self,_pn,REQUEST=None,_caching=None):      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:          if not _caching:
             _caching=self.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:
Line 325  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 519  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
         xmlt,self.baseUri=self.getTextInput()          xmlt,self.baseUri=self.getTextInput()
                   
         #get the text from cache, if existing          #get the text from cache, if existing
           try:
         fromCache=self.cache.retrieveObject(self.baseUri,"-1")          fromCache=self.cache.retrieveObject(self.baseUri,"-1")
           except:
               fromCache=None
         if fromCache and _caching=="yes":          if fromCache and _caching=="yes":
                       
             txt = fromCache              txt = fromCache
Line 335  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 532  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
                           
             self.cache.storeObject(self.baseUri,"-1",txt)              self.cache.storeObject(self.baseUri,"-1",txt)
                   
      
         dom=NonvalidatingReader.parseString(txt,self.baseUri)          dom=NonvalidatingReader.parseString(txt,self.baseUri)
                   
         #pb should have a namespache          #pb should have a namespache
                   
         pbs=dom.xpath("//mpiwg:pb",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})          pbs=dom.xpath("//mpiwg:pb",explicitNss={'mpiwg':'http://www.mpiwg-berlin.mpg.de/namespace'})
           
         if len(pbs)==0: # versuche nochmal ohne          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://www.w3.org/1999/xhtml','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://www.w3.org/1999/xhtml','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
               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)
Line 366  class ECHO_xslt(ECHO_pageTemplate,ECHO_l Line 582  class ECHO_xslt(ECHO_pageTemplate,ECHO_l
                 if parent:                  if parent:
                     parent[0].removeChild(node)                      parent[0].removeChild(node)
             except:              except:
                 zLOG.LOG("ECHO_Resource (getAccessRightMD)", zLOG.INFO,"%s (%s)"%sys.exc_info()[0:2])                  logger("ECHO_Resource (getAccessRightMD)", logging.INFO,"%s (%s)"%sys.exc_info()[0:2])
         strio = StringIO()          strio = StringIO()
         PrettyPrint(dom,strio)           PrettyPrint(dom,strio) 
         xmlstr = strio.getvalue()          xmlstr = strio.getvalue()
Line 424  def manage_addECHO_xslt(self, id, label, Line 640  def manage_addECHO_xslt(self, id, label,
             u = "%s/%s" % (u, quote(id))              u = "%s/%s" % (u, quote(id))
         REQUEST.RESPONSE.redirect(u+'/manage_main')          REQUEST.RESPONSE.redirect(u+'/manage_main')
     return ''      return ''
   

Removed from v.1.10  
changed lines
  Added in v.1.20


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