zaaReloaded2/zaaReloaded2/Formatter/DocumentWriter.cs

163 lines
4.3 KiB
C#
Executable File

/* DocumentWriter.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace zaaReloaded2.Formatter
{
/// <summary>
/// Helper class that serves to write text to a Word document or
/// to a linked DocumentWriter. Provides a buffer that can be
/// appended or prepended to, which facilitates conditional output
/// depending on whether there is text in the buffer or not.
/// </summary>
/// <remarks>
/// Linking several DocumentWriters permits a cascading work flow
/// with several buffers.
/// </remarks>
class DocumentWriter
{
#region Properties
/// <summary>
/// Gets the Document associated with this DocumentWriter,
/// or null if there is no associated Document.
/// </summary>
public Document Document { get; set; }
/// <summary>
/// Gets the parent DocumentWriter (if any).
/// </summary>
public DocumentWriter Parent { get; set; }
/// <summary>
/// Returns true if there is text in the buffer.
/// </summary>
public bool HasBufferedText { get { return _buffer.Length > 0; } }
#endregion
#region Constructors
public DocumentWriter()
{
_buffer = new StringBuilder();
}
/// <summary>
/// Creates a new DocumentWriter instance that is associated
/// with a Word Document.
/// </summary>
/// <param name="document"></param>
public DocumentWriter(Document document)
: this()
{
Document = document;
}
/// <summary>
/// Creates a new DocumentWriter instance that is associated
/// with a parent DocumentWriter.
/// </summary>
/// <param name="parent"></param>
public DocumentWriter(DocumentWriter parent)
: this()
{
Parent = parent;
}
#endregion
#region Overrides
public override string ToString()
{
return _buffer.ToString();
}
#endregion
#region Public methods
/// <summary>
/// Flushes the buffer to the associated Word document and/or the
/// parent DocumentWriter.
/// </summary>
public void Flush()
{
if (!HasBufferedText) return;
if (Document == null && Parent == null)
{
throw new InvalidOperationException(
"No document and no parent buffer to flush into.");
}
if (Document != null)
{
Document.Range().Text = _buffer.ToString();
}
if (Parent != null)
{
Parent.Write(_buffer.ToString());
}
_buffer.Clear();
}
/// <summary>
/// Writes text to the buffer.
/// </summary>
/// <param name="text">Text to write to the buffer.</param>
public void Write(string text)
{
_buffer.Append(text);
}
/// <summary>
/// Appends a line of text to the buffer.
/// </summary>
/// <param name="text">Text to append.</param>
public void WriteLine(string text)
{
_buffer.AppendLine(text);
}
/// <summary>
/// Inserts text at the start of the buffer.
/// </summary>
/// <param name="text">Text to insert at the start of the
/// buffer.</param>
public void Prepend(string text)
{
_buffer.Insert(0, text);
}
#endregion
#region Fields
StringBuilder _buffer;
#endregion
}
}