/* 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.string.padchop; import xbn.XBNObject; import xbn.string.UtilString; import xbn.string.UtilStringBuffer; import xbn.string.SOBString; import xbn.string.StringOrBuffer; /**
Pad and/or chop a string to a specific length. The configuration for this class is held in a PCSConfig.
Source code: PadChopString.java. Example code XmplPadChopString.
Create a PadChopString.
@param pcs_config The configuration for this PadChopString. May not be null. See setPCSConfig. **/ public PadChopString(PCSConfig pcs_config) { setPCSConfig(pcs_config); } /**Get the PCSConfig for direct manipulation.
@return pcs_config exactly as provided to setPCSConfig. **/ public final PCSConfig getPCSConfig() { return pcsc; } /**Set the configuration defining how the string should be padded/chopped.
Get with getPCSConfig
Get a copy of the boolean after it is padded/chopped.
@return The contents of getSOBFromS:padChop([UtilString].getSOBS(new Boolean(b_oolean).toString(), "get"))
**/
public String get(boolean b_oolean) {
SOBString ss = uStr.getSOBS((new Boolean(b_oolean)).toString(), "get");
padChop(ss);
return ss.toString();
}
/**
Get a copy of the char after it is padded/chopped.
@return The contents of getSOBFromS:padChop([UtilString].getSOBS(new Character(c_har).toString(), "get"))
**/
public String get(char c_har) {
SOBString ss = uStr.getSOBS((new Integer(c_har)).toString(), "get");
padChop(ss);
return ss.toString();
}
/**
Get a copy of the int after it is padded/chopped.
@return The contents of getSOBFromS:padChop([UtilString].getSOBS(new Integer(i_nt).toString(), "get"))
**/
public String get(int i_nt) {
SOBString ss = uStr.getSOBS((new Integer(i_nt)).toString(), "get");
padChop(ss);
return ss.toString();
}
/**
Get a copy of the string after it is padded/chopped.
@return The contents of getSOBFromS:padChop([UtilString].getSOBS(s_tr, "get"))
**/
public String get(String s_tr) {
SOBString ss = uStr.getSOBS(s_tr, "get");
padChop(ss);
return ss.toString();
}
/**
Pad/chop the StringBuffer.
Equal to padChop([UtilStringBuffer].getSOBSB(str_buffer, "get")).getStringBuffer()
Get the padded/chopped StringBuffer, based upon the configuration held in the PCSConfig. See getPCSConfig.
@param str_buffer The StringBuffer to pad or chop. @return A StringBuffer padded or chopped, based upon the configuration held in the PCSConfig. **/ public void padChop(StringOrBuffer str_orBfr) { if(str_orBfr == null) { if(getPCSConfig().getSpcfcCfgIfNull().doCrashIfNull()) { throwAX("getSB: str_orBfr is null and getPCSConfig().getSpcfcCfgIfNull().doCrashIfNull() is true."); } //getPCSConfig().getSpcfcCfgIfNull().doCrashIfNull() is false. Instead of crashng, //just return the if-null string. getPCSConfig().getSpcfcCfgIfNull().getIfNullSB(); return; } //str_orBfr is not null. if(str_orBfr.length() > getPCSConfig().getLength() && getPCSConfig().doChop()) { //The length of this string is greater than the desired //length. We want to cut the string down to its //desired length. int iMinusDDD = 0; if(getPCSConfig().getSpcfcCfgChop().getChopDotDotDot() != null) { iMinusDDD = (getPCSConfig().getSpcfcCfgChop().getChopDotDotDot().length() - getPCSConfig().getSpcfcCfgChop().getCDDDFlush()); } if(getPCSConfig().getSpcfcCfgChop().getChopSideEndStart()) { //Delete chars from end str_orBfr.setLength(getPCSConfig().getLength() - iMinusDDD); if(getPCSConfig().getSpcfcCfgChop().getChopDotDotDot() != null) { str_orBfr.append(getPCSConfig().getSpcfcCfgChop().getChopDotDotDot()); } } else { //Delete chars from start str_orBfr.delete(0, (str_orBfr.length() - getPCSConfig().getLength() - iMinusDDD)); if(getPCSConfig().getSpcfcCfgChop().getChopDotDotDot() != null) { str_orBfr.insert(0, getPCSConfig().getSpcfcCfgChop().getChopDotDotDot()); } } } //If the string is less than getLength(), it needs to be //padded. while(getPCSConfig().doPad() && str_orBfr.length() < getPCSConfig().getLength()) { //They *do* want to pad, and the string is still less //than the desired length. if(getPCSConfig().getSpcfcCfgPad().getPadSideEndStart()) { //We need to pad the right-hand side of this string. str_orBfr.append(getPCSConfig().getSpcfcCfgPad().getPadChar()); } else { //We need to pad the left-hand side of this string. //In other words, insert a character at the zero-th //char array index. str_orBfr.insert(0, getPCSConfig().getSpcfcCfgPad().getPadChar()); } } } /**Get a copy of the boolean after it is padded/chopped.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only get.
@returnget(b_oolean)
**/
public String get(int i_length, boolean b_oolean) {
getPCSConfig().setLength(i_length);
return get(b_oolean);
}
/**
Get a copy of the char after it is padded/chopped.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only get.
@returnget(c_har)
**/
public String get(int i_length, char c_har) {
getPCSConfig().setLength(i_length);
return get(c_har);
}
/**
Get a copy of the int after it is padded/chopped.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only get.
@returnget(i_nt)
**/
public String get(int i_length, int i_nt) {
getPCSConfig().setLength(i_length);
return get(i_nt);
}
/**
Get a copy of the string after it is padded/chopped.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only get.
@returnget(s_tr)
**/
public String get(int i_length, String s_tr) {
getPCSConfig().setLength(i_length);
return get(s_tr);
}
/**
Pad/chop the StringBuffer.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only padChop.
@returnpadChop(str_buffer)
**/
public void padChop(int i_length, StringBuffer str_buffer) {
getPCSConfig().setLength(i_length);
padChop(str_buffer);
}
/**
Get the padded/chopped StringBuffer, based upon the configuration held in the PCSConfig.
Before returning, this calls
getPCSConfig().setLength(i_length)
If you don't need to reset the length every time, then use only padChop.
@returnpadChop(str_orBfr)
**/
public void padChop(int i_length, StringOrBuffer str_orBfr) {
getPCSConfig().setLength(i_length);
padChop(str_orBfr);
}
/**
Get some information about this PadChopString
.