/// <summary>
        /// Retrieves an asset from the provider. If the second parameter
        /// is true and the asset cannot be found, an AssetException is
        /// thrown. Otherwise, this function will return null if it cannot
        /// be found.
        /// </summary>
        public IAsset GetAsset(NodeRef path, bool exceptionIfMissing)
        {
            // Go through the assets
            foreach (IAssetProvider provider in providers)
            {
                try
                {
                    // We always throw an exception to handle the processing
                    return provider.GetAsset(path, true);
                }
                catch {}
            }

            // If we get this far, every asset threw an exception trying to
            // get the asset path.
            if (exceptionIfMissing)
            {
                throw new AssetException("Cannot find " +
                    path + " in " + providers.Count
                    + " providers");
            }
            else
            {
                return null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Constructs an asset from the given assembly. If the resource
        /// does not exist, this throws an AssetException.
        /// </summary>
        public AssemblyAsset(AssemblyAssetProvider provider, NodeRef path)
        {
            // Save the fields
            this.provider = provider;
            this.path = path.ToString();

            if (provider.StripLeadingSlash)
                this.path = this.path.Substring(1);

            // Make sure it exists
            bool found = false;

            foreach (string name in provider.Assembly.GetManifestResourceNames())
            {
                if (name == this.path)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
                throw new AssetException("Cannot find resource '"
                    + path + "' from assembly '"
                    + provider.Assembly.FullName
                    + "'");
        }
Beispiel #3
0
        /// <summary>
        /// Creates an empty AttributeTree object.
        /// </summary>
        public AttributeTree()
        {
            // Set our path
            path = new NodeRef("/");

            // Create our children
            children = new AttributeTreeCollection(this);
        }
Beispiel #4
0
 public void DoubleDot()
 {
     NodeRef nr = new NodeRef("/a/b/../c");
     Assert.AreEqual("/a/c", nr.Path);
 }
Beispiel #5
0
 public void Count2()
 {
     NodeRef nr = new NodeRef("/a");
     Assert.AreEqual(1, nr.Count);
 }
Beispiel #6
0
 public void ChildIndex()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef c1 = up["sub2/sub3"];
     Assert.AreEqual("/dir1/sub1/sub2/sub3", c1.Path);
 }
Beispiel #7
0
 public void TestNodeCreateChild()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef c1 = up.CreateChild("sub2");
     Assert.AreEqual("/dir1/sub1/sub2", c1.Path);
 }
Beispiel #8
0
 public void TestEscapedNone()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual("/a/b/c", nr.ToString(),
         "String comparison");
 }
Beispiel #9
0
 public void SubRef4()
 {
     NodeRef nr = new NodeRef("/this/is/a/path");
     NodeRef sr = new NodeRef("/not/a/path");
     Assert.AreEqual("/not/a/path", nr.GetSubRef(sr).Path);
 }
Beispiel #10
0
 public void Includes4()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/this");
     Assert.AreEqual(false, nr.Includes(sr));
 }
Beispiel #11
0
 public void Includes2()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/not/in/is/a/path");
     Assert.AreEqual(false, nr.Includes(sr));
 }
Beispiel #12
0
 public void Includes1()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/this/is/a/path");
     Assert.AreEqual(true, nr.Includes(sr));
 }
Beispiel #13
0
        /// <summary>
        /// This parses the given path and builds up the internal
        /// representation into memory. This representation is used
        /// for path and additional processing.
        /// </summary>
        private void ParsePath(string path, NodeRef context)
        {
            // Perform some sanity checking on the path
            if (path == null)
                throw new InvalidPathException("Cannot create a node "
                    + "ref from a null");

            // Check for absolute path
            if (!path.StartsWith("/"))
            {
                // We don't have an absolute path, so check the context
                if (context == null)
                {
                    throw new NotAbsolutePathException("Cannot create "
                        + "absolute path from '"
                        + path
                        + "'");
                }

                // Construct the new path from this context
                path = context.Path + "/" + path;
            }

            // Save the original
            //string orig = path;

            // Start by escaping the escapes
            path = path.Replace("\\", "\\\\");

            // Normalize the path by adding a trailing "/" and reducing
            // double "//" into single ones. This is done with regexes to
            // make it easier and faster. We add the "/" to simplify our
            // regexes later; we will also remove it as the last bit.
            path += "/";
            path = findDoubleSlashRegex.Replace(path, "/");
            //Debug("Processing 1: {0}", path);

            // Normalize the invalid characters
            //Debug("Processing 2: {0}", path);

            // Normalize the "/./" and the "/../" references. Also remove
            // the "/../" stuff at the beginning, by just deleting it.
            path = findHereRegex.Replace(path, "/");
            path = findRefUpRegex.Replace(path, "/");
            path = findInvalidUpRegex.Replace(path, "/");
            //Debug("Processing: {0} => {1}", orig, path);

            // Finally, remove the leading and trailing slash that we put in.
            //Debug("Processing 1: {0} => {1}", orig, path);
            path = findTrailingSlashesRegex.Replace(path, "");
            path = findLeadingSlashesRegex.Replace(path, "").Trim();
            //Debug("Processing 2: {0} => {1}", orig, path);

            // We need to do is make sure the regex characters are not
            // allowed in the string. We do this by just replacing all
            // the important ones with escaped versions.
            regexable = "/" + path
                .Replace("+", "\\+")
                .Replace("(", "\\(")
                .Replace(")", "\\)")
                .Replace("[", "\\[")
                .Replace("]", "\\]")
                .Replace(".", "\\.")
                .Replace("*", "\\*")
                .Replace("?", "\\?");
            //Debug("Processing 3: {0}", path);

            // We now have a normalized path, without a leading or a
            // trailing slash. If this is a blank string (one with no
            // length), it means that the reference was "/". Otherwise, it
            // represents some sort of path that had at least one
            // element. We also save the entire string version because we
            // are both read-only and we make the assumption that memory can
            // handle it.
            if (path.Equals(""))
            {
                // This is an empty path, which means it was a "/" reference
                parts = new string [] {};
                pref = "/";
            }
            else
            {
                // Split it along the slash characters
                parts = path.Split('/');
                pref = "/" + path;
            }
        }
