Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/CodeFormat.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/CodeFormat.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/CodeFormat.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/CodeFormat.java Wed Jun 28 12:34:33 2006 @@ -17,684 +17,628 @@ /** - * <p>Encapsulates some common Java source code formatting options. The - * class can also be used as a buffer for formatted Java code.</p> + * <p>Encapsulates some common Java source code formatting options. The + * class can also be used as a buffer for formatted Java code.</p> * - * @author Abe White + * @author Abe White */ -public final class CodeFormat - implements Cloneable -{ - private static final String _sep = System.getProperty ("line.separator"); - - private String _tab = "\t"; - private boolean _spaceBeforeParen = false; - private boolean _spaceInParen = false; - private boolean _braceOnSameLine = true; - private boolean _braceAtSameTabLevel = false; - private boolean _scoreBeforeFieldName = false; - private int _linesBetweenSections = 2; - - private StringBuffer _buf = new StringBuffer (); - - - /** - * The number of spaces to use for tabs; 0 means to use actual tab - * characters. Defaults to 0. - */ - public int getTabSpaces () - { - return (_tab.equals ("\t")) ? 0 : _tab.length (); - } - - - /** - * The number of spaces to use for tabs; 0 means to use actual tab - * characters. Defaults to 0. - */ - public void setTabSpaces (int tab) - { - if (tab == 0) - _tab = "\t"; - else - { - StringBuffer tabs = new StringBuffer (tab); - for (int i = 0; i < tab; i++) - tabs.append (" "); - _tab = tabs.toString (); - } - } - - - /** - * Whether to place a space before parentheses. Defaults to false. - */ - public boolean getSpaceBeforeParen () - { - return _spaceBeforeParen; - } - - - /** - * Whether to place a space before parentheses. Defaults to false. - */ - public void setSpaceBeforeParen (boolean spaceBeforeParen) - { - _spaceBeforeParen = spaceBeforeParen; - } - - - /** - * Whether to place a space within parentheses. Defaults to false. - */ - public boolean getSpaceInParen () - { - return _spaceInParen; - } - - - /** - * Whether to place a space within parentheses. Defaults to false. - */ - public void setSpaceInParen (boolean spaceInParen) - { - _spaceInParen = spaceInParen; - } - - - /** - * Whether to place opening braces on the same line as the - * block declaration, or on the next line. Defaults to same line. - */ - public boolean getBraceOnSameLine () - { - return _braceOnSameLine; - } - - - /** - * Whether to place opening braces on the same line as the - * block declaration, or on the next line. Defaults to same line. - */ - public void setBraceOnSameLine (boolean braceOnSameLine) - { - _braceOnSameLine = braceOnSameLine; - } - - - /** - * Whether to place braces at the same tab level as the code within - * the block. Defaults to false. - */ - public boolean getBraceAtSameTabLevel () - { - return _braceAtSameTabLevel; - } - - - /** - * Whether to place braces at the same tab level as the code within - * the block. Defaults to false. - */ - public void setBraceAtSameTabLevel (boolean braceAtSameTabLevel) - { - _braceAtSameTabLevel = braceAtSameTabLevel; - } - - - /** - * Whether to place an underscore before private field names. Defaults - * to false. - */ - public boolean getScoreBeforeFieldName () - { - return _scoreBeforeFieldName; - } - - - /** - * Whether to place an underscore before private field names. Defaults - * to false. - */ - public void setScoreBeforeFieldName (boolean scoreBeforeFieldName) - { - _scoreBeforeFieldName = scoreBeforeFieldName; - } - - - /** - * The number of empty lines between code sections. Defaults to 2. - */ - public int getLinesBetweenSections () - { - return _linesBetweenSections; - } - - - /** - * The number of empty lines between sections. Defaults to 2. - */ - public void setLinesBetweenSections (int linesBetweenSections) - { - _linesBetweenSections = linesBetweenSections; - } - - - /** - * Return a new line character. - */ - public String getEndl () - { - return getEndl (1); - } - - - /** - * Return the given number of new line characters. - */ - public String getEndl (int num) - { - if (num == 0) - return ""; - if (num == 1) - return _sep; - - StringBuffer buf = new StringBuffer (_sep.length () * num); - for (int i = 0; i < num; i++) - buf.append (_sep); - return buf.toString (); - } - - - /** - * Return the given number of new line characters, followed by - * the given tab level indentation. - */ - public String getEndl (int num, int tabs) - { - return getEndl (num) + getTab (tabs); - } - - - /** - * Return {#getLinesBetweenSections} + 1 new line characters. - */ - public String getAfterSection () - { - return getEndl (getLinesBetweenSections () + 1); - } - - - /** - * Open parentheses string. Users can choose to place spaces before - * and within parentheses. - */ - public String getOpenParen (boolean methodOrIf) - { - if ((_spaceBeforeParen && methodOrIf) && _spaceInParen) - return " ( "; - if (_spaceBeforeParen && methodOrIf) - return " ("; - if (_spaceInParen) - return "( "; - return "("; - } - - - /** - * Close parentheses string. Users can choose to place spaces within - * parentheses. - */ - public String getCloseParen () - { - if (_spaceInParen) - return " )"; - return ")"; - } - - - /** - * Paired parentheses for empty method parameters. Users can choose - * to place spaces before parentheses. - */ - public String getParens () - { - if (_spaceBeforeParen) - return " ()"; - return "()"; - } - - - /** - * Open brace string. Users can choose to place braces on the same - * line, or on a new line, and can choose the indenting level. - * - * @param tabLevel the tab level of code within the brace - */ - public String getOpenBrace (int tabLevel) - { - if (_braceOnSameLine) - return " {"; - if (_braceAtSameTabLevel) - return getEndl () + getTab (tabLevel) + "{"; - return getEndl () + getTab (tabLevel - 1) + "{"; - } - - - /** - * Close brace string. Users can choose to place braces on the same - * line, or on a new line, and can choose the indenting level. - * - * @param tabLevel the tab level of code within the brace - */ - public String getCloseBrace (int tabLevel) - { - if (_braceAtSameTabLevel) - return getTab (tabLevel) + "}"; - return getTab (tabLevel - 1) + "}"; - } - - - /** - * Extends declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, - * but prints "extends" instead of a brace. - */ - public String getExtendsDec (int tabLevel) - { - if (_braceOnSameLine) - return " extends"; - if (_braceAtSameTabLevel) - return getEndl () + getTab (tabLevel) + "extends"; - return getEndl () + getTab (tabLevel) + "extends"; - } - - - /** - * Implements declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, - * but prints "implements" instead of a brace. - */ - public String getImplementsDec (int tabLevel) - { - if (_braceOnSameLine) - return " implements"; - if (_braceAtSameTabLevel) - return getEndl () + getTab (tabLevel) + "implements"; - return getEndl () + getTab (tabLevel) + "implements"; - } - - - /** - * Throws declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, - * but prints "throws" instead of a brace. - */ - public String getThrowsDec (int tabLevel) - { - if (_braceOnSameLine) - return " throws"; - if (_braceAtSameTabLevel) - return getEndl () + getTab (tabLevel) + "throws"; - return getEndl () + getTab (tabLevel) + "throws"; - } - - - /** - * Tab string. Users can choose to use spaces or tab characters. - */ - public String getTab () - { - return getTab (1); - } - - - /** - * Tab string. Users can choose to use spaces or tab characters. - * - * @param tabLevel the number of tabs - */ - public String getTab (int tabLevel) - { - if (tabLevel == 0) - return ""; - if (tabLevel == 1) - return _tab; - - StringBuffer tabs = new StringBuffer (_tab.length () * tabLevel); - for (int i = 0; i < tabLevel; i++) - tabs.append (_tab); - return tabs.toString (); - } - - - /** - * Return the field name for given suggested name, possibly adding - * leading underscore. - */ - public String getFieldName (String fieldName) - { - return (_scoreBeforeFieldName) ? "_" + fieldName : fieldName; - } - - - /** - * Return the internal code buffer. - */ - public StringBuffer getBuffer () - { - return _buf; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (boolean val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (byte val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (char val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (double val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (float val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (int val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (long val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (short val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - */ - public CodeFormat append (Object val) - { - _buf.append (val); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getEndl() - */ - public CodeFormat endl () - { - _buf.append (getEndl ()); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getEndl(int) - */ - public CodeFormat endl (int num) - { - _buf.append (getEndl (num)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getEndl(int, int) - */ - public CodeFormat endl (int num, int tabs) - { - _buf.append (getEndl (num, tabs)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getAfterSection - */ - public CodeFormat afterSection () - { - _buf.append (getAfterSection ()); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getOpenParen - */ - public CodeFormat openParen (boolean methodOrIf) - { - _buf.append (getOpenParen (methodOrIf)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getCloseParen - */ - public CodeFormat closeParen () - { - _buf.append (getCloseParen ()); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getParens - */ - public CodeFormat parens () - { - _buf.append (getParens ()); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getOpenBrace - */ - public CodeFormat openBrace (int tabLevel) - { - _buf.append (getOpenBrace (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getCloseBrace - */ - public CodeFormat closeBrace (int tabLevel) - { - _buf.append (getCloseBrace (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getExtendsDec - */ - public CodeFormat extendsDec (int tabLevel) - { - _buf.append (getExtendsDec (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getImplementsDec - */ - public CodeFormat implementsDec (int tabLevel) - { - _buf.append (getImplementsDec (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getThrowsDec - */ - public CodeFormat throwsDec (int tabLevel) - { - _buf.append (getThrowsDec (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getTab - */ - public CodeFormat tab () - { - _buf.append (getTab ()); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getTab - */ - public CodeFormat tab (int tabLevel) - { - _buf.append (getTab (tabLevel)); - return this; - } - - - /** - * Append the given value to the internal buffer. - * - * @see #getFieldName - */ - public CodeFormat fieldName (String name) - { - _buf.append (getFieldName (name)); - return this; - } - - - /** - * Clear the internal code buffer. - */ - public void clear () - { - _buf = new StringBuffer (); - } - - - /** - * Return the internal buffer as a string. - */ - public String toString () - { - return _buf.toString (); - } - - - /** - * Return the length of the internal buffer. - */ - public int length () - { - return _buf.length (); - } - - - /** - * Make a copy of this code format object with all the same formatting - * settings. - */ - public Object clone () - { - CodeFormat format = new CodeFormat (); - format._tab = _tab; - format._spaceBeforeParen = _spaceBeforeParen; - format._spaceInParen = _spaceInParen; - format._braceOnSameLine = _braceOnSameLine; - format._braceAtSameTabLevel = _braceAtSameTabLevel; - format._scoreBeforeFieldName = _scoreBeforeFieldName; - format._linesBetweenSections = _linesBetweenSections; - return format; - } +public final class CodeFormat implements Cloneable { + private static final String _sep = System.getProperty("line.separator"); + private String _tab = "\t"; + private boolean _spaceBeforeParen = false; + private boolean _spaceInParen = false; + private boolean _braceOnSameLine = true; + private boolean _braceAtSameTabLevel = false; + private boolean _scoreBeforeFieldName = false; + private int _linesBetweenSections = 2; + private StringBuffer _buf = new StringBuffer(); + + /** + * The number of spaces to use for tabs; 0 means to use actual tab + * characters. Defaults to 0. + */ + public int getTabSpaces() { + return (_tab.equals("\t")) ? 0 : _tab.length(); + } + + /** + * The number of spaces to use for tabs; 0 means to use actual tab + * characters. Defaults to 0. + */ + public void setTabSpaces(int tab) { + if (tab == 0) { + _tab = "\t"; + } else { + StringBuffer tabs = new StringBuffer(tab); + + for (int i = 0; i < tab; i++) + tabs.append(" "); + + _tab = tabs.toString(); + } + } + + /** + * Whether to place a space before parentheses. Defaults to false. + */ + public boolean getSpaceBeforeParen() { + return _spaceBeforeParen; + } + + /** + * Whether to place a space before parentheses. Defaults to false. + */ + public void setSpaceBeforeParen(boolean spaceBeforeParen) { + _spaceBeforeParen = spaceBeforeParen; + } + + /** + * Whether to place a space within parentheses. Defaults to false. + */ + public boolean getSpaceInParen() { + return _spaceInParen; + } + + /** + * Whether to place a space within parentheses. Defaults to false. + */ + public void setSpaceInParen(boolean spaceInParen) { + _spaceInParen = spaceInParen; + } + + /** + * Whether to place opening braces on the same line as the + * block declaration, or on the next line. Defaults to same line. + */ + public boolean getBraceOnSameLine() { + return _braceOnSameLine; + } + + /** + * Whether to place opening braces on the same line as the + * block declaration, or on the next line. Defaults to same line. + */ + public void setBraceOnSameLine(boolean braceOnSameLine) { + _braceOnSameLine = braceOnSameLine; + } + + /** + * Whether to place braces at the same tab level as the code within + * the block. Defaults to false. + */ + public boolean getBraceAtSameTabLevel() { + return _braceAtSameTabLevel; + } + + /** + * Whether to place braces at the same tab level as the code within + * the block. Defaults to false. + */ + public void setBraceAtSameTabLevel(boolean braceAtSameTabLevel) { + _braceAtSameTabLevel = braceAtSameTabLevel; + } + + /** + * Whether to place an underscore before private field names. Defaults + * to false. + */ + public boolean getScoreBeforeFieldName() { + return _scoreBeforeFieldName; + } + + /** + * Whether to place an underscore before private field names. Defaults + * to false. + */ + public void setScoreBeforeFieldName(boolean scoreBeforeFieldName) { + _scoreBeforeFieldName = scoreBeforeFieldName; + } + + /** + * The number of empty lines between code sections. Defaults to 2. + */ + public int getLinesBetweenSections() { + return _linesBetweenSections; + } + + /** + * The number of empty lines between sections. Defaults to 2. + */ + public void setLinesBetweenSections(int linesBetweenSections) { + _linesBetweenSections = linesBetweenSections; + } + + /** + * Return a new line character. + */ + public String getEndl() { + return getEndl(1); + } + + /** + * Return the given number of new line characters. + */ + public String getEndl(int num) { + if (num == 0) { + return ""; + } + + if (num == 1) { + return _sep; + } + + StringBuffer buf = new StringBuffer(_sep.length() * num); + + for (int i = 0; i < num; i++) + buf.append(_sep); + + return buf.toString(); + } + + /** + * Return the given number of new line characters, followed by + * the given tab level indentation. + */ + public String getEndl(int num, int tabs) { + return getEndl(num) + getTab(tabs); + } + + /** + * Return {#getLinesBetweenSections} + 1 new line characters. + */ + public String getAfterSection() { + return getEndl(getLinesBetweenSections() + 1); + } + + /** + * Open parentheses string. Users can choose to place spaces before + * and within parentheses. + */ + public String getOpenParen(boolean methodOrIf) { + if ((_spaceBeforeParen && methodOrIf) && _spaceInParen) { + return " ( "; + } + + if (_spaceBeforeParen && methodOrIf) { + return " ("; + } + + if (_spaceInParen) { + return "( "; + } + + return "("; + } + + /** + * Close parentheses string. Users can choose to place spaces within + * parentheses. + */ + public String getCloseParen() { + if (_spaceInParen) { + return " )"; + } + + return ")"; + } + + /** + * Paired parentheses for empty method parameters. Users can choose + * to place spaces before parentheses. + */ + public String getParens() { + if (_spaceBeforeParen) { + return " ()"; + } + + return "()"; + } + + /** + * Open brace string. Users can choose to place braces on the same + * line, or on a new line, and can choose the indenting level. + * + * @param tabLevel the tab level of code within the brace + */ + public String getOpenBrace(int tabLevel) { + if (_braceOnSameLine) { + return " {"; + } + + if (_braceAtSameTabLevel) { + return getEndl() + getTab(tabLevel) + "{"; + } + + return getEndl() + getTab(tabLevel - 1) + "{"; + } + + /** + * Close brace string. Users can choose to place braces on the same + * line, or on a new line, and can choose the indenting level. + * + * @param tabLevel the tab level of code within the brace + */ + public String getCloseBrace(int tabLevel) { + if (_braceAtSameTabLevel) { + return getTab(tabLevel) + "}"; + } + + return getTab(tabLevel - 1) + "}"; + } + + /** + * Extends declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, + * but prints "extends" instead of a brace. + */ + public String getExtendsDec(int tabLevel) { + if (_braceOnSameLine) { + return " extends"; + } + + if (_braceAtSameTabLevel) { + return getEndl() + getTab(tabLevel) + "extends"; + } + + return getEndl() + getTab(tabLevel) + "extends"; + } + + /** + * Implements declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, + * but prints "implements" instead of a brace. + */ + public String getImplementsDec(int tabLevel) { + if (_braceOnSameLine) { + return " implements"; + } + + if (_braceAtSameTabLevel) { + return getEndl() + getTab(tabLevel) + "implements"; + } + + return getEndl() + getTab(tabLevel) + "implements"; + } + + /** + * Throws declaration. Uses configuration of [EMAIL PROTECTED] #openBrace}, + * but prints "throws" instead of a brace. + */ + public String getThrowsDec(int tabLevel) { + if (_braceOnSameLine) { + return " throws"; + } + + if (_braceAtSameTabLevel) { + return getEndl() + getTab(tabLevel) + "throws"; + } + + return getEndl() + getTab(tabLevel) + "throws"; + } + + /** + * Tab string. Users can choose to use spaces or tab characters. + */ + public String getTab() { + return getTab(1); + } + + /** + * Tab string. Users can choose to use spaces or tab characters. + * + * @param tabLevel the number of tabs + */ + public String getTab(int tabLevel) { + if (tabLevel == 0) { + return ""; + } + + if (tabLevel == 1) { + return _tab; + } + + StringBuffer tabs = new StringBuffer(_tab.length() * tabLevel); + + for (int i = 0; i < tabLevel; i++) + tabs.append(_tab); + + return tabs.toString(); + } + + /** + * Return the field name for given suggested name, possibly adding + * leading underscore. + */ + public String getFieldName(String fieldName) { + return (_scoreBeforeFieldName) ? ("_" + fieldName) : fieldName; + } + + /** + * Return the internal code buffer. + */ + public StringBuffer getBuffer() { + return _buf; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(boolean val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(byte val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(char val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(double val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(float val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(int val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(long val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(short val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + */ + public CodeFormat append(Object val) { + _buf.append(val); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getEndl() + */ + public CodeFormat endl() { + _buf.append(getEndl()); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getEndl(int) + */ + public CodeFormat endl(int num) { + _buf.append(getEndl(num)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getEndl(int, int) + */ + public CodeFormat endl(int num, int tabs) { + _buf.append(getEndl(num, tabs)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getAfterSection + */ + public CodeFormat afterSection() { + _buf.append(getAfterSection()); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getOpenParen + */ + public CodeFormat openParen(boolean methodOrIf) { + _buf.append(getOpenParen(methodOrIf)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getCloseParen + */ + public CodeFormat closeParen() { + _buf.append(getCloseParen()); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getParens + */ + public CodeFormat parens() { + _buf.append(getParens()); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getOpenBrace + */ + public CodeFormat openBrace(int tabLevel) { + _buf.append(getOpenBrace(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getCloseBrace + */ + public CodeFormat closeBrace(int tabLevel) { + _buf.append(getCloseBrace(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getExtendsDec + */ + public CodeFormat extendsDec(int tabLevel) { + _buf.append(getExtendsDec(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getImplementsDec + */ + public CodeFormat implementsDec(int tabLevel) { + _buf.append(getImplementsDec(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getThrowsDec + */ + public CodeFormat throwsDec(int tabLevel) { + _buf.append(getThrowsDec(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getTab + */ + public CodeFormat tab() { + _buf.append(getTab()); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getTab + */ + public CodeFormat tab(int tabLevel) { + _buf.append(getTab(tabLevel)); + + return this; + } + + /** + * Append the given value to the internal buffer. + * + * @see #getFieldName + */ + public CodeFormat fieldName(String name) { + _buf.append(getFieldName(name)); + + return this; + } + + /** + * Clear the internal code buffer. + */ + public void clear() { + _buf = new StringBuffer(); + } + + /** + * Return the internal buffer as a string. + */ + public String toString() { + return _buf.toString(); + } + + /** + * Return the length of the internal buffer. + */ + public int length() { + return _buf.length(); + } + + /** + * Make a copy of this code format object with all the same formatting + * settings. + */ + public Object clone() { + CodeFormat format = new CodeFormat(); + format._tab = _tab; + format._spaceBeforeParen = _spaceBeforeParen; + format._spaceInParen = _spaceInParen; + format._braceOnSameLine = _braceOnSameLine; + format._braceAtSameTabLevel = _braceAtSameTabLevel; + format._scoreBeforeFieldName = _scoreBeforeFieldName; + format._linesBetweenSections = _linesBetweenSections; + + return format; + } }
Added: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/EventManager.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/EventManager.java?rev=417856&view=auto ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/EventManager.java (added) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/EventManager.java Wed Jun 28 12:34:33 2006 @@ -0,0 +1,56 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * 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. + */ +package org.apache.openjpa.lib.util; + +import java.util.*; + + +/** + * <p>Basic event manager interface.</p> + * + * @author Abe White + */ +public interface EventManager { + /** + * Add an event listener. + */ + public void addListener(Object listener); + + /** + * Remove an event listener. + */ + public boolean removeListener(Object listener); + + /** + * Return whether the given instance is in the list of listeners. + */ + public boolean hasListener(Object listener); + + /** + * Return true if there are any registered listeners. + */ + public boolean hasListeners(); + + /** + * Return a read-only list of listeners. + */ + public Collection getListeners(); + + /** + * Fire the given event to all listeners, returning any exceptions. + */ + public Exception[] fireEvent(Object event); +} Propchange: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/EventManager.java ------------------------------------------------------------------------------ svn:executable = * Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/Files.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/Files.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/Files.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/util/Files.java Wed Jun 28 12:34:33 2006 @@ -15,282 +15,303 @@ */ package org.apache.openjpa.lib.util; - -import java.io.*; -import java.net.*; - import org.apache.commons.lang.exception.*; import serp.util.*; +import java.io.*; + +import java.net.*; + /** - * <p>Utility operations on files.</p> + * <p>Utility operations on files.</p> * - * @author Abe White - * @nojavadoc - */ -public class Files -{ - /** - * Backup the given file to a new file called <file-name>~. If - * the file does not exist or a backup could not be created, returns null. - */ - public static File backup (File file, boolean copy) - { - if (file == null || !file.exists ()) - return null; - - // create new file object copy so we don't modify the original - File clone = new File (file.getAbsolutePath ()); - File bk = new File (file.getAbsolutePath () + "~"); - if (!clone.renameTo (bk)) - return null; - if (copy) - { - try - { - copy (bk, file); - } - catch (IOException ioe) - { - throw new NestableRuntimeException (ioe); - } - } - return bk; - } - - - /** - * Revert the given backup file to the original location. If the given - * file's name does not end in '~', the '~' is appended before proceeding. - * If the backup file does not exist or could not be reverted, - * returns null. - */ - public static File revert (File backup, boolean copy) - { - if (backup == null) - return null; - if (!backup.getName ().endsWith ("~")) - backup = new File (backup.getPath () + "~"); - if (!backup.exists ()) - return null; - - // create new file object copy so we don't modify the original - String path = backup.getAbsolutePath (); - File clone = new File (path); - File orig = new File (path.substring (0, path.length () - 1)); - if (!clone.renameTo (orig)) - return null; - if (copy) - { - try - { - copy (orig, backup); - } - catch (IOException ioe) - { - throw new NestableRuntimeException (ioe); - } - } - return orig; - } - - - /** - * Return the source file for the given class, or null if the - * source is not in the CLASSPATH. - */ - public static File getSourceFile (Class cls) - { - return getClassFile (cls, ".java"); - } - - - /** - * Return the class file of the given class, or null if the - * class is in a jar. - */ - public static File getClassFile (Class cls) - { - return getClassFile (cls, ".class"); - } - - - /** - * Return the file for the class resource with the given extension. - */ - private static File getClassFile (Class cls, String ext) - { - String name = Strings.getClassName (cls); - - // if it's an inner class, use the parent class name - int innerIdx = name.indexOf ('$'); - if (innerIdx != -1) - name = name.substring (0, innerIdx); - - URL rsrc = cls.getResource (name + ext); - if (rsrc != null && rsrc.getProtocol ().equals ("file")) - return new File (URLDecoder.decode (rsrc.getFile ())); - return null; - } - - - /** - * Return the file for the given package. If the given base directory - * matches the given package structure, it will be used as-is. If not, - * the package structure will be added beneath the base directory. If - * the base directory is null, the current working directory will be - * used as the base. - */ - public static File getPackageFile (File base, String pkg, boolean mkdirs) - { - if (base == null) - base = new File (System.getProperty ("user.dir")); - if (pkg == null || pkg.length () == 0) - { - if (mkdirs && !base.exists ()) - base.mkdirs (); - return base; - } - - pkg = pkg.replace ('.', File.separatorChar); - File file = null; - try - { - if (base.getCanonicalPath ().endsWith (pkg)) - file = base; - else - file = new File (base, pkg); - } - catch (IOException ioe) - { - throw new NestableRuntimeException (ioe); - } - - if (mkdirs && !file.exists ()) - file.mkdirs (); - return file; - } - - - /** - * Check the given string for a matching file. The string is first - * tested to see if it is an existing file path. If it does not - * represent an existing file, it is checked as a resource name of a - * file. If no resource exists, then it is interpreted as a path - * to a file that does not exist yet. - * - * @param name the file path or resource name - * @param loader a class loader to use in resource lookup, or null - * to use the thread's context loader - */ - public static File getFile (String name, ClassLoader loader) - { - if (name == null) - return null; - - File file = new File (name); - if (file.exists ()) - return file; - - if (loader == null) - loader = Thread.currentThread ().getContextClassLoader (); - URL url = loader.getResource (name); - if (url != null) - { - String urlFile = url.getFile (); - if (urlFile != null) - { - File rsrc = new File (URLDecoder.decode (urlFile)); - if (rsrc.exists ()) - return rsrc; - } - } - - // go back to original non-existant file path - return file; - } - - - /** - * Return a writer to the stream (stdout or stderr) or file named by the - * given string. - * - * @see #getFile - */ - public static Writer getWriter (String file, ClassLoader loader) - throws IOException - { - if (file == null) - return null; - if ("stdout".equals (file)) - return new PrintWriter (System.out); - if ("stderr".equals (file)) - return new PrintWriter (System.err); - try - { - return new FileWriter (getFile (file, loader)); - } - catch (IOException ioe) - { - throw new NestableRuntimeException (ioe); - } - } - - - /** - * Return an output stream to the stream (stdout or stderr) or file named - * by the given string. - * - * @see #getFile - */ - public static OutputStream getOutputStream (String file, ClassLoader loader) - { - if (file == null) - return null; - if ("stdout".equals (file)) - return System.out; - if ("stderr".equals (file)) - return System.err; - try - { - return new FileOutputStream (getFile (file, loader)); - } - catch (IOException ioe) - { - throw new NestableRuntimeException (ioe); - } - } - - - /** - * Copy a file. Return false if <code>from</code> does not exist. - */ - public static boolean copy (File from, File to) - throws IOException - { - if (from == null || to == null || !from.exists ()) - return false; - - FileInputStream in = null; - FileOutputStream out = null; - try - { - in = new FileInputStream (from); - BufferedInputStream inbuf = new BufferedInputStream (in); - out = new FileOutputStream (to); - BufferedOutputStream outbuf = new BufferedOutputStream (out); - for (int b; (b = inbuf.read ()) != -1; outbuf.write (b)); - outbuf.flush (); - return true; - } - finally - { - if (in != null) - try { in.close (); } catch (Exception e) {} - if (out != null) - try { out.close (); } catch (Exception e) {} - } - } + * @author Abe White + * @nojavadoc */ +public class Files { + /** + * Backup the given file to a new file called <file-name>~. If + * the file does not exist or a backup could not be created, returns null. + */ + public static File backup(File file, boolean copy) { + if ((file == null) || !file.exists()) { + return null; + } + + // create new file object copy so we don't modify the original + File clone = new File(file.getAbsolutePath()); + File bk = new File(file.getAbsolutePath() + "~"); + + if (!clone.renameTo(bk)) { + return null; + } + + if (copy) { + try { + copy(bk, file); + } catch (IOException ioe) { + throw new NestableRuntimeException(ioe); + } + } + + return bk; + } + + /** + * Revert the given backup file to the original location. If the given + * file's name does not end in '~', the '~' is appended before proceeding. + * If the backup file does not exist or could not be reverted, + * returns null. + */ + public static File revert(File backup, boolean copy) { + if (backup == null) { + return null; + } + + if (!backup.getName().endsWith("~")) { + backup = new File(backup.getPath() + "~"); + } + + if (!backup.exists()) { + return null; + } + + // create new file object copy so we don't modify the original + String path = backup.getAbsolutePath(); + File clone = new File(path); + File orig = new File(path.substring(0, path.length() - 1)); + + if (!clone.renameTo(orig)) { + return null; + } + + if (copy) { + try { + copy(orig, backup); + } catch (IOException ioe) { + throw new NestableRuntimeException(ioe); + } + } + + return orig; + } + + /** + * Return the source file for the given class, or null if the + * source is not in the CLASSPATH. + */ + public static File getSourceFile(Class cls) { + return getClassFile(cls, ".java"); + } + + /** + * Return the class file of the given class, or null if the + * class is in a jar. + */ + public static File getClassFile(Class cls) { + return getClassFile(cls, ".class"); + } + + /** + * Return the file for the class resource with the given extension. + */ + private static File getClassFile(Class cls, String ext) { + String name = Strings.getClassName(cls); + + // if it's an inner class, use the parent class name + int innerIdx = name.indexOf('$'); + + if (innerIdx != -1) { + name = name.substring(0, innerIdx); + } + + URL rsrc = cls.getResource(name + ext); + + if ((rsrc != null) && rsrc.getProtocol().equals("file")) { + return new File(URLDecoder.decode(rsrc.getFile())); + } + + return null; + } + + /** + * Return the file for the given package. If the given base directory + * matches the given package structure, it will be used as-is. If not, + * the package structure will be added beneath the base directory. If + * the base directory is null, the current working directory will be + * used as the base. + */ + public static File getPackageFile(File base, String pkg, boolean mkdirs) { + if (base == null) { + base = new File(System.getProperty("user.dir")); + } + + if ((pkg == null) || (pkg.length() == 0)) { + if (mkdirs && !base.exists()) { + base.mkdirs(); + } + + return base; + } + + pkg = pkg.replace('.', File.separatorChar); + + File file = null; + + try { + if (base.getCanonicalPath().endsWith(pkg)) { + file = base; + } else { + file = new File(base, pkg); + } + } catch (IOException ioe) { + throw new NestableRuntimeException(ioe); + } + + if (mkdirs && !file.exists()) { + file.mkdirs(); + } + + return file; + } + + /** + * Check the given string for a matching file. The string is first + * tested to see if it is an existing file path. If it does not + * represent an existing file, it is checked as a resource name of a + * file. If no resource exists, then it is interpreted as a path + * to a file that does not exist yet. + * + * @param name the file path or resource name + * @param loader a class loader to use in resource lookup, or null + * to use the thread's context loader + */ + public static File getFile(String name, ClassLoader loader) { + if (name == null) { + return null; + } + + File file = new File(name); + + if (file.exists()) { + return file; + } + + if (loader == null) { + loader = Thread.currentThread().getContextClassLoader(); + } + + URL url = loader.getResource(name); + + if (url != null) { + String urlFile = url.getFile(); + + if (urlFile != null) { + File rsrc = new File(URLDecoder.decode(urlFile)); + + if (rsrc.exists()) { + return rsrc; + } + } + } + + // go back to original non-existant file path + return file; + } + + /** + * Return a writer to the stream (stdout or stderr) or file named by the + * given string. + * + * @see #getFile + */ + public static Writer getWriter(String file, ClassLoader loader) + throws IOException { + if (file == null) { + return null; + } + + if ("stdout".equals(file)) { + return new PrintWriter(System.out); + } + + if ("stderr".equals(file)) { + return new PrintWriter(System.err); + } + + try { + return new FileWriter(getFile(file, loader)); + } catch (IOException ioe) { + throw new NestableRuntimeException(ioe); + } + } + + /** + * Return an output stream to the stream (stdout or stderr) or file named + * by the given string. + * + * @see #getFile + */ + public static OutputStream getOutputStream(String file, ClassLoader loader) { + if (file == null) { + return null; + } + + if ("stdout".equals(file)) { + return System.out; + } + + if ("stderr".equals(file)) { + return System.err; + } + + try { + return new FileOutputStream(getFile(file, loader)); + } catch (IOException ioe) { + throw new NestableRuntimeException(ioe); + } + } + + /** + * Copy a file. Return false if <code>from</code> does not exist. + */ + public static boolean copy(File from, File to) throws IOException { + if ((from == null) || (to == null) || !from.exists()) { + return false; + } + + FileInputStream in = null; + FileOutputStream out = null; + + try { + in = new FileInputStream(from); + + BufferedInputStream inbuf = new BufferedInputStream(in); + out = new FileOutputStream(to); + + BufferedOutputStream outbuf = new BufferedOutputStream(out); + + for (int b; (b = inbuf.read()) != -1; outbuf.write(b)) + ; + + outbuf.flush(); + + return true; + } finally { + if (in != null) { + try { + in.close(); + } catch (Exception e) { + } + } + + if (out != null) { + try { + out.close(); + } catch (Exception e) { + } + } + } + } }