001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: XMLElement.java,v $
023   Revision 1.2  2004/05/05 22:47:37  markl
024   comment block updates
025
026   Revision 1.1  2004/05/05 18:47:08  markl
027   classes renamed
028
029   Revision 1.6  2003/11/07 19:05:41  markl
030   javadoc corrections
031
032   Revision 1.5  2003/01/19 09:34:27  markl
033   Javadoc & comment header updates.
034
035   Revision 1.4  2001/03/12 06:10:57  markl
036   Javadoc cleanup.
037
038   Revision 1.3  2001/03/12 02:18:29  markl
039   Source code cleanup.
040
041   Revision 1.2  1999/01/10 03:37:18  markl
042   added GPL header & RCS tag
043   ----------------------------------------------------------------------------
044*/
045
046package kiwi.text;
047
048import java.util.*;
049
050/** This class represents an XML element. It includes the element tag as well
051  * as a hash table of all of the tag's parameters and their values.
052  * <p>
053  * An example XML element might look like this:
054  * <p>
055  * <code>
056  * &lt;img src=&quot;image.gif&quot; width=100 height=150&gt;
057  * </code>
058  * <p>
059  * In this case <tt>img</tt> is the tag, and <tt>src</tt>, <tt>width</tt> and
060  * <tt>height</tt> are the parameters, with values <tt>image.gif</tt>,
061  * <tt>100</tt> and <tt>150</tt>, respectively.
062  *
063  * @author Mark Lindner
064  */
065
066public class XMLElement extends Object
067  {
068  private String tag;
069  private Hashtable params;
070  private boolean end;
071
072  /** Construct a new <code>XMLElement</code>.
073    *
074    * @param tag The element's tag.
075    * @param end A boolean flag specifying whether this is an end tag. For
076    * example, <code></b></code> is an end tag.
077    */
078
079  public XMLElement(String tag, boolean end)
080    {
081    this.tag = tag;
082    this.end = end;
083    params = new Hashtable();
084    }
085
086  /** Construct a new <code>XMLElement</code>. The tag is set to the empty
087    * string and the end flag is set to <code>false</code>.
088    */
089
090  public XMLElement()
091    {
092    this("", false);
093    }
094
095  /** Check if this is an end tag.
096    *
097    * @return <code>true</code> if this is an end tag and <code>false</code>
098    * otherwise.
099    */
100
101  public boolean isEnd()
102    {
103    return(end);
104    }
105
106  /** Set the end tag flag.
107    *
108    * @param end The new end tag flag value.
109    */
110
111  public void setEnd(boolean end)
112    {
113    this.end = end;
114    }
115
116  /** Set the tag.
117    *
118    * @param tag The new tag.
119    */
120
121  public void setTag(String tag)
122    {
123    this.tag = tag.toLowerCase();
124    }
125
126  /** Get the tag.
127    *
128    * @return The element's tag.
129    */
130
131  public String getTag()
132    {
133    return(tag);
134    }
135
136  /** Add a parameter. Adds a parameter to the element's parameter list.
137    * If there is already a parameter with the given name in the list, it is
138    * replaced.
139    *
140    * @param name The name of the parameter.
141    * @param value The value of the parameter (may be <code>null</code>).
142    */
143
144  public void addParam(String name, String value)
145    {
146    if(value == null)
147      params.put(name.toLowerCase(), Void.class);
148    else
149      params.put(name.toLowerCase(), value);
150    }
151
152  /** Get a parameter value. Returns the value for the named paramter.
153    *
154    * @param name The name of the parameter.
155    *
156    * @return The value of the named parameter, or <code>null</code> if it's a
157    * valueless parameter.
158    */
159
160  public String getParamValue(String name)
161    {
162    Object o = params.get(name);
163    if(o != null)
164      if(o instanceof String)
165        return((String)o);
166
167    return(null);
168    }
169
170  /** Get parameter names. Returns an enumeration of strings representing
171    * the list of all parameters defined for this element.
172    */
173
174  public Enumeration getParamNames()
175    {
176    return(params.keys());
177    }
178
179  /** Create a string representatin of the element. */
180
181  public String toString()
182    {
183    StringBuffer s = new StringBuffer(50);
184
185    s.append('<');
186    if(end) s.append('/');
187    s.append(tag);
188
189    for(Enumeration e = getParamNames(); e.hasMoreElements();)
190      {
191      String key = (String)e.nextElement();
192
193      s.append(' ');
194      s.append(key);
195      String val = getParamValue(key);
196      if(val != null)
197        {
198        s.append('=');
199        s.append('"');
200        s.append((String)val);
201        s.append('"');
202        }
203      }
204    s.append('>');
205    return(s.toString());
206    }
207
208  }
209
210/* end of source file */