1 14 package org.wings.template.parser; 15 16 import org.wings.template.FileTemplateSource; 17 import org.wings.template.LabelTagHandler; 18 import org.wings.template.TemplateSource; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import java.io.*; 23 import java.util.*; 24 25 43 44 public class PageParser { 45 private final static Log log = LogFactory.getLog(PageParser.class); 46 47 private static PageParser sharedInstance = null; 48 49 54 public static PageParser getInstance() { 55 if (sharedInstance == null) { 56 synchronized (PageParser.class) { 57 if (sharedInstance == null) 58 sharedInstance = new PageParser(); 59 } 60 } 61 62 return sharedInstance; 63 } 64 65 71 private final Map pages = new HashMap(); 72 73 76 private final Map handlerClasses = new HashMap(); 77 78 81 public PageParser() { 82 } 83 84 94 public void process(TemplateSource source, 95 ParseContext context) 96 throws IOException { 97 interpretPage(source, getPageParts(source, context), context); 98 } 99 100 108 public void process(File file, ParseContext context) 109 throws IOException { 110 process(new FileTemplateSource(file), context); 111 } 112 113 public Map getLabels(TemplateSource source) { 114 String cName = source.getCanonicalName(); 115 if (cName == null) 116 return null; 117 118 TemplateSourceInfo sourceInfo = (TemplateSourceInfo) pages.get(cName); 119 if (sourceInfo == null) 120 return null; 121 122 return sourceInfo.labels; 123 } 124 125 137 public void addTagHandler(String tagname, String handlerClassName) 138 throws ClassNotFoundException { 139 handlerClasses.put(tagname.toUpperCase(), Class.forName(handlerClassName)); 140 } 141 142 152 public void addTagHandler(String tagname, Class handlerClass) { 153 handlerClasses.put(tagname.toUpperCase(), handlerClass); 154 } 155 156 160 public Iterator getRegisteredTags() { 161 return handlerClasses.keySet().iterator(); 162 } 163 164 173 private List getPageParts(TemplateSource source, ParseContext context) 174 throws IOException { 175 String cName = source.getCanonicalName(); 177 TemplateSourceInfo sourceInfo = null; 178 if (cName != null) 179 sourceInfo = (TemplateSourceInfo) pages.get(cName); 180 181 185 if (sourceInfo == null || 186 sourceInfo.lastModified != source.lastModified()) { 187 sourceInfo = parsePage(source, context); 189 if (cName != null) 190 pages.put(cName, sourceInfo); 191 } 192 return sourceInfo.parts; 193 } 194 195 206 private void interpretPage(TemplateSource source, 207 List parts, ParseContext context) 208 throws IOException { 209 210 OutputStream out = context.getOutputStream(); 211 InputStream inStream = null; 212 byte buf[] = null; 213 214 try { 215 inStream = source.getInputStream(); 217 long inPos = 0; 218 219 231 buf = new byte[4096]; 233 for (int i = 0; i < parts.size(); i++) { 234 235 SpecialTagHandler part = (SpecialTagHandler) parts.get(i); 236 copy(inStream, out, part.getTagStart() - inPos, buf); 238 239 context.startTag(i); 240 try { 241 part.executeTag(context, inStream); 242 } 243 catch (Throwable e) { 247 out.flush(); 248 PrintWriter pout = new PrintWriter(out); 249 pout.println("<!-- ERROR: ------------"); 250 e.printStackTrace(pout); 251 pout.println("-->"); 252 pout.flush(); 253 } 254 context.doneTag(i); 255 256 inPos = part.getTagStart() + part.getTagLength(); 257 258 } 259 copy(inStream, out, -1, buf); 261 } finally { 262 if (inStream != null) 264 inStream.close(); 265 buf = null; } 267 out.flush(); 268 } 269 270 271 281 private static void copy(InputStream in, OutputStream out, long length, 282 byte buf[]) 283 throws IOException { 284 int len; 285 boolean limited = (length >= 0); 286 int rest = limited ? (int) length : buf.length; 287 while (rest > 0 && 288 (len = in.read(buf, 0, 289 (rest > buf.length) ? buf.length : rest)) > 0) { 290 out.write(buf, 0, len); 291 if (limited) rest -= len; 292 } 293 } 294 295 310 private TemplateSourceInfo parsePage(TemplateSource source, ParseContext context) 311 throws IOException { 312 331 PositionReader fin = null; 332 fin = new PositionReader(new BufferedReader(new InputStreamReader(source.getInputStream(), "8859_1"))); 335 TemplateSourceInfo sourceInfo = new TemplateSourceInfo(); 336 337 try { 338 sourceInfo.lastModified = source.lastModified(); 340 sourceInfo.parts = new ArrayList(); 341 sourceInfo.labels = new HashMap(); 342 long startPos; 343 SGMLTag tag, endTag; 344 long startTime = System.currentTimeMillis(); 345 do { 346 endTag = null; 347 startPos = fin.getPosition(); 348 tag = new SGMLTag(fin, false); 349 if (tag.getName() != null) { 350 String upName = tag.getName().toUpperCase(); 351 if (handlerClasses.containsKey(upName)) { 352 SpecialTagHandler handler = null; 353 try { 354 Class handlerClass = (Class ) handlerClasses.get(upName); 355 handler = (SpecialTagHandler) handlerClass.newInstance(); 356 357 endTag = handler.parseTag(context, fin, startPos, tag); 358 } catch (Exception e) { 359 log.warn("Exception",e); 360 } 361 if (endTag != null) { 362 if ("LABEL".equals(upName)) { 363 LabelTagHandler labelHandler = (LabelTagHandler) handler; 364 sourceInfo.labels.put(labelHandler.getFor(), labelHandler.getContent()); 365 } 366 sourceInfo.parts.add(handler); 367 } 368 } 369 } 370 } while (!tag.finished()); 371 378 } finally { 379 if (fin != null) fin.close(); 380 } 381 return sourceInfo; 382 } 383 384 390 private static final class TemplateSourceInfo { 391 ArrayList parts; 392 Map labels; 393 long lastModified; 394 396 public TemplateSourceInfo() {} 397 } 398 } 399 400 406 407 | Popular Tags |