KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > Element


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx;
34
35 import java.util.Enumeration JavaDoc;
36
37 /**
38  * Element in an HTML page. An element runs from a start tag
39  * (like <ul>) to its matching end tag (</ul>),
40  * inclusive.
41  * An element may have an optional end tag (like <p>),
42  * in which case the element runs up to (but not including)
43  * the tag that implicitly closes it. For example:
44  * <PRE>&lt;p&gt;Paragraph 1&lt;p&gt;Paragraph 2</PRE>
45  * contains two elements, <PRE>&lt;p&gt;Paragraph 1</PRE>
46  * and <PRE>&lt;p&gt;Paragraph 2</PRE>.
47  */

48 public class Element extends Region {
49
50     protected Tag startTag;
51     protected Tag endTag;
52
53     protected Element sibling; // next sibling
54
protected Element parent;
55     protected Element child; // first child
56

57     /**
58      * Make an Element from a start tag and end tag. The tags
59      * must be on the same Page.
60      * @param startTag Start tag of element
61      * @param endTag End tag of element (may be null)
62      */

63     public Element (Tag startTag, Tag endTag) {
64         super (startTag.source, startTag.start, endTag != null ? endTag.end : startTag.end);
65         this.startTag = startTag;
66         this.endTag = endTag;
67     }
68
69     /**
70      * Make an Element from a start tag and an end position. Used
71      * when the end tag has been omitted (like &lt;p&gt;, frequently).
72      * @param startTag Start tag of element
73      * @param end Ending offset of element
74      */

75     public Element (Tag startTag, int end) {
76         super (startTag.source, startTag.start, end);
77         this.startTag = startTag;
78         this.endTag = null;
79     }
80
81     /**
82      * Get tag name.
83      * @return tag name (like "p"), in lower-case, String.intern()'ed form.
84      * Thus you can compare tag names with ==, as in:
85      * <CODE>getTagName() == Tag.IMG</CODE>.
86      */

87     public String JavaDoc getTagName () {
88         return startTag.getTagName();
89     }
90
91     /**
92      * Get start tag.
93      * @return start tag of element
94      */

95     public Tag getStartTag () {
96         return startTag;
97     }
98
99     /**
100      * Get end tag.
101      * @return end tag of element, or null if element has no end tag.
102      */

103     public Tag getEndTag () {
104         return endTag;
105     }
106
107     /**
108      * Get element's parent.
109      * @return element that contains this element, or null if at top-level.
110      */

111     public Element getParent () {
112         return parent;
113     }
114
115     /**
116      * Get element's next sibling.
117      * @return element that follows this element, or null if at end of
118      * parent's children.
119      */

120     public Element getSibling () {
121         return sibling;
122     }
123
124     /**
125      * Get element's first child.
126      * @return first element contained by this element, or null if no children.
127      */

128     public Element getChild () {
129         return child;
130     }
131     
132     /**
133      * Return next element in an inorder walk of the tree,
134      * assuming this element and its children have been visited.
135      * @return next element
136      */

137     public Element getNext () {
138         if (sibling != null)
139             return sibling;
140         else if (parent != null)
141             return parent.getNext ();
142         else
143             return null;
144     }
145
146     /**
147      * Test if tag has an HTML attribute.
148      * @param name Name of HTML attribute (e.g. "HREF"). Doesn't have to be
149      * converted with toHTMLAttributeName().
150      * @return true if tag has the attribute, false if not
151      */

152     public boolean hasHTMLAttribute (String JavaDoc name) {
153         return startTag.hasHTMLAttribute (name);
154     }
155
156     /**
157      * Get an HTML attribute's value.
158      * @param name Name of HTML attribute (e.g. "HREF"). Doesn't have to be
159      * converted with toHTMLAttributeName().
160      * @return value of attribute if it exists, TRUE if the attribute exists but has no value, or null if tag lacks the attribute.
161      */

162     public String JavaDoc getHTMLAttribute (String JavaDoc name) {
163         return startTag.getHTMLAttribute (name);
164     }
165
166     /**
167      * Get an HTML attribute's value, with a default value if it doesn't exist.
168      * @param name Name of HTML attribute (e.g. "HREF"). Doesn't have to be
169      * converted with toHTMLAttributeName().
170      * @param defaultValue default value to return if the attribute
171      * doesn't exist
172      * @return value of attribute if it exists, TRUE if the attribute exists but has no value, or defaultValue if tag lacks the attribute.
173      */

174     public String JavaDoc getHTMLAttribute (String JavaDoc name, String JavaDoc defaultValue) {
175         return startTag.getHTMLAttribute (name, defaultValue);
176     }
177     
178     /**
179      * Enumerate the HTML attributes found on this tag.
180      * @return enumeration of the attribute names found on this tag.
181      */

182     public Enumeration JavaDoc enumerateHTMLAttributes () {
183         return startTag.enumerateHTMLAttributes ();
184     }
185
186 }
187
Popular Tags