Procházet zdrojové kódy

HPCC-11819 Add option to output ESXDL to buffer

Signed-off-by: rpastrana <rodrigo.pastrana@lexisnexis.com>
rpastrana před 11 roky
rodič
revize
245da1dbeb

+ 7 - 3
tools/esdlcmd-xml/esdl2xml.hpp

@@ -44,7 +44,8 @@ public:
 
     void setVerbose(bool verbose){optVerbose = verbose;};
     bool getVerbose(){return optVerbose;};
-    void transform(const char * source, const char * outdir="")
+
+    void transform(const char * source, const char * outdir="" ,StringBuffer * out=NULL, bool generatefile=true)
     {
         if (added.getValue(source) == false)
         {
@@ -55,8 +56,11 @@ public:
                     fprintf(stdout, "Output directory not specified\n");
             }
 
-            ESDLcompiler hc(source, outdir);
+            ESDLcompiler hc(source, generatefile, outdir);
             hc.Process();
+            if (!generatefile && out)
+                out->append(hc.getEsxdlContent());
+
             added.setValue(source, true);
 
             if (optRecursive && hc.includes)
@@ -68,7 +72,7 @@ public:
                 for (ii=hc.includes;ii;ii=ii->next)
                 {
                    subfile.setf("%s%s.ecm", srcDir.str(), ii->pathstr.str());
-                   transform(subfile, outdir);
+                   transform(subfile, outdir, out, generatefile);
                 }
             }
         }

+ 25 - 19
tools/esdlcomp/esdlcomp.cpp

@@ -1099,7 +1099,7 @@ char* getTargetBase(const char* outDir, const char* src)
         return strdup(src);
 }
 
-ESDLcompiler::ESDLcompiler(const char * sourceFile,const char *outDir)
+ESDLcompiler::ESDLcompiler(const char * sourceFile, bool generatefile, const char *outDir)
 {
     //yydebug = 1;
     modules = NULL;
@@ -1126,14 +1126,17 @@ ESDLcompiler::ESDLcompiler(const char * sourceFile,const char *outDir)
 
     packagename = es_gettail(sourceFile);
 
-    if (!outDir || !*outDir)
-        outDir = srcDir.str();
+    if (generatefile)
+    {
+        if (!outDir || !*outDir)
+            outDir = srcDir.str();
 
-    char* targetBase = getTargetBase(outDir, sourceFile);
+        char* targetBase = getTargetBase(outDir, sourceFile);
 
-    esxdlo = es_createFile(targetBase,"xml");
+        esxdlo = es_createFile(targetBase,"xml");
 
-    free(targetBase);
+        free(targetBase);
+    }
 }
 
 ESDLcompiler::~ESDLcompiler()
@@ -1169,8 +1172,6 @@ ESDLcompiler::~ESDLcompiler()
         delete ser;
     }
 
-
-
     while (includes)
     {
         IncludeInfo *in=includes;
@@ -1233,33 +1234,30 @@ void ESDLcompiler::Process()
 
 void ESDLcompiler::write_esxdl()
 {
-    //create the *.esp file
-    gOutfile = esxdlo;
-
-    outf("<esxdl name=\"%s\">\n", name.str());
+    esxdlcontent.clear();
 
     VersionInfo * vi;
     for (vi=versions;vi;vi=vi->next)
     {
-        vi->write_esxdl();
+        vi->toString(esxdlcontent);
     }
 
     IncludeInfo * ii;
     for (ii=hcp->includes;ii;ii=ii->next)
     {
-        ii->write_esxdl();
+        ii->toString(esxdlcontent);
     }
 
     EspMessageInfo * mi;
     for (mi=msgs;mi;mi=mi->next)
     {
-        mi->write_esxdl();
+        mi->toString(esxdlcontent);
     }
 
     EspServInfo *si;
     for (si=servs;si;si=si->next)
     {
-        si->write_esxdl();
+        si->toString(esxdlcontent);
     }
 
     if (methods)
@@ -1267,12 +1265,20 @@ void ESDLcompiler::write_esxdl()
         EspMethodInfo *sm;
         for (sm=methods;sm;sm=sm->next)
         {
-            sm->write_esxdl();
+            sm->toString(esxdlcontent);
         }
     }
 
-    outs("</esxdl>");
-    gOutfile = -1;
+
+    if (esxdlo)
+    {
+        //create the *.esp file
+        StringBuffer tmp;
+        tmp.setf("<esxdl name=\"%s\">\n%s</esxdl>", name.str(), esxdlcontent.str());
+        gOutfile = esxdlo;
+        outs(tmp.str());
+        gOutfile = -1;
+    }
 }
 
 // end

