/* 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; import xbn.array.VWObject; import xbn.output.Outputter; import xbn.placeholder.sb_sb_c; import xbn.string.SOBStringBuffer; import xbn.string.UtilChar; import xbn.string.escape.UnescapeStringException; import xbn.util.ForLineRetrieval; /**

Analyzes source text that contains zero or more variables.

Source code:  ConfigReader.java. Example code: See the example code for ConfigReaderFA

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class ConfigReader extends XBNObject { //Utility UtilConfig uConfig = new UtilConfig(); //State private CRLAObjects crlao = null; int iLine = -1; int iMLCStartLine = -1; int iVarStartLine = -1; SOBStringBuffer ssbVarName = null; SOBStringBuffer ssbValue = null; char cVarDelim = ' '; /**

Create a ConfigReader. This constructor does nothing.

**/ public ConfigReader() { } /**

Get the VWObject containing all configuration variables read in from the source text.

@return [UtilConfig].getAOsb_sb_c((for_lineRetrieval, cr_config)) **/ public sb_sb_c[] getAOsb_sb_c(ForLineRetrieval for_lineRetrieval, CRConfig cr_config) throws ConfigFormatException { return uConfig.getAOsb_sb_c(getVWObject(for_lineRetrieval, cr_config)); } /**

Get the VWObject containing all configuration variables read in from the source text.

@return [UtilConfig].getAOsb_sb_c((for_lineRetrieval, cr_config, optr_dbg)) **/ public sb_sb_c[] getAOsb_sb_c(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, Outputter optr_dbg) throws ConfigFormatException { return uConfig.getAOsb_sb_c(getVWObject(for_lineRetrieval, cr_config, optr_dbg)); } /**

Get the VWObject containing all configuration variables read in from the source text.

@return [UtilConfig].getAOsb_sb_c((for_lineRetrieval, cr_config, optr_dbg, cr_filter)) **/ public sb_sb_c[] getAOsb_sb_c(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, VWObject vwoSbsbc_cfgVars, CRFilter cr_filter) throws ConfigFormatException { return uConfig.getAOsb_sb_c(getVWObject(for_lineRetrieval, cr_config, vwoSbsbc_cfgVars, cr_filter)); } /**

Get the sb_sb_c array, containing all configuration variables read in from the source text.

@return [UtilConfig].getAOsb_sb_c((for_lineRetrieval, cr_config, optr_dbg, vwoSbsbc_cfgVars, cr_filter)) **/ public sb_sb_c[] getAOsb_sb_c(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, Outputter optr_dbg, VWObject vwoSbsbc_cfgVars, CRFilter cr_filter) throws ConfigFormatException { return uConfig.getAOsb_sb_c(getVWObject(for_lineRetrieval, cr_config, optr_dbg, vwoSbsbc_cfgVars, cr_filter)); } /**

Get the VWObject containing all configuration variables read in from the source text.

@return getVWObject(for_lineRetrieval, cr_config, (new Outputter())) **/ public VWObject getVWObject(ForLineRetrieval for_lineRetrieval, CRConfig cr_config) throws ConfigFormatException { return getVWObject(for_lineRetrieval, cr_config, (new Outputter())); } /**

Get the VWObject containing all configuration variables read in from the source text.

@return getVWObject(for_lineRetrieval, cr_config, optr_dbg, (new VWObject()), null) **/ public VWObject getVWObject(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, Outputter optr_dbg) throws ConfigFormatException { return getVWObject(for_lineRetrieval, cr_config, optr_dbg, (new VWObject()), null); } /**

Get the VWObject containing all configuration variables read in from the source text.

@return getVWObject(for_lineRetrieval, cr_config, (new Outputter()), vwoSbsbc_cfgVars, cr_filter) **/ public VWObject getVWObject(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, VWObject vwoSbsbc_cfgVars, CRFilter cr_filter) throws ConfigFormatException { return getVWObject(for_lineRetrieval, cr_config, (new Outputter()), vwoSbsbc_cfgVars, cr_filter); } /**

Get the VWObject containing all configuration variables read in from the source text.

Known issue, Regarding errors stating 'unescaped [whatever]': [whatever] may be incorrect. Just check all delimiters (in CRCVTrim and CRCDelimiters) are escaped (or not) as applicable.

@param for_lineRetrieval The source text. May not be null. @param cr_config Configuration defining a ConfigReader. May not be null. @param optr_dbg Used for debugging output. May not be null. @param vwoSbsbc_cfgVars The VWObject to add read-in variables to (each being held in an sb_sb_c). May not be null. Use this when you have previous variables to append to, or when you need the VWObject to be ordered (by variable names). @param cr_filter If non-null, this determines which variables should be accepted, and which should be ignored. If null, all variables are accepted. @return An VWObject, where zero or more sb_sb_cs (each representing a read-in configuration variable) have been added to it. Within each sb_sb_c: **/ public VWObject getVWObject(ForLineRetrieval for_lineRetrieval, CRConfig cr_config, Outputter optr_dbg, VWObject vwoSbsbc_cfgVars, CRFilter cr_filter) throws ConfigFormatException { crlao = new CRLAObjects(cr_config); initVarState(); CRLineAnalyzer crla = null; try { while(for_lineRetrieval.hasMoreLines()) { StringBuffer sbLine = for_lineRetrieval.getNextLine(); iLine = for_lineRetrieval.getLineNumberPrev(); crla = new CRLineAnalyzer(sbLine, iLine, iMLCStartLine, iVarStartLine, ssbVarName, cVarDelim, optr_dbg, crlao); boolean bEOF = !for_lineRetrieval.hasMoreLines(); if((crla.isStartLine() || bEOF) && ssbVarName != null) { //This is (the start of a new comment/variable OR //the end of the source) AND there is a variable //that has not yet been processed. if(bEOF && crla.hasValue() && !crla.isStartLine()) { //This is the very last line in the source text AND //There is a value in this line AND //it is *not* the first line in a variable. ssbValue.append(crla.getValue()); } addVariable(iVarStartLine, ssbVarName, cVarDelim, ssbValue, null, optr_dbg, cr_filter, bEOF, crlao, vwoSbsbc_cfgVars); initVarState(); } boolean bSLValue = false; if(crla.hasName()) { //SANITY CHECK...start // if(!crla.hasValue()) { // throwAX("SANITY CHECK FAILURE: crla has a name '" + crla.getName() + "', but not a value for it. Bad programmer. Bad *bad* programmer."); // } //SANITY CHECK...end ssbVarName = new SOBStringBuffer(crla.getName()); cVarDelim = crla.getDelimiter(); iVarStartLine = iLine; if(crla.getValue() == null) { bSLValue = true; } else if(cr_config.getCRCVariable().is1stLineWSEmptyString() && crlao.uStr.hasLegalChars(crla.getValue(), UtilChar.getAOCInvisible())) { bSLValue = true; } if(bSLValue) { //This is a single-line value. Process it. String sValue = ((crla.getValue() != null) ? "" : null); addVariable(iVarStartLine, ssbVarName, cVarDelim, null, sValue, optr_dbg, cr_filter, false, crlao, vwoSbsbc_cfgVars); initVarState(); continue; } } if(!bSLValue && crla.hasValue()) { //This is the second or subseqent line in a //*multi*-line value (at least, it's multi-line //before trimming, which potentially happens //below). if(ssbValue == null) { ssbValue = new SOBStringBuffer(crla.getValue()); } else { ssbValue.append(crla.getValue()); } } if(iMLCStartLine != crla.getMLCStartLine()) { iMLCStartLine = crla.getMLCStartLine(); } } } catch(NullPointerException npx) { throwAXIfNull(for_lineRetrieval, "for_lineRetrieval", sCNSTR); throwAXIfNull(optr_dbg, "optr_dbg", sCNSTR); throwAX("constructor: UNKNOWN NPX: " + npx.toString()); } if(ssbVarName != null) { //The very last line in the source is part of variable //that is *not* single line. cVarDelim = crla.getDelimiter(); addVariable(iVarStartLine, ssbVarName, cVarDelim, ssbValue, null, optr_dbg, cr_filter, true, crlao, vwoSbsbc_cfgVars); } return vwoSbsbc_cfgVars; } private void addVariable(int i_startLine, SOBStringBuffer ssb_name, char c_delim, SOBStringBuffer ssb_value, String s_value, Outputter optr_dbg, CRFilter cr_filter, boolean b_endOfFile, CRLAObjects crla_objects, VWObject vwoSbsbc_cfgVars) throws ConfigFormatException { trimEscName(i_startLine, ssb_name, crla_objects); if(cr_filter != null) { if(cr_filter.wereAllVarsFound()) { //All variables defined by the filter have been found. if(cr_filter.doCrashIfUnacceptable()) { throwAX("constructor: cr_filter is non-null, cr_filter.doCrashIfUnacceptable() equals true, and variable named '" + ssb_name.toString() + "' is unacceptable."); } //No error necessary, just ignore/suppress. return; } //There is at least one more variable to find...or the //filter is infinite. String sOriginalName = ssb_name.toString(); String sFilteredName = cr_filter.getAcceptableVarName(sOriginalName); if(sFilteredName != null) { //This variable is acceptable. if(!sOriginalName.equals(sFilteredName)) { if(optr_dbg.isOn()) { optr_dbg.write(" [ ACCEPTABLE by cr_filter. New name='" + sFilteredName + "' ]"); } ssb_name.deleteAll(); ssb_name.append(sFilteredName); } else { optr_dbg.write(" [ ACCEPTABLE by cr_filter ]."); } } else if(cr_filter.doCrashIfUnacceptable()) { throwAX("constructor: cr_filter is non-null, cr_filter.doCrashIfUnacceptable() equals true, and variable named '" + ssb_name.toString() + "' is unacceptable."); } else { //This variable is unacceptable, so this variable is //unacceptable, and should be ignored/suppressed. optr_dbg.write(" [ UNacceptable by cr_filter, and ignored ]"); return; } } StringBuffer sbValue = null; if(ssb_value != null) { trimEscValue(i_startLine, ssb_value, b_endOfFile, crla_objects); sbValue = ssb_value.getStringBuffer(); } else if(s_value != null) { sbValue = new StringBuffer(""); } try { vwoSbsbc_cfgVars.add(new sb_sb_c(ssb_name.getStringBuffer(), sbValue, c_delim)); } catch(NullPointerException npx) { throwAX("getVWObject: vwoSbsbc_cfgVars is null."); } } private void trimEscName(int i_startLine, SOBStringBuffer ssb_name, CRLAObjects crla_objects) throws ConfigFormatException { if(crla_objects.crc.getCRCVariable().getCRCVTrim().doTrimName()) { ssb_name.trim(); if(crla_objects.crc.getCRCVariable().getCRCVTrim().doUseESChar()) { //Eliminate non-escaped special empty space characters //from the start and end of each line. crla_objects.tcESChar.trim(ssb_name); } } try { //Unescape everything that needs to be. crla_objects.usAll2BEsc.unescape(ssb_name); } catch(UnescapeStringException usx) { throw new ConfigFormatException("constructor (LINE " + i_startLine + "): Attempting to unescape all to-be-escaped strings from the name ('" + crla_objects.uSOB.getVisible(ssb_name) + "')..." + usx.toString()); } } private void trimEscValue(int i_startLine, SOBStringBuffer ssb_value, boolean b_endOfFile, CRLAObjects crla_objects) throws ConfigFormatException { //Whether the value is trimmed or not, the final line //separator is actually between this value, and //the *next* variable's name (or the next comment). //However, if this is the last variable in the source, and //there are no comments between it and the end of the //source, all existing line separators should remain part of //the value. if(!b_endOfFile) { //SANITY CHECK...start // if(!ssb_value.endsWith(sLINE_SEP)) { // throwAX("addVariable SANITY CHECK 1: b_endOfFile equals true, but ssb_value ('" + crla_objects.uSOB.getVisible(ssb_value) + "') does not end with sLINE_SEP ('" + crla_objects.uStr.getVisible(sLINE_SEP) + "')!"); // } //SANITY CHECK...end ssb_value.delete((ssb_value.length() - sLINE_SEP.length()), ssb_value.length()); } //Okay, now deal with the "real" trimming... if(crla_objects.crc.getCRCVariable().getCRCVTrim().doTrimVLs()) { //Eliminate whitespace only lines from the start and end //of the value. This also eliminates whitespace from the //start of the first line in the value, and the end of the //last. crla_objects.tci.trim(ssb_value); crla_objects.tci.trimAllLines(ssb_value); if(crla_objects.crc.getCRCVariable().getCRCVTrim().doUseESChar()) { //Eliminate non-escaped special empty space characters //from the start and end of each line. crla_objects.tcESChar.trimAllLines(ssb_value); } } try { //Unescape everything that needs to be. crla_objects.usAll2BEsc.unescape(ssb_value); } catch(UnescapeStringException usx) { throw new ConfigFormatException("constructor (LINE " + i_startLine + "): Attempting to unescape all to-be-escaped strings from the value ('" + crla_objects.uSOB.getVisible(ssb_value) + "')..." + usx.toString()); } } private void initVarState() { iMLCStartLine = -1; iVarStartLine = -1; ssbVarName = null; cVarDelim = ' '; ssbValue = null; } }