KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > template > LabelTagHandler


1 /*
2  * $Id: LabelTagHandler.java,v 1.4 2004/12/01 07:54:28 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24
25 /**
26  * A TemplateTagHandler
27  *
28  * @author <A HREF="mailto:hengels@mercatis.de">Holger Engels</A>
29  * @version $Revision: 1.4 $
30  */

31 public class LabelTagHandler
32         extends TemplateTagHandler {
33     boolean close_is_missing = false;
34     StringBuffer JavaDoc content = new StringBuffer JavaDoc();
35
36     /**
37      * @param context the parsing context
38      * @param input the PositionReader, located after the name token of the tag
39      * @param startPosition the position where parsing of this token began
40      * @param startTag the SGMLTag found in the file.
41      */

42     public SGMLTag parseTag(ParseContext context,
43                             PositionReader input,
44                             long startPosition,
45                             SGMLTag startTag)
46             throws IOException JavaDoc {
47         final String JavaDoc startTagName = startTag.getName();
48         final String JavaDoc endTagName = "/" + startTagName;
49
50         /*
51          * parse the full tag to get all parameters
52          * and to place the Reader to the position
53          * after the closing '>'
54          */

55         startTag.parse(input);
56
57         /*
58          * The offset is the space the reader skipped
59          * before it reached the opening '<'
60          */

61         startPos = startPosition + startTag.getOffset();
62
63         /*
64          * get properties
65          */

66         properties = startTag.getAttributes();
67
68         if (startTag.value("FOR", null) == null)
69             return null;
70
71         endPos = input.getPosition(); // in case </label> is missing
72

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 JavaDoc content)
89             throws IOException JavaDoc {
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 JavaDoc getContent() {
103         return content.toString();
104     }
105
106     public String JavaDoc getFor() {
107         return (String JavaDoc) properties.get("FOR");
108     }
109
110     /**
111      * actually perform the action associated with this tag.
112      *
113      * @throws Exception anything can happen .. and throw an Exception
114      * which is caught in PageParser
115      */

116     public void executeTag(ParseContext context, InputStream JavaDoc input)
117             throws Exception JavaDoc {
118         TemplateParseContext tcontext = (TemplateParseContext) context;
119         copy(input, tcontext.getDevice(), getTagLength(), new byte[512]);
120
121         // warn, if the closing tag was not found ..
122
if (close_is_missing) {
123             Device sink = tcontext.getDevice();
124             sink.print("<table bgcolor='#FFAA55'><tr><td>");
125             sink.print("&nbsp;<blink><b>");
126             sink.print("closing tag missing");
127             sink.print(" for '<em>" + name + "</em>'");
128             sink.print("</b></blink>&nbsp;");
129             sink.print("</td></tr></table>");
130         }
131     }
132
133     private static void copy(InputStream JavaDoc in, Device device, long length, byte buf[])
134             throws IOException JavaDoc {
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