Exemple #1
0
        public static void ParseInto(string s, object o, ZXPProxy zpp)
        {
            List <DIndices> delimiters = Delimiter.Delimit(s, DelimitType.Any);

            for (int i = 0; i < delimiters.Count;)
            {
                switch (delimiters[i].Type)
                {
                // Plain Data That Should Be Able To Be Parsed In
                case DelimitType.Bracket:
                    ParseSimpleData(o, zpp, s, delimiters, ref i);
                    break;

                // This Is A Special Type
                case DelimitType.Angled:
                    ParseComplexData(o, zpp, s, delimiters, ref i);
                    break;

                // This Is A Function
                case DelimitType.Paranthesis:
                    // Find The Key
                    ParseFunction(o, zpp, s, delimiters, ref i);
                    break;
                }
                i++;
            }
            return;
        }
Exemple #2
0
 public static void Write(StreamWriter writer, object o, ZXPProxy zpp, int spaces = 0)
 {
     foreach (var f in zpp.Data)
     {
         object value = f.GetValue(o);
         if (f.Converter == null)
         {
             if (value == null)
             {
                 writer.WriteLine("{2}{0} <{1}> {{ NULL }}", f.Key, f.Type.FullName, new string(' ', spaces));
             }
             else
             {
                 Type vType = value.GetType();
                 writer.WriteLine("{2}{0} <{1}> {{", f.Key, vType.FullName, new string(' ', spaces));
                 Write(writer, value, ZXPProxy.GetProxy(vType), spaces + 4);
                 writer.WriteLine("{0}", new string(' ', spaces) + "}");
             }
         }
         else
         {
             writer.WriteLine("{2}{0} [{1}]", f.Key, f.Converter.Convert(value), new string(' ', spaces));
         }
     }
 }
Exemple #3
0
        public static void Add(Type t)
        {
            if (proxies.ContainsKey(t.FullName))
            {
                return;
            }
            ZXPProxy zpp = new ZXPProxy(t);

            proxies.Add(t.FullName, zpp);
            zpp.CreateProxy();
            return;
        }
Exemple #4
0
        public static ZXPProxy GetProxy(Type t)
        {
            // Try To Get The Proxy
            ZXPProxy zpp;

            if (proxies.TryGetValue(t.FullName, out zpp))
            {
                return(zpp);
            }

            // Create A New Proxy
            zpp = new ZXPProxy(t);
            proxies.Add(t.FullName, zpp);
            zpp.CreateProxy();
            return(zpp);
        }
Exemple #5
0
        private static void ParseSimpleData(object o, ZXPProxy zpp, string s, List <DIndices> ld, ref int li)
        {
            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);

            if (string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            // Find The Field That Matches To This Key
            ZXPDatum datum = null;

            if (zpp.DataDict.TryGetValue(key, out datum))
            {
                object val = null;

                // Check For Array
                if (datum.Type.IsArray)
                {
                    if (!datum.Type.HasElementType)
                    {
                        return;
                    }
                    Type eType = datum.Type.GetElementType();
                    if (ParseArray(eType, s.Substring(ld[li].Start, ld[li].Length), out val))
                    {
                        datum.SetValue(o, val);
                    }
                }

                // Check For A Possible Conversion
                if (datum.Converter == null)
                {
                    return;
                }

                // Try To Convert
                string sValue = s.Substring(ld[li].Start, ld[li].Length);
                if (ReadValue(sValue, datum.Converter, out val))
                {
                    datum.SetValue(o, val);
                }
            }
        }
Exemple #6
0
        public static object ParseNew(string s, Type t)
        {
            // Check Arguments
            if (t == null || string.IsNullOrEmpty(s))
            {
                return(null);
            }

            // Attempt A Conversion
            IZXPConverter conv;

            if (GetConverter(t, out conv))
            {
                object value = null;
                conv.Convert(s, out value);
                return(value);
            }
            else if (t.IsArray)
            {
                object val = null;
                if (!t.HasElementType)
                {
                    return(null);
                }
                Type eType = t.GetElementType();
                if (ParseArray(eType, s, out val))
                {
                    return(val);
                }
                else
                {
                    return(null);
                }
            }

            // Create A Complex Value
            ZXPProxy zpp = ZXPProxy.GetProxy(t);
            object   o   = CreateDefaultFromType(zpp.Type);

            ParseInto(s, o, zpp);
            return(o);
        }
