/* 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.util; import xbn.XBNObject; import xbn.template.*; import xbn.AssertException; import xbn.array.VWObject; import xbn.array.VWString; import xbn.array.AOOValid; import xbn.array.AOOVCAll; import xbn.output.Outputter; import xbn.placeholder.i_i; import xbn.placeholder.s_s; import xbn.string.escape.EscapeString; import xbn.string.escape.ESConfig; import xbn.string.FLRString; import xbn.util.ForLineRetrieval; /**
Random functions related to Templates.
Source code: UtilTemplate.java.
Create a UtilTemplate. This constructor does nothing.
**/ public UtilTemplate() { } /**Get all the unique gap names that exist in the provided source text, or null if no gaps exist.
**/ public final String[] getAOSUniqueGapNames(ForLineRetrieval flr_srcTxt, GapConfig gap_config, Outputter optr_dbg) { VWString vws = new VWString(); addUnqGapNamesToACS(vws, flr_srcTxt, gap_config, optr_dbg); if(vws.size() == 0) { return null; } return vws.getAOString(); } /**Add all the unique gap names found in the source text to the VWString.
This creates a new TemplateDataOrString with the following constructor parameters:
(null, flr_srcTxt, (new TParseConfig(gap_config, true, optr_dbg)))
Get the resulting string, after filling in the single gap, existing in the provided template string.
Equal to getFilledXGapsTString(1, s_templateWithOneGap, (new String[] {s_fillText}))
Get the resulting string, after filling in the gaps existing in the provided template string.
@param i_rqdUniqueGaps The number of gaps required to exist in s_templateWithXGaps. Must be greater than zero. @param s_templateWithXGaps The template source text. Must be non null, and contain exactly i_rqdUniqueGaps gaps. All gap names are surrounded with tag delimiters which, by default, are TConfigDefaults.getTagTxtStart and TConfigDefaults.getTagTxtEnd. @param as_fillText The array of strings containing the text to fill into the unique gaps in s_templateWithXGaps. Must be valid according to AOOValid.crashIfBad, and its length must equal i_rqdUniqueGaps. @exception AssertException If a TemplateFormatException is thrown when parsing s_templateWithXGaps, for any reason. **/ public final String getFilledXGapsTString(int i_rqdUniqueGaps, String s_templateWithXGaps, String[] as_fillText) { if(i_rqdUniqueGaps < 1) { throwAX("getFilledXGapsTString: i_rqdUniqueGaps must be greater than zero. Currently " + i_rqdUniqueGaps + "."); } throwAXIfBadStr(s_templateWithXGaps, "s_templateWithXGaps", "getFilledXGapsTString"); aoov.crashIfBad("xbn.template.util.UtilTemplate.getFilledXGapsTString", "as_fillText", as_fillText); if(as_fillText.length != i_rqdUniqueGaps) { throwAX("getFilledXGapsTString: as_fillText.length (" + as_fillText.length + ") must equal i_rqdUniqueGaps (" + i_rqdUniqueGaps + ")."); } TemplateDataOrString tdos = null; try { tdos = new TemplateDataOrString(new FLRString(s_templateWithXGaps)); } catch(TemplateFormatException tfx) { throwAX("getFilledXGapsTString: " + tfx.toString()); } if(tdos.isString()) { throwAX("getFilledXGapsTString: There are no gaps found in s_templateWithXGaps."); } Template t = new Template(tdos.getTemplateData()); if(t.getUSAPUnique().getLength() != i_rqdUniqueGaps) { throwAX("getFilledXGapsTString: There are " + t.getUSAPUnique().getLength() + " unique gaps in s_templateWithOneGap. It is required to have exactly " + i_rqdUniqueGaps + "."); } tf.setTemplate(t); for(int i = 0; i < t.getUSAPUnique().getLength(); i++) { tf.fill(t.getUSAPUnique().getUniqueString(i).getString(), as_fillText[i]); } return tf.getResult(); } /**Want to see the original text, as it existed before it was parsed?
**/ public final String getOriginalText(Template t_emplate) { return getOriginalText(t_emplate.getTemplateData()); } /**Want to see the original text, as it existed before it was parsed?
**/ public final String getOriginalText(TemplateData template_data) { throwAXIfNull(template_data, "template_data", "getOriginalText"); StringBuffer sb = new StringBuffer(sES); try { EscapeString es = new EscapeString(new ESConfig(new char[] {template_data.getTagStart().charAt(0)})); for(int i = 0; i < template_data.getAPSSurrText().getLength(); i++) { sb.append(es.get(template_data.getAPSSurrText().getString(i))); //There are always exactly 1 less items in the gap int //array than in the surrounding text array. if(i < (template_data.getAPSSurrText().getLength() - 1)) { int iUnqFromAbsolute = template_data.getAOSLookup().getUSAPAbsolute().getUniqueString(i).getUnqArrIdx(); String s = template_data.getAOSLookup().getUSAPUnique().getUniqueString(iUnqFromAbsolute).getString(); sb.append(template_data.getTagStart()).append(s).append(template_data.getTagEnd()); } } } catch(Exception x) { throwAX("getOriginalText: template_data is non-null, but must somehow be invalid. This is only possible when it has been manually created or altered. " + x.toString()); } return sb.toString(); } /**Recursively fill a set of templates, and get the resulting name/value in a placeholder.s_s object (name=s1, value=s2).
At least one element must be a string, which is the "seed" to prevent circular fills. Every gap must be the name of an existing template/string (except itself). There must be at least one gap in at least one Template.
Example code XmplRecursiveFill.java