+ 173 - 3
tools/esdlcomp/esdlcomp.h

@@ -24,6 +24,7 @@
 #include "esdl_utils.hpp"
 #include "../../system/include/platform.h"
 #include "jmutex.hpp"
+#include "jstring.hpp"
 
 #undef YYSTYPE
 #define YYSTYPE attribute
@@ -352,7 +353,43 @@ public:
         }
     }
 
-
+    void toStringXmlAttr(StringBuffer & out)
+    {
+        switch (mttype_)
+        {
+        case mt_string:
+        case mt_const_id:
+        {
+            if (!str_val_)
+            {
+                out.appendf(" %s=\"\"", getName());
+            }
+            else
+            {
+                int len=strlen(str_val_);
+                StrBuffer val;
+                if (len>=2 && *str_val_=='\"')
+                    val.append(str_val_, 1, strlen(str_val_)-2);
+                else
+                    val.append(str_val_);
+                StrBuffer encoded;
+                encodeXML(val.str(), encoded);
+                out.appendf(" %s=\"%s\"", getName(), encoded.str());
+            }
+            break;
+        }
+        case mt_double:
+            out.appendf(" %s=\"%f\"", getName(), double_val_);
+            break;
+        case mt_int:
+            out.appendf(" %s=\"%d\"", getName(), int_val_);
+            break;
+        case mt_none:
+        default:
+            out.appendf(" %s=\"\"", getName());
+            break;
+        }
+    }
 };
 
 inline MetaTagInfo* findMetaTag(MetaTagInfo *list, const char *name)
@@ -548,6 +585,29 @@ public:
         }
     }
 
+    void toStringXmlAttr(StringBuffer & out)
+    {
+        const char *xsd_type = getMetaString("xsd_type", NULL);
+        if (xsd_type)
+        {
+            if (*xsd_type=='\"')
+                xsd_type++;
+            const char *finger = strchr(xsd_type, ':');
+            if (finger)
+                xsd_type=finger+1;
+            StrBuffer TypeName(xsd_type);
+            TypeName.replace('\"', 0);
+            out.appendf(" complex_type=\"%s\"", TypeName.str());
+        }
+        else
+        {
+            char typestr[256]={0};
+            cat_type(typestr, 0, 0);
+
+            out.appendf(" %s=\"%s\"", (kind==TK_ESPSTRUCT) ? "complex_type" : "type", typestr);
+        }
+    }
+
     void write_esxdl()
     {
         if (flags & PF_TEMPLATE && !strcmp(templ, "ESParray"))
@@ -582,6 +642,45 @@ public:
         }
     };
 
+    void toString(StringBuffer & out)
+    {
+        if (flags & PF_TEMPLATE && !strcmp(templ, "ESParray"))
+        {
+            out.appendf("\t\t<EsdlArray name=\"%s\" ", name);
+            toStringXmlAttr(out);
+            for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+            {
+                mtag->toStringXmlAttr(out);
+            }
+        }
+        else if (kind==TK_ENUM)
+        {
+            out.appendf("\t\t<EsdlEnumItem name=\"%s\"", name);
+            for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+            {
+                mtag->toStringXmlAttr(out);
+            }
+        }
+        else if (kind==TK_ESPENUM)
+        {
+            out.appendf("\t\t<EsdlEnum name=\"%s\" enum_type=\"%s\"", name, typname);
+            for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+            {
+                mtag->toStringXmlAttr(out);
+            }
+        }
+        else
+        {
+            out.appendf("\t\t<EsdlElement name=\"%s\"", name);
+            toStringXmlAttr(out);
+            for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+            {
+                mtag->toStringXmlAttr(out);
+            }
+        }
+        out.append("/>\n");
+    };
+
     type_kind kind;
     char      *name;
     char      *templ;
