Exemplo n.º 1
0
        /// <summary>
        /// Get a File/Blob resource as a stream. Method is applicable only for Zip/Directory Resources.
        /// </summary>
        /// <param name="configuration">Configuration instance.</param>
        /// <param name="path">Search path for the resource node.</param>
        /// <param name="file">Sub-path for the file</param>
        /// <returns>Stream Reader</returns>
        public static StreamReader GetResourceStream(this Configuration configuration, string path, string file)
        {
            Preconditions.CheckArgument(configuration);
            Preconditions.CheckArgument(path);
            Preconditions.CheckArgument(file);

            AbstractConfigNode node = configuration.Find(path);

            if (node != null && typeof(ConfigResourceNode).IsAssignableFrom(node.GetType()))
            {
                ConfigResourceNode rnode = (ConfigResourceNode)node;
                if (rnode.GetType() == typeof(ConfigResourceZip))
                {
                    ConfigResourceZip fnode = (ConfigResourceZip)rnode;
                    if (fnode.Downloaded)
                    {
                        Conditions.NotNull(fnode.File);
                        FileInfo fi = new FileInfo(fnode.File.FullName);
                        if (!fi.Exists)
                        {
                            throw new ConfigurationException(String.Format("Invalid File Resource Node: File not found. [file={0}]", fi.FullName));
                        }
                        string filename = String.Format("{0}/{1}", fi.FullName, file);
                        fi = new FileInfo(filename);
                        if (!fi.Exists)
                        {
                            throw new ConfigurationException(String.Format("File not found. [file={0}]", fi.FullName));
                        }
                        return(new StreamReader(fi.FullName));
                    }
                    else
                    {
                        lock (fnode)
                        {
                            DownloadResource(fnode);
                            UnzipResource(fnode);
                        }
                        string   filename = String.Format("{0}/{1}", fnode.Directory.FullName, file);
                        FileInfo fi       = new FileInfo(filename);

                        if (!fi.Exists)
                        {
                            throw new ConfigurationException(String.Format("Invalid File Resource Node: File not found. [file={0}]", fi.FullName));
                        }
                        return(new StreamReader(fi.FullName));
                    }
                }
                else if (rnode.GetType() == typeof(ConfigDirectoryResource))
                {
                    ConfigDirectoryResource dnode = (ConfigDirectoryResource)rnode;
                    if (!dnode.Directory.Exists)
                    {
                        throw new ConfigurationException(String.Format("Directory not found. [file={0}]", dnode.Directory.FullName));
                    }
                    string   filename = String.Format("{0}/{1}", dnode.Directory.FullName, file);
                    FileInfo fi       = new FileInfo(filename);

                    if (!fi.Exists)
                    {
                        throw new ConfigurationException(String.Format("Invalid File Resource Node: File not found. [file={0}]", fi.FullName));
                    }
                    return(new StreamReader(fi.FullName));
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a Resource node instance.
        /// </summary>
        /// <param name="parent">Parent Config node</param>
        /// <param name="elem">XML Element</param>
        private void AddResourceNode(ConfigPathNode parent, XmlElement elem)
        {
            string resourceName = elem.GetAttribute(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_NAME);

            if (String.IsNullOrWhiteSpace(resourceName))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_NAME);
            }
            string st = elem.GetAttribute(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);

            if (String.IsNullOrWhiteSpace(st))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);
            }
            EResourceType type = Enum.Parse <EResourceType>(st);

            if (type == EResourceType.NONE)
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);
            }
            Uri uri = null;

            if (elem.HasChildNodes)
            {
                foreach (XmlNode nn in elem.ChildNodes)
                {
                    if (nn.NodeType == XmlNodeType.Element && nn.Name == ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL)
                    {
                        string su = nn.InnerText;
                        if (String.IsNullOrWhiteSpace(su))
                        {
                            throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL);
                        }
                        uri = new Uri(su);
                        break;
                    }
                }
            }

            if (uri == null)
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL);
            }
            ConfigResourceNode node = null;

            switch (type)
            {
            case EResourceType.FILE:
                node = new ConfigResourceFile(configuration, parent);
                break;

            case EResourceType.DIRECTORY:
                node = new ConfigDirectoryResource(configuration, parent);
                break;

            case EResourceType.ZIP:
                node = new ConfigResourceZip(configuration, parent);
                break;
            }
            node.Name         = elem.Name;
            node.Type         = type;
            node.Location     = uri;
            node.ResourceName = resourceName;
            if (settings.DownloadOptions == EDownloadOptions.LoadRemoteResourcesOnStartup)
            {
                if (type == EResourceType.ZIP || type == EResourceType.FILE)
                {
                    ConfigResourceFile fnode = (ConfigResourceFile)node;
                    ConfigResourceHelper.DownloadResource(fnode);
                }
            }

            if (type == EResourceType.DIRECTORY)
            {
                ConfigDirectoryResource dnode = (ConfigDirectoryResource)node;
                if (!dnode.Location.IsFile)
                {
                    throw new ConfigurationException(String.Format("Invalid URI: Must be a local/mounted directory. [uri={0}]", dnode.Location.ToString()));
                }
                string        dir = dnode.Location.LocalPath;
                DirectoryInfo di  = new DirectoryInfo(dir);
                if (di.Exists)
                {
                    throw new ConfigurationException(String.Format("Invalid URI: Directory not found. [uri={0}]", dnode.Location.ToString()));
                }
                dnode.Downloaded = true;
                dnode.Directory  = di;
            }
            parent.AddChildNode(node);
        }