예제 #1
0
        /// <summary>
        /// Creates a new <see cref="Formatter"/> using the specified 
        /// <paramref name="tokenizer"/> and <paramref name="textAligner"/>.
        /// </summary>
        /// <param name="tokenizer">
        /// The tokenizer that will be used to extract the formatting tokens 
        /// from the format string.
        /// </param>
        /// <param name="textFormatter">
        /// The formatter that will be used to format the entity or property values
        /// before aligning and replacing the formatting tokens into the output string.
        /// </param>
        /// <param name="textAligner">
        /// A class able to align the formatted values before replacing 
        /// the formatting tokens into the output string.
        /// </param>
        /// <param name="constantSet">
        /// The default constant set.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// <i>tokenizer</i> or <i>textAligner</i> is a null reference (<b>Nothing</b> in Visual Basic).
        /// </exception>
        public Formatter( ITokenizer tokenizer, ITextFormatter textFormatter, ITextAligner textAligner, IConstantSet constantSet )
        {
            if ( tokenizer == null )
                throw new ArgumentNullException( "tokenizer" );
            if ( textFormatter == null )
                throw new ArgumentNullException( "textFormatter" );
            if ( textAligner == null )
                throw new ArgumentNullException( "textAligner" );
            if ( constantSet == null )
                throw new ArgumentNullException( "constantSet" );

            this.tokenizer = tokenizer;
            this.textFormatter = textFormatter;
            this.textAligner = textAligner;
            this.constantSet = constantSet;

            this.Reset();
        }
예제 #2
0
        /// <summary>
        /// Generates the <see cref="FormatParameters"/> related to the specified 
        /// array of <see cref="System.Object"/>s.
        /// </summary>
        /// <param name="args">The object that will be formatted.</param>
        /// <param name="constantSet"></param>
        /// <returns>
        /// The <see cref="FormatParameters"/> related to the specified array of <see cref="System.Object"/>s.
        /// </returns>
        public FormatParameters GetFormatParameters( object[] args, IConstantSet constantSet )
        {
            if ( args == null )
                throw new ArgumentNullException( "args" );

            var positionString = match.Groups["position"].Value;
            positionString = string.IsNullOrEmpty( positionString ) ? "0" : positionString;
            int position;
            if ( !int.TryParse( positionString, out position ) )
                throw new FormatException( "Invalid position: " + positionString );

            if ( position < 0 || position >= args.Length )
                throw new FormatException( "Invalid position: " + positionString + " should be between 0 and " + (args.Length - 1) );

            var alignmentString = match.Groups["alignment"].Value;
            alignmentString = string.IsNullOrEmpty( alignmentString ) ? "0" : alignmentString;
            int alignment;
            if ( !int.TryParse( alignmentString, out alignment ) )
                throw new FormatException( "Invalid alignment: " + alignmentString );

            var propertyName = match.Groups["property"].Value;

            var propertyPath = (propertyName.StartsWith( "." ) ? propertyName.Substring( 1 ) : propertyName).Split( '.' );
            if ( propertyPath.Length == 1 && string.IsNullOrEmpty( propertyPath[0] ) ) propertyPath = new string[0];
            if ( propertyPath.Length > 1 && propertyPath.Any( property => string.IsNullOrEmpty( property ) ) )
                throw new FormatException( "Invalid property: double dot in property path." );

            var format = match.Groups["format"].Value;

            object constantValue = null;
            var constantGroup = match.Groups["constant"];
            if ( constantGroup.Success )
            {
                var constantName = constantGroup.Value;
                constantValue = constantSet.GetValue( constantName );
            }

            return new FormatParameters( position, propertyPath, alignment, format, constantValue );
        }