← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/image-module-fix into lp:zorba/image-module

 

Ghislain Fourny has proposed merging lp:~zorba-coders/zorba/image-module-fix into lp:zorba/image-module.

Requested reviews:
  Matthias Brantner (matthias-brantner)
  Chris Hillery (ceejatec)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/image-module-fix/+merge/108746

Suggest fixing some errors (== vs. !=) as well as fixing throwErrorWithQName in graphviz, which crashed because it was looking for the QName in the dynamic context instead of just building it. Other parts of this module having a similar throwErrorWithQName implementation might need to be fixed too.
-- 
https://code.launchpad.net/~zorba-coders/zorba/image-module-fix/+merge/108746
Your team Zorba Coders is subscribed to branch lp:zorba/image-module.
=== modified file 'src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.cpp'
--- src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.cpp	2012-03-10 01:09:45 +0000
+++ src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.cpp	2012-06-05 13:37:36 +0000
@@ -57,12 +57,12 @@
 /******************************************************************************
  *****************************************************************************/
 std::string
-GraphvizFunction::getGraphvizTmpFileName(const zorba::DynamicContext*  aDctx) {
+GraphvizFunction::getGraphvizTmpFileName(zorba::ItemFactory* aFactory) {
   char lTmpDir[FILENAME_MAX];
 
   if (!GetCurrentDir(lTmpDir, sizeof(lTmpDir)))
   {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "Test");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "Test");
   }
   zorba::String test(lTmpDir);
   std::ostringstream  lTmpFileNameTemplate;
@@ -90,7 +90,7 @@
   while (lAttributes->next(attr)) {
     Item lNodeName;
     attr.getNodeName(lNodeName);
-    if (lNodeName.getLocalName() != lIdQName.getLocalName()) {
+    if (lNodeName.getLocalName() == lIdQName.getLocalName()) {
       return true;
     }
   }
@@ -101,7 +101,7 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::printTypeAndAttr(const zorba::DynamicContext*  aDctx,
+GraphvizFunction::printTypeAndAttr(
     ItemFactory* aFactory,
     const Item& in,
     std::fstream& os)
@@ -125,7 +125,7 @@
     if (lNodeName.getLocalName() != lAttrQName.getLocalName()) {
       Item lNameAttr;
       if (!getAttribute(aFactory, "name", lItem, lNameAttr)) {
-        GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: attr node does not have a name attribute");
+        GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: attr node does not have a name attribute");
       }
 
       os << "    \"" << lNameAttr.getStringValue() << "\"=\"";
@@ -152,7 +152,7 @@
     } else if (lNodeName.getStringValue() == lTypeQName.getStringValue()) {
       Item lHRefAttr;
       if (!getAttribute(aFactory, "href", lItem, lHRefAttr)) {
-        GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: type node does not have a href attribute");
+        GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: type node does not have a href attribute");
       }
 
       os << "    _gxl_type=\"" << lHRefAttr.getStringValue() << "\"" << std::endl;
@@ -163,19 +163,19 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::visitNode(const zorba::DynamicContext*  aDctx,
+GraphvizFunction::visitNode(
     ItemFactory* aFactory,
     const Item& in, std::fstream& os)
 {
   Item lItem;
   if (!getAttribute(aFactory, "id", in, lItem)) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: node does not have an id attribute");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: node does not have an id attribute");
   }
 
   // start node with id attribute as name
   os << "  \"" << lItem.getStringValue() << "\" [ " << std::endl;
 
-  printTypeAndAttr(aDctx, aFactory, in, os);
+  printTypeAndAttr(aFactory, in, os);
 
   // end of node
   os << "  ]" << std::endl;
@@ -185,7 +185,7 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::visitEdge(const zorba::DynamicContext*  aDctx,
+GraphvizFunction::visitEdge(
     ItemFactory* aFactory,
     const Item& in, std::fstream& os)
 {
@@ -194,19 +194,19 @@
   Item lToAttr;
 
   if (!getAttribute(aFactory, "id", in, lIdAttr)) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: edge does not have an 'id' attribute");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have an 'id' attribute");
   }
   if (!getAttribute(aFactory, "to", in, lToAttr)) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: edge does not have a 'to' attribute");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have a 'to' attribute");
   }
   if (!getAttribute(aFactory, "from", in, lFromAttr)) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: edge does not have a 'from' attribute");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have a 'from' attribute");
   }
 
   os << "  \"" << lFromAttr.getStringValue() << "\" -> \"" << lToAttr.getStringValue() << "\" [ " << std::endl
      << "    _gxl_id=\"" << lIdAttr.getStringValue() << "\"" << std::endl;
   
-  printTypeAndAttr(aDctx,aFactory, in, os);
+  printTypeAndAttr(aFactory, in, os);
 
   os << "  ]" << std::endl;
 } /* GraphvizFunction::visitEdge */
