/// <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; } }
/// <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 + "'"); }
/// <summary> /// Creates an empty AttributeTree object. /// </summary> public AttributeTree() { // Set our path path = new NodeRef("/"); // Create our children children = new AttributeTreeCollection(this); }
public void DoubleDot() { NodeRef nr = new NodeRef("/a/b/../c"); Assert.AreEqual("/a/c", nr.Path); }
public void Count2() { NodeRef nr = new NodeRef("/a"); Assert.AreEqual(1, nr.Count); }
public void ChildIndex() { NodeRef up = new NodeRef("/dir1/sub1"); NodeRef c1 = up["sub2/sub3"]; Assert.AreEqual("/dir1/sub1/sub2/sub3", c1.Path); }
public void TestNodeCreateChild() { NodeRef up = new NodeRef("/dir1/sub1"); NodeRef c1 = up.CreateChild("sub2"); Assert.AreEqual("/dir1/sub1/sub2", c1.Path); }
public void TestEscapedNone() { NodeRef nr = new NodeRef("/a/b/c"); Assert.AreEqual("/a/b/c", nr.ToString(), "String comparison"); }
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); }
public void Includes4() { NodeRef nr = new NodeRef("/this/is"); NodeRef sr = new NodeRef("/this"); Assert.AreEqual(false, nr.Includes(sr)); }
public void Includes2() { NodeRef nr = new NodeRef("/this/is"); NodeRef sr = new NodeRef("/not/in/is/a/path"); Assert.AreEqual(false, nr.Includes(sr)); }
public void Includes1() { NodeRef nr = new NodeRef("/this/is"); NodeRef sr = new NodeRef("/this/is/a/path"); Assert.AreEqual(true, nr.Includes(sr)); }
/// <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; } }
/// <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); }
/// <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); }
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()); }
public void SubRef2() { NodeRef nr = new NodeRef("/this/is"); NodeRef sr = new NodeRef("/this/is"); Assert.AreEqual("/", nr.GetSubRef(sr).Path); }
public void InvalidDoubleDot2() { NodeRef nr = new NodeRef("/../.."); Assert.AreEqual("/", nr.Path); }
public void TestDoubleStarMatch() { NodeRef nr = new NodeRef("/a/b/c"); Assert.IsTrue(nr.IsMatch("/**/c")); }
public void LeadingDotSlash() { NodeRef context = new NodeRef("/"); NodeRef nr = new NodeRef("./", context); Assert.AreEqual("/", nr.Path); }
public void TestEscapedStar() { NodeRef nr = new NodeRef("/*/*/*"); Assert.AreEqual("/*/*/*", nr.ToString(), "String comparison"); Assert.IsTrue(nr.Includes(new NodeRef("/*/*/*/abb")), "nr.Includes"); }
public void Name() { NodeRef nr = new NodeRef("/a/b/c"); Assert.AreEqual("c", nr.Name); }
public void TestSingleStarMatch3() { NodeRef nr = new NodeRef("/a/b/c"); Assert.IsFalse(nr.IsMatch("/*/*/*/c")); }
public void ParentPath() { NodeRef up = new NodeRef("/dir1/sub1"); string up1 = up.ParentPath; Assert.AreEqual("/dir1", up1); }
public void Count1() { NodeRef nr = new NodeRef("/a/b/c"); Assert.AreEqual(3, nr.Count); }
public void ParentRef() { NodeRef up = new NodeRef("/dir1/sub1"); NodeRef up1 = up.ParentRef; Assert.AreEqual("/dir1", up1.Path); }
public void Count3() { NodeRef nr = new NodeRef("/"); Assert.AreEqual(0, nr.Count); }
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); }
public void DoubleDotTop() { NodeRef nr = new NodeRef("/a/.."); Assert.AreEqual("/", nr.Path); }
public void Simple() { NodeRef up = new NodeRef("/dir1/sub1"); Assert.AreEqual("/dir1/sub1", up.Path); }