source: xmlTools/trunk/posUpdate/Util.cs@ 719

Last change on this file since 719 was 718, checked in by s10k, 12 years ago

reverted the no backup test

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Text.RegularExpressions;
6using System.Xml;
7
8namespace xmlTools
9{
10 /// <summary>
11 /// Utilities class. Contain function used along all the program and classes.
12 /// </summary>
13 class Util
14 {
15 /// <summary>
16 /// Backup a file, return true if sucessful otherwise returns false
17 /// </summary>
18 /// <param name="file"></param>
19 static public bool backupFile(string file)
20 {
21 try
22 {
23 System.IO.File.Copy(file, file + ".bak", false); //make a backup first //false is to not override already created backups
24 }
25 catch (Exception e)
26 {
27 Program.printAppError(Program.appErrors.BACKUPS_ALREADY_EXISTS, "Couldn't backup file " + file + " :\n" + e.Message);
28 return false;
29 }
30 return true;
31 }
32
33 /// <summary>
34 /// Get all elements with a determined name and optionally a parent name. Result is saved on the paremeter list "result"
35 /// </summary>
36 /// <param name="rootNode"></param>
37 /// <param name="result"></param>
38 /// <param name="posElement"></param>
39 /// <param name="posParentElement"></param>
40 public static void getAllSpecificElements(XmlNode rootNode, ref List<XmlNode> result, String posElement, String posParentElement = "")
41 {
42 foreach (XmlNode element in rootNode.ChildNodes)
43 {
44 if (element.Name == posElement && (posParentElement == "" || posParentElement == element.ParentNode.Name))
45 {
46 result.Add(element);
47 continue;
48 }
49 getAllSpecificElements(element, ref result, posElement, posParentElement); //If not found in this node continue search in subnodes
50 }
51 }
52
53 /// <summary>
54 /// Converts a string to a xmlNode. Throws XmlException if the parsing of xml fails.
55 /// </summary>
56 /// <param name="xmlContent"></param>
57 /// <returns></returns>
58 public static XmlNode stringToXmlNode(string xmlContent)
59 {
60 XmlDocument doc = new XmlDocument();
61 try
62 {
63 doc.LoadXml(xmlContent);
64 }
65 catch (XmlException e)
66 {
67 throw e;
68 }
69 return doc.DocumentElement;
70 }
71
72 /// <summary>
73 /// Gets all xml files in the same directory of the executable
74 /// </summary>
75 /// <returns></returns>
76 static public List<String> getAllXmlFiles()
77 {
78 return getXmlFilesWildcard("*.xml");
79 }
80
81 /// <summary>
82 /// Converts wildcard to regex and uses it to make the match
83 /// </summary>
84 /// <param name="filewildcard"></param>
85 /// <returns></returns>
86 public static List<String> getXmlFilesWildcard(String filewildcard)
87 {
88 List<String> xmlFiles = new List<String>();
89 String[] files = System.IO.Directory.GetFiles(getExePath()); //Get all files in executable directory
90
91 foreach (String file in files)
92 {
93 Regex wildcardRegex = new Regex(Util.WildcardToRegex(filewildcard), RegexOptions.IgnoreCase); //case insensitivity
94 if (wildcardRegex.IsMatch(Path.GetFileName(file)))
95 {
96 xmlFiles.Add(file);
97 }
98 }
99
100 return xmlFiles;
101 }
102
103 /// <summary>
104 /// Converts wildcard to regex (from here: http://www.codeproject.com/Articles/11556/Converting-Wildcards-to-Regexes)
105 /// </summary>
106 /// <param name="pattern"></param>
107 /// <returns></returns>
108 private static string WildcardToRegex(string pattern)
109 {
110 return "^" + Regex.Escape(pattern).
111 Replace("\\*", ".*").
112 Replace("\\?", ".") + "$";
113 }
114
115 /// <summary>
116 /// Check when a string contains a wildcard or not
117 /// </summary>
118 /// <param name="myString"></param>
119 /// <returns></returns>
120 public static bool containsWildcard(String myString)
121 {
122 return (myString.Contains("*") || myString.Contains("?"));
123 }
124
125 public static string getExePath()
126 {
127 return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
128 }
129
130 public static string getExeFileName()
131 {
132 return Environment.GetCommandLineArgs()[0];
133 }
134
135 public static bool IsRunningOnMono()
136 {
137 return Type.GetType("Mono.Runtime") != null;
138 }
139 }
140}
Note: See TracBrowser for help on using the repository browser.