@@ -214,7 +214,7 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::printGraph(const zorba::DynamicContext*  aDctx,
+GraphvizFunction::printGraph(
     ItemFactory* aFactory,
     const Item& in, std::fstream& os)
 {
@@ -226,7 +226,7 @@
   // print the graph with all its children
   Item lGraphId;
   if (!getAttribute(aFactory, "id", in, lGraphId)) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: edge does not have an 'id' attribute");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have an 'id' attribute");
   }
 
   os << "digraph \"" << lGraphId.getStringValue() << "\" {" << std::endl;
@@ -239,9 +239,9 @@
     Item lNodeName;
     item.getNodeName(lNodeName);
     if (lNodeName.getLocalName() == lNodeQName.getLocalName()) {
-      visitNode(aDctx, aFactory, item, os);
+      visitNode(aFactory, item, os);
     } else if (lNodeName.getLocalName() == lEdgeQName.getLocalName()) {
-      visitEdge(aDctx, aFactory, item, os);
+      visitEdge(aFactory, item, os);
     } 
   }
 
@@ -251,7 +251,7 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::gxl2dot(const zorba::DynamicContext*  aDctx,
+GraphvizFunction::gxl2dot(
     ItemFactory* aFactory,
     const Item& in, std::fstream& os)
 {
@@ -259,12 +259,12 @@
   Item lGraphQName = aFactory->createQName("", "", "graph");
 
   if (!in.isNode()) {
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: item is not a node");
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: item is not a node");
   }
 
   Item lNodeName;
   in.getNodeName(lNodeName);
-  if (lNodeName.getLocalName() == lGXLQName.getLocalName()) {
+  if (lNodeName.getLocalName() != lGXLQName.getLocalName()) {
     Item lNodeName;
     in.getNodeName(lNodeName);
 
@@ -272,7 +272,7 @@
     lErrorMsg << "GXL parse error: only element with name "
               << lGXLQName.getStringValue() << " allowed (got " << lNodeName.getStringValue()
               << ").";
-    GraphvizFunction::throwErrorWithQName(aDctx, "IM003", lErrorMsg.str());
+    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", lErrorMsg.str());
   }
 
   Iterator_t lGraphs = in.getChildren();
@@ -281,11 +281,11 @@
   Item lGraph;
   while(lGraphs->next(lGraph)) {
     if (!lGraph.isNode()) {
-      GraphvizFunction::throwErrorWithQName(aDctx, "IM003", "GXL parse error: item is not a node");
+      GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: item is not a node");
     }
 
     lGraph.getNodeName(lNodeName);
-    if (lNodeName.getLocalName() == lGraphQName.getLocalName()) {
+    if (lNodeName.getLocalName() != lGraphQName.getLocalName()) {
       std::ostringstream lErrorMsg;
       Item lNodeName;
       lGraph.getNodeName(lNodeName);
@@ -293,10 +293,10 @@
       lErrorMsg << "GXL parse error: only elements with name "
                 << lGraphQName.getStringValue() << " allowed (got "
                 << lNodeName.getLocalName() << ").";
-      GraphvizFunction::throwErrorWithQName(aDctx, "IM003", lErrorMsg.str());
+      GraphvizFunction::throwErrorWithQName(aFactory, "IM003", lErrorMsg.str());
     }
 
-    printGraph(aDctx, aFactory, lGraph, os);
+    printGraph(aFactory, lGraph, os);
 
   }
 
@@ -305,23 +305,21 @@
 /******************************************************************************
  *****************************************************************************/
 void
-GraphvizFunction::throwErrorWithQName (const DynamicContext* aDynamicContext,
+GraphvizFunction::throwErrorWithQName (ItemFactory* aFactory,
                                        const String& aLocalName,
                                        const String& aMessage) {
    String lNamespace = "http://www.zorba-xquery.com/modules/image/error";;
-   Item lQName;
-   Iterator_t lDummyIterator;
-   aDynamicContext->getVariable(lNamespace, aLocalName, lQName, lDummyIterator);
+   Item lQName = aFactory->createQName(lNamespace, "", aLocalName);
    throw USER_EXCEPTION(lQName, aMessage);
 }
+
 /******************************************************************************
  *****************************************************************************/
-DotFunction::LazyDotSequence::LazyDotSequence(const DotFunction* aFunc,
-    ItemSequence* aArg,
-    const zorba::DynamicContext* aDctx)
+DotFunction::LazyDotSequence::LazyDotSequence(
+    const DotFunction* aFunc,
+    ItemSequence* aArg)
   : theFunc(aFunc),
-    theArg(aArg),
-    theDctx(aDctx)
+    theArg(aArg)
 {
 }
 
@@ -360,7 +358,7 @@
 {
   if(!is_open)
   {
-    GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "DotFunction::LazyDotSequence Iterator consumed without open");
+    GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "DotFunction::LazyDotSequence Iterator consumed without open");
   }
   Item          lItem;
   Agraph_t      *lGraph = 0;
@@ -378,18 +376,18 @@
 
     lGraph = agmemread(const_cast<char*>(lGraphInput.c_str()));
     if (!lGraph) {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not read input");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not read input");
     }
 
     if ( gvLayout(lGvc, lGraph, const_cast<char*>("dot")) != 0 ) {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not generate layout");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not generate layout");
     }
 
-    std::string lTmpFile = theItemSequence->theFunc->getGraphvizTmpFileName(theItemSequence->getDctx());
+    std::string lTmpFile = theItemSequence->theFunc->getGraphvizTmpFileName(theItemSequence->theFunc->theModule->getItemFactory());
 
     if ( gvRenderFilename(lGvc, lGraph, const_cast<char*>("svg"),
                           const_cast<char*>(lTmpFile.c_str())) != 0 )  {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not render graph");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not render graph");
     }
 
     lSVGFile.open(lTmpFile.c_str());
@@ -397,7 +395,7 @@
       std::ostringstream lErrorMsg;
       lErrorMsg << "could not read from file "
                 << lTmpFile.c_str();
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", lErrorMsg.str());
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", lErrorMsg.str());
     }
 
     aItem = Zorba::getInstance(0)->getXmlDataManager()->parseXML(lSVGFile);
