1 package org.sapia.util.text; 2 3 4 15 public class VariableElement implements TemplateElementIF { 16 20 21 private String _theName; 22 23 private boolean _throwEx; 24 25 29 34 public VariableElement(String aName, boolean throwEx) { 35 _throwEx = throwEx; 36 _theName = aName; 37 } 38 39 43 48 public String getName() { 49 return _theName; 50 } 51 52 56 64 public String render(TemplateContextIF aContext) throws TemplateException { 65 Object aValue = aContext.getValue(_theName); 66 67 if (aValue == null) { 68 if(_throwEx) 69 throw new TemplateException("No value found for: " + _theName); 70 else 71 aValue = _theName; 72 } else if (!(aValue instanceof String )) { 73 throw new TemplateException("The value for " + _theName + 74 " is not a string : " + aValue); 75 } 76 77 return (String ) aValue; 78 } 79 80 87 public void render(TemplateContextIF aContext, StringBuffer aBuffer) 88 throws TemplateException { 89 Object aValue = aContext.getValue(_theName); 90 91 if (aValue == null) { 92 if(_throwEx) 93 throw new TemplateException("No value found for: " + _theName); 94 else 95 aValue = _theName; 96 } else if (!(aValue instanceof String )) { 97 throw new TemplateException("The value for " + _theName + 98 " is not a string : " + aValue); 99 } 100 101 aBuffer.append(aValue); 102 } 103 } 104 | Popular Tags |