Exemple #7
0
        public static void ParseInto(string s, object o)
        {
            ZXPProxy zpp = ZXPProxy.GetProxy(o.GetType());

            ParseInto(s, o, zpp);
        }
Exemple #8
0
        private static void ParseFunction(object o, ZXPProxy zpp, string s, List <DIndices> ld, ref int li)
        {
            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);

            if (string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            // Find The Method That Matches To This Key
            ZXPFunc    func;
            MethodInfo method = null;

            if (!zpp.FuncsDict.TryGetValue(key, out func))
            {
                return;
            }
            method = func.Method;

            // Check For Simple Invoke
            ParameterInfo[] paramInfo = method.GetParameters();
            if (paramInfo.Length < 1 || paramInfo == null)
            {
                method.Invoke(o, null);
                return;
            }

            // Parse Parameters
            string sArgs = s.Substring(ld[li].Start, ld[li].Length);

            object[] args = new object[paramInfo.Length];
            var      nl   = Delimiter.Delimit(sArgs, DelimitType.Angled | DelimitType.Curly);

            // Check Number Of Arguments
            int ca = 0;

            foreach (var ndi in nl)
            {
                if (ndi.Type == DelimitType.Curly)
                {
                    ca++;
                }
            }
            if (ca != args.Length)
            {
                return;
            }

            // Create Parameters
            int ai = 0, pi = 0;

            for (; pi < args.Length;)
            {
                if (nl[ai].Type == DelimitType.Angled)
                {
                    if (ai >= nl.Count - 1 || nl[ai + 1].Type != DelimitType.Curly)
                    {
                        ai++;
                        continue;
                    }

                    // Get The Argument Type
                    string atValue = sArgs.Substring(nl[ai].Start, nl[ai].Length);
                    if (string.IsNullOrWhiteSpace(atValue))
                    {
                        break;
                    }
                    Type aType = GetTypeFromString(atValue);
                    if (aType == null)
                    {
                        break;
                    }


                    // Get The Argument
                    string        sValue = sArgs.Substring(nl[ai + 1].Start, nl[ai + 1].Length);
                    IZXPConverter conv;
                    ZXParser.GetConverter(aType, out conv);
                    if (ReadValue(sValue, conv, aType, out args[pi]))
                    {
                        pi++;
                    }
                    ai += 2;
                }
                else
                {
                    // Simple Parse
                    Type          aType  = paramInfo[pi].ParameterType;
                    string        sValue = sArgs.Substring(nl[ai].Start, nl[ai].Length);
                    IZXPConverter conv;
                    ZXParser.GetConverter(aType, out conv);
                    if (ReadValue(sValue, conv, aType, out args[pi]))
                    {
                        pi++;
                    }
                    ai++;
                }
            }
            // Check That All Arguments Are OK
            if (pi == args.Length)
            {
                method.Invoke(o, args);
            }
        }
Exemple #9
0
        private static void ParseComplexData(object o, ZXPProxy zpp, string s, List <DIndices> ld, ref int li)
        {
            // Check For Data Availability
            if (li + 1 >= ld.Count || ld[li].Length < 1 || ld[li + 1].Type != DelimitType.Curly)
            {
                return;
            }

            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);

            if (string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            // Get Substrings
            DIndices diType = ld[li], diData = ld[li + 1];

            li++;

            // Find The Field That Matches To This Key
            ZXPDatum datum = null;

            if (!zpp.DataDict.TryGetValue(key, out datum))
            {
                return;
            }

            // Check For A Possible Conversion
            if (datum.Converter != null)
            {
                // Try To Convert
                object val    = null;
                string sValue = s.Substring(ld[li].Start, ld[li].Length);
                if (ReadValue(sValue, datum.Converter, out val))
                {
                    datum.SetValue(o, val);
                }
                return;
            }
            else
            {
                // Get The Special Type
                string sType = s.Substring(diType.Start, diType.Length);
                if (string.IsNullOrWhiteSpace(sType))
                {
                    return;
                }
                Type cType = GetTypeFromString(sType);
                if (cType == null)
                {
                    return;
                }

                // Create And Set The Data
                object val    = null;
                string sValue = s.Substring(diData.Start, diData.Length);
                if (ReadValue(sValue, datum.Converter, cType, out val))
                {
                    datum.SetValue(o, val);
                }
            }
        }
