Esempio n. 1
0
 public static object parse(cape.File file)
 {
     if (file == null)
     {
         return(null);
     }
     return(cape.JSONParser.parse(file.getContentsString("UTF-8")));
 }
Esempio n. 2
0
        public static capex.text.TextTemplate forFile(cape.File file, string markerBegin, string markerEnd, int type = 0, System.Collections.Generic.List <cape.File> includeDirs = null, cape.LoggingContext logContext = null)
        {
            if (file == null)
            {
                return(null);
            }
            var text = file.getContentsString("UTF-8");

            if (object.Equals(text, null))
            {
                return(null);
            }
            var ids = includeDirs;

            if (ids == null)
            {
                ids = new System.Collections.Generic.List <cape.File>();
                ids.Add(file.getParent());
            }
            return(capex.text.TextTemplate.forString(text, markerBegin, markerEnd, type, ids, logContext));
        }
Esempio n. 3
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. 4
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);
        }