Exemplo n.º 1
0
        public static IWoopsaElement ByPathOrNull(this IWoopsaContainer element, string path)
        {
            string workPath = WoopsaUtils.RemoveInitialSeparator(path);

            if (workPath == string.Empty)
            {
                return(element);
            }
            else
            {
                string[] pathElements = workPath.Split(WoopsaConst.WoopsaPathSeparator);

                IWoopsaElement result = element;
                foreach (var item in pathElements)
                {
                    if (result is IWoopsaContainer)
                    {
                        result = ((IWoopsaContainer)result).ByNameOrNull(item);
                    }
                    else
                    {
                        result = null;
                        break;
                    }
                }
                return(result);
            }
        }
Exemplo n.º 2
0
        internal string GetMetadata(string path)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaContainer)
            {
                string result;
                if (item is IWoopsaObject)
                {
                    result = (item as IWoopsaObject).SerializeMetadata();
                }
                else
                {
                    result = (item as IWoopsaContainer).SerializeMetadata();
                }
                OnLog(WoopsaVerb.Meta, path, NoArguments, result, true);
                return(result);
            }
            else
            {
                string message = String.Format("Cannot get metadata for a WoopsaElement of type {0}", item.GetType());
                OnLog(WoopsaVerb.Meta, path, NoArguments, message, false);
                throw new WoopsaInvalidOperationException(message);
            }
        }
Exemplo n.º 3
0
        private string WriteValue(string path, Func <WoopsaValueType, WoopsaValue> getValue)
        {
            IWoopsaElement item = FindByPath(path);

            if ((item is IWoopsaProperty))
            {
                IWoopsaProperty property = item as IWoopsaProperty;
                WoopsaValue     argument = getValue(property.Type);
                if (!property.IsReadOnly)
                {
                    property.Value = argument;
                    string result = property.Value.Serialize();
                    OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, result, true);
                    return(result);
                }
                else
                {
                    string message = String.Format(
                        "Cannot write a read-only WoopsaProperty for path {0}", path);
                    OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, message, false);
                    throw new WoopsaInvalidOperationException(message);
                }
            }
            else
            {
                WoopsaValue argument = getValue(WoopsaValueType.Text);
                string      message  = String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path);
                OnLog(WoopsaVerb.Write, path, new WoopsaValue[] { argument }, message, false);
                throw new WoopsaInvalidOperationException(message);
            }
        }
Exemplo n.º 4
0
 private static void BuildPath(StringBuilder stringBuilder, IWoopsaElement element)
 {
     if (element != null)
     {
         BuildPath(stringBuilder, element.Owner);
         if (element.Owner != null) // it is not the root
         {
             stringBuilder.Append(WoopsaConst.WoopsaPathSeparator);
             stringBuilder.Append(element.Name);
         }
     }
 }
Exemplo n.º 5
0
        public static IWoopsaElement ByName(this IWoopsaContainer woopsaContainer, string name)
        {
            IWoopsaElement result = ByNameOrNull(woopsaContainer, name);

            if (result != null)
            {
                return(result);
            }
            else
            {
                throw new WoopsaNotFoundException(string.Format("Woopsa element not found : {0}", name));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Finds the WoopsaElement specified by path, relative to a container
        /// </summary>
        /// <param name="element">The element in which to start the search</param>
        /// <param name="path">The path, specified relative to elemen</param>
        /// <returns>An IWoopsaElement if found, throws IWoopsaNotFoundException if not</returns>
        ///
        public static IWoopsaElement ByPath(this IWoopsaContainer element, string path)
        {
            IWoopsaElement result = ByPathOrNull(element, path);

            if (result != null)
            {
                return(result);
            }
            else
            {
                throw new WoopsaNotFoundException(string.Format("Woopsa element not found at path : {0}", path));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the path of a WoopsaElement, going all the way to the root
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetPath(this IWoopsaElement element)
        {
            StringBuilder stringBuilder = new StringBuilder();

            BuildPath(stringBuilder, element);
            if (stringBuilder.Length == 0) //Special case when it's the root
            {
                return(WoopsaConst.WoopsaRootPath);
            }
            else
            {
                return(stringBuilder.ToString());
            }
        }
Exemplo n.º 8
0
        internal string ReadValue(string path)
        {
            IWoopsaElement item = FindByPath(path);

            if ((item is IWoopsaProperty))
            {
                IWoopsaProperty property = item as IWoopsaProperty;
                return(property.Value.Serialize());
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format(
                                                              "Cannot read value of a non-WoopsaProperty for path {0}", path));
            }
        }
Exemplo n.º 9
0
 public static IWoopsaContainer GetRoot(this IWoopsaElement element)
 {
     if (element == null)
     {
         return(null);
     }
     else if (element is IWoopsaContainer)
     {
         return(GetContainerRoot(element as IWoopsaContainer));
     }
     else
     {
         return(GetContainerRoot(element.Owner));
     }
 }
Exemplo n.º 10
0
        internal string GetMetadata(string path)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaObject)
            {
                return((item as IWoopsaObject).SerializeMetadata());
            }
            else if (item is IWoopsaContainer)
            {
                return((item as IWoopsaContainer).SerializeMetadata());
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format("Cannot get metadata for a WoopsaElement of type {0}", item.GetType()));
            }
        }
