瀏覽代碼

Fixes gh-404 Refactor existing XSL cache

Make behavior of IXslTransform more obvious by using explicit methods
for loading XSL from a file and for using XSL without caching it.

Make comparison of file paths case sensitive.

Fix/optimize how entries are removed when cache is full.

Signed-off-by: Anthony Fishbeck <Anthony.Fishbeck@lexisnexis.com>
Anthony Fishbeck 14 年之前
父節點
當前提交
50a4d15a14

+ 2 - 2
common/wuwebview/wuwebview.cpp

@@ -372,7 +372,7 @@ void WuWebView::renderExpandedResults(const char *viewName, const StringBuffer &
     t->setIncludeHandler(this);
     StringBuffer cacheId(viewName);
     cacheId.append('@').append(dllname.str()); //using dllname, cloned workunits can share cache entry
-    t->setXslSource(xslt.str(), xslt.length(), rootpath.str(), cacheId.str());
+    t->setXslSource(xslt.str(), xslt.length(), cacheId.str(), rootpath.str());
     t->setXmlSource(expanded.str(), expanded.length());
     t->transform(out);
 }
@@ -413,7 +413,7 @@ void WuWebView::applyResultsXSLT(const char *filename, const char *xml, StringBu
     //mapped to resources and need to be distinguished in cache
     StringBuffer cacheId(filename);
     cacheId.append('@').append(dllname.str()); //cloned workunits have same dll and resources
-    t->setXslSource(filename, true, cacheId.str());
+    t->loadXslFromFile(filename, cacheId.str());
     t->setXmlSource(buffer.str(), buffer.length());
     t->transform(out);
 }

+ 1 - 1
deployment/deploy/DeployTask.cpp

@@ -356,7 +356,7 @@ public:
          if (m_instanceName.length())
            transform.setParameter("instance", StringBuffer("'").append(m_instanceName).append("'").str());
 
