/** Creates a new NodeWriter object. */ protected NodeWriter(CompositeFactory factory) { this.state = new PrinterState(this); this._factory = factory; this.lineSeparator = File.separator; this.settings = Convention.getInstance(); this.indentSize = AbstractPrinter.settings.getInt(ConventionKeys.INDENT_SIZE, ConventionDefaults.INDENT_SIZE); this.insertTrailingEmpty = AbstractPrinter.settings.getBoolean( ConventionKeys.INSERT_TRAILING_NEWLINE, ConventionDefaults.INSERT_TRAILING_NEWLINE); this.continuationIndentSize = AbstractPrinter.settings.getInt( ConventionKeys.INDENT_SIZE_CONTINUATION, ConventionDefaults.INDENT_SIZE_CONTINUATION); this.leftBraceNewline = AbstractPrinter.settings.getBoolean( ConventionKeys.BRACE_NEWLINE_LEFT, ConventionDefaults.BRACE_NEWLINE_LEFT); this.leftBraceIndent = AbstractPrinter.settings.getInt( ConventionKeys.INDENT_SIZE_BRACE_LEFT, ConventionDefaults.INDENT_SIZE_BRACE_LEFT); this.leadingIndentSize = AbstractPrinter.settings.getInt( ConventionKeys.INDENT_SIZE_LEADING, ConventionDefaults.INDENT_SIZE_LEADING); this.useTabs = AbstractPrinter.settings.getBoolean( ConventionKeys.INDENT_WITH_TABS, ConventionDefaults.INDENT_WITH_TABS); this.useLeadingTabs = AbstractPrinter.settings.getBoolean( ConventionKeys.INDENT_WITH_TABS_ONLY_LEADING, ConventionDefaults.INDENT_WITH_TABS_ONLY_LEADING); this.footer = AbstractPrinter.settings.getBoolean(ConventionKeys.FOOTER, ConventionDefaults.FOOTER); _indentChars = new char[150]; for (int i = 0; i < _indentChars.length; i++) { _indentChars[i] = ' '; } if (this.leadingIndentSize > 0) { _leadingIndentSizeString = getString(this.leadingIndentSize); if (this.useTabs) { _leadingIndentSizeString = StringHelper.replace(_leadingIndentSizeString, getString(this.indentSize), TAB); } } }
/** * Outputs the given string of the given type to the underlying writer. * * @param string string to write. * @param type type of the string. * @return the column offset were the string started. * @throws IOException if an I/O error occured. */ public int print(String string, int type) throws IOException { int offset = 1; if (this.newline) { if (leadingIndentSize > 0) { _out.write(_leadingIndentSizeString); this.column += leadingIndentSize; } int length = this.indentLevel * this.indentSize; if (continuation) // use continuation indentation { length += continuationIndentSize; } switch (type) { case JavaTokenTypes.WS: { if (!useTabs) { String s = generateIndentString(length + string.length()); this.column += s.length(); _out.write(s); } else { if (!this.useLeadingTabs) { String s = generateIndentString(length + string.length()); this.column += s.length(); s = StringHelper.replace(s, generateIndentString(this.indentSize), TAB); _out.write(s); } else { String s = generateIndentString(length); this.column += length; s = StringHelper.replace(s, generateIndentString(this.indentSize), TAB); _out.write(s); this.column += string.length(); _out.write(string); } } break; } default: { String s = generateIndentString(length); offset += length; this.column += (length + string.length()); if (this.useTabs) { s = StringHelper.replace(s, generateIndentString(this.indentSize), TAB); } _out.write(s); _out.write(string); break; } } this.newline = false; } else { switch (type) { case JavaTokenTypes.WS: if (this.useTabs && !useLeadingTabs && (string.length() > this.indentSize)) { int tabCount = this.column / this.indentSize; int spacesCount = this.column - 1 - (tabCount * this.indentSize); this.column += string.length(); if (spacesCount == 0) { string = StringHelper.replace(string, generateIndentString(this.indentSize), TAB); _out.write(string); } else { if (spacesCount < 0) { _out.write(TAB); } _out.write(TAB); string = StringHelper.replace( string.substring(this.indentSize - spacesCount), generateIndentString(this.indentSize), TAB); _out.write(string); } break; } // fall through default: offset = this.column; this.column += string.length(); _out.write(string); break; } } this.last = type; return offset; }