Exemplo n.º 11
0
        private string InvokeMethod(string path, int argumentsCount,
                                    Func <string, WoopsaValueType, WoopsaValue> getArgumentByName)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaMethod)
            {
                IWoopsaMethod method = item as IWoopsaMethod;
                if (argumentsCount == method.ArgumentInfos.Count())
                {
                    List <WoopsaValue> woopsaArguments = new List <WoopsaValue>();
                    foreach (var argInfo in method.ArgumentInfos)
                    {
                        WoopsaValue argumentValue = getArgumentByName(argInfo.Name, argInfo.Type);
                        if (argumentValue == null)
                        {
                            string message = String.Format("Missing argument {0} for method {1}", argInfo.Name, item.Name);
                            OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                            throw new WoopsaInvalidOperationException(message);
                        }
                        else
                        {
                            woopsaArguments.Add(argumentValue);
                        }
                    }
                    WoopsaValue[] argumentsArray = woopsaArguments.ToArray();
                    IWoopsaValue  methodResult   = method.Invoke(argumentsArray);
                    string        result         = methodResult != null?methodResult.Serialize() : WoopsaConst.WoopsaNull;

                    OnLog(WoopsaVerb.Invoke, path, argumentsArray, result, true);
                    return(result);
                }
                else
                {
                    string message = String.Format("Wrong argument count for method {0}", item.Name);
                    OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                    throw new WoopsaInvalidOperationException(message);
                }
            }
            else
            {
                string message = String.Format("Cannot invoke a {0}", item.GetType());
                OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                throw new WoopsaInvalidOperationException(message);
            }
        }
Exemplo n.º 12
0
        internal string ReadValue(string path)
        {
            IWoopsaElement item = FindByPath(path);

            if ((item is IWoopsaProperty))
            {
                IWoopsaProperty property = item as IWoopsaProperty;
                string          result   = property.Value.Serialize();
                OnLog(WoopsaVerb.Read, path, NoArguments, result, true);
                return(result);
            }
            else
            {
                string message = String.Format("Cannot read value of a non-WoopsaProperty for path {0}", path);
                OnLog(WoopsaVerb.Read, path, NoArguments, message, false);
                throw new WoopsaInvalidOperationException(message);
            }
        }
Exemplo n.º 13
0
        internal string WriteValue(string path, string value)
        {
            IWoopsaElement item = FindByPath(path);

            if ((item is IWoopsaProperty))
            {
                IWoopsaProperty property = item as IWoopsaProperty;
                if (property.IsReadOnly)
                {
                    throw new WoopsaInvalidOperationException(String.Format(
                                                                  "Cannot write a read-only WoopsaProperty for path {0}", path));
                }
                property.Value = WoopsaValue.CreateUnchecked(value, property.Type);
                return(property.Value.Serialize());
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path));
            }
        }
Exemplo n.º 14
0
        internal string WriteValueDeserializedJson(string path, object deserializedJson)
        {
            IWoopsaElement item = FindByPath(path);

            if ((item is IWoopsaProperty))
            {
                IWoopsaProperty property = item as IWoopsaProperty;
                if (property.IsReadOnly)
                {
                    throw new WoopsaInvalidOperationException(String.Format(
                                                                  "Cannot write a read-only WoopsaProperty for path {0}", path));
                }
                property.Value = WoopsaValue.DeserializedJsonToWoopsaValue(deserializedJson,
                                                                           property.Type);
                return(WoopsaValue.Null.Serialize());
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format("Cannot write value of a non-WoopsaProperty for path {0}", path));
            }
        }
Exemplo n.º 15
0
        internal string InvokeMethod(string path, NameValueCollection arguments)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaMethod)
            {
                int           argumentsCount = arguments != null ? arguments.Count : 0;
                IWoopsaMethod method         = item as IWoopsaMethod;
                if (argumentsCount == method.ArgumentInfos.Count())
                {
                    List <WoopsaValue> woopsaArguments = new List <WoopsaValue>();
                    foreach (var argInfo in method.ArgumentInfos)
                    {
                        string argumentValue = arguments[argInfo.Name];
                        if (argumentValue == null)
                        {
                            throw new WoopsaInvalidOperationException(String.Format(
                                                                          "Missing argument {0} for method {1}", argInfo.Name, item.Name));
                        }
                        woopsaArguments.Add(WoopsaValue.CreateChecked(argumentValue, argInfo.Type));
                    }
                    IWoopsaValue result = method.Invoke(woopsaArguments.ToArray());
                    return(result != null?result.Serialize() : WoopsaConst.WoopsaNull);
                }
                else
                {
                    throw new WoopsaInvalidOperationException(String.Format(
                                                                  "Wrong argument count for method {0}", item.Name));
                }
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format(
                                                              "Cannot invoke a {0}", item.GetType()));
            }
        }