-         transform.setXslSource(xsl);
+         transform.loadXslFromFile(xsl);
          if (!m_dummy)
          {
            if(m_machineOS != MachineOsLinux)

+ 1 - 1
deployment/deploy/deploy.cpp

@@ -259,7 +259,7 @@ public:
         externalFunction.setown(m_transform->createExternalFunction("validationMessage", validationMessageFromXSLT));
         m_transform->setExternalFunction(SEISINT_NAMESPACE, externalFunction.get(), true);
         
-        m_transform->setXslSource("validateAll.xsl");
+        m_transform->loadXslFromFile("validateAll.xsl");
         m_transform->setUserData(this);
         
         try

+ 3 - 4
deployment/deployutils/deployutils.cpp

@@ -2905,15 +2905,14 @@ const char* expandXPath(StringBuffer& xpath, IPropertyTree* pNode, IPropertyTree
 
 bool xsltTransform(const StringBuffer& xml, const char* sheet, IProperties *params, StringBuffer& ret)
 {
-  StringBuffer xsl;
-  if (esp::readFile(sheet, xsl)<=0)
-    throw MakeStringException(-1, "Can not open stylesheet %s",sheet);
+  if (!checkFileExists(sheet))
+    throw MakeStringException(-1, "Could not find stylesheet %s",sheet);
 
   Owned<IXslProcessor> proc  = getXslProcessor();
   Owned<IXslTransform> trans = proc->createXslTransform();
 
   trans->setXmlSource(xml.str(), xml.length());
-  trans->setXslSource(xsl, xsl.length());
+  trans->loadXslFromFile(sheet);
 
   if (params)
   {

+ 1 - 1
ecl/eclplus/formattypes.cpp

@@ -217,7 +217,7 @@ void TextFormatType::printBody(FILE* fp, int len, char* txt)
     StringBuffer xmlbuf;
     xmlbuf.append("<Result>").append(len, txt).append("</Result>");
     transform->setXmlSource(xmlbuf.str(), xmlbuf.length());
-    transform->setXslSource(formatxsl, strlen(formatxsl));
+    transform->setXslNoCache(formatxsl, strlen(formatxsl));
     transform->setParameter("showHeader", displayNamesHeader()?"1":"0");
     transform->setParameter("showName", embedNames()?"1":"0");
     transform->setParameter("showRecordNumber", displayRecordNumber()?"1":"0");

+ 1 - 1
esp/bindings/bindutil.cpp

@@ -810,7 +810,7 @@ void xslTransformHelper(IXslProcessor *xslp, const char* xml, const char* xslFil
         if (!strnicmp(xslFile, "/esp/xslt/", 10))
             if (!checkFileExists(xslpath.append(getCFD()).append("smc_xslt/").append(xslFile+10).str()) && !checkFileExists(xslpath.append(getCFD()).append("xslt/").append(xslFile+10).str()))
                 return;
-        xform->setXslSource((xslpath.length()) ? xslpath.str() : xslFile);
+        xform->loadXslFromFile((xslpath.length()) ? xslpath.str() : xslFile);
         xform->setXmlSource(xml, strlen(xml)+1);
         if (params) xform->copyParameters(params);
         xform->transform(output.clear());

+ 3 - 3
esp/bindings/http/platform/httpbinding.cpp

@@ -1003,7 +1003,7 @@ int EspHttpBinding::onGetSoapBuilder(IEspContext &context, CHttpRequest* request
 
     IXslProcessor* xslp = getXmlLibXslProcessor();
     Owned<IXslTransform> xform = xslp->createXslTransform();
-    xform->setXslSource(StringBuffer(getCFD()).append("./xslt/soap_page.xsl").str());
+    xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/soap_page.xsl").str());
     xform->setXmlSource("<xml/>", 6);
 
     // params
@@ -1949,7 +1949,7 @@ int EspHttpBinding::onGetXForm(IEspContext &context, CHttpRequest* request, CHtt
         //DBGLOG("Schema: %s", schema.str());
 
         Owned<IXslTransform> xform = xslp->createXslTransform();
-        xform->setXslSource(StringBuffer(getCFD()).append("./xslt/gen_form.xsl").str());
+        xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/gen_form.xsl").str());
         xform->setXmlSource(schema.str(), schema.length()+1);
 
         // params
@@ -2393,7 +2393,7 @@ void EspHttpBinding::sortResponse(IEspContext& context, CHttpRequest* request, M
         IXslProcessor* xslp = getXmlLibXslProcessor();
 
         Owned<IXslTransform> xform = xslp->createXslTransform();
-        xform->setXslSource(StringBuffer(getCFD()).append("./xslt/dict_sort.xsl").str());   
+        xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/dict_sort.xsl").str());
 
         xform->setXmlSource(respXML,respXML.length());
         xform->transform(result);

+ 3 - 3
esp/platform/espprotocol.cpp

@@ -137,7 +137,7 @@ const StringBuffer &CEspApplicationPort::getAppFrameHtml(time_t &modified, const
         xml.appendf("<EspApplicationFrame title=\"%s\" navWidth=\"%d\" navResize=\"%d\" navScroll=\"%d\" inner=\"%s\" params=\"%s\"/>", 
             getESPContainer()->getFrameTitle(), navWidth, navResize, navScroll, (inner&&*inner) ? encoded_inner.str() : "?main", params.str());
         Owned<IXslTransform> xform = xslp->createXslTransform();
-        xform->setXslSource(StringBuffer(getCFD()).append("./xslt/appframe.xsl").str());
+        xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/appframe.xsl").str());
         xform->setXmlSource(xml.str(), xml.length()+1);
         xform->transform( (needRefresh || embedded_url) ? html.clear() : appFrameHtml.clear());
     }
@@ -171,7 +171,7 @@ const StringBuffer &CEspApplicationPort::getTitleBarHtml(IEspContext& ctx, bool
         else
         {
             Owned<IXslTransform> xform = xslp->createXslTransform();
-            xform->setXslSource(StringBuffer(getCFD()).append("./xslt/espheader.xsl").str());
+            xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/espheader.xsl").str());
             xform->setXmlSource(titleBarXml.str(), titleBarXml.length()+1);
             xform->transform(titleBarHtml.clear());
         }
@@ -211,7 +211,7 @@ const StringBuffer &CEspApplicationPort::getNavBarContent(IEspContext &context,
                 xslsource.append(getCFD()).append("./xslt/nav.xsl");
                     
             }
-            xform->setXslSource(xslsource.str());
+            xform->loadXslFromFile(xslsource.str());
 
 
             xform->setXmlSource(xml.str(), xml.length()+1);

+ 1 - 1
esp/services/ws_dfu/ws_dfuService.cpp

@@ -1492,7 +1492,7 @@ void CWsDfuEx::xsltTransformer(const char* xsltPath,StringBuffer& source,StringB
 
     Owned<IXslTransform> xform = m_xsl->createXslTransform();
 
-    xform->setXslSource(xsltPath);
+    xform->loadXslFromFile(xsltPath);
     xform->setXmlSource(source.str(), source.length()+1);
     xform->transform(returnStr.clear());
 }

+ 5 - 5
esp/services/ws_ecl/ws_ecl_service.cpp

@@ -387,7 +387,7 @@ void CWsEclBinding::xsltTransform(const char* xml, unsigned int len, const char*
 
     StringBuffer xslpath(getCFD());
     xslpath.append(xslFileName);
-    trans->setXslSource(xslpath.str());
+    trans->loadXslFromFile(xslpath.str());
 
     if (params)
     {
@@ -858,7 +858,7 @@ int CWsEclBinding::getWsEclLinks(IEspContext &context, CHttpRequest* request, CH
 
     Owned<IXslProcessor> xslp = getXslProcessor();
     Owned<IXslTransform> xform = xslp->createXslTransform();
-    xform->setXslSource(StringBuffer(getCFD()).append("./xslt/wsecl3_links.xslt").str());
+    xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_links.xslt").str());
     xform->setXmlSource(xml.str(), xml.length());
 
     StringBuffer page;
@@ -1362,7 +1362,7 @@ int CWsEclBinding::getGenForm(IEspContext &context, CHttpRequest* request, CHttp
     Owned<IXslTransform> xform = xslp->createXslTransform();
 
     StringBuffer xslfile(getCFD());
-    xform->setXslSource(xslfile.append("./xslt/wsecl3_form.xsl").str());
+    xform->loadXslFromFile(xslfile.append("./xslt/wsecl3_form.xsl").str());
     xform->setXmlSource(formxml.str(), formxml.length()+1);
 
     // pass params to form (excluding form and __querystring)
@@ -1594,7 +1594,7 @@ int CWsEclBinding::getXmlTestForm(IEspContext &context, CHttpRequest* request, C
 
     Owned<IXslProcessor> xslp = getXslProcessor();
     Owned<IXslTransform> xform = xslp->createXslTransform();
-    xform->setXslSource(StringBuffer(getCFD()).append("./xslt/wsecl3_xmltest.xsl").str());
+    xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_xmltest.xsl").str());
 
     StringBuffer srcxml;
     srcxml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><srcxml><soapbody><![CDATA[");
@@ -1658,7 +1658,7 @@ int CWsEclBinding::getJsonTestForm(IEspContext &context, CHttpRequest* request,
 
     Owned<IXslProcessor> xslp = getXslProcessor();
     Owned<IXslTransform> xform = xslp->createXslTransform();
-    xform->setXslSource(StringBuffer(getCFD()).append("./xslt/wsecl3_jsontest.xsl").str());
+    xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_jsontest.xsl").str());
 
     StringBuffer srcxml;
     srcxml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><srcxml><jsonreq><![CDATA[");

+ 2 - 2
esp/services/ws_fs/ws_fsBinding.cpp

@@ -364,14 +364,14 @@ IPropertyTree* CFileSpraySoapBindingEx::createPTreeForXslt(const char* method, c
 void CFileSpraySoapBindingEx::xsltTransform(const char* xml, const char* sheet, IProperties *params, StringBuffer& ret)
 {
     StringBuffer xsl;
-    if (esp::readFile(sheet,xsl)<=0)
+    if (!checkFileExists(sheet))
         throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Cannot open stylesheet %s",sheet);
 
     Owned<IXslProcessor> proc  = getXslProcessor();
     Owned<IXslTransform> trans = proc->createXslTransform();
 
     trans->setXmlSource(xml, strlen(xml));
-    trans->setXslSource(xsl, xsl.length());
+    trans->loadXslFromFile(sheet);
 
     if (params)
     {

+ 4 - 6
esp/services/ws_topology/ws_topologyService.cpp

@@ -1712,16 +1712,14 @@ bool CWsTopologyEx::onTpGetComponentFile(IEspContext &context,
                 const char* plainText = req.getPlainText();
                 if (plainText && (!stricmp(plainText, "yes")))
                 {
-                    StringBuffer xslBuf, xmlBuf;
-                    if (esp::readFile("xslt/xmlformatter.xsl", xslBuf)<=0)
-                        throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Cannot open stylesheet xmlformatter.xsl");
+                    if (!checkFileExists("xslt/xmlformatter.xsl"))
+                        throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Could not find stylesheet xmlformatter.xsl");
 
                     Owned<IXslProcessor> proc  = getXslProcessor();
                     Owned<IXslTransform> trans = proc->createXslTransform();
 
-                    xmlBuf.append(buf.toByteArray(), 0, buf.length());
-                    trans->setXmlSource(xmlBuf.str(), xmlBuf.length());
-                    trans->setXslSource(xslBuf, xslBuf.length());
+                    trans->setXmlSource(pchBuf, buf.length());
+                    trans->loadXslFromFile("xslt/xmlformatter.xsl");
                     
                     StringBuffer htmlBuf;
                     trans->transform(htmlBuf);

+ 3 - 3
esp/services/ws_workunits/ws_workunitsService.cpp

@@ -2171,12 +2171,12 @@ void CWsWorkunitsEx::init(IPropertyTree *cfg, const char *process, const char *s
 void CWsWorkunitsEx::xsltTransform(const char* xml,const char* sheet,IProperties *params,StringBuffer& ret)
 {
     StringBuffer xsl;
-    if(readFile(sheet,xsl)<=0)
-        throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Cannot load stylesheet %s.",sheet);
+    if(!checkFileExists(sheet))
+        throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Could not find stylesheet %s.",sheet);
     Owned<IXslProcessor> proc = getXslProcessor();
     Owned<IXslTransform> trans = proc->createXslTransform();
     trans->setXmlSource(xml, strlen(xml));
-    trans->setXslSource(xsl, xsl.length());
+    trans->loadXslFromFile(sheet);
     if (params)
     {
         Owned<IPropertyIterator> it = params->getIterator();

+ 11 - 7
system/xmllib/xslcache.cpp

@@ -50,7 +50,7 @@ public:
 
     bool operator==(CXslIncludeSignature& incl)
     {
-        return (strieq(filePath.str(), incl.filePath.str())) && (fileSize == incl.fileSize) && (fileTime == incl.fileTime);
+        return (streq(filePath.str(), incl.filePath.str())) && (fileSize == incl.fileSize) && (fileTime == incl.fileTime);
     }
 
     const char* getPath()
@@ -164,7 +164,7 @@ public:
             return false;
         if(!matched && !match(entry))
             return false;
-        if (m_buffer->getType() == IO_TYPE_FILE && (fileModTime != entry->fileModTime || m_size != entry->m_size))
+        if (fileModTime != entry->fileModTime || m_size != entry->m_size)
             return false;
         if(m_includecomp.get() && m_includecomp->hasChanged())
             return false;
@@ -235,21 +235,25 @@ public:
 
     void freeOneCacheEntry()
     {
-        CXslEntry *oldest = NULL;
+        IMapping *oldest = NULL;
+        time_t oldTime = 0;
         HashIterator iter(xslMap);
         for (iter.first(); iter.isValid(); iter.next())
         {
             CXslEntry *entry = xslMap.mapToValue(&iter.query());
             if (entry->isExpired(m_cachetimeout))
             {
-                xslMap.remove(dynamic_cast<MappingStringToIInterface*>(&iter.query()));
+                xslMap.removeExact(&iter.query());
                 return;
             }
-            if (!oldest || entry->createTime < oldest->createTime)
-                oldest=entry;
+            if (!oldest || entry->createTime < oldTime)
+            {
+                oldest=&iter.query();
+                oldTime=entry->createTime;
+            }
         }
         if (oldest)
-            xslMap.remove(oldest->m_cacheId.sget());
+            xslMap.removeExact(oldest);
     }
 
     void setCacheTimeout(int timeout)

+ 11 - 3
system/xmllib/xslprocessor.cpp

@@ -545,16 +545,24 @@ int CXslTransform::setXmlSource(const char *pszBuffer, unsigned int nSize)
     return theResult;
 }
 
-int CXslTransform::setXslSource(const char *pszFileName, const char *cacheId)
+int CXslTransform::loadXslFromFile(const char *pszFileName, const char *cacheId)
 {
     m_xslsource.setown(new CXslSource(pszFileName, m_sourceResolver?m_sourceResolver->getIncludeHandler():NULL, cacheId));
 
     return 0;
 }
 
-int CXslTransform::setXslSource(const char *pszBuffer, unsigned int nSize, const char *rootpath, const char *cacheId)
+int CXslTransform::setXslSource(const char *pszBuffer, unsigned int nSize, const char *cacheId, const char *rootpath)
 {
-    m_xslsource.setown(new CXslSource(pszBuffer, nSize, m_sourceResolver?m_sourceResolver->getIncludeHandler():NULL, rootpath, cacheId));
+    assertex(cacheId && *cacheId);
+    m_xslsource.setown(new CXslSource(pszBuffer, nSize, m_sourceResolver?m_sourceResolver->getIncludeHandler():NULL, cacheId, rootpath));
+
+    return 0;
+}
+
+int CXslTransform::setXslNoCache(const char *pszBuffer, unsigned int nSize, const char *rootpath)
+{
+    m_xslsource.setown(new CXslSource(pszBuffer, nSize, m_sourceResolver?m_sourceResolver->getIncludeHandler():NULL, NULL, rootpath));
 
     return 0;
 }

+ 4 - 3
system/xmllib/xslprocessor.hpp

@@ -52,9 +52,10 @@ public:
  
     // setXslSource - specifies the source of the XSLT "script" to use
     //
-    virtual int setXslSource(const char *pszFileName, const char *cacheId=NULL) = 0;
-    virtual int setXslSource(const char *pszBuffer, unsigned int nSize, const char *rootpath=NULL, const char *cacheId=NULL) = 0;
- 
+    virtual int setXslSource(const char *pszBuffer, unsigned int nSize, const char *cacheId, const char *rootpath) = 0;
+    virtual int setXslNoCache(const char *pszBuffer, unsigned int nSize, const char *rootpath=NULL) = 0;
+    virtual int loadXslFromFile(const char *pszFileName, const char *altCacheId=NULL) = 0;
+
     // setResultTarget - Specifies where the post transform data is to be placed
     // must be set before calling parameterless variety of transform
     virtual int setResultTarget(const char *pszFileName) = 0; 

+ 4 - 3
system/xmllib/xslprocessor.ipp

@@ -181,7 +181,7 @@ public:
             setIncludeHandler(handler);
     }
 
-    CXslSource(const char* buf, int len, IIncludeHandler* handler, const char *rootpath = NULL, const char *cacheId=NULL) : m_XalanTransformer()
+    CXslSource(const char* buf, int len, IIncludeHandler* handler, const char *cacheId, const char *rootpath = NULL) : m_XalanTransformer()
     {
         m_xsltext.append(len, buf);
         m_sourcetype = IO_TYPE_BUFFER;
@@ -509,8 +509,9 @@ public:
 
     virtual int setXmlSource(const char *pszFileName);
     virtual int setXmlSource(const char *pszBuffer, unsigned int nSize);
-    virtual int setXslSource(const char *pszFileName, const char *cacheId=NULL);
-    virtual int setXslSource(const char *pszBuffer, unsigned int nSize, const char *rootpath, const char *cacheId=NULL);
+    virtual int loadXslFromFile(const char *pszFileName, const char *altCacheId=NULL);
+    virtual int setXslSource(const char *pszBuffer, unsigned int nSize, const char *cacheId, const char *rootpath);
+    virtual int setXslNoCache(const char *pszBuffer, unsigned int nSize, const char *rootpath);
     virtual int setResultTarget(char *pszBuffer, unsigned int nSize);
     virtual int setResultTarget(const char *pszFileName);
     virtual int closeResultTarget();

+ 1 - 1
tools/hidl/hidlcomp.cpp

@@ -5453,7 +5453,7 @@ void EspServInfo::write_esp_binding_ipp()
             "\t\t\tif (!strnicmp(xslFile, \"/esp/xslt/\", 10))\n"
             "\t\t\t\tif (!checkFileExists(xslpath.append(getCFD()).append(\"smc_xslt/\").append(xslFile+10).str()) && !checkFileExists(xslpath.append(getCFD()).append(\"xslt/\").append(xslFile+10).str()))\n"
             "\t\t\t\t\treturn;\n"
-            "\t\t\txform->setXslSource((xslpath.length()) ? xslpath.str() : xslFile);\n"
+            "\t\t\txform->loadXslFromFile((xslpath.length()) ? xslpath.str() : xslFile);\n"
             "\t\t\txform->setXmlSource(xml, strlen(xml)+1);\n"
             "\t\t\tif (params) xform->copyParameters(params);\n"
             "\t\t\txform->transform(output.clear());\n"