@@ -456,7 +454,7 @@
 {
   if(!is_open)
   {
-    GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "GxlFunction::LazyGxlSequence Iterator consumed without open");
+    GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "GxlFunction::LazyGxlSequence Iterator consumed without open");
   }
   Item              lItem;
   Agraph_t          *lGraph = 0;
@@ -471,42 +469,42 @@
 
   try {
 
-    std::string lTmpFile = theItemSequence->theFunc->getGraphvizTmpFileName(theItemSequence->getDctx());
+    std::string lTmpFile = theItemSequence->theFunc->getGraphvizTmpFileName(theItemSequence->theFunc->theModule->getItemFactory());
     lSVGFile.open(lTmpFile.c_str(),
         std::fstream::in | std::fstream::out | std::fstream::trunc);
 
-    gxl2dot(theItemSequence->getDctx(), theItemSequence->theFunc->theModule->getItemFactory(), lItem, lSVGFile);
+    gxl2dot(theItemSequence->theFunc->theModule->getItemFactory(), lItem, lSVGFile);
     lSVGFile.close();
 
     lFile = fopen(lTmpFile.c_str(), "r");
     if (!lFile) {
       std::ostringstream lErrorMsg;
       lErrorMsg << "could not read from file " << lTmpFile.c_str();
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", lErrorMsg.str());
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", lErrorMsg.str());
     }
 
     lGraph = agread(lFile);
     fclose(lFile);
 
     if (!lGraph) {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not read input");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not read input");
     }
 
     int blub =  gvLayout(lGvc, lGraph, const_cast<char*>("dot"));
     if ( blub != 0 ) {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not generate layout");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not generate layout");
     }
 
     if ( gvRenderFilename(lGvc, lGraph, const_cast<char*>("svg"),
                           const_cast<char*>(lTmpFile.c_str())) != 0 )  {
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", "could not render graph");
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", "could not render graph");
     }
 
     lSVGFile.open(lTmpFile.c_str(), std::ifstream::in);
     if (lSVGFile.bad()) {
       std::ostringstream lErrorMsg;
       lErrorMsg << "could not read from file " << lTmpFile.c_str();
-      GraphvizFunction::throwErrorWithQName(theItemSequence->getDctx(), "IM003", lErrorMsg.str());
+      GraphvizFunction::throwErrorWithQName(theItemSequence->theFunc->theModule->getItemFactory(), "IM003", lErrorMsg.str());
     }
 
     aItem = Zorba::getInstance(0)->getXmlDataManager()->parseXML(lSVGFile);
@@ -531,12 +529,11 @@
 
 /******************************************************************************
  *****************************************************************************/
-GxlFunction::LazyGxlSequence::LazyGxlSequence(const GxlFunction* aFunc,
-    ItemSequence* aArg,
-    const zorba::DynamicContext*  aDctx)
+GxlFunction::LazyGxlSequence::LazyGxlSequence(
+    const GxlFunction* aFunc,
+    ItemSequence* aArg)
   : theFunc(aFunc),
-    theArg(aArg),
-    theDctx(aDctx)
+    theArg(aArg)
 {
 }
 
