public static List <PathElement> GetSuggestionList(EMDocument doc, string userPath, PathElementType options) { var list = new List <PathElement>(); var emptyInputPath = false; if (string.IsNullOrWhiteSpace(userPath)) { emptyInputPath = true; userPath = ""; } var langId = LangIdPattern.Match(Path.GetFileName(doc.LocalPath)).Groups["langId"].Value; var m = RootKeywordPattern.Match(userPath); userPath = m.Success ? userPath.Replace(m.Value, GetSourceDir(doc.LocalPath)) : Path.Combine(Path.GetDirectoryName(doc.LocalPath), userPath); DirectoryInfo userDir; try { userDir = new DirectoryInfo(userPath); } catch (Exception e) { if (e is ArgumentNullException || e is SecurityException || e is ArgumentException || e is PathTooLongException) { // ignore and output empty list return(list); } throw; } if (userDir.Exists) { if ((options & (PathElementType.Image | PathElementType.Attachment | PathElementType.Folder)) > 0) { // File system infos. var dirs = userDir.GetDirectories(); if (options.HasFlag(PathElementType.Image)) { var imagesDirs = dirs.Where(d => d.Name.ToLower() == "images").ToArray(); if (imagesDirs.Length > 0) { list.AddRange( imagesDirs[0].GetFiles().Select(f => new PathElement(f.Name, PathElementType.Image))); } } if (options.HasFlag(PathElementType.Attachment)) { var attachmentsDirs = dirs.Where(d => d.Name.ToLower() == "attachments").ToArray(); if (attachmentsDirs.Length > 0) { list.AddRange( attachmentsDirs[0].GetFiles() .Select(f => new PathElement(f.Name, PathElementType.Attachment))); } } if (options.HasFlag(PathElementType.Folder)) { if (emptyInputPath) { list.Add(new PathElement("%ROOT%", PathElementType.Folder)); } list.AddRange( dirs.Where(d => d.Name.ToLower() != "attachments" && d.Name.ToLower() != "images") .Select(d => new PathElement(d.Name, PathElementType.Folder))); } } if ((options & (PathElementType.Bookmark | PathElementType.Excerpt | PathElementType.Variable)) > 0) { var files = userDir.GetFiles(string.Format("*.{0}.udn", langId)); if (files.Length > 0) { var linkedDocument = files[0].FullName.Equals(doc.LocalPath, StringComparison.InvariantCultureIgnoreCase) ? doc : doc.TransformationData.ProcessedDocumentCache.Get(files[0].FullName); if (options.HasFlag(PathElementType.Bookmark) && emptyInputPath) { list.AddRange(linkedDocument.GetBookmarkKeys().Select(n => new PathElement(n, PathElementType.Bookmark))); } if (options.HasFlag(PathElementType.Excerpt)) { list.AddRange(linkedDocument.GetExcerptNames().Select(n => new PathElement(n, PathElementType.Excerpt))); } if (options.HasFlag(PathElementType.Variable)) { list.AddRange(linkedDocument.GetVariableNames().Select(n => new PathElement(n, PathElementType.Variable))); } } } } return(list); }
public static List<PathElement> GetSuggestionList(EMDocument doc, string userPath, PathElementType options) { var list = new List<PathElement>(); var emptyInputPath = false; if (string.IsNullOrWhiteSpace(userPath)) { emptyInputPath = true; userPath = ""; } var langId = LangIdPattern.Match(Path.GetFileName(doc.LocalPath)).Groups["langId"].Value; var m = RootKeywordPattern.Match(userPath); userPath = m.Success ? userPath.Replace(m.Value, GetSourceDir(doc.LocalPath)) : Path.Combine(Path.GetDirectoryName(doc.LocalPath), userPath); DirectoryInfo userDir; try { userDir = new DirectoryInfo(userPath); } catch (Exception e) { if (e is ArgumentNullException || e is SecurityException || e is ArgumentException || e is PathTooLongException) { // ignore and output empty list return list; } throw; } if (userDir.Exists) { if ((options & (PathElementType.Image | PathElementType.Attachment | PathElementType.Folder)) > 0) { // File system infos. var dirs = userDir.GetDirectories(); if (options.HasFlag(PathElementType.Image)) { var imagesDirs = dirs.Where(d => d.Name.ToLower() == "images").ToArray(); if (imagesDirs.Length > 0) { list.AddRange( imagesDirs[0].GetFiles().Select(f => new PathElement(f.Name, PathElementType.Image))); } } if (options.HasFlag(PathElementType.Attachment)) { var attachmentsDirs = dirs.Where(d => d.Name.ToLower() == "attachments").ToArray(); if (attachmentsDirs.Length > 0) { list.AddRange( attachmentsDirs[0].GetFiles() .Select(f => new PathElement(f.Name, PathElementType.Attachment))); } } if (options.HasFlag(PathElementType.Folder)) { if (emptyInputPath) { list.Add(new PathElement("%ROOT%", PathElementType.Folder)); } list.AddRange( dirs.Where(d => d.Name.ToLower() != "attachments" && d.Name.ToLower() != "images") .Select(d => new PathElement(d.Name, PathElementType.Folder))); } } if ((options & (PathElementType.Bookmark | PathElementType.Excerpt | PathElementType.Variable)) > 0) { var files = userDir.GetFiles(string.Format("*.{0}.udn", langId)); if (files.Length > 0) { var linkedDocument = files[0].FullName.Equals(doc.LocalPath, StringComparison.InvariantCultureIgnoreCase) ? doc : doc.TransformationData.ProcessedDocumentCache.Get(files[0].FullName); if (options.HasFlag(PathElementType.Bookmark) && emptyInputPath) { list.AddRange(linkedDocument.GetBookmarkKeys().Select(n => new PathElement(n, PathElementType.Bookmark))); } if (options.HasFlag(PathElementType.Excerpt)) { list.AddRange(linkedDocument.GetExcerptNames().Select(n => new PathElement(n, PathElementType.Excerpt))); } if (options.HasFlag(PathElementType.Variable)) { list.AddRange(linkedDocument.GetVariableNames().Select(n => new PathElement(n, PathElementType.Variable))); } } } } return list; }