//---------------------------------------------------------------------

        private static string ConvertToString(MultiLineText message)
        {
            if (message == null)
            {
                throw new System.ArgumentNullException();
            }
            return(message.ToString());
        }
        //---------------------------------------------------------------------

        public static MultiLineText operator+(MultiLineText x,
                                              MultiLineText y)
        {
            MultiLineText result = x.Clone();

            foreach (string line in y)
            {
                result.Add(line);
            }
            return(result);
        }
        //---------------------------------------------------------------------

        private void SetMultiLineMessage(MultiLineText message,
                                         MultiLineText innerMessage)
        {
            if (message.Count == 1 && innerMessage != null)
            {
                multiLineMessage = new MultiLineText(message.ToString() + ":");
            }
            else
            {
                multiLineMessage = new MultiLineText(message);
            }
            if (innerMessage != null)
            {
                foreach (string line in innerMessage)
                {
                    multiLineMessage.Add(Indent + line);
                }
            }
        }
        //---------------------------------------------------------------------

        public MultiLineException(MultiLineText message,
                                  System.Exception innerException)
            : base(ConvertToString(message), innerException)
        {
            if (innerException == null)
            {
                SetMultiLineMessage(message, null);
            }
            else
            {
                MultiLineException inner = innerException as MultiLineException;
                if (inner != null)
                {
                    SetMultiLineMessage(message, inner.MultiLineMessage);
                }
                else
                {
                    SetMultiLineMessage(message, innerException.Message);
                }
            }
        }
        //---------------------------------------------------------------------

        public MultiLineException(MultiLineText message,
                                  MultiLineText innerMessage)
            : base(ConvertToString(message))
        {
            SetMultiLineMessage(message, innerMessage);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Checks a template for output filenames to ensure that all the
        /// variables used in the template are known.
        /// </summary>
        /// <param name="template">
        /// The template for a set of output filenames.
        /// </param>
        /// <param name="variables">
        /// A dictionary collection of known variables where a key is the
        /// variable's name and its value indicates whether the variable
        /// is required or not.
        /// </param>
        /// <remarks>
        /// A variable is used by enclosing its name in curly braces, e.g.,
        /// "{variable-name}".
        /// </remarks>
        /// <exception cref="InputValueException">
        /// The template is missing a required variable or has unknown
        /// variable.
        /// </exception>
        public static void CheckTemplateVars(string template,
                                             IDictionary <string, bool> variables)
        {
            IList <string> namesUsed = Macros.GetNames(template);

            IList <string> unknownVars = new List <string>();

            foreach (string name in namesUsed)
            {
                if (!variables.ContainsKey(name))
                {
                    unknownVars.Add(name);
                }
            }
            if (unknownVars.Count == 1)
            {
                string mesg = string.Format("The template uses an unknown variable: {{{0}}}",
                                            unknownVars[0]);
                throw new InputValueException(template, mesg);
            }
            if (unknownVars.Count > 1)
            {
                MultiLineText innerMesg = new MultiLineText();
                foreach (string name in unknownVars)
                {
                    innerMesg.Add(string.Format("{{{0}}}", name));
                }
                throw new InputValueException(template,
                                              "The template uses these unknown variables",
                                              innerMesg);
            }

            //  No unknown variables; check if all required variables were used
            IList <string> missingReqdVars = new List <string>();

            foreach (string name in variables.Keys)
            {
                if (variables[name])
                {
                    //  Required
                    if (!namesUsed.Contains(name))
                    {
                        missingReqdVars.Add(name);
                    }
                }
            }
            if (missingReqdVars.Count == 1)
            {
                MultiLineText mesg = new MultiLineText();
                mesg.Add(string.Format("The template must include the variable {{{0}}} to ensure",
                                       missingReqdVars[0]));
                mesg.Add("that the names of all output files are unique.");
                throw new InputValueException(template, mesg);
            }
            if (missingReqdVars.Count > 1)
            {
                MultiLineText mesg = new MultiLineText();
                mesg.Add("The template must use the following variables to ensure");
                mesg.Add("that the names of all output files are unique:");

                MultiLineText innerMesg = new MultiLineText();
                foreach (string name in missingReqdVars)
                {
                    innerMesg.Add(string.Format("{{{0}}}", name));
                }
                throw new InputValueException(template, mesg, innerMesg);
            }
        }
        //---------------------------------------------------------------------

        public MultiLineText Clone()
        {
            MultiLineText clone = new MultiLineText(lines);

            return(clone);
        }