@@ -546,9 +543,9 @@
 DotFunction::evaluate(
   const Arguments_t& aArgs,
   const zorba::StaticContext* aSctx,
-  const zorba::DynamicContext* aDctx) const
+  const zorba::DynamicContext *aDctx) const
 {
-  return ItemSequence_t(new LazyDotSequence(this, aArgs[0], aDctx));
+  return ItemSequence_t(new LazyDotSequence(this, aArgs[0]));
 } /* DotFunction::evaluate */
 
 /******************************************************************************
@@ -557,9 +554,9 @@
 GxlFunction::evaluate(
   const Arguments_t& aArgs,
   const zorba::StaticContext* aSctx,
-  const zorba::DynamicContext* aDctx) const
+  const zorba::DynamicContext *aDctx) const
 {
-  return ItemSequence_t(new LazyGxlSequence(this, aArgs[0], aDctx));
+  return ItemSequence_t(new LazyGxlSequence(this, aArgs[0]));
 } /* GxlFunction::evaluate */
 
 /******************************************************************************

=== modified file 'src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.h'
--- src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.h	2011-10-06 08:18:47 +0000
+++ src/com/zorba-xquery/www/modules/image/graphviz.xq.src/graphviz.h	2012-06-05 13:37:36 +0000
@@ -26,9 +26,11 @@
 
 namespace zorba
 {
-  namespace graphvizmodule
-  {
-    class GraphvizModule;
+namespace graphvizmodule
+{
+
+class GraphvizModule;
+
 /******************************************************************************
  *****************************************************************************/
 class GraphvizFunction : public ContextualExternalFunction
@@ -40,7 +42,7 @@
     : theModule(aModule) {}
 
     static std::string
-    getGraphvizTmpFileName(const zorba::DynamicContext*  aDctx);
+    getGraphvizTmpFileName(zorba::ItemFactory* aFactory);
 
     static bool
     getAttribute(zorba::ItemFactory* aFactory,
@@ -49,33 +51,28 @@
         zorba::Item& attr);
 
     static void
-    printTypeAndAttr(const zorba::DynamicContext*  aDctx,
-                     zorba::ItemFactory* aFactory,
+    printTypeAndAttr(zorba::ItemFactory* aFactory,
                      const zorba::Item& in,
                      std::fstream& os);
 
     static void
-    visitNode(const zorba::DynamicContext*  aDctx,
-              zorba::ItemFactory* aFactory,
-              const zorba::Item& in, std::fstream& os);
-
-    static void
-    visitEdge(const zorba::DynamicContext*  aDctx,
-              zorba::ItemFactory* aFactory,
-              const zorba::Item& in, std::fstream& os);
-
-    static void
-    printGraph(const zorba::DynamicContext*  aDctx,
-               zorba::ItemFactory* aFactory,
+    visitNode(zorba::ItemFactory* aFactory,
+              const zorba::Item& in, std::fstream& os);
+
+    static void
+    visitEdge(zorba::ItemFactory* aFactory,
+              const zorba::Item& in, std::fstream& os);
+
+    static void
+    printGraph(zorba::ItemFactory* aFactory,
                const zorba::Item& in, std::fstream& os);
 
     static void
-    gxl2dot(const zorba::DynamicContext*  aDctx,
-            zorba::ItemFactory* aFactory,
+    gxl2dot(zorba::ItemFactory* aFactory,
             const zorba::Item& in, std::fstream& os);
 
     static void
-    throwErrorWithQName (const DynamicContext* aDynamicContext,
+    throwErrorWithQName (zorba::ItemFactory* aFactory,
                          const String& aLocalName,
                          const String& aMessage);
 
@@ -122,17 +119,13 @@
     };
     public:
       LazyDotSequence(const DotFunction*,
-                      ItemSequence* aArg,
-                      const zorba::DynamicContext*  aDctx);
+                      ItemSequence* aArg);
 
       virtual Iterator_t    getIterator();
 
-      const zorba::DynamicContext* getDctx() {return theDctx;};
-
     protected:
       const DotFunction*            theFunc;
       ItemSequence*                 theArg;
-      const zorba::DynamicContext*  theDctx;
   };
 };
 
@@ -173,17 +166,13 @@
     };
     public:
       LazyGxlSequence(const GxlFunction*,
-                      ItemSequence* aArg,
-                      const zorba::DynamicContext*  aDctx);
+                      ItemSequence* aArg);
 
       Iterator_t  getIterator();
 
-      const zorba::DynamicContext* getDctx() {return theDctx;};
-
     protected:
       const GxlFunction*            theFunc;
       ItemSequence*                 theArg;
-      const zorba::DynamicContext*  theDctx;
   };
 };
 
@@ -239,7 +228,7 @@
   }
 };
 
-  } /* namespace zorba */
+} /* namespace zorba */
 } /* namespace graphvizmodule */
 
 #endif


Follow ups