Exemple #10
0
 public static void ParseInto(string s, object o, ZXPProxy zpp)
 {
     List<DIndices> delimiters = Delimiter.Delimit(s, DelimitType.Any);
     for(int i = 0; i < delimiters.Count; ) {
         switch(delimiters[i].Type) {
             // Plain Data That Should Be Able To Be Parsed In
             case DelimitType.Bracket:
                 ParseSimpleData(o, zpp, s, delimiters, ref i);
                 break;
             // This Is A Special Type
             case DelimitType.Angled:
                 ParseComplexData(o, zpp, s, delimiters, ref i);
                 break;
             // This Is A Function
             case DelimitType.Paranthesis:
                 // Find The Key
                 ParseFunction(o, zpp, s, delimiters, ref i);
                 break;
         }
         i++;
     }
     return;
 }
Exemple #11
0
 public static void Write(Stream s, object o, ZXPProxy zpp, int spaces = 0)
 {
     Write(new StreamWriter(s), o, zpp, spaces);
 }
Exemple #12
0
        public static ZXPProxy GetProxy(Type t)
        {
            // Try To Get The Proxy
            ZXPProxy zpp;
            if(proxies.TryGetValue(t.FullName, out zpp)) return zpp;

            // Create A New Proxy
            zpp = new ZXPProxy(t);
            proxies.Add(t.FullName, zpp);
            zpp.CreateProxy();
            return zpp;
        }
Exemple #13
0
        private static void ParseSimpleData(object o, ZXPProxy zpp, string s, List<DIndices> ld, ref int li)
        {
            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);
            if(string.IsNullOrWhiteSpace(key))
                return;

            // Find The Field That Matches To This Key
            ZXPDatum datum = null;
            if(zpp.DataDict.TryGetValue(key, out datum)) {
                object val = null;

                // Check For Array
                if(datum.Type.IsArray) {
                    if(!datum.Type.HasElementType) return;
                    Type eType = datum.Type.GetElementType();
                    if(ParseArray(eType, s.Substring(ld[li].Start, ld[li].Length), out val))
                        datum.SetValue(o, val);
                }

                // Check For A Possible Conversion
                if(datum.Converter == null) return;

                // Try To Convert
                string sValue = s.Substring(ld[li].Start, ld[li].Length);
                if(ReadValue(sValue, datum.Converter, out val))
                    datum.SetValue(o, val);
            }
        }
Exemple #14
0
        private static void ParseFunction(object o, ZXPProxy zpp, string s, List<DIndices> ld, ref int li)
        {
            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);
            if(string.IsNullOrWhiteSpace(key))
                return;

            // Find The Method That Matches To This Key
            ZXPFunc func;
            MethodInfo method = null;
            if(!zpp.FuncsDict.TryGetValue(key, out func)) return;
            method = func.Method;

            // Check For Simple Invoke
            ParameterInfo[] paramInfo = method.GetParameters();
            if(paramInfo.Length < 1 || paramInfo == null) {
                method.Invoke(o, null);
                return;
            }

            // Parse Parameters
            string sArgs = s.Substring(ld[li].Start, ld[li].Length);
            object[] args = new object[paramInfo.Length];
            var nl = Delimiter.Delimit(sArgs, DelimitType.Angled | DelimitType.Curly);

            // Check Number Of Arguments
            int ca = 0;
            foreach(var ndi in nl) {
                if(ndi.Type == DelimitType.Curly) ca++;
            }
            if(ca != args.Length) return;

            // Create Parameters
            int ai = 0, pi = 0;
            for(; pi < args.Length; ) {
                if(nl[ai].Type == DelimitType.Angled) {
                    if(ai >= nl.Count - 1 || nl[ai + 1].Type != DelimitType.Curly) {
                        ai++;
                        continue;
                    }

                    // Get The Argument Type
                    string atValue = sArgs.Substring(nl[ai].Start, nl[ai].Length);
                    if(string.IsNullOrWhiteSpace(atValue))
                        break;
                    Type aType = GetTypeFromString(atValue);
                    if(aType == null)
                        break;

                    // Get The Argument
                    string sValue = sArgs.Substring(nl[ai + 1].Start, nl[ai + 1].Length);
                    IZXPConverter conv;
                    ZXParser.GetConverter(aType, out conv);
                    if(ReadValue(sValue, conv, aType, out args[pi])) {
                        pi++;
                    }
                    ai += 2;
                }
                else {
                    // Simple Parse
                    Type aType = paramInfo[pi].ParameterType;
                    string sValue = sArgs.Substring(nl[ai].Start, nl[ai].Length);
                    IZXPConverter conv;
                    ZXParser.GetConverter(aType, out conv);
                    if(ReadValue(sValue, conv, aType, out args[pi])) {
                        pi++;
                    }
                    ai++;
                }
            }
            // Check That All Arguments Are OK
            if(pi == args.Length) {
                method.Invoke(o, args);
            }
        }
