KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tags > EndTag


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tags/EndTag.java,v 1.2 2004/02/10 13:41:07 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.tags;
34
35 import org.htmlparser.Node;
36 import org.htmlparser.tags.data.TagData;
37 import org.htmlparser.visitors.NodeVisitor;
38 /**
39  * EndTag can identify closing tags, like </A>, </FORM>, etc.
40  */

41 public class EndTag extends Tag
42 {
43     public final static String JavaDoc TYPE = "END_TAG";
44     public final static int ENDTAG_BEFORE_PARSING_STATE = 0;
45     public final static int ENDTAG_WAIT_FOR_SLASH_STATE = 1;
46     public final static int ENDTAG_BEGIN_PARSING_STATE = 2;
47     public final static int ENDTAG_FINISHED_PARSING_STATE = 3;
48
49     /**
50      * Constructor takes 3 arguments to construct an EndTag object.
51      * @param nodeBegin Beginning position of the end tag
52      * @param nodeEnd Ending position of the end tag
53      * @param tagContents Text contents of the tag
54      */

55     public EndTag(TagData tagData)
56     {
57         super(tagData);
58     }
59     /**
60      * Locate the end tag withing the input string, by parsing from the given position
61      * @param input Input String
62      * @param position Position to start parsing from
63      */

64     public static Node find(String JavaDoc input, int position)
65     {
66         int state = ENDTAG_BEFORE_PARSING_STATE;
67         StringBuffer JavaDoc tagContents = new StringBuffer JavaDoc();
68         int tagBegin = 0;
69         int tagEnd = 0;
70         int inputLen = input.length();
71         char ch;
72         int i;
73         for (i = position;
74             (i < inputLen && state != ENDTAG_FINISHED_PARSING_STATE);
75             i++)
76         {
77             ch = input.charAt(i);
78             if (ch == '>' && state == ENDTAG_BEGIN_PARSING_STATE)
79             {
80                 state = ENDTAG_FINISHED_PARSING_STATE;
81                 tagEnd = i;
82             }
83             if (state == ENDTAG_BEGIN_PARSING_STATE)
84             {
85                 tagContents.append(ch);
86             }
87             if (state == ENDTAG_WAIT_FOR_SLASH_STATE)
88             {
89                 if (ch == '/')
90                 {
91                     state = ENDTAG_BEGIN_PARSING_STATE;
92                 }
93                 else
94                     return null;
95             }
96
97             if (ch == '<')
98             {
99                 if (state == ENDTAG_BEFORE_PARSING_STATE)
100                 {
101                     // Transition from State 0 to State 1 - Record data till > is encountered
102
tagBegin = i;
103                     state = ENDTAG_WAIT_FOR_SLASH_STATE;
104                 }
105                 else if (state == ENDTAG_BEGIN_PARSING_STATE)
106                 {
107                     state = ENDTAG_FINISHED_PARSING_STATE;
108                     tagEnd = i;
109                 }
110             }
111             else if (state == ENDTAG_BEFORE_PARSING_STATE)
112                 // text before the end tag
113
return (null);
114         }
115         // If parsing did not complete, it might be possible to accept
116
if (state == ENDTAG_BEGIN_PARSING_STATE)
117         {
118             tagEnd = i;
119             state = ENDTAG_FINISHED_PARSING_STATE;
120         }
121         if (state == ENDTAG_FINISHED_PARSING_STATE)
122             return new EndTag(
123                 new TagData(tagBegin, tagEnd, tagContents.toString(), input));
124         else
125             return null;
126     }
127     public String JavaDoc toPlainTextString()
128     {
129         return "";
130     }
131     public String JavaDoc toHtml()
132     {
133         return "</" + getTagName() + ">";
134     }
135     public String JavaDoc toString()
136     {
137         return "EndTag : "
138             + tagContents
139             + "; begins at : "
140             + elementBegin()
141             + "; ends at : "
142             + elementEnd();
143     }
144
145     public void accept(NodeVisitor visitor)
146     {
147         visitor.visitEndTag(this);
148     }
149
150     public String JavaDoc getType()
151     {
152         return TYPE;
153     }
154
155 }
156
Popular Tags