View Javadoc

1   /*
2    * Copyright (C) 2003-2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
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, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21   */
22  package de.fu_berlin.ties.xml;
23  
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  
26  /***
27   * A tag in an XML document. An instance of this class represents a single start
28   * tag, end tag or empty tag.
29   *
30   * @author Christian Siefkes
31   * @version $Revision: 1.1 $, $Date: 2004/02/02 18:50:16 $, $Author: siefkes $
32   */
33  public class TagConstituent extends XMLConstituent {
34  
35      /***
36       * Type constant: a start tag.
37       */
38      public static final short START_TAG = 0;
39  
40      /***
41       * Type constant: an end tag.
42       */
43      public static final short END_TAG = 1;
44  
45      /***
46       * Type constant: an empty tag.
47       */
48      public static final short EMPTY_TAG = 2;
49  
50      /***
51       * The name of this tag.
52       */
53      private final String name;
54  
55      /***
56       * The number of the markup series this tag is part of. Markup series are
57       * series of markup (tags, PIs, comments) not interrupted by textual
58       * content.
59       */
60      private int markupSeriesNo = -1;
61  
62      /***
63       * The {@link TagVariety} of this tag.
64       * Defaults to {@link TagVariety#REGULAR}.
65       */
66      private TagVariety variety = TagVariety.REGULAR;
67  
68      /***
69       * Creates a new instance, automatically generating a suitable
70       * representation without attributes (i.e. "&lt;<em>tagName</em>&gt;" for
71       * a start tag, "&lt;/<em>tagName</em>&gt;" for an end tag,
72       * "&lt;<em>tagName</em>/&gt;" for an empty tag).
73       *
74       * @param constType the typ of this constituent (must be one of the static
75       * type constants defined in this class)
76       * @param tagName the name of this tag
77       * @throws IllegalArgumentException if <code>constType</code> differs from
78       * the static type constants defined in this class
79       */
80      public TagConstituent(final short constType, final String tagName)
81              throws IllegalArgumentException {
82          this(constType, tagName, -1);
83      }
84  
85      /***
86       * Creates a new instance, automatically generating a suitable
87       * representation without attributes (i.e. "&lt;<em>tagName</em>&gt;" for
88       * a start tag, "&lt;/<em>tagName</em>&gt;" for an end tag,
89       * "&lt;<em>tagName</em>/&gt;" for an empty tag).
90       *
91       * @param constType the typ of this constituent (must be one of the static
92       * type constants defined in this class)
93       * @param tagName the name of this tag
94       * @param markupSeries the number of the markup series this tag is part of
95       * @throws IllegalArgumentException if <code>constType</code> differs from
96       * the static type constants defined in this class
97       */
98      public TagConstituent(final short constType, final String tagName,
99              final int markupSeries)
100             throws IllegalArgumentException {
101         this(constType, tagName,
102             '<' + (constType == END_TAG ? "/" : "") + tagName
103                 + (constType == EMPTY_TAG ? "/" : "") + '>',
104             markupSeries, null, null);
105     }
106 
107     /***
108      * Creates a new instance, without storing a markup series number.
109      *
110      * @param constType the typ of this constituent (must be one of the static
111      * type constants defined in this class)
112      * @param tagName the name of this tag
113      * @param rep the representation of this constituent within the XML
114      * document, i.e. the string fragment from the document representing this
115      * constituent
116      * @throws IllegalArgumentException if <code>constType</code> differs from
117      * the static type constants defined in this class
118      */
119     public TagConstituent(final short constType, final String tagName,
120             final String rep) throws IllegalArgumentException {
121         this(constType, tagName, rep,  -1, null, null);
122     }
123 
124     /***
125      * Creates a new instance, without setting a reference to a next
126      * constituent.
127      *
128      * @param constType the typ of this constituent (must be one of the static
129      * type constants defined in this class)
130      * @param tagName the name of this tag
131      * @param rep the representation of this constituent within the XML
132      * document, i.e. the string fragment from the document representing this
133      * constituent
134      * @param markupSeries the number of the markup series this tag is part of
135      * @throws IllegalArgumentException if <code>constType</code> differs from
136      * the static type constants defined in this class
137      */
138     public TagConstituent(final short constType, final String tagName,
139             final String rep, final int markupSeries)
140             throws IllegalArgumentException {
141         this(constType, tagName, rep,  markupSeries, null, null);
142     }
143 
144     /***
145      * Creates a new instance.
146      *
147      * @param constType the typ of this constituent (must be one of the static
148      * type constants defined in this class)
149      * @param tagName the name of this tag
150      * @param rep the representation of this constituent within the XML
151      * document, i.e. the string fragment from the document representing this
152      * constituent
153      * @param markupSeries the number of the markup series this tag is part of
154      * @param prevEntry a reference to the previous constituent
155      * @param nextEntry a reference to the next constituent
156      * @throws IllegalArgumentException if <code>constType</code> differs from
157      * the static type constants defined in this class
158      */
159     public TagConstituent(final short constType, final String tagName,
160             final String rep, final int markupSeries,
161             final XMLConstituent prevEntry, final XMLConstituent nextEntry)
162             throws IllegalArgumentException {
163         super(constType, rep, prevEntry, nextEntry);
164 
165         // ensure range of type constant
166         if ((constType < START_TAG) || (constType > EMPTY_TAG)) {
167             throw new IllegalArgumentException("Specified tag type must "
168                 + "be one of the static type constants defined in "
169                 + "TagConstituent, but it is " + constType);
170         }
171         name = tagName;
172         markupSeriesNo = markupSeries;
173     }
174 
175     /***
176      * Returns the name of this tag.
177      *
178      * @return the tag name
179      */
180     public final String getName() {
181         return name;
182     }
183 
184     /***
185      * Returns the number of the markup series this tag is part of.
186      *
187      * @return the series number; or <code>-1</code> if the markup series number
188      * is not known
189      */
190     public int getMarkupSeriesNo() {
191         return markupSeriesNo;
192     }
193 
194     /***
195      * Returns the {@link TagVariety} of this tag.
196      * Defaults to {@link TagVariety#REGULAR}.
197      *
198      * @return the value
199      */
200     public TagVariety getVariety() {
201         return variety;
202     }
203 
204     /***
205      * Sets the number of the markup series this tag is part of. Tag series are
206      * series of tags not interrupted by textual content.
207      *
208      * @param i the new number
209      */
210     public void setMarkupSeriesNo(final int i) {
211         markupSeriesNo = i;
212     }
213 
214     /***
215      * Specifies the {@link TagVariety} of this tag.
216      *
217      * @param newValue the new value to set
218      */
219     public void setVariety(final TagVariety newValue) {
220         variety = newValue;
221     }
222 
223     /***
224      * Returns a string representation of this object.
225      *
226      * @return a textual representation
227      */
228     public String toString() {
229         return new ToStringBuilder(this)
230             .appendSuper(super.toString())
231             .append("name", name)
232             .append("markup series no", markupSeriesNo)
233             .append("variety", variety)
234             .toString();
235     }
236 
237 }