Exemplo n.º 1
0
 public ElementIdentifier RelativeTo(ElementIdentifier baseElement)
 {
     if (baseElement != null)
     {
         ElementIdentifier           relative               = new ElementIdentifier();
         Dictionary <string, string> baseAnchorDictionary   = baseElement.ToAnchorDictionary();
         Dictionary <string, string> targetAnchorDictionary = this.ToAnchorDictionary();
         foreach (string baseAnchor in baseAnchorDictionary.Keys)
         {
             string basePath = baseAnchorDictionary[baseAnchor];
             string targetPath;
             if (targetAnchorDictionary.TryGetValue(baseAnchor, out targetPath))
             {
                 string[] baseParts   = basePath.Split('/');
                 string[] targetParts = targetPath.Split('/');
                 string   relativePath;
                 if (baseParts.Length == targetParts.Length)
                 {
                     relativePath = ".";
                 }
                 else
                 {
                     relativePath = String.Join("/", targetParts, baseParts.Length, targetParts.Length - baseParts.Length);
                 }
                 relativePath = String.Format("/{0}", relativePath);
                 relative.Identifiers.Add(relativePath);
                 //We only need one path here relative to the base because all paths relative to base paths of matching anchors will be the same.
                 return(relative);
             }
         }
     }
     return(null);
 }