using System;
using System.IO;
using System.Text;
using Cobilas.IO.Atlf.Text;
namespace Cobilas.IO.Atlf {
/// Base class for ATLF writing classes.
public abstract class ATLFWriter : ATLFBase {
/// Represents the character of indentation.
public abstract string IndentChars { get; set; }
/// When overridden in a derived class, clears all buffers for this stream and causes any
/// buffered data to be written to the underlying device.
public abstract void Flush();
/// Writes the atlf header to the stream.
public abstract void WriteHeader();
/// Write a comment in the stream.
/// Write the message.
public abstract void WriteComment(string value);
/// Writes an escape character to the stream.
public abstract void WriteWhitespace(string value);
/// Writes an ATLF node to the stream.
public abstract void WriteNode(string name, string value);
/// Writes an escape character to the stream.
public abstract void WriteWhitespace(int count, string value);
/// Gets the ATLF encoding.
/// The method returns the encoder corresponding to the version passed in the parameter.
/// If the previous version does not exist, the default version will be returned.
protected abstract ATLFEncoding GetATLFEncoding(string targetVersion);
/// Adds a new node to the buffer.
protected abstract void AddNode(string name, string value, ATLFNodeType nodeType);
#pragma warning disable CS1591
public static T Create(Stream stream) where T : ATLFSBWriter => Create((MarshalByRefObject)stream);
public static T Create(string filePath) where T : ATLFSBWriter => Create(File.OpenWrite(filePath), true);
public static T Create(TextWriter text) where T : ATLFTBWriter => Create((MarshalByRefObject)text);
public static T Create(StringBuilder text) where T : ATLFTBWriter => Create(new StringWriter(text), true);
public static T Create(StringBuilder text, IFormatProvider formatProvider) where T : ATLFTBWriter => Create(new StringWriter(text, formatProvider), true);
public static ATLFWriter Create(Stream stream) => Create(stream);
public static ATLFWriter Create(string filePath) => Create(filePath);
public static ATLFWriter Create(TextWriter text) => Create(text);
public static ATLFWriter Create(StringBuilder text) => Create(text);
public static ATLFWriter Create(StringBuilder text, IFormatProvider formatProvider) => Create(text, formatProvider);
#pragma warning restore
private static T Create(MarshalByRefObject refObject, bool closeFlow = false) where T : ATLFWriter {
T writer = Activator.CreateInstance();
writer.RefObject = refObject;
writer.Indent = true;
writer.CloseFlow = closeFlow;
return writer;
}
}
}