@@ -683,6 +782,11 @@ public:
        outf(1, "<EsdlInclude file=\"%s\"/>\n", pathstr.str());
    }
 
+   void toString(StringBuffer & out)
+   {
+      out.appendf("\t<EsdlInclude file=\"%s\"/>\n", pathstr.str());
+   }
+
    StrBuffer pathstr;
    IncludeInfo  *next;
 };
@@ -704,6 +808,11 @@ public:
        outf(1, "<EsdlVersion name=\"%s\" version=\"%f\" />\n", version_name.str(), version_value);
    }
 
+   void toString(StringBuffer & out)
+   {
+       out.appendf("\t<EsdlVersion name=\"%s\" version=\"%f\" />\n", version_name.str(), version_value);
+   }
+
    StrBuffer version_name;
    double version_value;
    VersionInfo *next;
@@ -941,6 +1050,36 @@ public:
         }
     }
 
+    void toString(StringBuffer & out)
+    {
+        const char *esdltype = query_esxdl_type();
+        if (esdltype)
+        {
+            out.appendf("\t<%s name=\"%s\"", esdltype, name_);
+            if (base_ && *base_)
+            {
+                out.appendf(" base=\"%s\"", base_);
+            }
+            if (parent)
+            {
+                out.appendf(" base_type=\"%s\"", parent);
+            }
+            for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+            {
+                mtag->toStringXmlAttr(out);
+            }
+
+            out.append(">\n");
+
+            for (ParamInfo *param=attrs_; param; param=param->next)
+            {
+                param->toString(out);
+            }
+
+            out.appendf("\t</%s>\n", esdltype);
+        }
+    }
+
     MetaTagInfo     *tags;
     EspMessageInfo  *next;
 
@@ -1079,6 +1218,16 @@ public:
         outs("/>\n");
     }
 
+    void toString(StringBuffer & out)
+    {
+        out.appendf("\t<EsdlMethod name=\"%s\" request_type=\"%s\" response_type=\"%s\" ", name_, request_, response_);
+        for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+        {
+            mtag->toStringXmlAttr(out);
+        }
+        out.append("/>\n");
+    }
+
     MetaTagInfo     *tags;
     EspMethodInfo   *next;
 };
@@ -1258,6 +1407,22 @@ public:
         outs(2, "</EsdlService>");
     }
 
+    void toString(StringBuffer & out)
+    {
+        out.appendf("\t\t<EsdlService name=\"%s\" ", name_);
+        for (MetaTagInfo *mtag=tags; mtag; mtag=mtag->next)
+        {
+            mtag->toStringXmlAttr(out);
+        }
+        out.append(">\n");
+
+        for (EspMethodInfo *mth=methods; mth; mth=mth->next)
+        {
+            mth->toString(out);
+        }
+        out.append("\t\t</EsdlService>");
+    }
+
     EspStructInfo  *structs;
     EspMethodInfo   *methods;
     EspMountInfo    *mounts;
@@ -1270,7 +1435,7 @@ public:
 class ESDLcompiler
 {
 public:
-    ESDLcompiler(const char * sourceFile, const char * outDir);
+    ESDLcompiler(const char * sourceFile, bool generatefile, const char * outDir);
     ~ESDLcompiler();
 
     void Process();
@@ -1281,6 +1446,11 @@ public:
         return srcDir.str();
     }
 
+    const char * getEsxdlContent() const
+    {
+        return esxdlcontent.str();
+    }
+
     const char* getPackageName()
     {
         return packagename;
@@ -1293,7 +1463,7 @@ private:
     int esxdlo;
     char* packagename;
     StrBuffer srcDir;
-    StrBuffer outBuffer;
+    StringBuffer esxdlcontent;
 
 public:
     static CriticalSection m_critSect;