Example #1
0
            private Resource LoadResource(System.Xml.XmlNode node, Resource parent)
            {
                System.Diagnostics.Debug.Assert(node.Name == "resource");

                /// get resource type
                string resourceTypeName = node.Attributes["type"].Value;

                SNAP.Resources.ResourceType resourceType = _resourceTypes[resourceTypeName];

                /// get resource name
                string resourceName = node.Attributes["name"].Value;

                /// get resource id
                /// if the id is not available (for instance when importing an externallay generated resource)
                /// generate a new one instead
                Guid guid = (node.Attributes["id"] != null) ?
                            (new Guid(node.Attributes["id"].Value)) : (Guid.NewGuid());

                /// create the new resource,
                /// fields are initialized to default values
                Resource resource = new Resource(guid, resourceType, resourceName);

                /// update field values using the information in the file
                foreach (System.Xml.XmlNode fieldNode in node.SelectNodes("field"))
                {
                    string         fieldName  = fieldNode.Attributes["name"].Value;
                    IResourceType  fieldType  = resource.MyType.SubTypes [fieldName];
                    IResourceValue fieldValue = null;

                    System.Diagnostics.Debug.Assert(resource.SubValues.ContainsKey(fieldName));
                    fieldValue = resource.SubValues [fieldName];
                    fieldValue.LoadFromXML(fieldNode);
                }

                /// add this node to it's parent
                if (parent == null)
                {
                    string family = node["family"].InnerText;
                    parent = _resourceList[family];
                }
                parent.Children.Add(resource);
                return(resource);
            }
Example #2
0
        /// <summary>
        /// Interprets the specified parameters.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="resource">The resource.</param>
        /// <returns></returns>
        public static string Interpret(Script script, string parameters, Resource resource)
        {
            string result = parameters;

            do
            {
                System.Text.RegularExpressions.Match match =
                    System.Text.RegularExpressions.Regex.Match(result,
                                                               @"\$([a-zA-Z0-9 ]+(?:\.[a-zA-Z0-9 ]+)*)\$"
                                                               );

                if (match == null || !match.Success)
                {
                    break;
                }


                /// break the variable name into its inner structure
                /// For instance: Sequence File.File
                string varValue        = null;
                string completeVarName = match.Groups[1].Captures[0].Value;

                switch (completeVarName)
                {
                case "ExecutionFolder":
                    varValue = GetExecutionFolder(script, resource);
                    varValue = varValue.TrimEnd('/');
                    break;

                case "PluginFolder":
                    varValue = Controller.PluginFolder;
                    varValue = varValue.TrimEnd('/');
                    break;

                /// TODO: make this more general
                case "PluginFolder.Unix":
                    varValue = Controller.PluginFolder;
                    varValue = varValue.Replace('\\', '/');
                    varValue = varValue.Replace(" ", @"\ ");
                    varValue = varValue.TrimEnd('/');
                    break;

                case "ExecutionFolder.Unix":
                    varValue = GetExecutionFolder(script, resource);
                    varValue = varValue.Replace('\\', '/');
                    varValue = varValue.Replace(" ", @"\ ");
                    varValue = varValue.TrimEnd('/');
                    break;

                default:
                {
                    string[] varNameParts = completeVarName.Split('.');

                    /// follow the inner structure to the last internal reference
                    IResourceValue currentResource = resource;
                    for (int i = 0; i < varNameParts.Length; ++i)
                    {
                        currentResource = currentResource.GetDynamicProperty(varNameParts[i]);
                    }

                    varValue = currentResource.ToString();
                }
                break;
                }

                /// replace the $variable$ with the value
                result = System.Text.RegularExpressions.Regex.Replace(
                    result, @"\$" + completeVarName + @"\$", varValue);
            }while (true);

            return(result);
        }