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.StringUtils;
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  
27  import de.fu_berlin.ties.util.ListEntry;
28  
29  /***
30   * A constituent in an XML document.
31   *
32   * @author Christian Siefkes
33   * @version $Revision: 1.1 $, $Date: 2004/02/02 18:50:17 $, $Author: siefkes $
34   */
35  public abstract class XMLConstituent extends ListEntry {
36  
37      /***
38       * The number of characters from the representations of neighboring
39       * constituents to include in {@link #toString()} call (including ellipses,
40       * if required).
41       */
42      private static final int NEIGHBOR_LENGTH = 15;
43  
44      /***
45       * The maximum number of characters from the representation to include in
46       * {@link #toString()} call (including ellipses, if required).
47       */
48      private static final int REPRESENTATION_LENGTH = 33;
49  
50      /***
51       * The representation of this constituent within the XML document, i.e. the
52       * string fragment from the document representing this constituent.
53       */
54      private String representantion;
55  
56      /***
57       * The type of this constituent. Suitable type constants are defined in
58       * subclasses of this class.
59       */
60      private final short type;
61  
62      /***
63       * Creates a new instance, without setting a reference to a next
64       * constituent.
65       *
66       * @param constType the typ of this constituent (one of the type constants
67       * defined in subclasses of this type)
68       * @param rep the representation of this constituent within the XML
69       * document, i.e. the string fragment from the document representing this
70       * constituent
71       */
72      public XMLConstituent(final short constType, final String rep) {
73          this(constType, rep, null, null);
74      }
75  
76      /***
77       * Creates a new instance.
78       *
79       * @param constType the typ of this constituent (one of the type constants
80       * defined in subclasses of this type)
81       * @param rep the representation of this constituent within the XML
82       * document, i.e. the string fragment from the document representing this
83       * constituent
84       * @param prevEntry a reference to the next constituent
85       * @param nextEntry a reference to the next constituent
86       */
87      public XMLConstituent(final short constType, final String rep,
88              final XMLConstituent prevEntry,
89              final XMLConstituent nextEntry) {
90          super(prevEntry, nextEntry);
91          type = constType;
92          representantion = rep;
93      }
94  
95      /***
96       * Returns the representation of this tag within the XML document.
97       *
98       * @return the representation, i.e. the string fragment from the document
99       * representing this constituent
100      */
101     public String getRepresentantion() {
102         return representantion;
103     }
104 
105     /***
106      * Returns the type of this constituent. Suitable type constants are
107      * defined in subclasses of this class.
108      *
109      * @return the constituent type
110      */
111     public short getType() {
112         return type;
113     }
114 
115     /***
116      * Returns the next constituent in the list, if any. A convience alternative
117      * to calling {@link ListEntry#next()} and casting the result to an
118      * <code>XMLConstituent</code>.
119      *
120      * @return the next constituent in the file
121      */
122     public XMLConstituent nextConstituent() {
123         return (XMLConstituent) next();
124     }
125 
126     /***
127      * Returns the previous constituent in the list, if any. A convience
128      * alternative to calling {@link ListEntry#previous()} and casting the
129      * result to an <code>XMLConstituent</code>.
130      *
131      * @return the previous constituent in the file
132      */
133     public XMLConstituent previousConstituent() {
134         return (XMLConstituent) previous();
135     }
136 
137     /***
138      * Sets the representation of this tag within the XML document.
139      *
140      * @param string the representation, i.e. the string fragment from the
141      * document representing this constituent
142      */
143     public void setRepresentantion(final String string) {
144         representantion = string;
145     }
146 
147     /***
148      * Returns a string representation of this object.
149      *
150      * @return a textual representation
151      */
152     public String toString() {
153         final ToStringBuilder builder = new ToStringBuilder(this)
154             .append("type", type)
155             .append("representation",
156                 StringUtils.abbreviate(representantion, REPRESENTATION_LENGTH));
157 
158         // include fragments of neighbor representations instead of including
159         // output from super.toString
160         if (hasPrevious()) {
161             final String prevRep = previousConstituent().getRepresentantion();
162             builder.append("previous", (prevRep.length() > NEIGHBOR_LENGTH)
163                 ? "..." + StringUtils.right(prevRep, NEIGHBOR_LENGTH - 3)
164                 : prevRep);
165         }
166         if (hasNext()) {
167             builder.append("next", StringUtils.abbreviate(
168                 nextConstituent().getRepresentantion(), NEIGHBOR_LENGTH));
169         }
170 
171         return builder.toString();
172     }
173 
174 }