/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Collections.Generic; using System.Globalization; using System.IO; namespace Opc.Ua.SourceGeneration { /// /// Template writer that handles indentation and line break management. /// internal sealed class TemplateWriter : IDisposable, ITemplateWriter { /// /// Create template /// public TemplateWriter(TextWriter writer, bool leaveOpen = true) { m_writer = writer; m_leaveOpen = leaveOpen; m_indentCharCount = new Stack(); m_indentCharCount.Push(0); } /// /// Indent character count /// public int IndentationCharCount => m_indentCharCount.Peek(); /// public void Dispose() { while (m_newLineCount > 0) { m_writer.Write(Environment.NewLine); m_newLineCount--; } if (!m_leaveOpen) { m_writer.Dispose(); } } /// /// Increases the current indentation level by the /// specified number of characters. /// /// The number of characters /// to add to the current indentation level. Must be /// zero or greater. public void PushIndentChars(int charCount) { m_indentCharCount.Push(IndentationCharCount + charCount); } /// /// Decreases the current indentation level back /// public void PopIndentation() { if (m_indentCharCount.Count > 1) { m_indentCharCount.Pop(); TrimLineBreak(0); } } /// /// Trim any whitespace and line breaks on the current line. /// public void TrimLineBreak(int maxNewLines = 1) { if (m_newLineCount > maxNewLines) { m_newLineCount = maxNewLines; } } /// public void Write(char text) { WriteWhiteSpaceIfNeeded(); m_writer.Write(text); } /// public void Write(string? text) { WriteWhiteSpaceIfNeeded(); m_writer.Write(text ?? string.Empty); } /// public void Write(string format, object arg1) { WriteWhiteSpaceIfNeeded(); m_writer.Write( string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, arg1)); } /// public void Write(string format, object arg1, object arg2) { WriteWhiteSpaceIfNeeded(); m_writer.Write( string.Format(CultureInfo.InvariantCulture, format ?? string.Empty, arg1, arg2)); } /// public void Write(string format, object arg1, object arg2, object arg3) { WriteWhiteSpaceIfNeeded(); m_writer.Write(string.Format( CultureInfo.InvariantCulture, format ?? string.Empty, arg1, arg2, arg3)); } /// public void WriteLine() { WriteNewLine(2); // do not write more than 2 new lines - make configurable } /// public void WriteLine(string text) { WriteWhiteSpaceIfNeeded(); m_writer.Write(text ?? string.Empty); WriteNewLine(int.MaxValue); } /// public void WriteLine(string text, params object?[] args) { WriteWhiteSpaceIfNeeded(); // Always format invariantly: the sink is generated source code and // schema documents, which must not pick up the machine's culture // (a culture specific minus sign or decimal separator would make the // generated output invalid). m_writer.Write( string.Format(CultureInfo.InvariantCulture, text ?? string.Empty, args ?? [])); WriteNewLine(int.MaxValue); } /// /// Write whitespace /// public void WriteWhiteSpace(int charCount) { // Queue up spaces to write on next write to drop trailing whitespace m_writeSpaceCount += charCount; } /// /// Begin new line /// public bool WriteNewLine(int maxNewLines) { if (m_newLineCount >= maxNewLines) { return false; } m_newLineCount++; // Reset to current indent level m_writeSpaceCount = IndentationCharCount; return true; } /// /// Write indent if needed. /// private void WriteWhiteSpaceIfNeeded() { // Write pending spaces. while (m_newLineCount > 0) { m_writer.Write(Environment.NewLine); m_newLineCount--; } if (m_writeSpaceCount > 0) { m_writer.Write(new string(' ', m_writeSpaceCount)); m_writeSpaceCount = 0; } } private int m_writeSpaceCount; private int m_newLineCount; private readonly TextWriter m_writer; private readonly bool m_leaveOpen; private readonly Stack m_indentCharCount; } }