Esempio n. 1
0
        public bool loadFile(cape.LoggingContext ctx, cape.File file)
        {
            if (!(file != null))
            {
                ctx.logError("DynamicModule" + ": Null file");
                return(false);
            }
            if (!file.isFile())
            {
                ctx.logError("DynamicModule" + ": Not a file: `" + file.getPath() + "'");
                return(false);
            }
            ctx.logDebug("Loading dynamic module: `" + file.getPath() + "'");
            string error = null;

            ctx.logError("Loading assemblies on UWP: Not supported");
            if (assembly == null)
            {
                if (error != null)
                {
                    ctx.logDebug(error);
                }
                ctx.logError("Failed to load assembly: `" + file.getPath() + "'");
                return(false);
            }
            ctx.logDebug("Assembly successfully loaded: `" + file.getPath() + "'");
            return(true);
        }
Esempio n. 2
0
 public cape.ProcessLauncher addToParams(cape.File file)
 {
     if (file != null)
     {
         addToParams(file.getPath());
     }
     return(this);
 }
Esempio n. 3
0
        public virtual bool isSame(cape.File file)
        {
            if (!(file != null))
            {
                return(false);
            }
            var path = getPath();

            if (path != null && object.Equals(path, file.getPath()))
            {
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
 public static bool openFile(cape.File file)
 {
     if (cape.OS.isWindows())
     {
         var pl = cape.ProcessLauncher.forCommand("explorer", new object[] {
             file.getPath()
         });
         if (pl.executeSilent() == 0)
         {
             return(true);
         }
         return(false);
     }
     if (cape.OS.isOSX())
     {
         var pl1 = cape.ProcessLauncher.forCommand("open", new object[] {
             file.getPath()
         });
         if (pl1.executeSilent() == 0)
         {
             return(true);
         }
         return(false);
     }
     if (cape.OS.isLinux())
     {
         var pl2 = cape.ProcessLauncher.forCommand("xdg-open", new object[] {
             file.getPath()
         });
         if (pl2.executeSilent() == 0)
         {
             return(true);
         }
         return(false);
     }
     return(false);
 }
Esempio n. 5
0
 public override bool addFile(cape.File file, string filename)
 {
     if (!(archive != null))
     {
         return(false);
     }
     if (!(file != null))
     {
         return(false);
     }
     if (System.IO.Compression.ZipFileExtensions.CreateEntryFromFile(archive, file.getPath(), filename) == null)
     {
         return(false);
     }
     return(true);
 }
Esempio n. 6
0
        /// <summary>
        /// Produces a string representation of this command with the command itself,
        /// parameters and environment variables included.
        /// </summary>

        public string toString(bool includeEnv = true)
        {
            var sb = new cape.StringBuilder();

            if (includeEnv)
            {
                System.Collections.Generic.List <string> keys = cape.Map.getKeys(env);
                if (keys != null)
                {
                    var n = 0;
                    var m = keys.Count;
                    for (n = 0; n < m; n++)
                    {
                        var key = keys[n];
                        if (key != null)
                        {
                            sb.append(key);
                            sb.append("=");
                            sb.append(env[key]);
                            sb.append(" ");
                        }
                    }
                }
            }
            sb.append("\"");
            if (file != null)
            {
                sb.append(file.getPath());
            }
            sb.append("\"");
            if (@params != null)
            {
                var n2 = 0;
                var m2 = @params.Count;
                for (n2 = 0; n2 < m2; n2++)
                {
                    var p = @params[n2];
                    if (p != null)
                    {
                        appendProperParam(sb, p);
                    }
                }
            }
            return(sb.toString());
        }
Esempio n. 7
0
        public capex.util.ZipReaderForDotNet initialize()
        {
            if (!(file != null))
            {
                return(null);
            }
            var fp = file.getPath();

            if (!(fp != null))
            {
                return(null);
            }
            archive = System.IO.Compression.ZipFile.Open(fp, System.IO.Compression.ZipArchiveMode.Read);
            if (!(archive != null))
            {
                return(null);
            }
            return(this);
        }
Esempio n. 8
0
        public override bool move(cape.File dest, bool replace)
        {
            onError(null);
            if (!(dest != null))
            {
                onError("null destination");
                return(false);
            }
            if (dest.exists())
            {
                if (!replace)
                {
                    onError("target file already exists");
                    return(false);
                }
                if (dest.remove() == false)
                {
                    onError("Error when removing `" + dest.getPath() + "': " + dest.getLastErrorDescription());
                    return(false);
                }
            }
            var destf = dest as cape.FileForDotNet;

            if (!(destf != null))
            {
                return(false);
            }
            var v = true;

            try {
                System.IO.File.Move(completePath, destf.completePath);
            }
            catch (System.Exception e) {
                onError(e.ToString());
                v = false;
            }
            return(v);
        }
Esempio n. 9
0
        public capex.util.ZipWriterForDotNet initialize()
        {
            if (!(file != null))
            {
                return(null);
            }
            var fp = file.getPath();

            if (!(fp != null))
            {
                return(null);
            }
            try {
                archive = System.IO.Compression.ZipFile.Open(fp, System.IO.Compression.ZipArchiveMode.Create);
            }
            catch (System.Exception e) {
                return(null);
            }
            if (!(archive != null))
            {
                return(null);
            }
            return(this);
        }
Esempio n. 10
0
        private bool doExecute(System.Collections.Generic.List <object> data, cape.DynamicMap avars, cape.StringBuilder result, System.Collections.Generic.List <cape.File> includeDirs)
        {
            if (data == null)
            {
                return(true);
            }
            var blockctr = 0;

            System.Collections.Generic.List <object> blockdata = null;
            System.Collections.Generic.List <string> blocktag  = null;
            var vars = avars;

            if (vars == null)
            {
                vars = new cape.DynamicMap();
            }
            if (data != null)
            {
                var n2 = 0;
                var m  = data.Count;
                for (n2 = 0; n2 < m; n2++)
                {
                    var o = data[n2];
                    if (o != null)
                    {
                        string tagname = null;
                        System.Collections.Generic.List <string> words = null;
                        var tagData = o as capex.text.TextTemplate.TagData;
                        if (tagData != null)
                        {
                            words = tagData.words;
                            if (words != null)
                            {
                                tagname = cape.Vector.get(words, 0);
                                if (cape.String.isEmpty(tagname))
                                {
                                    cape.Log.warning(logContext, "Empty tag encountered. Ignoring it.");
                                    continue;
                                }
                            }
                        }
                        if (object.Equals(tagname, "end"))
                        {
                            blockctr--;
                            if (blockctr == 0 && blockdata != null)
                            {
                                if (handleBlock(vars, blocktag, blockdata, result, includeDirs) == false)
                                {
                                    cape.Log.error(logContext, "Handling of a block failed");
                                    continue;
                                }
                                blockdata = null;
                                blocktag  = null;
                            }
                        }
                        if (blockctr > 0)
                        {
                            if (object.Equals(tagname, "for") || object.Equals(tagname, "if"))
                            {
                                blockctr++;
                            }
                            if (blockdata == null)
                            {
                                blockdata = new System.Collections.Generic.List <object>();
                            }
                            blockdata.Add(o);
                            continue;
                        }
                        if (o is string || o is cape.StringObject)
                        {
                            result.append(cape.String.asString(o));
                            continue;
                        }
                        if (object.Equals(tagname, "=") || object.Equals(tagname, "printstring"))
                        {
                            var varname = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(varname) == false)
                            {
                                var vv = getVariableValueString(vars, varname);
                                if (cape.String.isEmpty(vv) == false)
                                {
                                    if (this.type == capex.text.TextTemplate.TYPE_HTML)
                                    {
                                        vv = capex.text.HTMLString.sanitize(vv);
                                    }
                                    result.append(vv);
                                }
                                else
                                {
                                    var defaultvalue = substituteVariables(cape.Vector.get(words, 2), vars);
                                    if (cape.String.isEmpty(defaultvalue) == false)
                                    {
                                        if (this.type == capex.text.TextTemplate.TYPE_HTML)
                                        {
                                            defaultvalue = capex.text.HTMLString.sanitize(defaultvalue);
                                        }
                                        result.append(defaultvalue);
                                    }
                                }
                            }
                        }
                        else if (object.Equals(tagname, "printRaw"))
                        {
                            var varname1 = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(varname1) == false)
                            {
                                var vv1 = getVariableValueString(vars, varname1);
                                if (cape.String.isEmpty(vv1) == false)
                                {
                                    result.append(vv1);
                                }
                                else
                                {
                                    var defaultvalue1 = substituteVariables(cape.Vector.get(words, 2), vars);
                                    if (cape.String.isEmpty(defaultvalue1) == false)
                                    {
                                        result.append(defaultvalue1);
                                    }
                                }
                            }
                        }
                        else if (object.Equals(tagname, "catPath"))
                        {
                            var hasSlash = false;
                            var n        = 0;
                            if (words != null)
                            {
                                var n3 = 0;
                                var m2 = words.Count;
                                for (n3 = 0; n3 < m2; n3++)
                                {
                                    var word = words[n3];
                                    if (word != null)
                                    {
                                        n++;
                                        if (n == 1)
                                        {
                                            continue;
                                        }
                                        word = substituteVariables(word, vars);
                                        var it = cape.String.iterate(word);
                                        if (it == null)
                                        {
                                            continue;
                                        }
                                        if (n > 2 && hasSlash == false)
                                        {
                                            result.append('/');
                                            hasSlash = true;
                                        }
                                        while (true)
                                        {
                                            var c = it.getNextChar();
                                            if (c < 1)
                                            {
                                                break;
                                            }
                                            if (c == '/')
                                            {
                                                if (hasSlash == false)
                                                {
                                                    result.append(c);
                                                    hasSlash = true;
                                                }
                                            }
                                            else
                                            {
                                                result.append(c);
                                                hasSlash = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (object.Equals(tagname, "printJson"))
                        {
                            var varname2 = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(varname2) == false)
                            {
                                var vv2 = getVariableValue(vars, varname2);
                                if (vv2 != null)
                                {
                                    result.append(cape.JSONEncoder.encode(vv2));
                                }
                            }
                        }
                        else if (object.Equals(tagname, "printRichText"))
                        {
                            var markup = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(markup) == false)
                            {
                                var doc = capex.text.RichTextDocument.forWikiMarkupString(markup);
                                if (doc != null)
                                {
                                    result.append(doc.toHtml(null));
                                }
                            }
                        }
                        else if (object.Equals(tagname, "printDate"))
                        {
                            var timestamp = substituteVariables(cape.Vector.get(words, 1), vars);
                            var aslong    = cape.String.toLong(timestamp);
                            var asstring  = cape.DateTime.forTimeSeconds(aslong).toStringDate('/');
                            result.append(asstring);
                        }
                        else if (object.Equals(tagname, "printTime"))
                        {
                            var timestamp1 = substituteVariables(cape.Vector.get(words, 1), vars);
                            var aslong1    = cape.String.toLong(timestamp1);
                            var asstring1  = cape.DateTime.forTimeSeconds(aslong1).toStringTime(':');
                            result.append(asstring1);
                        }
                        else if (object.Equals(tagname, "printDateTime"))
                        {
                            var timestamp2 = substituteVariables(cape.Vector.get(words, 1), vars);
                            var aslong2    = cape.String.toLong(timestamp2);
                            var dt         = cape.DateTime.forTimeSeconds(aslong2);
                            result.append(dt.toStringDate('/'));
                            result.append(' ');
                            result.append(dt.toStringTime(':'));
                        }
                        else if (object.Equals(tagname, "import"))
                        {
                            var type     = cape.Vector.get(words, 1);
                            var filename = substituteVariables(cape.Vector.get(words, 2), vars);
                            if (cape.String.isEmpty(filename))
                            {
                                cape.Log.warning(logContext, "Invalid import tag with empty filename");
                                continue;
                            }
                            cape.File ff = null;
                            if (includeDirs != null)
                            {
                                var n4 = 0;
                                var m3 = includeDirs.Count;
                                for (n4 = 0; n4 < m3; n4++)
                                {
                                    var dir = includeDirs[n4];
                                    if (dir != null)
                                    {
                                        ff = cape.FileInstance.forRelativePath(filename, dir);
                                        if (ff != null && ff.isFile())
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            if (ff == null || ff.isFile() == false)
                            {
                                cape.Log.error(logContext, "Unable to find file to import: `" + filename + "'");
                                continue;
                            }
                            cape.Log.debug(logContext, "Attempting to import file: `" + ff.getPath() + "'");
                            var content = ff.getContentsString("UTF-8");
                            if (cape.String.isEmpty(content))
                            {
                                cape.Log.error(logContext, "Unable to read import file: `" + ff.getPath() + "'");
                                continue;
                            }
                            if (object.Equals(type, "html"))
                            {
                                content = capex.text.HTMLString.sanitize(content);
                            }
                            else if (object.Equals(type, "template"))
                            {
                                var t = capex.text.TextTemplate.forString(content, markerBegin, markerEnd, this.type, includeDirs);
                                if (t == null)
                                {
                                    cape.Log.error(logContext, "Failed to parse imported template file: `" + ff.getPath() + "'");
                                    continue;
                                }
                                if (doExecute(t.getTokens(), vars, result, includeDirs) == false)
                                {
                                    cape.Log.error(logContext, "Failed to process imported template file: `" + ff.getPath() + "'");
                                    continue;
                                }
                                content = null;
                            }
                            else if (object.Equals(type, "raw"))
                            {
                                ;
                            }
                            else
                            {
                                cape.Log.error(logContext, "Unknown type for import: `" + type + "'. Ignoring the import.");
                                continue;
                            }
                            if (cape.String.isEmpty(content) == false)
                            {
                                result.append(content);
                            }
                        }
                        else if (object.Equals(tagname, "escapeHtml"))
                        {
                            var content1 = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(content1) == false)
                            {
                                content1 = capex.text.HTMLString.sanitize(content1);
                                if (cape.String.isEmpty(content1) == false)
                                {
                                    result.append(content1);
                                }
                            }
                        }
                        else if (object.Equals(tagname, "set"))
                        {
                            if (cape.Vector.getSize(words) != 3)
                            {
                                cape.Log.warning(logContext, "Invalid number of parameters for set tag encountered: " + cape.String.forInteger(cape.Vector.getSize(words)));
                                continue;
                            }
                            var varname3 = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(varname3))
                            {
                                cape.Log.warning(logContext, "Empty variable name in set tag encountered.");
                                continue;
                            }
                            var newValue = substituteVariables(cape.Vector.get(words, 2), vars);
                            vars.set(varname3, (object)newValue);
                        }
                        else if (object.Equals(tagname, "assign"))
                        {
                            if (cape.Vector.getSize(words) != 3)
                            {
                                cape.Log.warning(logContext, "Invalid number of parameters for assign tag encountered: " + cape.String.forInteger(cape.Vector.getSize(words)));
                                continue;
                            }
                            var varname4 = substituteVariables(cape.Vector.get(words, 1), vars);
                            if (cape.String.isEmpty(varname4))
                            {
                                cape.Log.warning(logContext, "Empty variable name in assign tag encountered.");
                                continue;
                            }
                            var vv3 = cape.Vector.get(words, 2);
                            if (object.Equals(vv3, "none"))
                            {
                                vars.remove(varname4);
                            }
                            else
                            {
                                vars.set(varname4, getVariableValue(vars, vv3));
                            }
                        }
                        else if (object.Equals(tagname, "for") || object.Equals(tagname, "if"))
                        {
                            if (blockctr == 0)
                            {
                                blocktag = words;
                            }
                            blockctr++;
                        }
                        else if (object.Equals(tagname, "end"))
                        {
                            ;
                        }
                        else if (type == capex.text.TextTemplate.TYPE_HTML)
                        {
                            onHTMLTag(vars, result, includeDirs, tagname, words);
                        }
                        else if (type == capex.text.TextTemplate.TYPE_JSON)
                        {
                            onJSONTag(vars, result, includeDirs, tagname, words);
                        }
                        else
                        {
                            ;
                        }
                    }
                }
            }
            return(true);
        }
Esempio n. 11
0
        private System.Collections.Generic.List <object> tokenizeString(string inputdata, System.Collections.Generic.List <cape.File> includeDirs)
        {
            if (object.Equals(markerBegin, null) || object.Equals(markerEnd, null))
            {
                cape.Log.error(logContext, "No template markers were given");
                return(null);
            }
            if (cape.String.getLength(markerBegin) != 2 || cape.String.getLength(markerEnd) != 2)
            {
                cape.Log.error(logContext, "Invalid template markers: `" + markerBegin + "' and `" + markerEnd + "'");
                return(null);
            }
            var mb1 = cape.String.charAt(markerBegin, 0);
            var mb2 = cape.String.charAt(markerBegin, 1);
            var me1 = cape.String.charAt(markerEnd, 0);
            var me2 = cape.String.charAt(markerEnd, 1);
            var pc  = ' ';

            cape.StringBuilder tag  = null;
            cape.StringBuilder data = null;
            var it = cape.String.iterate(inputdata);
            var v  = new System.Collections.Generic.List <object>();

            while (it != null)
            {
                var c = it.getNextChar();
                if (c <= 0)
                {
                    break;
                }
                if (tag != null)
                {
                    if (pc == me1 && tag.count() > 2)
                    {
                        tag.append(pc);
                        tag.append(c);
                        if (c == me2)
                        {
                            var tt    = tag.toString();
                            var tts   = cape.String.strip(cape.String.subString(tt, 2, cape.String.getLength(tt) - 4));
                            var words = cape.String.quotedStringToVector(tts, ' ');
                            if (object.Equals(cape.Vector.get(words, 0), "include"))
                            {
                                var fileName = cape.Vector.get(words, 1);
                                if (cape.String.isEmpty(fileName))
                                {
                                    cape.Log.warning(logContext, "Include tag with empty filename. Ignoring it.");
                                }
                                else
                                {
                                    cape.File ff = null;
                                    if (cape.Environment.isAbsolutePath(fileName))
                                    {
                                        ff = cape.FileInstance.forPath(fileName);
                                    }
                                    else if (includeDirs != null)
                                    {
                                        var n = 0;
                                        var m = includeDirs.Count;
                                        for (n = 0; n < m; n++)
                                        {
                                            var includeDir = includeDirs[n];
                                            if (includeDir != null)
                                            {
                                                var x = cape.FileInstance.forRelativePath(fileName, includeDir);
                                                if (x.isFile())
                                                {
                                                    ff = x;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (ff == null || ff.isFile() == false)
                                    {
                                        cape.Log.warning(logContext, "Included file was not found: `" + fileName + "'");
                                    }
                                    else
                                    {
                                        var cc = ff.getContentsString("UTF-8");
                                        if (object.Equals(cc, null))
                                        {
                                            cape.Log.warning(logContext, "Failed to read included file: `" + ff.getPath() + "'");
                                        }
                                        else
                                        {
                                            var nt = capex.text.TextTemplate.forString(cc, markerBegin, markerEnd, type, includeDirs);
                                            if (nt == null)
                                            {
                                                cape.Log.warning(logContext, "Failed to read included template file: `" + ff.getPath() + "'");
                                            }
                                            else
                                            {
                                                var array = nt.getTokens();
                                                if (array != null)
                                                {
                                                    var n2 = 0;
                                                    var m2 = array.Count;
                                                    for (n2 = 0; n2 < m2; n2++)
                                                    {
                                                        var token = array[n2];
                                                        if (token != null)
                                                        {
                                                            v.Add(token);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                v.Add(new capex.text.TextTemplate.TagData(words));
                            }
                            tag = null;
                        }
                    }
                    else if (c != me1)
                    {
                        tag.append(c);
                    }
                }
                else if (pc == mb1)
                {
                    if (c == mb2)
                    {
                        if (data != null)
                        {
                            v.Add(data);
                            data = null;
                        }
                        tag = new cape.StringBuilder();
                        tag.append(pc);
                        tag.append(c);
                    }
                    else
                    {
                        if (data == null)
                        {
                            data = new cape.StringBuilder();
                        }
                        data.append(pc);
                        data.append(c);
                    }
                }
                else if (c != mb1)
                {
                    if (data == null)
                    {
                        data = new cape.StringBuilder();
                    }
                    data.append(c);
                }
                pc = c;
            }
            if (pc == mb1)
            {
                if (data == null)
                {
                    data = new cape.StringBuilder();
                }
                data.append(pc);
            }
            if (data != null)
            {
                v.Add(data);
                data = null;
            }
            if (tag != null)
            {
                cape.Log.error(logContext, "Unfinished tag: `" + tag.toString() + "'");
                return(null);
            }
            return(v);
        }