1 52 53 package freemarker.core; 54 55 import freemarker.template.*; 56 57 62 public abstract class TemplateObject { 63 64 private Template template; 65 int beginColumn, beginLine, endColumn, endLine; 66 67 final void setLocation(Template template, Token begin, Token end) 68 throws 69 ParseException 70 { 71 setLocation(template, begin.beginColumn, begin.beginLine, end.endColumn, end.endLine); 72 } 73 74 final void setLocation(Template template, Token begin, TemplateObject end) 75 throws 76 ParseException 77 { 78 setLocation(template, begin.beginColumn, begin.beginLine, end.endColumn, end.endLine); 79 } 80 81 final void setLocation(Template template, TemplateObject begin, Token end) 82 throws 83 ParseException 84 { 85 setLocation(template, begin.beginColumn, begin.beginLine, end.endColumn, end.endLine); 86 } 87 88 final void setLocation(Template template, TemplateObject begin, TemplateObject end) 89 throws 90 ParseException 91 { 92 setLocation(template, begin.beginColumn, begin.beginLine, end.endColumn, end.endLine); 93 } 94 95 public final int getBeginColumn() { 96 return beginColumn; 97 } 98 99 public final int getBeginLine() { 100 return beginLine; 101 } 102 103 public final int getEndColumn() { 104 return endColumn; 105 } 106 107 public final int getEndLine() { 108 return endLine; 109 } 110 111 void setLocation(Template template, int beginColumn, int beginLine, int endColumn, int endLine) 112 throws 113 ParseException 114 { 115 this.template = template; 116 this.beginColumn = beginColumn; 117 this.beginLine = beginLine; 118 this.endColumn = endColumn; 119 this.endLine = endLine; 120 } 121 122 static void assertNonNull(TemplateModel model, Expression exp, Environment env) throws InvalidReferenceException { 123 if (model == null) { 124 throw new InvalidReferenceException( 125 "Expression " + exp + " is undefined " + 126 exp.getStartLocation() + ".", env); 127 } 128 } 129 130 static TemplateException invalidTypeException(TemplateModel model, Expression exp, Environment env, String expected) 131 throws 132 TemplateException 133 { 134 assertNonNull(model, exp, env); 135 return new TemplateException( 136 "Expected " + expected + ". " + 137 exp + " evaluated instead to " + 138 model.getClass().getName() + " " + 139 exp.getStartLocation() + ".", env); 140 } 141 145 public String getStartLocation() { 146 String templateName = template != null ? template.getName() : "input"; 147 return "on line " 148 + beginLine 149 + ", column " 150 + beginColumn 151 + " in " 152 + templateName; 153 } 154 155 public String getEndLocation() { 156 String templateName = template != null ? template.getName() : "input"; 157 return "on line " 158 + endLine 159 + ", column " 160 + endColumn 161 + " in " 162 + templateName; 163 } 164 165 public final String getSource() { 166 if (template != null) { 167 return template.getSource(beginColumn, beginLine, endColumn, endLine); 168 } else { 169 return getCanonicalForm(); 170 } 171 } 172 173 public String toString() { 174 return getSource(); 175 } 176 177 181 public boolean contains(int column, int line) { 182 if (line < beginLine || line > endLine) { 183 return false; 184 } 185 if (line == beginLine) { 186 if (column < beginColumn) { 187 return false; 188 } 189 } 190 if (line == endLine) { 191 if (column > endColumn) { 192 return false; 193 } 194 } 195 return true; 196 } 197 198 public Template getTemplate() 199 { 200 return template; 201 } 202 203 TemplateObject copyLocationFrom(TemplateObject from) 204 { 205 template = from.template; 206 beginColumn = from.beginColumn; 207 beginLine = from.beginLine; 208 endColumn = from.endColumn; 209 endLine = from.endLine; 210 return this; 211 } 212 213 abstract public String getCanonicalForm(); 214 } 215 | Popular Tags |