/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.config; import xbn.XBNObject; /**
Configure if the names and value lines of variables should have tabs and spaces trimmed from the ends. Provided to ConfigReader via CRConfig, via CRCVariable.
Source code: CRCVTrim.java
new CRCVTrim(true, '|')
This activates everything: Trimming the name, trimming value lines, and using the special empty space character '|
'. To deactivate anything, create this class with different constructor parameters.
Step one: [VARIABLE NAME as a String].trim()
The first step is to eliminate all tabs and spaces from the ends.
Step two: (new TrimChars(new char[] {this.getESChar()}).get([VARIABLE NAME])
This step eliminates the special empty-string character from the ends of the name/every line within the value.
Step one: [VARIABLE VALUE as a String].trim()
The first step is to eliminate all whitespace from the ends of the value, as a whole (remember values can span multiple lines). This eliminates all whitespace-only lines from the start and end, plus any tabs and spaces existing on the beginning of the first (non-whitespace) line, and the end of the last.
Step two: (new TrimChars()).getAllLines([VARIABLE VALUE])
This eliminates tabs and spaces from the ends of every line in the value.
Step three: (new TrimChars(new char[] {this.getESChar()}).getAllLines([VARIABLE VALUE])
This step eliminates the special empty-string character from the ends of every line within the value.
@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class CRCVTrim extends XBNObject { private boolean bTrimName = false; private boolean bTrimVLs = false; private boolean bUseESChar = false; private char cEmptyString = ' '; /**Create a CRCVTrim with default settings.
Equal to CRCVTrim(true)
Create a CRCVTrim.
Equal to CRCVTrim(b_trim, b_trim)
Create a CRCVTrim.
Equal to CRCVTrim(b_trimName, b_trimVLs, '|')
Create a CRCVTrim.
Equal to CRCVTrim(true, c_emptyString)
Create a CRCVTrim.
Equal to CRCVTrim(b_trim, b_trim, c_emptyString)
Create a CRCVTrim.
Equal to CRCVTrim(b_trimName, b_trimVLs, true, c_emptyString)
Create a CRCVTrim.
Equal to CRCVTrim(b_trimName, b_trimVLs, b_useESChar, '|')
Create a CRCVTrim.
This object, as a whole, is provided directly to the CRCVariable constructor.
@param b_trimName Do you want the name of configuration variables to be trimmed? See doTrimName. @param b_trimVLs Do you want every line in the value of configuration variables to be trimmed? See doTrimVLs. @param b_useESChar Should the special empty-string character (c_emptyString)? When both b_trimName and b_trimVLs are false, this parameter is ignored. @param c_emptyString Which character should represent the empty string? If b_useESChar is false, this parameter is ignored. **/ public CRCVTrim(boolean b_trimName, boolean b_trimVLs, boolean b_useESChar, char c_emptyString) { bTrimName = b_trimName; bTrimVLs = b_trimVLs; if(!b_trimName && !b_trimVLs) { bUseESChar = false; cEmptyString = ' '; } else if(!b_useESChar) { cEmptyString = ' '; } else { bUseESChar = true; cEmptyString = c_emptyString; } } /**Should name of a configuration variable be trimmed?
@return b_trimName Exactly as provided to the constructor. **/ public final boolean doTrimName() { return bTrimName; } /**Should every line in the value of a configuration variable be trimmed?
@return b_trimVLs Exactly as provided to the constructor. **/ public final boolean doTrimVLs() { return bTrimVLs; } /**Should the special empty-string character be used?
If this returns true, then when an non-escaped empty-string character is the first or last character in the line, then it is used to surround any characters that would normally be trimmed (tabs and spaces are normally trimmed), but should not be. It is also used to represent an empty string in the last line of a value, which also would normally be trimmed away.
When this returns false, then no empty-string character is used.
After trimming, these special empty string characters are eliminated. Note that they may only exist as the first or last character in a variable's name or value. It may also be the only character within any line in a variable's value.
@return b_useESChar Exactly as provided to the constructor. **/ public final boolean doUseESChar() { return bUseESChar; } /**Get the special empty string character.
This is only used when doUseESChar equals true.
The special empty string character allows you to specify exactly what whitespace should and should not exist at the ends of the name and value (the value as a whole, and within each line).
In the name, the special empty string character (assume the default value: '|
') can exist as the first and/or last non-whitespace character in the name. For example:
| name_of_my_variable = Here is the value
The resulting name equals " name_of_my_variable
".
In the value, the special empty string character can exist as the first and/or last non-whitespace character overall, and the first and/or last character in each line. For example:
name_of_my_variable= Here
|
The resulting value equals:
Here
|
And again, where each line is surrounded by "[
" and "]
" to highlight tabs and spaces:
name_of_my_variable=[ Here]
|
The resulting value equals:
[Here]
|
My favorite text editor, TextPad, is configured to trim all ending/right-side whitespace (tabs and spaces) every time I save a document. This causes frustration when you need whitespace to be preserved on the right-hand side.
Tabs and spaces existing on the left-side of text is never eliminated (or indentation would be pointless!). However, the special empty-string character is still useful on the left when you don't want to be forced to hug it against the left-margin. Depending on your taste, you want to indent values to make the config file look less "busy"...
Actually, the idea of the special empty-string character came to me first, and then led to the concept of trimming.
@return c_emptyString Exactly as provided to the constructor. **/ public final char getESChar() { return cEmptyString; } /**Get some information about this CRCVTrim.
**/ public String toString() { return this.getClass().getName() + ": doTrimName()=" + doTrimName() + ", doTrimVLs()=" + doTrimVLs() + ", doUseESChar()=" + doUseESChar() + ", getESChar()='" + getESChar(); } }