/** 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); } } }
/** * Creates a new entry. * * @param location location of the package to add. * @param path path to add. * @param types the package types. * @param verify if <code>true</code> the package root check will be performed. * @return if <code>verify == true</code> returns <code>true</code> when the given location could * be verified, <code>false</code> if the given location is no package root; otherwise always * returns <code>true</code>. */ private static boolean createEntryImpl( File location, String path, final Set types, boolean verify) { // strip extension path = path.substring(0, path.lastIndexOf('.')); int pos = path.lastIndexOf('$'); // is this an inner class? if (pos > -1) { String className = path.substring(pos + 1); // skip anonymous inner classes if (StringHelper.isNumber(className)) { return true; } path = path.replace('$', '.'); } else if (verify) { try { // we setup a classloader with the given location and check // whether the location is indeed a package root URL[] url = new URL[] {location.toURL()}; ClassLoader loader = new URLClassLoader(url, ClassRepository.class.getClassLoader()); loader.loadClass(path.replace(File.separatorChar, '.')); } catch (ClassNotFoundException ex) { Object[] args = {path, location}; Loggers.IO.l7dlog(Level.WARN, "REPOSITORY_NOT_PACKAGE_ROOT", args, null); return false; } catch (Throwable ex) { return false; } } String typeName = StringHelper.getClassName(path); // HACK skip obfuscated classes // this is necessary to make our import declaration expanding working // as it could easily be, that an identifier reported by the parser // would be wrongly taken as a type name: // // Database d = new Database(); // d.shutdown(); // ^ // 'd' would be reported as an identifier which is perfect but // lead to wrong results as there could be a class 'd.class' for // obfuscated librarys if ((typeName.length() == 1) && Character.isLowerCase(typeName.charAt(0))) { return true; } String packageName = StringHelper.getPackageName(path); // we place this marker in front of every subpackage so we know // where a package starts (purely for the searching facility in // ImportTransformation.java) if (!EMPTY_STRING.equals(packageName)) { types.add(packageName + '#'); } types.add(path); return true; }
/** * 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; }