Exemple #15
0
        private static void ParseComplexData(object o, ZXPProxy zpp, string s, List<DIndices> ld, ref int li)
        {
            // Check For Data Availability
            if(li + 1 >= ld.Count || ld[li].Length < 1 || ld[li + 1].Type != DelimitType.Curly)
                return;

            // Check If A Key Is Available
            string key = GetKeyString(s, ld, li);
            if(string.IsNullOrWhiteSpace(key))
                return;

            // Get Substrings
            DIndices diType = ld[li], diData = ld[li + 1];
            li++;

            // Find The Field That Matches To This Key
            ZXPDatum datum = null;
            if(!zpp.DataDict.TryGetValue(key, out datum)) return;

            // Check For A Possible Conversion
            if(datum.Converter != null) {
                // Try To Convert
                object val = null;
                string sValue = s.Substring(ld[li].Start, ld[li].Length);
                if(ReadValue(sValue, datum.Converter, out val))
                    datum.SetValue(o, val);
                return;
            }
            else {
                // Get The Special Type
                string sType = s.Substring(diType.Start, diType.Length);
                if(string.IsNullOrWhiteSpace(sType)) return;
                Type cType = GetTypeFromString(sType);
                if(cType == null) return;

                // Create And Set The Data
                object val = null;
                string sValue = s.Substring(diData.Start, diData.Length);
                if(ReadValue(sValue, datum.Converter, cType, out val))
                    datum.SetValue(o, val);
            }
        }
Exemple #16
0
 public static void Write(Stream s, object o, ZXPProxy zpp, int spaces = 0)
 {
     Write(new StreamWriter(s), o, zpp, spaces);
 }
Exemple #17
0
 public static void Write(StreamWriter writer, object o, ZXPProxy zpp, int spaces = 0)
 {
     foreach(var f in zpp.Data) {
         object value = f.GetValue(o);
         if(f.Converter == null) {
             if(value == null) writer.WriteLine("{2}{0} <{1}> {{ NULL }}", f.Key, f.Type.FullName, new string(' ', spaces));
             else {
                 Type vType = value.GetType();
                 writer.WriteLine("{2}{0} <{1}> {{", f.Key, vType.FullName, new string(' ', spaces));
                 Write(writer, value, ZXPProxy.GetProxy(vType), spaces + 4);
                 writer.WriteLine("{0}", new string(' ', spaces) + "}");
             }
         }
         else writer.WriteLine("{2}{0} [{1}]", f.Key, f.Converter.Convert(value), new string(' ', spaces));
     }
 }
Exemple #18
0
 public static void Write(StreamWriter writer, object o)
 {
     Write(writer, o, ZXPProxy.GetProxy(o.GetType()));
 }
Exemple #19
0
 public static void Add(Type t)
 {
     if(proxies.ContainsKey(t.FullName)) return;
     ZXPProxy zpp = new ZXPProxy(t);
     proxies.Add(t.FullName, zpp);
     zpp.CreateProxy();
     return;
 }