1 14 package org.wings.template; 15 16 import org.wings.io.Device; 17 import org.wings.template.parser.ParseContext; 18 import org.wings.template.parser.PositionReader; 19 import org.wings.template.parser.SGMLTag; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 25 31 public class LabelTagHandler 32 extends TemplateTagHandler { 33 boolean close_is_missing = false; 34 StringBuffer content = new StringBuffer (); 35 36 42 public SGMLTag parseTag(ParseContext context, 43 PositionReader input, 44 long startPosition, 45 SGMLTag startTag) 46 throws IOException { 47 final String startTagName = startTag.getName(); 48 final String endTagName = "/" + startTagName; 49 50 55 startTag.parse(input); 56 57 61 startPos = startPosition + startTag.getOffset(); 62 63 66 properties = startTag.getAttributes(); 67 68 if (startTag.value("FOR", null) == null) 69 return null; 70 71 endPos = input.getPosition(); 73 SGMLTag endTag; 74 int len; 75 do { 76 len = readContent(input, content); 77 endTag = new SGMLTag(input, false); 78 } while (!endTag.finished() && !endTag.isNamed(endTagName)); 79 80 if (startTag.finished()) 81 close_is_missing = true; 82 else 83 endPos = input.getPosition(); 84 85 return endTag; 86 } 87 88 public int readContent(Reader r, StringBuffer content) 89 throws IOException { 90 int c, len = 0; 91 do { 92 r.mark(1); 93 c = r.read(); 94 len++; 95 content.append((char) c); 96 } while (c >= 0 && c != '<'); 97 r.reset(); 98 content.setLength(content.length() - 1); 99 return len - 1; 100 } 101 102 public String getContent() { 103 return content.toString(); 104 } 105 106 public String getFor() { 107 return (String ) properties.get("FOR"); 108 } 109 110 116 public void executeTag(ParseContext context, InputStream input) 117 throws Exception { 118 TemplateParseContext tcontext = (TemplateParseContext) context; 119 copy(input, tcontext.getDevice(), getTagLength(), new byte[512]); 120 121 if (close_is_missing) { 123 Device sink = tcontext.getDevice(); 124 sink.print("<table bgcolor='#FFAA55'><tr><td>"); 125 sink.print(" <blink><b>"); 126 sink.print("closing tag missing"); 127 sink.print(" for '<em>" + name + "</em>'"); 128 sink.print("</b></blink> "); 129 sink.print("</td></tr></table>"); 130 } 131 } 132 133 private static void copy(InputStream in, Device device, long length, byte buf[]) 134 throws IOException { 135 int len; 136 boolean limited = (length >= 0); 137 int rest = limited ? (int) length : buf.length; 138 while (rest > 0 && 139 (len = in.read(buf, 0, 140 (rest > buf.length) ? buf.length : rest)) > 0) { 141 device.write(buf, 0, len); 142 if (limited) rest -= len; 143 } 144 } 145 } 146 147 148 | Popular Tags |