public void Translate(ValueStack stack, System.IO.Stream input, System.Xml.XmlWriter w) { XmlDocument doc = new XmlDocument(); doc.Load(input); ProcessNode(stack, doc.DocumentElement.FirstChild, w); }
private void DoInclude(Include tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { String fName = String.Format(@"{0}\{1}", rootFolder, tag.File); using (System.IO.Stream input = System.IO.File.OpenRead(fName)) { new Translator(rootFolder).Translate(stack, input, w); } }
private void ProcessChildren(ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { System.Xml.XmlNode child = node.FirstChild; while (child != null) { ProcessNode(stack, child, w); child = child.NextSibling; } }
public void Translate(ValueStack stack, System.IO.Stream input, System.IO.Stream output) { XmlDocument doc = new XmlDocument(); doc.Load(input); using (System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(output, new XmlWriterSettings() { NewLineHandling = NewLineHandling.Entitize, Indent = false, NewLineOnAttributes = false })) { ProcessNode(stack, doc.DocumentElement.FirstChild, w); w.Flush(); } }
private void DoProperty(Property tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { String s = tag.Value; if (s.StartsWith("$")) { object value = stack.Evaluate(s); if (value == null) { s = ""; } else { s = value.ToString(); } } w.WriteString(s); }
private void DoIf(If tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { bool value = stack.BooleanExpression(tag.Test); if (value) { ProcessChildren(stack, node, w); } else { System.Xml.XmlNode ifNode = node.NextSibling; if (ifNode != null) { Type t = GetType(ifNode); if (t == typeof(BitMobile.ValueStack.Else)) { ProcessChildren(stack, ifNode, w); } } } }
private void DoIterator(Iterator tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { System.Collections.IEnumerable collection = (System.Collections.IEnumerable)stack.Evaluate(tag.Value, null, false); if (collection is System.Data.IDataReader) { while (((System.Data.IDataReader)collection).Read()) { stack.Push(tag.Id, collection as System.Data.IDataRecord); ProcessChildren(stack, node, w); } } else { System.Collections.IEnumerator enumerator = collection.GetEnumerator(); while (enumerator.MoveNext()) { stack.Push(tag.Id, enumerator.Current); ProcessChildren(stack, node, w); } } }
public XmlTemplateView(BitMobile.ValueStack.ValueStack stack, String rootFolder, String name) : base(stack, rootFolder, name) { }
public Configuration.Configuration CreateConfiguration(ValueStack.ValueStack stack) { System.IO.Stream bpData = BitMobile.Application.ApplicationContext.Context.DAL.GetConfiguration(); ObjectFactory factory = new ObjectFactory(); return (Configuration.Configuration)factory.CreateObject(stack, bpData); }
object Build(IContainer parent, XmlNode node, ValueStack.ValueStack stack) { if (node is XmlComment) return null; Type t = TypeFromNode(node); if (t.IsSubclassOf(typeof(ValueStackTag))) { //control flow tag ConstructorInfo ci = t.GetConstructor(new Type[] { }); var tag = (ValueStackTag)ci.Invoke(new object[] { }); foreach (XmlAttribute a in node.Attributes) { PropertyInfo pi = t.GetProperty(a.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); string value = ApplicationContext.Context.DAL.TranslateString (a.Value); pi.SetValue(tag, Convert.ChangeType(value, pi.PropertyType), null); } if (tag is Include) { return DoInclude((Include)tag, parent, stack); } if (tag is If) { return DoIf((If)tag, parent, node, stack); } if (tag is Else) { return parent; } if (tag is Iterator) { return DoIterator((Iterator)tag, parent, node, stack); } if (tag is Push) { object newObject = null; if (node.ChildNodes.Count == 1) { newObject = Build(null, node.FirstChild, stack); } return DoPush((Push)tag, parent, stack, newObject); } throw new Exception("Unknown tag"); } else { //ui control object obj = CreateObject(t, stack); string id = InitObjectId(node, stack, obj); SetProperties(node, stack, obj); if (parent != null) parent.AddChild(obj); if (obj is IDataBind) (obj as IDataBind).DataBind(); if (id != null) LoadStepState(stack, obj, id); if (obj is IContainer) ProcessChildren((IContainer)obj, node, stack); return obj; } }
private IContainer DoPush(Push tag, IContainer parent, ValueStack.ValueStack stack, object newObject = null) { object expr = newObject; if (expr == null) { Type type = null; if (!String.IsNullOrEmpty(tag.Type)) type = GetType().Assembly.GetType(tag.Type, false, true); /* if (!String.IsNullOrEmpty(tag.Value)) if (!tag.Value.StartsWith("$")) //TODO throw new ArgumentException(String.Format("Invalid Push expression '{0}'. Should start with '$'", tag.Value)); */ expr = stack.Evaluate(tag.Value, type); } stack.Push(tag.Id, expr); return parent; }
private IContainer DoIf(If tag, IContainer parent, XmlNode node, ValueStack.ValueStack stack) { bool value = stack.BooleanExpression(tag.Test); if (value) ProcessChildren(parent, node, stack); else { XmlNode ifNode = node.NextSibling; if (ifNode != null) { Type t = TypeFromNode(ifNode); if (t == typeof(Else)) ProcessChildren(parent, ifNode, stack); } } return parent; }
private static void SetProperties(XmlNode node, ValueStack.ValueStack stack, object obj) { foreach (XmlAttribute a in node.Attributes) { if (String.IsNullOrEmpty(a.Prefix)) { PropertyInfo prop = obj.GetType() .GetProperty(a.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (prop == null) throw new Exception(String.Format("Property '{0}' is not found in type '{1}'", a.Name, obj.GetType().Name)); String aValue = ApplicationContext.Context.DAL.TranslateString(a.Value.Trim()); if (prop.PropertyType == typeof(DataBinder)) { object bindedObject; String propertyName; stack.Evaluate(aValue, out bindedObject, out propertyName); var binder = new DataBinder(obj, a.Name, bindedObject, propertyName); prop.SetValue(obj, binder, null); } else if (prop.PropertyType == typeof(ActionHandler)) { var handler = new ActionHandler(aValue, stack); prop.SetValue(obj, handler, null); } else if (prop.PropertyType == typeof(ActionHandlerEx)) { var handler = new ActionHandlerEx(aValue, stack, obj); prop.SetValue(obj, handler, null); } else { object mexpr = stack.Evaluate(aValue, prop.PropertyType); prop.SetValue(obj, mexpr, null); } } } }
private static string InitObjectId(XmlNode node, ValueStack.ValueStack stack, object obj) { foreach (XmlAttribute a in node.Attributes) { if (string.IsNullOrEmpty(a.Prefix) && a.Name.ToLower().Equals("id")) { string aValue = ApplicationContext.Context.DAL.TranslateString (a.Value); string id = stack.Evaluate(aValue).ToString(); stack.Push(id, obj); return id; } } return null; }
private static object CreateObject(Type t, ValueStack.ValueStack stack) { object obj; MethodInfo mi = t.GetMethod("CreateInstance", BindingFlags.Static | BindingFlags.Public); if (mi != null) { ParameterInfo[] parameters = mi.GetParameters(); var args = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { args[i] = stack.Values[parameters[i].Name]; } obj = mi.Invoke(null, args); } else { ConstructorInfo[] constructors = t.GetConstructors(); if (constructors.Length == 0) throw new Exception(String.Format("Unable to create instance of {0}", t.FullName)); ConstructorInfo ci = constructors[constructors.Length - 1]; //last one.. ParameterInfo[] parameters = ci.GetParameters(); var args = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) args[i] = stack.Values[parameters[i].Name]; obj = ci.Invoke(args); } var context = (IApplicationContext)stack.Values["context"]; var aware = obj as IApplicationContextAware; if (aware != null) aware.SetApplicationContext(context); return obj; }
private void ProcessNode(ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { switch (node.NodeType) { case XmlNodeType.Element: Type t = GetType(node); if (t != null) { //control flow tag System.Reflection.ConstructorInfo ci = t.GetConstructor(new System.Type[] { }); ValueStackTag tag = (ValueStackTag)ci.Invoke(new object[] { }); foreach (System.Xml.XmlAttribute a in node.Attributes) { System.Reflection.PropertyInfo pi = t.GetProperty(a.Name, BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); pi.SetValue(tag, System.Convert.ChangeType(a.Value, pi.PropertyType), null); } if (tag is Include) { DoInclude((Include)tag, stack, node, w); } if (tag is Property) { DoProperty((Property)tag, stack, node, w); } if (tag is If) { DoIf((If)tag, stack, node, w); } if (tag is Else) { } if (tag is Iterator) { DoIterator((Iterator)tag, stack, node, w); } } else { w.WriteStartElement(node.LocalName); foreach (System.Xml.XmlAttribute a in node.Attributes) { if (String.IsNullOrEmpty(a.Prefix)) { String aValue = a.Value.Trim(); if (String.IsNullOrEmpty(node.NamespaceURI)) { w.WriteAttributeString(a.LocalName, aValue); } } } ProcessChildren(stack, node, w); w.WriteEndElement(); } break; case XmlNodeType.Text: w.WriteString(node.Value); break; default: throw new Exception("Unsupported node type"); } }
private void ProcessNode(ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { switch (node.NodeType) { case XmlNodeType.Element: Type t = GetType(node); if (t != null) { //control flow tag System.Reflection.ConstructorInfo ci = t.GetConstructor(new System.Type[] { }); ValueStackTag tag = (ValueStackTag)ci.Invoke(new object[] { }); foreach (System.Xml.XmlAttribute a in node.Attributes) { System.Reflection.PropertyInfo pi = t.GetProperty(a.Name, BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); pi.SetValue(tag, System.Convert.ChangeType(a.Value, pi.PropertyType), null); } if (tag is Include) { DoInclude((Include)tag, stack, node, w); } if (tag is Property) { DoProperty((Property)tag, stack, node, w); } if (tag is If) { DoIf((If)tag, stack, node, w); } if (tag is Else) { } if (tag is Iterator) { DoIterator((Iterator)tag, stack, node, w); } } else { w.WriteStartElement(node.LocalName); foreach (System.Xml.XmlAttribute a in node.Attributes) { if (String.IsNullOrEmpty(a.Prefix)) { String aValue = a.Value.Trim(); if (String.IsNullOrEmpty(node.NamespaceURI)) w.WriteAttributeString(a.LocalName, aValue); } } ProcessChildren(stack, node, w); w.WriteEndElement(); } break; case XmlNodeType.Text: w.WriteString(node.Value); break; default: throw new Exception("Unsupported node type"); } }
private Stream CallFunction(String module, String function, Stream messageBody) { WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; ValueStack stack = new ValueStack(); AddParameters(stack, messageBody); try { String modulePath = String.Format(@"{0}\resource\server\script\{1}.js", solution.SolutionFolder, module); if (System.IO.File.Exists(modulePath)) { System.IO.FileInfo fi = new FileInfo(modulePath); using (System.IO.Stream scriptStream = System.IO.File.OpenRead(modulePath)) { BitMobile.Script.ScriptEngine engine = BitMobile.Script.ScriptEngine.LoadScript(scriptStream, module, fi.LastWriteTimeUtc); BitMobile.Script.ScriptEngine.RegisterType("DateTime", typeof(DateTime)); BitMobile.Script.ScriptEngine.RegisterType("Guid", typeof(Guid)); engine.AddVariable("Guid", typeof(Guid)); engine.AddVariable("DateTime", typeof(DateTime)); var db = new DB(solution.ConnectionString); engine.AddVariable("DB", db); engine.AddVariable("Tracker", new Tracker()); engine.AddVariable("$", stack); engine.AddVariable("View", new BitMobile.MVC.ViewFactory(String.Format(@"{0}\resource\server\view", solution.SolutionFolder), stack)); engine.AddVariable("Telegram", new TelegramFactory(new TelegramSettignsPersist(solution.ConnectionString))); object result = engine.CallFunction(function); if (result == null) return Common.Utils.MakeTextAnswer("ok"); else { if (result is BitMobile.MVC.BaseView) { BitMobile.MVC.BaseView view = (BitMobile.MVC.BaseView)result; WebOperationContext.Current.OutgoingResponse.ContentType = view.ContentType(); return view.Translate(); } else return Common.Utils.MakeTextAnswer(result.ToString()); } } } else return Common.Utils.MakeTextAnswer("Invalid module name"); } catch (Exception e) { return Common.Utils.MakeExceptionAnswer(e); } }
public object CreateObject(ValueStack.ValueStack stack, System.IO.Stream stream) { var doc = new XmlDocument(); doc.Load(stream); return Build(null, doc.DocumentElement, stack); }
private void AddParameters(ValueStack stack, Stream messageBody) { var ps = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters; for (int i = 0; i < ps.Count; i++) { object v = ps.GetValues(i); if (v.GetType() == typeof(String[])) v = ((String[])v)[0].ToString(); stack.Push(ps.GetKey(i), v); } if (messageBody != null) { using (System.IO.StreamReader r = new StreamReader(messageBody, System.Text.Encoding.UTF8, false, 1024, true)) { while (!r.EndOfStream) { string line = r.ReadLine(); if(!String.IsNullOrEmpty(line)) { string[] arr = line.Split('='); if (arr.Length == 2) { String v = System.Net.WebUtility.UrlDecode(arr[1].Trim()); stack.Push(arr[0].Trim(), v); } } } } } }
private static void LoadStepState(ValueStack.ValueStack stack, object obj, string id) { var persistable = obj as IPersistable; if (persistable != null) { stack.Persistables.Add(id, persistable); var context = (IApplicationContext)stack.Values["context"]; object state; if (context.Workflow.CurrentStep.State.TryGetValue(id, out state)) persistable.SetState(state); } }
private void DoIf(If tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { bool value = stack.BooleanExpression(tag.Test); if (value) { ProcessChildren(stack, node, w); } else { System.Xml.XmlNode ifNode = node.NextSibling; if (ifNode != null) { Type t = GetType(ifNode); if (t == typeof(BitMobile.ValueStack.Else)) ProcessChildren(stack, ifNode, w); } } }
private IContainer DoInclude(Include tag, IContainer parent, ValueStack.ValueStack stack) { var dal = ApplicationContext.Context.DAL; object component = CreateObject(stack, dal.GetScreenByName(tag.File)); if (!(component is Component)) throw new Exception("Only Component container is allowed to be included"); object[] controls = ((IContainer)component).Controls; foreach (object child in controls) parent.AddChild(child); return parent; }
private void DoProperty(Property tag, ValueStack stack, System.Xml.XmlNode node, System.Xml.XmlWriter w) { String s = tag.Value; if (s.StartsWith("$")) { object value = stack.Evaluate(s); if (value == null) s = ""; else s = value.ToString(); } w.WriteString(s); }
private IContainer DoIterator(Iterator tag, IContainer parent, XmlNode node, ValueStack.ValueStack stack) { var collection = (IEnumerable)stack.Evaluate(tag.Value, null, false); var status = new IteratorStatus(); if (!String.IsNullOrEmpty(tag.Status)) stack.Push(tag.Status, status); //push status variable var list = new ArrayList(); var reader = collection as IDataReader; if (reader != null) { while (reader.Read()) { stack.Push(tag.Id, reader); ProcessChildren(parent, node, stack); status.Inc(); } } else { foreach (var item in collection) list.Add(item); foreach (var item in list) { stack.Push(tag.Id, item); ProcessChildren(parent, node, stack); status.Inc(); } } if (!String.IsNullOrEmpty(tag.Status)) stack.Values.Remove(tag.Status); //remove status variable return parent; }
private void ProcessChildren(IContainer parent, XmlNode node, ValueStack.ValueStack stack) { XmlNode child = node.FirstChild; while (child != null) { Build(parent, child, stack); child = child.NextSibling; } }
public TemplateView(BitMobile.ValueStack.ValueStack stack, String rootFolder, String name) { this.stack = stack; this.rootFolder = rootFolder; this.name = name; }
public BusinessProcess.BusinessProcess CreateBusinessProcess(String bpName, ValueStack.ValueStack stack) { System.IO.Stream bpData = BitMobile.Application.ApplicationContext.Context.DAL.GetBusinessProcess(bpName); ObjectFactory factory = new ObjectFactory(); return (BusinessProcess.BusinessProcess)factory.CreateObject(stack, bpData); }