context: //
  // NAntContrib
  // Copyright (C) 2002 Tomas Restrepo (tomasr@mvps.org)
  //
  // This library is free software; you can redistribute it and/or
  // modify it under the terms of the GNU Lesser General Public
  // License as published by the Free Software Foundation; either
  // version 2.1 of the License, or (at your option) any later version.
  //
  // This library is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  // Lesser General Public License for more details.
  //
  // You should have received a copy of the GNU Lesser General Public
  // License along with this library; if not, write to the Free Software
  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  //
  
  using System;
  using System.Globalization;
  using System.IO;
  using System.Xml;
  
  using InterOpStarTeam = StarTeam;
  
  using NAnt.Core;
  using NAnt.Core.Attributes;
  
  namespace NAnt.Contrib.Tasks.StarTeam {
      /// <summary>
      /// Task for supporting labeling of repositories with incremented version 
      /// numbers. The version number calculated will be concatenated to the 
      /// <see cref="LabelTask.Label" />.
      /// </summary>
      /// <remarks>
      /// <para>
      /// Instruments root of repository with <c>versionnumber.xml</c> file.
      /// </para>
      /// <para>
      /// If this file is not present, it is created and checked into StarTeam. 
      /// The default version number is 1.0.0. By default the build number is 
      /// incremented. Properties are present to allow setting and incrementing 
      /// of major, minor, and build versions.
      /// </para>
      /// <para>
      /// When label is created, properties are set to expose version information 
      /// and the new label :
      /// </para>
      /// <list type="bullet">
      ///     <item>
      ///         <description>label</description>
      ///     </item>
      ///     <item>
      ///         <description>Version.text</description>
      ///     </item>
      ///     <item>
      ///         <description>Version.major</description>
      ///     </item>
      ///     <item>
      ///         <description>Version.minor</description>
      ///     </item>
      ///     <item>
      ///         <description>Version.build</description>
      ///     </item>
      /// </list>
      /// <note>
      /// Incrementing or setting major or minor versions does NOT reset the 
      /// build version.
      /// </note>
      /// </remarks>
      /// <example>
      ///   <para>Increment the build version.</para>
      ///   <code>
      ///     <![CDATA[
      /// <stautolabel url="${ST.url}" />
      ///     ]]>
      ///   </code>
      /// </example>
      /// <example>
      ///   <para>Set the major version.</para>
      ///   <code>
      ///     <![CDATA[
      /// <stautolabel majorversion="2" url="${ST.url}" />
      ///     ]]>
      ///   </code>
      /// </example>
      /// <example>
      ///   <para>Increment the minor version.</para>
      ///   <code>
      ///     <![CDATA[
      /// <stautolabel incrementminor="true" url="${ST.url}" />
      ///     ]]>
      ///   </code>
      /// </example>
      /// <example>
      ///   <para>Example <c>versionnumber.xml</c> file.</para>
      ///   <code>
      ///     <![CDATA[
      /// <?xml version="1.0"?>
      /// <stautolabel>
      ///     <version major="1" minor="0" build="0" />
      /// </stautolabel>
      ///     ]]>
      ///   </code>
      /// </example>
      [TaskName("stautolabel")]
      public class StarTeamAutoLabel : LabelTask {
          #region Private Instance Fields
  
          private bool _doIncrement = true;
          private bool _incrementMajor= false;
          private bool _incrementMinor = false;
          private bool _incrementBuild = true;
          private int _versionMajor = -1;
          private int _versionMinor = -1;
          private int _versionBuild = -1;
          private string _versionFile = "versionnumber.xml";
          private InterOpStarTeam.StItem_LockTypeStaticsClass starTeamLockTypeStatics = new InterOpStarTeam.StItem_LockTypeStaticsClass(); 
  
          #endregion Private Instance Fields
  
          /// <summary> 
          /// Allows user to specify the filename where the version xml is stored. 
          /// The default is <c>versionnumber.xml</c>.
          /// </summary>
          [TaskAttribute("versionfile", Required=false)]
          public virtual string VersionFile {
              get { return _versionFile; }
              set { _versionFile = value; }
          }
  
          /// <summary> 
          /// Increment major version number. The default is <see langword="false" />.
          /// If <see cref="MajorVersion"/> is set, this property is ignored.
          /// </summary>
          [TaskAttribute("incrementmajor", Required=false)]
          [BooleanValidator]
          public virtual bool IncrementMajor {
              get { return _incrementMajor; }
              set { _incrementMajor = value; }
          }
  
          /// <summary> 
          /// Increment minor version number. The default is <see langword="false" />. 
          /// If <see cref="MinorVersion"/> is set, this property is ignored.
          /// </summary>
          [TaskAttribute("incrementminor", Required=false)]
          [BooleanValidator]
          public virtual bool IncrementMinor {
              get { return _incrementMinor; }
              set { _incrementMinor = value; }
          }
  
          /// <summary> 
          /// Increment build version number. The default is <see langword="true" />.
          /// If <see cref="BuildVersion"/> is set, this property is ignored.
          /// </summary>
          [TaskAttribute("incrementbuild", Required=false)]
          [BooleanValidator]
          public virtual bool IncrementBuild {
              get { return _incrementBuild; }
              set { _incrementBuild = value; }
          }
  
          /// <summary> 
          /// Major version number used for label.  If this value is set, 
          /// <see cref="IncrementMajor"/> is ignored.
          /// </summary>
          [TaskAttribute("majorversion", Required=false)]
          [Int32Validator(0,9999999)]
          public virtual int MajorVersion {
              get { return _versionMajor; }
              set {
                  _doIncrement = false;
                  _versionMajor = value;
              }
          }
  
          /// <summary> 
          /// Minor version number used for label. If this value is set, 
          /// <see cref="IncrementMinor"/> is ignored.
          /// </summary>
          [TaskAttribute("minorversion", Required=false)]
          [Int32Validator(0,9999999)]
          public virtual int MinorVersion {
              get { return _versionMinor; }
              set {
                  _doIncrement = false;
                  _versionMinor = value;
              }
          }
  
          /// <summary> 
          /// Build version number used for label. 
          /// If this value is set. <see cref="IncrementBuild"/> is ignored.
          /// </summary>
          [TaskAttribute("buildversion", Required=false)]
          [Int32Validator(0,9999999)]
          public virtual int BuildVersion {
              get { return _versionBuild; }
              set {
                  _doIncrement = false;
                  _versionBuild = value;
              }
          }
  
          /// <summary>
          /// Looks for versionnumber.xml at root of repository. 
          /// Updates the xml in this file to correspond with properties set by user and checks in changes. 
          /// A label is then created based on properties set. 
          /// </summary>
          /// <remarks>
          /// Default behavior is to <see cref="IncrementBuild"/> number. 
          /// If user sets <see cref="MajorVersion"/>, <see cref="MinorVersion"/>, or <see cref="BuildVersion"/> no incrementing is done 
          /// and the exact version set and/or read from versionnumber.xml is used.
          /// <para>The title of the Label is the <see cref="LabelTask.Label"/> property concatenated with the version number Major.Minor.Build</para>
          /// </remarks>
          protected override void  ExecuteTask() {
              InterOpStarTeam.StView snapshot = openView();
              InterOpStarTeam.StFile stFile = getVersionStFile(snapshot);
  
              try {
                  //load xml document find versions and save incremented version 
  
gt:                 XmlDocument xmlDoc = new XmlDocument();