Ejemplo n.º 1
0
		/// <summary>
		/// Constructs the XML output pipe.
		/// </summary>
		/// <param name="outputPipe">The target output pipe which to write to.</param>
		/// <param name="format">The <see cref="CsvOutputFormat"/> which to use.</param>
		public CsvOutputPipe(IOutputPipe outputPipe, CsvOutputFormat format)
		{
			// validate arguments
			if (outputPipe == null)
				throw new ArgumentNullException("outputPipe");
			if (format == null)
				throw new ArgumentNullException("format");

			// set values
			this.outputPipe = outputPipe;
			this.format = format;

			// write the document start delimitor when available
			if (format.HasDocumentStartDelimitor)
				outputPipe.Writer.Write(format.DocumentStartDelimitor);

			// write the column headers when needed
			if (format.IncludeColumnHeaders)
				Write(format.ColumnHeaders);
		}
		/// <summary>
		/// </summary>
		/// <param name="context"></param>
		protected override void DoExecute(IMansionContext context)
		{
			// check if a default format is used or a custom one is specified
			var formatName = GetAttribute<string>(context, "format");
			CsvOutputFormat format;
			if (!string.IsNullOrEmpty(formatName))
			{
				// get the format
				format = CsvOutputFormat.GetFormat(formatName);
			}
			else
			{
				// create a custom format
				format = new CsvOutputFormat
				         {
				         	ColumnDelimitor = GetAttribute<string>(context, "columnDelimitor"),
				         	ColumnProperties = GetAttribute<string>(context, "columnProperties").Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries),
				         	ColumnHeaders = GetAttribute(context, "columnHeaders", GetAttribute<string>(context, "columnProperties")).Split(new[] {','}),
				         	DocumentEndDelimitor = GetAttribute<string>(context, "documentEndDelimitor"),
				         	DocumentStartDelimitor = GetAttribute<string>(context, "documentStartDelimitor"),
				         	IncludeColumnHeaders = GetAttribute<bool>(context, "includeColumnHeaders"),
				         	RowDelimitor = GetAttribute<string>(context, "rowDelimitor"),
				         	TextQualifier = GetAttribute<string>(context, "textQualifier")
				         };
			}

			// check if the format is sane
			if (format.ColumnProperties.Length != format.ColumnHeaders.Length)
				throw new InvalidOperationException("Unbalanced header with properties");

			// create the XML pipe and push it to the stack
			using (var pipe = new CsvOutputPipe(context.OutputPipe, format))
			using (context.OutputPipeStack.Push(pipe))
			{
				// execute the children
				ExecuteChildTags(context);
			}
		}