AcUtils
A high performance abstraction layer for AccuRev
Extensions.cs
Go to the documentation of this file.
1 
16 using System;
17 using System.Diagnostics;
18 using System.Globalization;
19 using System.Linq;
20 using System.Xml.Linq;
21 
22 namespace AcUtils
23 {
24  #region enums
25 
26  public enum RealVirtual
31  {
34  Real,
37  Virtual
38  };
40  #endregion
41 
45 
46  public static class Extensions
47  {
54 
59  public static DateTime? acxTime(this XElement element, XName name)
60  {
61  DateTime? dt = null;
62  XAttribute attr = element.Attribute(name);
63  if (attr != null)
64  dt = AcDateTime.AcDate2DateTime((long)element.Attribute(attr.Name));
65  return dt;
66  }
67 
74 
78  public static ElementType acxType(this XElement element, XName name)
79  {
80  ElementType type = ElementType.unknown;
81  XAttribute attr = element.Attribute(name);
82  if (attr != null)
83  {
84  string temp = (string)element.Attribute(attr.Name);
85  if (!String.Equals(temp, "* unknown *")) // known AccuRev defect
86  type = (ElementType)Enum.Parse(typeof(ElementType), temp);
87  }
88 
89  return type;
90  }
91 
97  public static bool acxIsPromote(this XElement transaction)
98  {
99  Debug.Assert(transaction.Name == "transaction", @"transaction.Name == ""transaction""");
100  return String.Equals((string)transaction.Attribute("type"), "promote");
101  }
102 
109  public static string acxFromStream(this XElement transaction)
110  {
111  Debug.Assert(transaction.Name == "transaction", @"transaction.Name == ""transaction""");
112  string name = null;
113  if (transaction.acxIsPromote())
114  name = $"{(string)transaction.Attribute("fromStreamName")} ({(int)transaction.Attribute("fromStreamNumber")})";
115 
116  return name;
117  }
118 
125  public static string acxToStream(this XElement transaction)
126  {
127  Debug.Assert(transaction.Name == "transaction", @"transaction.Name == ""transaction""");
128  string name = null;
129  if (transaction.acxIsPromote())
130  name = $"{(string)transaction.Attribute("streamName")} ({(int)transaction.Attribute("streamNumber")})";
131 
132  return name;
133  }
134 
140  public static bool acxIsCheckOut(this XElement transaction)
141  {
142  Debug.Assert(transaction.Name == "transaction", @"transaction.Name == ""transaction""");
143  return String.Equals((string)transaction.Attribute("type"), "co");
144  }
145 
152 
156  public static string acxVirtualNamed(this XElement transaction)
157  {
158  Debug.Assert(transaction.Name == "transaction", @"transaction.Name == ""transaction""");
159  string name = null;
160  if (transaction.acxIsPromote() || transaction.acxIsCheckOut())
161  {
162  XElement first = transaction.Elements("version").FirstOrDefault();
163  if (first != null)
164  name = $"{(string)first.Attribute("virtualNamedVersion")} ({(string)first.Attribute("virtual")})";
165  }
166 
167  return name;
168  }
169 
176 
179  public static string acxRealNamed(this XElement version)
180  {
181  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
182  XElement transaction = version.Parent;
183  XElement first = transaction.Elements("version").FirstOrDefault();
184  // return null for promote transactions when this version is the first version element in the transaction
185  if (first != null && version == first && transaction.acxIsPromote())
186  return null;
187 
188  string name = $"{(string)version.Attribute("realNamedVersion")} ({(string)version.Attribute("real")})";
189  return name;
190  }
191 
198  public static string acxAncestorNamed(this XElement version)
199  {
200  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
201  string name = null;
202  string ancestorNamedVersion = (string)version.Attribute("ancestorNamedVersion");
203  if (!String.IsNullOrEmpty(ancestorNamedVersion))
204  name = $"{ancestorNamedVersion} ({(string)version.Attribute("ancestor")})";
205  return name;
206  }
207 
214  public static string acxMergedAgainstNamed(this XElement version)
215  {
216  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
217  string name = null;
218  string mergedAgainstNamedVersion = (string)version.Attribute("mergedAgainstNamedVersion");
219  if (!String.IsNullOrEmpty(mergedAgainstNamedVersion))
220  name = $"{mergedAgainstNamedVersion} ({(string)version.Attribute("merged_against")})";
221  return name;
222  }
223 
231 
235  public static string acxComment(this XElement element)
236  {
237  Debug.Assert(element.Name == "transaction" || element.Name == "version",
238  @"element.Name == ""transaction"" || element.Name == ""version""");
239  XElement xe = null;
240  if (element.Name == "transaction")
241  xe = element.Elements().FirstOrDefault();
242  else if (element.Name == "version")
243  {
244  XElement trans = element.Parent;
245  XElement first = trans.Elements("version").FirstOrDefault();
246  // return null for promote transactions when this version is the first version element in the transaction
247  if (first != null && element == first && trans.acxIsPromote())
248  return null;
249  // get sibling element directly above this version element
250  xe = element.ElementsBeforeSelf().LastOrDefault();
251  }
252 
253  string comment = null;
254  if (xe != null && xe.Name == "comment")
255  comment = (string)xe;
256  return comment;
257  }
258 
264  public static string acxWSpaceName(this XElement version)
265  {
266  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
267  string temp = (string)version.Attribute("realNamedVersion");
268  string name = temp.Substring(0, temp.IndexOf('/'));
269  return name;
270  }
271 
277  public static string acxWSpaceOwner(this XElement version)
278  {
279  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
280  string prncpl = null;
281  string wspace = version.acxWSpaceName();
282  int ii = wspace.LastIndexOf('_');
283  if (ii != -1) prncpl = wspace.Substring(++ii);
284  return prncpl;
285  }
286 
294 
295  public static int[] acxStreamVersion(this XElement version, RealVirtual request)
296  {
297  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
298  string temp = (request == RealVirtual.Real) ? (string)version.Attribute("real") :
299  (string)version.Attribute("virtual");
300  if (temp == null) return null;
301  string[] val = temp.Split('/');
302  int[] arr = new int[2];
303  if (!Int32.TryParse(val[0], NumberStyles.Integer, null, out arr[0])) return null;
304  if (!Int32.TryParse(val[1], NumberStyles.Integer, null, out arr[1])) return null;
305  return arr;
306  }
307 
314 
315  public static string acxStreamName(this XElement version, RealVirtual request)
316  {
317  Debug.Assert(version.Name == "version", @"version.Name == ""version""");
318  string temp = (request == RealVirtual.Real) ? (string)version.Attribute("realNamedVersion") :
319  (string)version.Attribute("virtualNamedVersion");
320  if (temp == null) return null;
321  string stream = temp.Substring(0, temp.IndexOf('/'));
322  return stream;
323  }
324  }
325 }
ElementType
Indicates the element's type. By default, AccuRev determines the element type for a newly created ver...
Definition: Stat.cs:35
static DateTime acxTime(this XElement element, XName name)
Convert element's Epoch time name XML attribute value to a .NET DateTime object.
Definition: Extensions.cs:59
static string acxAncestorNamed(this XElement version)
Get the ancestor-named version for version.
Definition: Extensions.cs:198
static bool acxIsPromote(this XElement transaction)
Determine if transaction was a promote operation.
Definition: Extensions.cs:97
static int[] acxStreamVersion(this XElement version, RealVirtual request)
Get the real or virtual stream and version numbers for version.
Definition: Extensions.cs:295
Use to validate and convert date/time values to and from .NET and AccuRev/Unix formats.
Definition: AcDateTime.cs:25
static string acxVirtualNamed(this XElement transaction)
Get the virtual-named version for the promote or co transaction.
Definition: Extensions.cs:156
static DateTime AcDate2DateTime(long seconds)
Convert an AccuRev date given in Unix time (seconds param) to a .NET DateTime in local time...
Definition: AcDateTime.cs:61
static string acxStreamName(this XElement version, RealVirtual request)
Get the real or virtual stream name for version.
Definition: Extensions.cs:315
static string acxComment(this XElement element)
Get the comment for the transaction or version element.
Definition: Extensions.cs:235
static bool acxIsCheckOut(this XElement transaction)
Determine if transaction was a co operation.
Definition: Extensions.cs:140
static string acxWSpaceOwner(this XElement version)
Get the workspace owner's principal name for version.
Definition: Extensions.cs:277
static string acxMergedAgainstNamed(this XElement version)
Get the merged-against-named version for version.
Definition: Extensions.cs:214
Custom query operators for use when using LINQ to XML.
Definition: Extensions.cs:46
RealVirtual
Use to specify real or virtual values.
Definition: Extensions.cs:30
static string acxFromStream(this XElement transaction)
Get the from-stream version for the promote transaction.
Definition: Extensions.cs:109
static ElementType acxType(this XElement element, XName name)
Convert element's type name attribute value to an AcUtils.ElementType enum.
Definition: Extensions.cs:78
static string acxWSpaceName(this XElement version)
Get the workspace name for version.
Definition: Extensions.cs:264
static string acxRealNamed(this XElement version)
Get the real-named version for version.
Definition: Extensions.cs:179
static string acxToStream(this XElement transaction)
Get the to-stream version for the promote transaction.
Definition: Extensions.cs:125