KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > au > id > jericho > lib > html > EndTagTypeGenericImplementation


1 // Jericho HTML Parser - Java based library for analysing and manipulating HTML
2
// Version 2.2
3
// Copyright (C) 2006 Martin Jericho
4
// http://sourceforge.net/projects/jerichohtml/
5
//
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 2.1 of the License, or (at your option) any later version.
10
// http://www.gnu.org/copyleft/lesser.html
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20

21 package au.id.jericho.lib.html;
22
23 import java.util.*;
24
25 /**
26  * Provides a generic implementation of the abstract {@link EndTagType} class based on the most common end tag behaviour.
27  * <p>
28  * This class is only of interest to users who wish to create <a HREF="TagType.html#Custom">custom tag types</a>.
29  * <p>
30  * The differences between this class and its abstract superclass {@link EndTagType} are:
31  * <ul>
32  * <li>The introduction of the {@link #isStatic() IsStatic} property.
33  * <li>The {@link #constructTagAt(Source, int pos)} method has a default implementation.
34  * </ul>
35  * <p>
36  * Most of the <a HREF="Tag.html#Predefined">predefined</a> end tag types are implemented using this class or a subclass of it.
37  *
38  * @see StartTagTypeGenericImplementation
39  */

40 public class EndTagTypeGenericImplementation extends EndTagType {
41     private final String JavaDoc staticString;
42
43     /**
44      * Constructs a new <code>EndTagTypeGenericImplementation</code> object based on the specified properties.
45      * <br />(<a HREF="TagType.html#ImplementationAssistance">implementation assistance</a> method)
46      * <p>
47      * The purpose of the <code>isStatic</code> parameter is explained in the {@link #isStatic() IsStatic} property description.
48      *
49      * @param description a {@linkplain #getDescription() description} of the new end tag type useful for debugging purposes.
50      * @param startDelimiter the {@linkplain #getStartDelimiter() start delimiter} of the new end tag type.
51      * @param closingDelimiter the {@linkplain #getClosingDelimiter() closing delimiter} of the new end tag type.
52      * @param isServerTag indicates whether the new end tag type is a {@linkplain #isServerTag() server tag}.
53      * @param isStatic determines whether the end tag text {@linkplain #isStatic() is static}.
54      */

55     protected EndTagTypeGenericImplementation(final String JavaDoc description, final String JavaDoc startDelimiter, final String JavaDoc closingDelimiter, final boolean isServerTag, final boolean isStatic) {
56         super(description,startDelimiter,closingDelimiter,isServerTag);
57         staticString=isStatic ? (startDelimiter+closingDelimiter) : null;
58     }
59     
60     /**
61      * Indicates whether the {@linkplain #generateHTML(String) end tag text} is static.
62      * <br />(<a HREF="TagType.html#Property">property</a> and <a HREF="#ImplementationAssistance">implementation assistance</a> method)
63      * <p>
64      * The purpose of this property is to determine the behaviour of the {@link #generateHTML(String startTagName)} method.
65      * <p>
66      * If this property is <code>true</code>, the {@linkplain #generateHTML(String) end tag text} is constant for all tags of this type.
67      * <p>
68      * If this property is <code>false</code>, the {@linkplain #generateHTML(String) end tag text} includes the
69      * {@linkplain StartTag#getName() name} of the {@linkplain #getCorrespondingStartTagType corresponding}
70      * {@linkplain StartTag start tag}.
71      * <p>
72      * {@link MasonTagTypes#MASON_COMPONENT_CALLED_WITH_CONTENT_END} is the only <a HREF="TagType.html#Predefined">predefined</a> end tag
73      * for which this property is <code>true</code>.
74      * All tags of this type have the constant tag text "<code>&lt;/&amp;&gt;</code>".
75      *
76      * @return <code>true</code> if the {@linkplain #generateHTML(String) end tag text} is static, otherwise <code>false</code>.
77      */

78     protected final boolean isStatic() {
79         return staticString!=null;
80     }
81
82     /**
83      * Generates the HTML text of an {@linkplain EndTag end tag} of this type given the {@linkplain StartTag#getName() name} of a {@linkplain #getCorrespondingStartTagType() corresponding} {@linkplain StartTag start tag}.
84      * <br />(<a HREF="TagType.html#Property">property</a> method)
85      * <p>
86      * This implementation overrides the default implementation in {@link EndTagType#generateHTML(String startTagName)}.
87      * <p>
88      * If the value of the {@link #isStatic() IsStatic} property is <code>false</code>, it returns the same string
89      * as the default implementation:<br />
90      * {@link #getStartDelimiter() getStartTagDelimiter()}<code>+startTagName+</code>{@link #getClosingDelimiter() getClosingDelimiter()}.
91      * <p>
92      * If the value of the {@link #isStatic() IsStatic} property is <code>true</code>, it returns the cached static string:<br />
93      * {@link #getStartDelimiter() getStartDelimiter()}<code>+</code>{@link #getClosingDelimiter() getClosingDelimiter()}.
94      *
95      * @param startTagName the {@linkplain StartTag#getName() name} of a {@linkplain #getCorrespondingStartTagType() corresponding} {@linkplain StartTag start tag}.
96      * @return the HTML text of an {@linkplain EndTag end tag} of this type given the {@linkplain StartTag#getName() name} of a {@linkplain #getCorrespondingStartTagType() corresponding} {@linkplain StartTag start tag}.
97      */

98     public String JavaDoc generateHTML(final String JavaDoc startTagName) {
99         return staticString!=null ? staticString : START_DELIMITER_PREFIX+startTagName+getClosingDelimiter();
100     }
101
102     /**
103      * Constructs a tag of this type at the specified position in the specified source document if it matches all of the required features.
104      * <br />(<a HREF="TagType.html#DefaultImplementation">default implementation</a> method)
105      * <p>
106      * This default implementation ensures that the source text matches the possible output of the
107      * {@link #generateHTML(String startTagName)} method.
108      * <p>
109      * If the value of the {@link #isStatic() IsStatic} property is <code>false</code>, this implementation ensures that the
110      * source text matches the expression:<br />
111      * {@link #getStartDelimiter() getStartTagDelimiter()}<code>+"</code><i>name</i><code>"+</code>{@link #getClosingDelimiter() getClosingDelimiter()}<br />
112      * where <i>name</i> is a valid {@linkplain Tag#isXMLName(CharSequence) XML tag name}.
113      * The {@linkplain Tag#getName() name} of the constructed end tag becomes {@link #getNamePrefix() getNamePrefix()}<code>+"</code><i>name</i><code>"</code>.
114      * <p>
115      * If the value of the {@link #isStatic() IsStatic} property is <code>true</code>, this implementation ensures that the
116      * source text matches the static expression:<br />
117      * {@link #getStartDelimiter() getStartTagDelimiter()}<code>+</code>{@link #getClosingDelimiter() getClosingDelimiter()}<br />
118      * The {@linkplain Tag#getName() name} of the constructed end tag is the value of the {@link #getNamePrefix() getNamePrefix()} method.
119      * <p>
120      * See {@link TagType#constructTagAt(Source, int pos)} for more important information about this method.
121      *
122      * @param source the {@link Source} document.
123      * @param pos the position in the source document.
124      * @return a tag of this type at the specified position in the specified source document if it meets all of the required features, or <code>null</code> if it does not meet the criteria.
125      */

126     protected Tag constructTagAt(final Source source, final int pos) {
127         final ParseText parseText=source.getParseText();
128         final int nameBegin=pos+START_DELIMITER_PREFIX.length();
129         String JavaDoc name=null;
130         final int startDelimiterEnd=pos+getStartDelimiter().length();
131         int end=-1;
132         if (isStatic()) {
133             name=getNamePrefix();
134             if (!parseText.containsAt(getClosingDelimiter(),startDelimiterEnd)) {
135                 if (source.isLoggingEnabled()) source.log(source.getRowColumnVector(pos).appendTo(new StringBuffer JavaDoc(200).append("EndTag of expected format ").append(staticString).append(" at ")).append(" not recognised as type '").append(getDescription()).append("' because it is missing the closing delimiter").toString());
136                 return null;
137             }
138             end=startDelimiterEnd+getClosingDelimiter().length();
139         } else {
140             final int nameEnd=source.findNameEnd(startDelimiterEnd);
141             if (nameEnd==-1) return null;
142             name=parseText.substring(nameBegin,nameEnd);
143             if (!parseText.containsAt(getClosingDelimiter(),nameEnd)) {
144                 if (source.isLoggingEnabled()) source.log(source.getRowColumnVector(pos).appendTo(new StringBuffer JavaDoc(200).append("EndTag ").append(name).append(" at ")).append(" not recognised as type '").append(getDescription()).append("' because its closing delimiter does not immediately follow its name").toString());
145                 return null;
146             }
147             end=nameEnd+getClosingDelimiter().length();
148         }
149         return constructEndTag(source,pos,end,name);
150     }
151 }
152
Popular Tags