0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: AbstractTemplate.java 3792 2007-06-19 05:41:00Z gbevin $
0007: */
0008: package com.uwyn.rife.template;
0009:
0010: import java.io.IOException;
0011: import java.io.OutputStream;
0012: import java.net.URL;
0013: import java.util.*;
0014:
0015: import com.uwyn.rife.config.Config;
0016: import com.uwyn.rife.config.RifeConfig;
0017: import com.uwyn.rife.resources.ResourceFinder;
0018: import com.uwyn.rife.template.exceptions.*;
0019:
0020: public abstract class AbstractTemplate implements Template {
0021: protected TemplateInitializer mInitializer = null;
0022: protected Map<String, InternalString> mFixedValues = new HashMap<String, InternalString>();
0023: protected Map<String, InternalValue> mConstructedValues = new HashMap<String, InternalValue>();
0024: protected BeanHandler mBeanHandler = null;
0025: protected TemplateEncoder mEncoder = EncoderDummy.getInstance();
0026: protected List<ResourceBundle> mDefaultResourceBundles = null;
0027: protected List<ResourceBundle> mResourceBundles = null;
0028: protected Map<String, Object> mExpressionVars = null;
0029: protected String mLanguage = null;
0030: protected Map<String, Object> mCache = null;
0031: protected String mDefaultContentType = null;
0032:
0033: public final void appendBlock(String valueId, String blockId)
0034: throws TemplateException {
0035: if (null == valueId || 0 == valueId.length()
0036: || !hasValueId(valueId)) {
0037: throw new ValueUnknownException(valueId);
0038: }
0039: if (null == blockId || 0 == blockId.length()) {
0040: throw new BlockUnknownException(blockId);
0041: }
0042:
0043: if (mFixedValues.containsKey(valueId)) {
0044: InternalValue constructed_value = new InternalValue(this );
0045: constructed_value.appendText(mFixedValues.get(valueId));
0046: if (!appendBlockInternalForm(blockId, constructed_value)) {
0047: throw new BlockUnknownException(blockId);
0048: }
0049: mConstructedValues.put(valueId, constructed_value);
0050: mFixedValues.remove(valueId);
0051: } else if (mConstructedValues.containsKey(valueId)) {
0052: if (!appendBlockInternalForm(blockId, mConstructedValues
0053: .get(valueId))) {
0054: throw new BlockUnknownException(blockId);
0055: }
0056: } else {
0057: InternalValue constructed_value = new InternalValue(this );
0058: if (!appendBlockInternalForm(blockId, constructed_value)) {
0059: throw new BlockUnknownException(blockId);
0060: }
0061: mConstructedValues.put(valueId, constructed_value);
0062: }
0063: }
0064:
0065: public final void setBlock(String valueId, String blockId)
0066: throws TemplateException {
0067: if (null == valueId || 0 == valueId.length()
0068: || !hasValueId(valueId)) {
0069: throw new ValueUnknownException(valueId);
0070: }
0071: if (null == blockId || 0 == blockId.length()) {
0072: throw new BlockUnknownException(blockId);
0073: }
0074:
0075: if (mFixedValues.containsKey(valueId)) {
0076: mFixedValues.remove(valueId);
0077: }
0078:
0079: InternalValue constructed_value = new InternalValue(this );
0080: if (!appendBlockInternalForm(blockId, constructed_value)) {
0081: throw new BlockUnknownException(blockId);
0082: }
0083: mConstructedValues.put(valueId, constructed_value);
0084: }
0085:
0086: public String getBlock(String id) throws TemplateException {
0087: if (null == id || 0 == id.length()) {
0088: throw new BlockUnknownException(id);
0089: }
0090:
0091: ExternalValue result = new ExternalValue();
0092:
0093: if (!appendBlockExternalForm(id, result)) {
0094: throw new BlockUnknownException(id);
0095: }
0096: return result.toString();
0097: }
0098:
0099: public final String getContent() throws TemplateException {
0100: List<String> set_values = processLateTags();
0101:
0102: ExternalValue result = new ExternalValue();
0103:
0104: if (!appendBlockExternalForm("", result)) {
0105: throw new BlockUnknownException("");
0106: }
0107: String content = result.toString();
0108: removeValues(set_values);
0109: return content;
0110: }
0111:
0112: public void writeBlock(String id, OutputStream out)
0113: throws IOException, TemplateException {
0114: writeBlock(id, out, null);
0115: }
0116:
0117: public void writeBlock(String id, OutputStream out,
0118: String charsetName) throws IOException, TemplateException {
0119: if (null == out) {
0120: return;
0121: }
0122: if (null == id || 0 == id.length()) {
0123: throw new BlockUnknownException(id);
0124: }
0125:
0126: ExternalValue result = new ExternalValue();
0127:
0128: if (!appendBlockExternalForm(id, result)) {
0129: throw new BlockUnknownException(id);
0130: }
0131:
0132: result.write(out, charsetName);
0133: }
0134:
0135: public final void writeContent(OutputStream out)
0136: throws IOException, TemplateException {
0137: writeContent(out, null);
0138: }
0139:
0140: public final void writeContent(OutputStream out, String charsetName)
0141: throws IOException, TemplateException {
0142: if (null == out) {
0143: return;
0144: }
0145:
0146: ExternalValue result = new ExternalValue();
0147:
0148: if (!appendBlockExternalForm("", result)) {
0149: throw new BlockUnknownException("");
0150: }
0151:
0152: result.write(out, charsetName);
0153: }
0154:
0155: public final void write(OutputStream out) throws IOException,
0156: TemplateException {
0157: writeContent(out);
0158: }
0159:
0160: public final List<CharSequence> getDeferredBlock(String id)
0161: throws TemplateException {
0162: if (null == id || 0 == id.length()) {
0163: throw new BlockUnknownException(id);
0164: }
0165:
0166: ExternalValue result = new ExternalValue();
0167:
0168: if (!appendBlockExternalForm(id, result)) {
0169: throw new BlockUnknownException(id);
0170: }
0171:
0172: return result;
0173: }
0174:
0175: public final List<CharSequence> getDeferredContent()
0176: throws TemplateException {
0177: List<String> set_values = processLateTags();
0178:
0179: ExternalValue result = new ExternalValue();
0180:
0181: if (!appendBlockExternalForm("", result)) {
0182: throw new BlockUnknownException("");
0183: }
0184:
0185: removeValues(set_values);
0186:
0187: return result;
0188: }
0189:
0190: private List<String> processLateTags() {
0191: List<String> set_values = new ArrayList<String>();
0192:
0193: _evaluateL10nTags(set_values);
0194: _evaluateRenderTags(set_values);
0195: _evaluateLangTags(set_values, null);
0196: _evaluateOgnlTags(set_values, null);
0197: _evaluateOgnlConfigTags(set_values, null);
0198: _evaluateMvelTags(set_values, null);
0199: _evaluateMvelConfigTags(set_values, null);
0200: _evaluateGroovyTags(set_values, null);
0201: _evaluateGroovyConfigTags(set_values, null);
0202: _evaluateJaninoTags(set_values, null);
0203: _evaluateJaninoConfigTags(set_values, null);
0204:
0205: return set_values;
0206: }
0207:
0208: public List<String> evaluateRenderTags() throws TemplateException {
0209: List<String> set_values = new ArrayList<String>();
0210: _evaluateRenderTags(set_values);
0211: return set_values;
0212: }
0213:
0214: private void _evaluateRenderTags(List<String> setValues)
0215: throws TemplateException {
0216: if (hasFilteredValues(TemplateFactory.TAG_RENDER)) {
0217: List<String[]> render_tags = getFilteredValues(TemplateFactory.TAG_RENDER);
0218: for (String[] captured_groups : render_tags) {
0219: // only execute the renderer if the value hasn't been set in the
0220: // template yet
0221: if (!isValueSet(captured_groups[0])) {
0222: String classname = captured_groups[1];
0223: try {
0224: Class klass = Class.forName(classname);
0225: if (!ValueRenderer.class
0226: .isAssignableFrom(klass)) {
0227: throw new RendererWrongTypeException(this ,
0228: classname);
0229: }
0230:
0231: ValueRenderer renderer = null;
0232: try {
0233: renderer = (ValueRenderer) klass
0234: .newInstance();
0235: } catch (Exception e) {
0236: throw new RendererInstantiationException(
0237: this , classname, e);
0238: }
0239:
0240: setValue(captured_groups[0], renderer.render(
0241: this , captured_groups[0],
0242: captured_groups[2]));
0243: if (setValues != null) {
0244: setValues.add(captured_groups[0]);
0245: }
0246: } catch (ClassNotFoundException e) {
0247: throw new RendererNotFoundException(this ,
0248: classname, e);
0249: }
0250: }
0251: }
0252: }
0253: }
0254:
0255: public List<String> evaluateConfigTags() {
0256: List<String> set_values = new ArrayList<String>();
0257: _evaluateConfigTags(set_values);
0258: return set_values;
0259: }
0260:
0261: private void _evaluateConfigTags(List<String> setValues) {
0262: // process the config tags
0263: List<String[]> config_tags = getFilteredValues(TemplateFactory.TAG_CONFIG);
0264: if (config_tags != null && Config.hasRepInstance()) {
0265: String config_key = null;
0266: String config_value = null;
0267:
0268: for (String[] captured_groups : config_tags) {
0269: // only set the config value if the value hasn't been set in the
0270: // template yet
0271: if (!isValueSet(captured_groups[0])) {
0272: config_key = captured_groups[1];
0273:
0274: // obtain the configuration value
0275: config_value = Config.getRepInstance().getString(
0276: config_key);
0277:
0278: // don't continue if the config parameter doesn't exist
0279: if (config_value != null) {
0280: // set the config value in the template
0281: setValue(captured_groups[0], getEncoder()
0282: .encode(config_value));
0283: if (setValues != null) {
0284: setValues.add(captured_groups[0]);
0285: }
0286: }
0287: }
0288: }
0289: }
0290: }
0291:
0292: public List<String> evaluateL10nTags() {
0293: List<String> set_values = new ArrayList<String>();
0294: _evaluateL10nTags(set_values);
0295: return set_values;
0296: }
0297:
0298: private void _evaluateL10nTags(List<String> setValues) {
0299: // process the localization keys
0300: List<String[]> l10n_tags = getFilteredValues(TemplateFactory.TAG_L10N);
0301: if (l10n_tags != null && hasResourceBundles()) {
0302: String l10n_key = null;
0303: String l10n_value = null;
0304:
0305: for (String[] captured_groups : l10n_tags) {
0306: // only set the config value if the value hasn't been set in the
0307: // template yet
0308: if (!isValueSet(captured_groups[0])) {
0309: l10n_key = captured_groups[1];
0310:
0311: l10n_value = null;
0312: for (ResourceBundle bundle : mResourceBundles) {
0313: // obtain the configuration value
0314: try {
0315: l10n_value = bundle.getString(l10n_key);
0316: break;
0317: } catch (MissingResourceException e) {
0318: }
0319: }
0320:
0321: // don't continue if the config parameter doesn't exist
0322: if (l10n_value != null) {
0323: // set the config value in the template
0324: setValue(captured_groups[0], getEncoder()
0325: .encodeDefensive(l10n_value));
0326: if (setValues != null) {
0327: setValues.add(captured_groups[0]);
0328: }
0329: }
0330: }
0331: }
0332: }
0333: }
0334:
0335: public List<String> evaluateLangTags(String id) {
0336: if (null == id)
0337: throw new IllegalArgumentException("id can't be null.");
0338:
0339: List<String> set_values = new ArrayList<String>();
0340: _evaluateLangTags(set_values, TemplateFactory.PREFIX_LANG + id);
0341: return set_values;
0342: }
0343:
0344: private void _evaluateLangTags(List<String> setValues, String id) {
0345: // process the lang keys
0346: List<String[]> lang_blocks = getFilteredBlocks(TemplateFactory.TAG_LANG);
0347: String language = getLanguage();
0348: if (lang_blocks != null && language != null) {
0349:
0350: for (String[] lang_block : lang_blocks) {
0351: if (id != null && !id.equals(lang_block[1])) {
0352: continue;
0353: }
0354:
0355: if (null == id && isValueSet(lang_block[1])) {
0356: continue;
0357: }
0358:
0359: String block_lang = lang_block[lang_block.length - 1];
0360: if (block_lang.equals(language)) {
0361: setBlock(lang_block[1], lang_block[0]);
0362: if (setValues != null) {
0363: setValues.add(lang_block[1]);
0364: }
0365: }
0366: }
0367: }
0368: }
0369:
0370: public List<String> evaluateExpressionTags(String id) {
0371: if (null == id)
0372: throw new IllegalArgumentException("id can't be null.");
0373:
0374: List<String> set_values = new ArrayList<String>();
0375: _evaluateOgnlTags(set_values, TemplateFactory.PREFIX_OGNL + id);
0376: _evaluateMvelTags(set_values, TemplateFactory.PREFIX_MVEL + id);
0377: _evaluateGroovyTags(set_values, TemplateFactory.PREFIX_GROOVY
0378: + id);
0379: _evaluateJaninoTags(set_values, TemplateFactory.PREFIX_JANINO
0380: + id);
0381: return set_values;
0382: }
0383:
0384: public List<String> evaluateExpressionConfigTags(String id) {
0385: if (null == id)
0386: throw new IllegalArgumentException("id can't be null.");
0387:
0388: List<String> set_values = new ArrayList<String>();
0389: _evaluateOgnlConfigTags(set_values,
0390: TemplateFactory.PREFIX_OGNL_CONFIG + id);
0391: _evaluateMvelConfigTags(set_values,
0392: TemplateFactory.PREFIX_MVEL_CONFIG + id);
0393: _evaluateGroovyConfigTags(set_values,
0394: TemplateFactory.PREFIX_GROOVY_CONFIG + id);
0395: _evaluateJaninoConfigTags(set_values,
0396: TemplateFactory.PREFIX_JANINO_CONFIG + id);
0397: return set_values;
0398: }
0399:
0400: private void _evaluateOgnlTags(List<String> setValues, String id) {
0401: if (hasFilteredBlocks(TemplateFactory.TAG_OGNL)) {
0402: FilteredTagProcessorOgnl.getInstance().processTags(
0403: setValues, this ,
0404: getFilteredBlocks(TemplateFactory.TAG_OGNL), id,
0405: Template.class, "template", this , null);
0406: }
0407: }
0408:
0409: private void _evaluateOgnlConfigTags(List<String> setValues,
0410: String id) {
0411: if (hasFilteredBlocks(TemplateFactory.TAG_OGNL_CONFIG)) {
0412: FilteredTagProcessorOgnl.getInstance().processTags(
0413: setValues, this ,
0414: getFilteredBlocks(TemplateFactory.TAG_OGNL_CONFIG),
0415: id, Config.class, "config",
0416: Config.getRepInstance(), null);
0417: }
0418: }
0419:
0420: private void _evaluateMvelTags(List<String> setValues, String id) {
0421: if (hasFilteredBlocks(TemplateFactory.TAG_MVEL)) {
0422: FilteredTagProcessorMvel.getInstance().processTags(
0423: setValues, this ,
0424: getFilteredBlocks(TemplateFactory.TAG_MVEL), id,
0425: Template.class, "template", this , null);
0426: }
0427: }
0428:
0429: private void _evaluateMvelConfigTags(List<String> setValues,
0430: String id) {
0431: if (hasFilteredBlocks(TemplateFactory.TAG_MVEL_CONFIG)) {
0432: FilteredTagProcessorMvel.getInstance().processTags(
0433: setValues, this ,
0434: getFilteredBlocks(TemplateFactory.TAG_MVEL_CONFIG),
0435: id, Config.class, "config",
0436: Config.getRepInstance(), null);
0437: }
0438: }
0439:
0440: private void _evaluateGroovyTags(List<String> setValues, String id) {
0441: if (hasFilteredBlocks(TemplateFactory.TAG_GROOVY)) {
0442: FilteredTagProcessorGroovy.getInstance().processTags(
0443: setValues, this ,
0444: getFilteredBlocks(TemplateFactory.TAG_GROOVY), id,
0445: Template.class, "template", this , null);
0446: }
0447: }
0448:
0449: private void _evaluateGroovyConfigTags(List<String> setValues,
0450: String id) {
0451: if (hasFilteredBlocks(TemplateFactory.TAG_GROOVY_CONFIG)) {
0452: FilteredTagProcessorGroovy
0453: .getInstance()
0454: .processTags(
0455: setValues,
0456: this ,
0457: getFilteredBlocks(TemplateFactory.TAG_GROOVY_CONFIG),
0458: id, Config.class, "config",
0459: Config.getRepInstance(), null);
0460: }
0461: }
0462:
0463: private void _evaluateJaninoTags(List<String> setValues, String id) {
0464: if (hasFilteredBlocks(TemplateFactory.TAG_JANINO)) {
0465: FilteredTagProcessorJanino.getInstance().processTags(
0466: setValues, this ,
0467: getFilteredBlocks(TemplateFactory.TAG_JANINO), id,
0468: Template.class, "template", this , null);
0469: }
0470: }
0471:
0472: private void _evaluateJaninoConfigTags(List<String> setValues,
0473: String id) {
0474: if (hasFilteredBlocks(TemplateFactory.TAG_JANINO_CONFIG)) {
0475: FilteredTagProcessorJanino
0476: .getInstance()
0477: .processTags(
0478: setValues,
0479: this ,
0480: getFilteredBlocks(TemplateFactory.TAG_JANINO_CONFIG),
0481: id, Config.class, "config",
0482: Config.getRepInstance(), null);
0483: }
0484: }
0485:
0486: public final InternalValue createInternalValue() {
0487: return new InternalValue(this );
0488: }
0489:
0490: public final void setValue(String id,
0491: List<CharSequence> deferredContent)
0492: throws TemplateException {
0493: if (null == id || 0 == id.length() || !hasValueId(id)) {
0494: throw new ValueUnknownException(id);
0495: }
0496:
0497: if (mFixedValues.containsKey(id)) {
0498: mFixedValues.remove(id);
0499: }
0500:
0501: mConstructedValues.put(id, new InternalValue(this ,
0502: deferredContent));
0503: }
0504:
0505: public final void setValue(String id, InternalValue internalValue)
0506: throws TemplateException {
0507: if (null == id || 0 == id.length() || !hasValueId(id)) {
0508: throw new ValueUnknownException(id);
0509: }
0510: if (null == internalValue) {
0511: internalValue = createInternalValue();
0512: }
0513:
0514: if (mFixedValues.containsKey(id)) {
0515: mFixedValues.remove(id);
0516: }
0517:
0518: mConstructedValues.put(id, internalValue);
0519: }
0520:
0521: public final void setValue(String id, Template template)
0522: throws TemplateException {
0523: if (null == template) {
0524: setValue(id, "");
0525: }
0526:
0527: setValue(id, template.getContent());
0528: }
0529:
0530: public final void setValue(String id, Object value)
0531: throws TemplateException {
0532: setValue(id, String.valueOf(value));
0533: }
0534:
0535: public final void setValue(String id, boolean value)
0536: throws TemplateException {
0537: setValue(id, String.valueOf(value));
0538: }
0539:
0540: public final void setValue(String id, char value)
0541: throws TemplateException {
0542: setValue(id, String.valueOf(value));
0543: }
0544:
0545: public final void setValue(String id, char[] value)
0546: throws TemplateException {
0547: setValue(id, String.valueOf(value));
0548: }
0549:
0550: public final void setValue(String id, char[] value, int offset,
0551: int count) throws TemplateException {
0552: setValue(id, String.valueOf(value, offset, count));
0553: }
0554:
0555: public final void setValue(String id, double value)
0556: throws TemplateException {
0557: setValue(id, String.valueOf(value));
0558: }
0559:
0560: public final void setValue(String id, float value)
0561: throws TemplateException {
0562: setValue(id, String.valueOf(value));
0563: }
0564:
0565: public final void setValue(String id, int value)
0566: throws TemplateException {
0567: setValue(id, String.valueOf(value));
0568: }
0569:
0570: public final void setValue(String id, long value)
0571: throws TemplateException {
0572: setValue(id, String.valueOf(value));
0573: }
0574:
0575: public final void setValue(String id, String value)
0576: throws TemplateException {
0577: setValue(id, (CharSequence) value);
0578: }
0579:
0580: public final void setValue(String id, CharSequence value)
0581: throws TemplateException {
0582: if (null == id || 0 == id.length() || !hasValueId(id)) {
0583: throw new ValueUnknownException(id);
0584: }
0585: if (null == value) {
0586: value = "";
0587: }
0588:
0589: mFixedValues.remove(id);
0590: mConstructedValues.remove(id);
0591: mFixedValues.put(id, new InternalString(value));
0592: }
0593:
0594: public void setBean(Object bean) throws TemplateException {
0595: setBean(bean, null, true);
0596: }
0597:
0598: public void setBean(Object bean, String prefix)
0599: throws TemplateException {
0600: setBean(bean, prefix, true);
0601: }
0602:
0603: public void setBean(Object bean, String prefix, boolean encode)
0604: throws TemplateException {
0605: if (null == mBeanHandler) {
0606: throw new BeanHandlerUnsupportedException(this , bean);
0607: }
0608:
0609: mBeanHandler.setBean(this , bean, prefix, encode);
0610: }
0611:
0612: public void removeBean(Object bean) throws TemplateException {
0613: removeBean(bean, null);
0614: }
0615:
0616: public void removeBean(Object bean, String prefix)
0617: throws TemplateException {
0618: if (null == mBeanHandler) {
0619: throw new BeanHandlerUnsupportedException(this , bean);
0620: }
0621:
0622: mBeanHandler.removeBean(this , bean, prefix);
0623: }
0624:
0625: public final void appendValue(String id, Object value)
0626: throws TemplateException {
0627: appendValue(id, String.valueOf(value));
0628: }
0629:
0630: public final void appendValue(String id, boolean value)
0631: throws TemplateException {
0632: appendValue(id, String.valueOf(value));
0633: }
0634:
0635: public final void appendValue(String id, char value)
0636: throws TemplateException {
0637: appendValue(id, String.valueOf(value));
0638: }
0639:
0640: public final void appendValue(String id, char[] value)
0641: throws TemplateException {
0642: appendValue(id, String.valueOf(value));
0643: }
0644:
0645: public final void appendValue(String id, char[] value, int offset,
0646: int count) throws TemplateException {
0647: appendValue(id, String.valueOf(value, offset, count));
0648: }
0649:
0650: public final void appendValue(String id, double value)
0651: throws TemplateException {
0652: appendValue(id, String.valueOf(value));
0653: }
0654:
0655: public final void appendValue(String id, float value)
0656: throws TemplateException {
0657: appendValue(id, String.valueOf(value));
0658: }
0659:
0660: public final void appendValue(String id, int value)
0661: throws TemplateException {
0662: appendValue(id, String.valueOf(value));
0663: }
0664:
0665: public final void appendValue(String id, long value)
0666: throws TemplateException {
0667: appendValue(id, String.valueOf(value));
0668: }
0669:
0670: public final void appendValue(String id, String value)
0671: throws TemplateException {
0672: if (null == id || 0 == id.length() || !hasValueId(id)) {
0673: throw new ValueUnknownException(id);
0674: }
0675: if (null == value) {
0676: return;
0677: }
0678:
0679: if (mFixedValues.containsKey(id)) {
0680: mFixedValues.get(id).append(value);
0681: } else if (mConstructedValues.containsKey(id)) {
0682: mConstructedValues.get(id).appendText(value);
0683: } else {
0684: mFixedValues.put(id, new InternalString(value));
0685: }
0686: }
0687:
0688: public final String getValue(String id) throws TemplateException {
0689: if (null == id || 0 == id.length() || !hasValueId(id)) {
0690: throw new ValueUnknownException(id);
0691: }
0692:
0693: if (mFixedValues.containsKey(id)) {
0694: return mFixedValues.get(id).toString();
0695: }
0696: if (mConstructedValues.containsKey(id)) {
0697: ExternalValue result = new ExternalValue();
0698: mConstructedValues.get(id).appendExternalForm(result);
0699: return result.toString();
0700: }
0701:
0702: return getDefaultValue(id);
0703: }
0704:
0705: public abstract String getDefaultValue(String id);
0706:
0707: public boolean hasDefaultValue(String id) {
0708: if (null == getDefaultValue(id)) {
0709: return false;
0710: }
0711:
0712: return true;
0713: }
0714:
0715: abstract public List<String[]> getFilteredBlocks(String filter);
0716:
0717: abstract public boolean hasFilteredBlocks(String filter);
0718:
0719: abstract public List<String[]> getFilteredValues(String filter);
0720:
0721: abstract public boolean hasFilteredValues(String filter);
0722:
0723: public final boolean hasBlock(String id) {
0724: if (null == id || 0 == id.length()) {
0725: return false;
0726: }
0727:
0728: ExternalValue temp_value = new ExternalValue();
0729:
0730: return appendBlockExternalForm(id, temp_value);
0731: }
0732:
0733: public final boolean isValueSet(String id) {
0734: if (null == id || 0 == id.length()) {
0735: return false;
0736: }
0737:
0738: return mFixedValues.containsKey(id)
0739: || mConstructedValues.containsKey(id);
0740: }
0741:
0742: public final int countValues() {
0743: return mFixedValues.size() + mConstructedValues.size();
0744: }
0745:
0746: public final void removeValue(String id) {
0747: if (null == id || 0 == id.length() || !hasValueId(id)) {
0748: throw new ValueUnknownException(id);
0749: }
0750:
0751: mFixedValues.remove(id);
0752: mConstructedValues.remove(id);
0753: }
0754:
0755: public final void removeValues(List<String> ids) {
0756: if (null == ids || 0 == ids.size()) {
0757: return;
0758: }
0759:
0760: for (String id : ids) {
0761: removeValue(id);
0762: }
0763: }
0764:
0765: public final void blankValue(String id) {
0766: setValue(id, "");
0767: }
0768:
0769: public final void clear() {
0770: mFixedValues = new HashMap<String, InternalString>();
0771: mConstructedValues = new HashMap<String, InternalValue>();
0772: mResourceBundles = null;
0773: if (mDefaultResourceBundles != null) {
0774: mResourceBundles = new ArrayList<ResourceBundle>(
0775: mDefaultResourceBundles);
0776: }
0777: initialize();
0778: }
0779:
0780: public abstract String[] getAvailableValueIds();
0781:
0782: public abstract Collection<String> getUnsetValueIds();
0783:
0784: public abstract boolean hasValueId(String id);
0785:
0786: public abstract long getModificationTime();
0787:
0788: // make the template only instantiateable from within this package or from derived classes
0789: protected AbstractTemplate() {
0790: }
0791:
0792: protected void appendTextInternal(InternalValue value,
0793: CharSequence text) {
0794: value.appendText(text);
0795: }
0796:
0797: protected void increasePartsCapacityInternal(InternalValue value,
0798: int size) {
0799: value.increasePartsCapacity(size);
0800: }
0801:
0802: protected void increaseValuesCapacityInternal(InternalValue value,
0803: int size) {
0804: value.increaseValuesCapacity(size);
0805: }
0806:
0807: protected abstract boolean appendBlockExternalForm(String id,
0808: ExternalValue result);
0809:
0810: protected abstract boolean appendBlockInternalForm(String id,
0811: InternalValue result);
0812:
0813: protected final void appendValueExternalForm(String id, String tag,
0814: ExternalValue result) {
0815: assert id != null;
0816: assert id.length() != 0;
0817:
0818: CharSequence fixed_value = mFixedValues.get(id);
0819: if (fixed_value != null) {
0820: result.add(fixed_value);
0821: return;
0822: }
0823:
0824: InternalValue constructed_value = mConstructedValues.get(id);
0825: if (constructed_value != null) {
0826: constructed_value.appendExternalForm(result);
0827: return;
0828: }
0829:
0830: if (!appendDefaultValueExternalForm(id, result)) {
0831: result.add(tag);
0832: }
0833: }
0834:
0835: protected abstract boolean appendDefaultValueExternalForm(
0836: String id, ExternalValue result);
0837:
0838: protected final void appendValueInternalForm(String id, String tag,
0839: InternalValue result) {
0840: CharSequence fixed_value = mFixedValues.get(id);
0841: if (fixed_value != null) {
0842: result.appendText(fixed_value);
0843: return;
0844: }
0845:
0846: InternalValue constructed_value = mConstructedValues.get(id);
0847: if (constructed_value != null) {
0848: result.appendConstructedValue(constructed_value);
0849: return;
0850: }
0851:
0852: if (!appendDefaultValueInternalForm(id, result)) {
0853: result.appendValueId(id, tag);
0854: }
0855: }
0856:
0857: protected abstract boolean appendDefaultValueInternalForm(
0858: String id, InternalValue result);
0859:
0860: public final BeanHandler getBeanHandler() {
0861: return mBeanHandler;
0862: }
0863:
0864: final void setBeanHandler(BeanHandler beanHandler) {
0865: mBeanHandler = beanHandler;
0866: }
0867:
0868: public final TemplateEncoder getEncoder() {
0869: return mEncoder;
0870: }
0871:
0872: final void setEncoder(TemplateEncoder encoder) {
0873: if (null == encoder) {
0874: mEncoder = EncoderDummy.getInstance();
0875: } else {
0876: mEncoder = encoder;
0877: }
0878: }
0879:
0880: void setDefaultResourceBundles(ArrayList<ResourceBundle> bundles) {
0881: mDefaultResourceBundles = bundles;
0882: if (bundles != null) {
0883: mResourceBundles = new ArrayList<ResourceBundle>(bundles);
0884: }
0885: }
0886:
0887: public final void addResourceBundle(ResourceBundle resourceBundle) {
0888: if (null == resourceBundle) {
0889: return;
0890: }
0891:
0892: if (null == mResourceBundles) {
0893: mResourceBundles = new ArrayList<ResourceBundle>();
0894: }
0895:
0896: mResourceBundles.add(resourceBundle);
0897: }
0898:
0899: public final Collection<ResourceBundle> getResourceBundles() {
0900: if (null == mResourceBundles) {
0901: mResourceBundles = new ArrayList<ResourceBundle>();
0902: }
0903:
0904: return mResourceBundles;
0905: }
0906:
0907: public final boolean hasResourceBundles() {
0908: return mResourceBundles != null && mResourceBundles.size() > 0;
0909: }
0910:
0911: public void setLanguage(String lang) {
0912: mLanguage = lang;
0913: }
0914:
0915: public String getLanguage() {
0916: if (null == mLanguage) {
0917: return RifeConfig.Tools.getDefaultLanguage();
0918: }
0919:
0920: return mLanguage;
0921: }
0922:
0923: public void setExpressionVar(String name, Object value) {
0924: if (null == mExpressionVars) {
0925: mExpressionVars = new HashMap<String, Object>();
0926: }
0927:
0928: mExpressionVars.put(name, value);
0929: }
0930:
0931: public void setExpressionVars(Map<String, Object> map) {
0932: mExpressionVars = map;
0933: }
0934:
0935: public Map<String, Object> getExpressionVars() {
0936: return mExpressionVars;
0937: }
0938:
0939: final void initialize() throws TemplateException {
0940: _evaluateConfigTags(null);
0941: _evaluateL10nTags(null);
0942:
0943: if (null == mInitializer) {
0944: return;
0945: }
0946:
0947: mInitializer.initialize(this );
0948: }
0949:
0950: final void setInitializer(TemplateInitializer initializer) {
0951: mInitializer = initializer;
0952: }
0953:
0954: public void cacheObject(String key, Object value) {
0955: if (null == key) {
0956: return;
0957: }
0958:
0959: if (null == mCache) {
0960: mCache = new HashMap<String, Object>();
0961: }
0962:
0963: mCache.put(key, value);
0964: }
0965:
0966: public Object getCacheObject(String key) {
0967: if (null == mCache) {
0968: return null;
0969: }
0970:
0971: return mCache.get(key);
0972: }
0973:
0974: public String getDefaultContentType() {
0975: return mDefaultContentType;
0976: }
0977:
0978: public void setDefaultContentType(String defaultContentType) {
0979: mDefaultContentType = defaultContentType;
0980: }
0981:
0982: protected static boolean isTemplateClassModified(
0983: URL templateResource, long templateModificationTime,
0984: Map templateDependencies, String templateModificationState,
0985: ResourceFinder resourceFinder, String modificationState) {
0986: try {
0987: if (Parser.getModificationTime(resourceFinder,
0988: templateResource) > templateModificationTime) {
0989: return true;
0990: }
0991:
0992: if (templateDependencies.size() > 0) {
0993: Iterator url_it = templateDependencies.keySet()
0994: .iterator();
0995: URL dependency_resource = null;
0996: while (url_it.hasNext()) {
0997: dependency_resource = (URL) url_it.next();
0998: if (Parser.getModificationTime(resourceFinder,
0999: dependency_resource) > ((Long) templateDependencies
1000: .get(dependency_resource)).longValue()) {
1001: return true;
1002: }
1003: }
1004: }
1005:
1006: if (templateModificationState != null
1007: || modificationState != null) {
1008: if (null == templateModificationState
1009: || null == modificationState) {
1010: return true;
1011: }
1012:
1013: if (!templateModificationState
1014: .equals(modificationState)) {
1015: return true;
1016: }
1017: }
1018:
1019: } catch (TemplateException e) {
1020: return false;
1021: }
1022:
1023: return false;
1024: }
1025:
1026: public Template clone() {
1027: AbstractTemplate new_template = null;
1028: try {
1029: new_template = (AbstractTemplate) super .clone();
1030: } catch (CloneNotSupportedException e) {
1031: new_template = null;
1032: }
1033:
1034: new_template.mBeanHandler = mBeanHandler;
1035: new_template.mInitializer = mInitializer;
1036: new_template.mEncoder = mEncoder;
1037: new_template.mLanguage = mLanguage;
1038: new_template.mDefaultContentType = mDefaultContentType;
1039:
1040: new_template.mFixedValues = new HashMap<String, InternalString>();
1041:
1042: for (String value_id : mFixedValues.keySet()) {
1043: new_template.mFixedValues.put(value_id, mFixedValues
1044: .get(value_id));
1045: }
1046:
1047: new_template.mConstructedValues = new HashMap<String, InternalValue>();
1048:
1049: for (String constructed_value_id : mConstructedValues.keySet()) {
1050: new_template.mConstructedValues.put(constructed_value_id,
1051: mConstructedValues.get(constructed_value_id));
1052: }
1053:
1054: if (mExpressionVars != null) {
1055: new_template.mExpressionVars = new HashMap<String, Object>(
1056: mExpressionVars);
1057: }
1058:
1059: return new_template;
1060: }
1061: }
|