private Reach extract(Reach source, bool includeTokens) { Reach ret = source.upto(0) + source.from(source.len + 1); // FromLeftToRight strategy Reach ret = source.from(source.len + 1) for (long i = 1; i <= def.Length; i++) { long k = 1; while ((k <= rpt[i - 1]) || ((rpt[i - 1] == 0) && (source.len > 0))) { Reach leadIn = source.at(1, utl.dmyBool("al(occur, token) is planned"), def[i - 1].Name); if ((leadIn.len == 0) && (def[i - 1].Name.Length > 0)) { return(ret); } if (includeTokens) { if (ret.len == 0) { ret = leadIn; } else { ret = ret + leadIn; } } Reach res = source.after(leadIn); Reach leadOut = null; int occur = 0; // first we try the first closing bracelet, only in case that this will be a non-matching bracelet then wee will try the second, third, fourth etc. closing closing bracelet do { occur++; for (int j = (def[i - 1]).Len; j > 0; j--) { Reach token = res.at(occur, utl.dmyBool("al(occur, token) is planned"), (def[i - 1])[j]); if (token.len > 0) { leadOut = token; } } } while (nested[i - 1] && (res.upto(leadOut).at(-occur, utl.dmyBool("al(occur, token) is planned"), leadIn.text).len > 0)); if (leadOut != null) { res = res.before(leadOut); } if (leadOut == null) { return((ret.len == 0) ? res : ret + res); } if (ret.len == 0) { ret = includeTokens ? source.after(leadIn).upto(leadOut) : source.after(leadIn).before(leadOut); } else { ret = includeTokens ? ret + source.after(leadIn).upto(leadOut) : ret + source.after(leadIn).before(leadOut); } source = source.after(leadOut); k++; } } return(ret); }
public string SourceCode(string name, long type) { Reach cSharp = new Reach(""); cSharp = cSharp + new Reach("\n"); cSharp = cSharp + new Reach("\n"); cSharp = cSharp + new Reach("/**\n"); cSharp = cSharp + new Reach(" *\n"); cSharp = cSharp + new Reach(" * generated source code, do not change\n"); cSharp = cSharp + new Reach(" */\n"); cSharp = cSharp + new Reach("\n"); cSharp = cSharp + new Reach("using System;\n"); cSharp = cSharp + new Reach("using System.Collections.Generic;\n"); cSharp = cSharp + new Reach("using System.Linq;\n"); cSharp = cSharp + new Reach("using System.Text;\n"); cSharp = cSharp + new Reach("\n"); cSharp = cSharp + new Reach("using ndBase;\n"); cSharp = cSharp + new Reach("using ndString;\n"); cSharp = cSharp + new Reach("using ndData;\n"); cSharp = cSharp + new Reach("using ndUdf;\n"); cSharp = cSharp + new Reach("\n"); cSharp = cSharp + new Reach("public class " + name + "Call\n"); cSharp = cSharp + new Reach("{\n"); cSharp = cSharp + new Reach(" private static string parserName = \"" + name + "\";\n"); cSharp = cSharp + new Reach(" private static bool tested = false;\n"); cSharp = cSharp + new Reach(" private UdfDisp Udf = null;\n"); cSharp = cSharp + new Reach("\n"); Reach Java = new Reach(""); Java = Java + new Reach("\n"); Java = Java + new Reach("\n"); Java = Java + new Reach("/**\n"); Java = Java + new Reach(" *\n"); Java = Java + new Reach(" * generated source code, do not change\n"); Java = Java + new Reach(" */\n"); Java = Java + new Reach("import org.getr.ndBase.Pile;\n"); Java = Java + new Reach("import org.getr.ndString.Reach;\n"); Java = Java + new Reach("import org.getr.ndData.Tag;\n"); Java = Java + new Reach("\n"); Java = Java + new Reach("public class " + name + "Call\n"); Java = Java + new Reach("{\n"); Java = Java + new Reach("\n"); Java = Java + new Reach(" private static String parserName = \"" + name + "\";\n"); Java = Java + new Reach(" private static boolean tested = false;\n"); Java = Java + new Reach(" private UdfDisp Udf = null;\n"); Java = Java + new Reach("\n"); Tag def = new Tag(GetParser(name, 0), true); Tag requestDef = def.tags(2, new Pile <string>("", true, "[type]==request"))[1]; Tag parserDef = def.tags(2, new Pile <string>("", true, "[type]==parser"))[1]; Tag responseDef = def.tags(2, new Pile <string>("", true, "[type]==response"))[1]; KeyPile <string, Reach> rqParams = new KeyPile <string, Reach>(); KeyPile <string, Reach> rpParams = new KeyPile <string, Reach>(); long ctr = 0; while (true) { Pile <Tag> found = requestDef.tags(1, new Pile <string>("", true, "[type]==param"), new Pile <string>("", true, "key==" + (++ctr) + "")); if (found.Len == 0) { break; } Tag paramTag = found[1]; string key = paramTag.txt.after(1, "[").before(1, "]").text; string test = ""; if (paramTag.attr.hasKey("test")) { test = paramTag.attr["test"].Value; } rqParams.Add(key, test); } foreach (Tag t in responseDef.tags(1, new Pile <string>("", true, "[type]==param"))) { string key = t.attr["name"].Value; string test = ""; if (t.attr.hasKey("test")) { test = t.attr["test"].Value; } rpParams.Add(key, test); } foreach (string paramName in rpParams.Keys) { cSharp = cSharp + new Reach(" private Reach m_" + Proper(paramName) + ";\n"); Java = Java + new Reach(" private Reach m_" + Proper(paramName) + ";\n"); } cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); foreach (string paramName in rpParams.Keys) { cSharp = cSharp + new Reach(" public Reach " + Proper(paramName) + " { get { return m_" + Proper(paramName) + "; } }\n"); Java = Java + new Reach(" public Reach get" + Proper(paramName) + " () { return m_" + Proper(paramName) + "; }\n"); } cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); cSharp = cSharp + new Reach(" public " + name + "Call(UdfDisp Udf)\n"); Java = Java + new Reach(" public " + name + "Call(UdfDisp Udf) throws Exception\n"); cSharp = cSharp + new Reach(" {\n"); Java = Java + new Reach(" {\n"); cSharp = cSharp + new Reach(" this.Udf = Udf;\n"); Java = Java + new Reach(" this.Udf = Udf;\n"); cSharp = cSharp + new Reach(" if (!tested)\n"); Java = Java + new Reach(" if (!tested)\n"); cSharp = cSharp + new Reach(" {\n"); Java = Java + new Reach(" {\n"); cSharp = cSharp + new Reach(" tested = true;\n"); Java = Java + new Reach(" tested = true;\n"); string testParams = ""; foreach (string paramName in rpParams.Keys) { testParams += rpParams[paramName].text; } if (testParams.Length == 0) { cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); } else { foreach (string paramName in rpParams.Keys) { cSharp = cSharp + new Reach(" bool p_" + Proper(paramName) + " = false;\n"); Java = Java + new Reach(" boolean p_" + Proper(paramName) + " = false;\n"); } Reach execParams = new Reach(""); foreach (Reach test in rqParams) { execParams += new Reach("\"") + test + new Reach("\", "); } execParams = execParams.upto(-3); cSharp = cSharp + new Reach(" foreach (" + name + "Call res in exec( " + execParams.text + "))\n"); Java = Java + new Reach(" for (" + name + "Call res : exec( " + execParams.text + "))\n"); cSharp = cSharp + new Reach(" {\n"); Java = Java + new Reach(" {\n"); string passedCondition = ""; foreach (string paramName in rpParams.Keys) { string test = rpParams[paramName].text; passedCondition += " && p_" + Proper(paramName); cSharp = cSharp + new Reach(" if (res." + Proper(paramName) + ".Equals(\"" + test + "\")) p_" + Proper(paramName) + " = true;\n"); Java = Java + new Reach(" if (res.get" + Proper(paramName) + "().Equals(\"" + test + "\")) p_" + Proper(paramName) + " = true;\n"); } passedCondition = "(!(" + passedCondition.Substring(4) + "))"; cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); cSharp = cSharp + new Reach(" if " + passedCondition + " throw new Exception(\"" + name + "Call selfTest FAILED! Check test values in " + name + ".xml against the WebPage you get from the url! (has the html structure changed?)\");\n"); Java = Java + new Reach(" if " + passedCondition + " throw new Exception(\"" + name + "Call selfTest FAILED! Check test values in " + name + ".xml against the WebPage you get from the url! (has the html structure changed?)\");\n"); cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); } cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); Reach cSharpParams = new Reach(""); Reach javaParams = new Reach(""); foreach (string key in rqParams.Keys) { cSharpParams += new Reach("string ") + key + new Reach(", "); javaParams += new Reach("String ") + key + new Reach(", "); } cSharpParams = cSharpParams.upto(-3); javaParams = javaParams.upto(-3); cSharp = cSharp + new Reach(" public Pile<" + name + "Call> exec(" + cSharpParams + ")\n"); Java = Java + new Reach(" public Pile<" + name + "Call> exec(" + javaParams + ") throws Exception\n"); cSharp = cSharp + new Reach(" {\n"); Java = Java + new Reach(" {\n"); cSharp = cSharp + new Reach(" string callParams = \"\";\n"); Java = Java + new Reach(" String callParams = \"\";\n"); foreach (string key in rqParams.Keys) { cSharp = cSharp + new Reach(" callParams += \"<param name=\\\"" + key + "\\\" value=\\\"\" + " + key + " + \"\\\"/>\";\n"); Java = Java + new Reach(" callParams += \"<param name=\\\"" + key + "\\\" value=\\\"\" + " + key + " + \"\\\"/>\";\n"); } cSharp = cSharp + new Reach(" Tag result = new Tag(Udf.Exec(parserName, callParams), true);\n"); Java = Java + new Reach(" Tag result = new Tag(Udf.exec(parserName, callParams), true);\n"); cSharp = cSharp + new Reach(" Pile<" + name + "Call> ret = new Pile<" + name + "Call>(0);\n"); Java = Java + new Reach(" Pile<" + name + "Call> ret = new Pile<" + name + "Call>(0);\n"); cSharp = cSharp + new Reach(" ret.Name = result.struc(-1);\n"); Java = Java + new Reach(" ret.setName(result.struc(-1));\n"); cSharp = cSharp + new Reach(" foreach (Tag tr in result.tags(1, new Pile<string>(\"\", \"[type]==tr\")))\n"); Java = Java + new Reach(" for (Tag tr : result.tags(1, new Pile<String>(\"\", \"[type]==tr\")))\n"); cSharp = cSharp + new Reach(" {\n"); Java = Java + new Reach(" {\n"); cSharp = cSharp + new Reach(" " + name + "Call res = new " + name + "Call(Udf);\n"); Java = Java + new Reach(" " + name + "Call res = new " + name + "Call(Udf);\n"); long rpCtr = 0; foreach (string key in rpParams.Keys) { rpCtr++; cSharp = cSharp + new Reach(" res.m_" + Proper(key) + " = tr.Tags[" + rpCtr + "].txt;\n"); Java = Java + new Reach(" res.m_" + Proper(key) + " = tr.Tags().get(" + rpCtr + ").txt();\n"); } cSharp = cSharp + new Reach(" ret.Add(res);\n"); Java = Java + new Reach(" ret.add(res);\n"); cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); cSharp = cSharp + new Reach(" return ret;\n"); Java = Java + new Reach(" return ret;\n"); cSharp = cSharp + new Reach(" }\n"); Java = Java + new Reach(" }\n"); cSharp = cSharp + new Reach("}\n"); Java = Java + new Reach("}\n"); cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); cSharp = cSharp + new Reach("\n"); Java = Java + new Reach("\n"); switch (type) { case 1: return(cSharp.text); case 2: return(Java.text); } return(""); }
public ndUrl(string urlString) { urlString = urlString.Trim(); if (urlString.IndexOf("://") < 0) { urlString = "http://" + urlString; } _url = new Reach(urlString); Protocol = _url.before(1, "://"); _url = _url.after(Protocol).from(4); Domain = _url.before(1, "/"); _url = _url.after(Domain).from(2); Password = Domain.before(-1, "@"); if (Password.len > 0) { Domain = Domain.after(Password).from(2); User = Password.before(1, ":"); if (User.len > 0) { Password = Password.after(User).from(2); } } else { User = Password.upto(0); } Port = Domain.after(1, ":"); if (Port.len > 0) { Domain = Domain.before(Port).upto(-2); } Extension = Domain.after(-2, "."); if (_allExtensions.IndexOf(" " + Extension.uText + " ") < 0) { Extension = Domain.after(-1, "."); } if (_allExtensions.IndexOf(" " + Extension.uText + " ") < 0) { Extension = Domain.from(Domain.len + 1); } if (Extension.len > 0) { Domain = Domain.before(Extension).upto(-2); } Server = Domain.before(-1, "."); if (Server.len > 0) { Domain = Domain.after(Server).from(2); } Path = _url.before(1, "?"); if (Path.len > 0) { _url = _url.after(Path).from(2); } Query = new Assoc(_url, "=", "&", ""); File = Path.after(-1, "/"); if (File.len > 0) { Path = Path.before(File).upto(-2); } }
private void init(Reach src, Tag parent, bool xml) { isRelevant = true; type = src.upto(0); //AttGeTr: type was null in new Tag("", false) ctn = src.upto(0); //AttGeTr: ctn was null in some TestCase ... isXmlTag = xml; _tags = new Pile <Tag>(); this.parent = parent; def = src; if (parent == null) { root = this; all = new Pile <Tag>(); int done = 0; while (done < def.len) { all.Add(new Tag(def.after(done), this, xml)); if (all[-1].isWhiteSpace()) { all[-1].isRelevant = false; } if (all.Len == 1) { all[-1].parent = this; } else { if (all[-1].type.startsWith("/")) { for (int i = all.Len - 1; i > 0; i--) { if ((all[-1].type.Equals("/" + all[i].type.text)) && (all[i].closingTag == null)) { all[i].closingTag = all[-1]; all[-1].parent = all[i].parent; break; } } if (all[-1].parent == null) { all[-1].parent = all[-2].parent; // orphaned closing tag } } else { if ((all[-1].type.len == 0) && (!all[-2].type.startsWith("/"))) // new definition: a "plain text tag" may be contained in any tag but may not always be relevant ... { if (!all[-2].mayContain(all[-1].type.text)) { all[-1].isRelevant = false; } if (all[-2].closingTag == null) { all[-1].parent = all[-2]; } else { all[-1].parent = all[-2].parent; } } else { if ((all[-2].mayContain(all[-1].type.text))) { all[-1].parent = all[-2]; } else { all[-1].parent = all[-2].parent; if (all[-2].closingTag == null) { all[-2].closingTag = all[-2]; } } } } } done = done + all[-1]._htm.len; } for (int i = 1; i <= all.Len; i++) { if ((all[i].isRelevant) && (!all[i].type.startsWith("/"))) { all[i].parent._tags.Add(all[i]); } } } else { root = parent.root; if (def.startsWith("<")) { def = def.upto(1, ">"); type = def.after(1).before(-1).before(1, " "); if (type.startsWith("!")) { if (type.startsWith("!--")) { type = type.upto(3); } else { type = type.upto(1); } } if (type.endsWith("/")) { type = type.before(-1); } if (type.Equals("!--")) { closingTag = this; type = type.upto(1); def = root.def.from(def).upto(1, "-->"); ctn = def.after(-1).upto(0); _htm = def; attrib = def.after(type).upto(-2).lTrim().rTrim(); if (attrib.endsWith("/")) { closingTag = this; attrib = attrib.before(-1); } } else { if (type.startsWith("!")) { closingTag = this; type = type.upto(1); } if (type.Equals("script")) { ctn = root.def.from(def).Before(1, "</script"); _htm = ctn.upto(-1); //htm = def + ctn + root.def.after(ctn).upto(1, ">"); attrib = def.after(type).upto(-2).lTrim().rTrim(); if (attrib.endsWith("/")) { closingTag = this; attrib = attrib.before(-1); } } else { ctn = root.def.after(def).upto(0); _htm = def.upto(-1); //_htm = def.plus(ctn); attrib = def.after(type).upto(-2).lTrim().rTrim(); if (attrib.endsWith("/")) { closingTag = this; attrib = attrib.before(-1); } } } } else { closingTag = this; ctn = def.before(1, "<"); type = def.upto(0); def = def.upto(0); attrib = def.upto(0); _htm = ctn.upto(-1); } } }