Beispiel #14
0
 /// <summary>
 /// Returns true if this path includes the given path. This means
 /// that given path is under or part of this node's path.
 /// </summary>
 public bool Includes(NodeRef path)
 {
     // We have a real easy way of finding this
     return Regex.IsMatch(path.ToString(), "^" + regexable);
 }
Beispiel #15
0
        /// <summary>
        /// Returns a NodeRef which has this node's path removed from the
        /// beginning. If the given reference is not included (as per the
        /// Includes), it will be returned completely.
        /// </summary>
        public NodeRef GetSubRef(NodeRef nodeRef)
        {
            // Check for includes
            if (!Includes(nodeRef))
                return nodeRef;

            // Remove the first part. There is an easy method because we are
            // so strict about paths. We use the root context in the case
            // where the leading / is removed.
            string path =
                Regex.Replace(nodeRef.Path, "^" + regexable, "");

            return new NodeRef(path, RootContext);
        }
Beispiel #16
0
 public void SubPluses()
 {
     NodeRef nr = new NodeRef("/Test +1/A");
     NodeRef nr2 = new NodeRef("/Test +1");
     NodeRef nr3 = nr2.GetSubRef(nr);
     Assert.AreEqual("/A", nr3.ToString());
 }
Beispiel #17
0
 public void SubRef2()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/this/is");
     Assert.AreEqual("/", nr.GetSubRef(sr).Path);
 }
Beispiel #18
0
 public void InvalidDoubleDot2()
 {
     NodeRef nr = new NodeRef("/../..");
     Assert.AreEqual("/", nr.Path);
 }
Beispiel #19
0
 public void TestDoubleStarMatch()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.IsTrue(nr.IsMatch("/**/c"));
 }
Beispiel #20
0
 public void LeadingDotSlash()
 {
     NodeRef context = new NodeRef("/");
     NodeRef nr = new NodeRef("./", context);
     Assert.AreEqual("/", nr.Path);
 }
Beispiel #21
0
 public void TestEscapedStar()
 {
     NodeRef nr = new NodeRef("/*/*/*");
     Assert.AreEqual("/*/*/*", nr.ToString(),
         "String comparison");
     Assert.IsTrue(nr.Includes(new NodeRef("/*/*/*/abb")),
         "nr.Includes");
 }
Beispiel #22
0
 public void Name()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual("c", nr.Name);
 }
Beispiel #23
0
 public void TestSingleStarMatch3()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.IsFalse(nr.IsMatch("/*/*/*/c"));
 }
Beispiel #24
0
 public void ParentPath()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     string up1 = up.ParentPath;
     Assert.AreEqual("/dir1", up1);
 }
Beispiel #25
0
 public void Count1()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual(3, nr.Count);
 }
Beispiel #26
0
 public void ParentRef()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef up1 = up.ParentRef;
     Assert.AreEqual("/dir1", up1.Path);
 }
Beispiel #27
0
 public void Count3()
 {
     NodeRef nr = new NodeRef("/");
     Assert.AreEqual(0, nr.Count);
 }
Beispiel #28
0
 public void ParsePluses()
 {
     NodeRef nr = new NodeRef("/Test/Test +1/Test +2");
     Assert.AreEqual("/Test/Test +1/Test +2", nr.ToString());
     Assert.AreEqual("Test +2", nr.Name);
 }
Beispiel #29
0
 public void DoubleDotTop()
 {
     NodeRef nr = new NodeRef("/a/..");
     Assert.AreEqual("/", nr.Path);
 }
Beispiel #30
0
 public void Simple()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     Assert.AreEqual("/dir1/sub1", up.Path);
 }