#region License
//
// Copyright 2015-2013 Giacomo Stelluti Scala
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
#region Using Directives
using System;
using System.Globalization;
using System.Reflection;
using System.Text;
using CommandLine.Infrastructure;
#endregion
namespace CommandLine.Text
{
///
/// Models the copyright part of an help text.
/// You can assign it where you assign any instance.
///
public class CopyrightInfo
{
private const string DefaultCopyrightWord = "Copyright";
private const string SymbolLower = "(c)";
private const string SymbolUpper = "(C)";
private readonly AssemblyCopyrightAttribute _attribute;
private readonly bool _isSymbolUpper;
private readonly int[] _copyrightYears;
private readonly string _author;
private readonly int _builderSize;
///
/// Initializes a new instance of the class
/// specifying author and year.
///
/// The company or person holding the copyright.
/// The year of coverage of copyright.
/// Thrown when parameter is null or empty string.
public CopyrightInfo(string author, int year)
: this(true, author, new[] { year })
{
}
///
/// Initializes a new instance of the class
/// specifying author and copyrightYears.
///
/// The company or person holding the copyright.
/// The copyrightYears of coverage of copyright.
/// Thrown when parameter is null or empty string.
/// Thrown when parameter is not supplied.
public CopyrightInfo(string author, params int[] years)
: this(true, author, years)
{
}
///
/// Initializes a new instance of the class
/// specifying symbol case, author and copyrightYears.
///
/// The case of the copyright symbol.
/// The company or person holding the copyright.
/// The copyrightYears of coverage of copyright.
/// Thrown when parameter is null or empty string.
/// Thrown when parameter is not supplied.
public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears)
{
Assumes.NotNullOrEmpty(author, "author");
Assumes.NotZeroLength(copyrightYears, "copyrightYears");
const int ExtraLength = 10;
_isSymbolUpper = isSymbolUpper;
_author = author;
_copyrightYears = copyrightYears;
_builderSize = 12 + author.Length + (4 * copyrightYears.Length) + ExtraLength;
}
///
/// Initializes a new instance of the class.
///
protected CopyrightInfo()
{
}
///
/// Initializes a new instance of the class
/// with an assembly attribute, this overrides all formatting.
///
/// The attribute which text to use.
private CopyrightInfo(AssemblyCopyrightAttribute attribute)
{
_attribute = attribute;
}
///
/// Gets the default copyright information.
/// Retrieved from , if it exists,
/// otherwise it uses as copyright holder with the current year.
/// If neither exists it throws an .
///
public static CopyrightInfo Default
{
get
{
// if an exact copyright string has been specified, it takes precedence
var copyright = ReflectionHelper.GetAttribute();
if (copyright != null)
{
return new CopyrightInfo(copyright);
}
// if no copyright attribute exist but a company attribute does, use it as copyright holder
var company = ReflectionHelper.GetAttribute();
if (company != null)
{
return new CopyrightInfo(company.Company, DateTime.Now.Year);
}
throw new InvalidOperationException(SR.InvalidOperationException_CopyrightInfoRequiresAssemblyCopyrightAttributeOrAssemblyCompanyAttribute);
}
}
///
/// Gets a different copyright word when overridden in a derived class.
///
protected virtual string CopyrightWord
{
get { return DefaultCopyrightWord; }
}
///
/// Converts the copyright instance to a .
///
/// This instance.
/// The that contains the copyright.
public static implicit operator string(CopyrightInfo info)
{
return info.ToString();
}
///
/// Returns the copyright as a .
///
/// The that contains the copyright.
public override string ToString()
{
if (_attribute != null)
{
return _attribute.Copyright;
}
var builder = new StringBuilder(_builderSize);
builder.Append(CopyrightWord);
builder.Append(' ');
builder.Append(_isSymbolUpper ? SymbolUpper : SymbolLower);
builder.Append(' ');
builder.Append(FormatYears(_copyrightYears));
builder.Append(' ');
builder.Append(_author);
return builder.ToString();
}
///
/// When overridden in a derived class, allows to specify a new algorithm to render copyright copyrightYears
/// as a instance.
///
/// A array of copyrightYears.
/// A instance with copyright copyrightYears.
protected virtual string FormatYears(int[] years)
{
if (years.Length == 1)
{
return years[0].ToString(CultureInfo.InvariantCulture);
}
var yearsPart = new StringBuilder(years.Length * 6);
for (int i = 0; i < years.Length; i++)
{
yearsPart.Append(years[i].ToString(CultureInfo.InvariantCulture));
int next = i + 1;
if (next < years.Length)
{
yearsPart.Append(years[next] - years[i] > 1 ? " - " : ", ");
}
}
return yearsPart.ToString();
}
}
}