/* 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.template; import xbn.XBNObject; import xbn.named.Named; import xbn.output.Outputter; import xbn.array.AOSLCreator; import xbn.array.VWString; import xbn.array.APString; import xbn.placeholder.s_s; import xbn.util.ForLineRetrieval; /**
Analyzes source text that may or may not have gaps in it.
Source code: TemplateDataOrString.java
Create a TemplateDataOrString.
Equal to TemplateDataOrString(for_lineRetrieval, (new TParseConfig()))
Create a TemplateDataOrString.
Equal to TemplateDataOrString(null, for_lineRetrieval, tp_config)
Create a TemplateDataOrString.
@param s_templateName The name of the template. @param for_lineRetrieval The source text for this TemplateDataOrString. May not be null. @param tp_config The configuration defining a gap. May not be null. **/ public TemplateDataOrString(String s_name, ForLineRetrieval for_lineRetrieval, TParseConfig tp_config) throws TemplateFormatException { throwAXIfNull(for_lineRetrieval, "for_lineRetrieval", sCNSTR); throwAXIfNull(tp_config, "tp_config", sCNSTR); sName = s_name; Outputter optr_dbg = tp_config.getOptrDebug(); if(optr_dbg.isOn()) { optr_dbg.write("TemplateDataOrString: Parsing...START"); optr_dbg.write("\tTemplate name: '" + getName() + "'"); optr_dbg.write("[" + tp_config.toString() + "]"); } VWString acsSurroundingText = new VWString(); StringBuffer sbCurrentSurrText = new StringBuffer(sES); AOSLCreator aoslc = new AOSLCreator(); TLAObjects tlao = new TLAObjects(tp_config); while(for_lineRetrieval.hasMoreLines()) { StringBuffer sbLine = for_lineRetrieval.getNextLine(); TemplateLineAnalyzer tla = new TemplateLineAnalyzer(getName(), for_lineRetrieval.getLineNumberPrev(), sbLine, tlao); String sFinalSurrTxt = null; while(!tla.isDoneAnalyzing()) { if(tla.hasAnotherGap()) { s_s ss = tla.getNextSurrTxtAndGap(); if(!tp_config.doIgnoreSurrTxt()) { sbCurrentSurrText.append(ss.s1); addCurrSurrTextOntoArray(acsSurroundingText, sbCurrentSurrText); } aoslc.addString(ss.s2); if(aoslc.wasLastStringUnique()) { //THIS IS PRINTING OUT AFTER THE GAP *FOLLOWING* //THE ONE THAT IS UNIQUE. HMMMMM. optr_dbg.write("\t\t\t[ UNIQUE ]"); } } else { //The end of the line has been reached. Get the final //surrounding text. sFinalSurrTxt = tla.getFinalSurrTxt(); } } if(!tp_config.doIgnoreSurrTxt()) { sbCurrentSurrText.append(sFinalSurrTxt); } } if(aoslc.getCountUnq() < 1) { //There are no gaps, so the FLR contains a plain string. sString = sbCurrentSurrText.toString(); tData = null; optr_dbg.write("No gaps found. isTemplate() equals false. Use getString()."); } else { //There is at least one gap. Add the final surrounding //text to the array. if(!tp_config.doIgnoreSurrTxt()) { addCurrSurrTextOntoArray(acsSurroundingText, sbCurrentSurrText); } sString = null; aoslc.lock(); tData = new TemplateData(getName(), aoslc.getAOSLookup(), new APString(acsSurroundingText.getAOString()), tlao.sTagStart, tlao.sTagEnd); sName = null; if(optr_dbg.isOn()) { optr_dbg.write(tData.getAOSLookup().getUSAPUnique().getLength() + " unique gaps found. isTemplate() equals true. Use getTemplateData()."); } } optr_dbg.write("TemplateDataOrString: Parsing...END"); } /**Does the source text (provided to the constructor) contain a template?
@return true If the source text contains at least one \~G\~gap\~EG\~.Does the source text (provided to the constructor) contain no gaps?
Equal to !isTemplate()
Get the TemplateData object derived from the source text.
@return The TemplateData object representing the source text provided to the constructor, assuming it is a template.Get the string that was passed into the constructor, but only if it is not a template.
@return The exact source text as passed into the constructor, if it is not a template.Get some information about this TemplateDataOrString.
**/ public String toString() { return this.getClass().getName() + ": isTemplate()=" + isTemplate() + ", isString()=" + isString(); } private void addCurrSurrTextOntoArray(VWString acsSurroundingText, StringBuffer sbCurrentSurrText) { String sSurroundingText = sbCurrentSurrText.toString(); //Since we've reached a gap, we need to take all the //current surrounding text and concatenate it onto the //surrounding text array. acsSurroundingText.add(sSurroundingText); //Now, start over with the "current" surrounding text //object. sbCurrentSurrText.setLength(0); } //REQUIRED BY xbn.named.Named...START public final String getName() { if(isTemplate()) { return tData.getName(); } return sName; } //REQUIRED BY xbn.named.Named...END }