Ejemplo n.º 1
0
        /// <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;
            }
        }
Ejemplo n.º 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
                    + "'");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an empty AttributeTree object.
        /// </summary>
        public AttributeTree()
        {
            // Set our path
            path = new NodeRef("/");

            // Create our children
            children = new AttributeTreeCollection(this);
        }
Ejemplo n.º 4
0
 public void DoubleDot()
 {
     NodeRef nr = new NodeRef("/a/b/../c");
     Assert.AreEqual("/a/c", nr.Path);
 }
Ejemplo n.º 5
0
 public void Count2()
 {
     NodeRef nr = new NodeRef("/a");
     Assert.AreEqual(1, nr.Count);
 }
Ejemplo n.º 6
0
 public void ChildIndex()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef c1 = up["sub2/sub3"];
     Assert.AreEqual("/dir1/sub1/sub2/sub3", c1.Path);
 }
Ejemplo n.º 7
0
 public void TestNodeCreateChild()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef c1 = up.CreateChild("sub2");
     Assert.AreEqual("/dir1/sub1/sub2", c1.Path);
 }
Ejemplo n.º 8
0
 public void TestEscapedNone()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual("/a/b/c", nr.ToString(),
         "String comparison");
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 10
0
 public void Includes4()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/this");
     Assert.AreEqual(false, nr.Includes(sr));
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
 }
Ejemplo n.º 17
0
 public void SubRef2()
 {
     NodeRef nr = new NodeRef("/this/is");
     NodeRef sr = new NodeRef("/this/is");
     Assert.AreEqual("/", nr.GetSubRef(sr).Path);
 }
Ejemplo n.º 18
0
 public void InvalidDoubleDot2()
 {
     NodeRef nr = new NodeRef("/../..");
     Assert.AreEqual("/", nr.Path);
 }
Ejemplo n.º 19
0
 public void TestDoubleStarMatch()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.IsTrue(nr.IsMatch("/**/c"));
 }
Ejemplo n.º 20
0
 public void LeadingDotSlash()
 {
     NodeRef context = new NodeRef("/");
     NodeRef nr = new NodeRef("./", context);
     Assert.AreEqual("/", nr.Path);
 }
Ejemplo n.º 21
0
 public void TestEscapedStar()
 {
     NodeRef nr = new NodeRef("/*/*/*");
     Assert.AreEqual("/*/*/*", nr.ToString(),
         "String comparison");
     Assert.IsTrue(nr.Includes(new NodeRef("/*/*/*/abb")),
         "nr.Includes");
 }
Ejemplo n.º 22
0
 public void Name()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual("c", nr.Name);
 }
Ejemplo n.º 23
0
 public void TestSingleStarMatch3()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.IsFalse(nr.IsMatch("/*/*/*/c"));
 }
Ejemplo n.º 24
0
 public void ParentPath()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     string up1 = up.ParentPath;
     Assert.AreEqual("/dir1", up1);
 }
Ejemplo n.º 25
0
 public void Count1()
 {
     NodeRef nr = new NodeRef("/a/b/c");
     Assert.AreEqual(3, nr.Count);
 }
Ejemplo n.º 26
0
 public void ParentRef()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     NodeRef up1 = up.ParentRef;
     Assert.AreEqual("/dir1", up1.Path);
 }
Ejemplo n.º 27
0
 public void Count3()
 {
     NodeRef nr = new NodeRef("/");
     Assert.AreEqual(0, nr.Count);
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 29
0
 public void DoubleDotTop()
 {
     NodeRef nr = new NodeRef("/a/..");
     Assert.AreEqual("/", nr.Path);
 }
Ejemplo n.º 30
0
 public void Simple()
 {
     NodeRef up = new NodeRef("/dir1/sub1");
     Assert.AreEqual("/dir1/sub1", up.Path);
 }