Source for gnu.javax.swing.text.html.parser.htmlAttributeSet

   1: /* htmlAttributeSet.java -- A set to store HTML attributes
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.swing.text.html.parser;
  40: 
  41: import java.util.Enumeration;
  42: 
  43: import javax.swing.text.AttributeSet;
  44: import javax.swing.text.SimpleAttributeSet;
  45: import javax.swing.text.html.HTML;
  46: 
  47: /**
  48:  * A set, adapted to store HTML attributes.
  49:  *
  50:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  51:  */
  52: public class htmlAttributeSet
  53:   extends SimpleAttributeSet
  54: {
  55:   public static final htmlAttributeSet EMPTY_HTML_ATTRIBUTE_SET =
  56:     new htmlAttributeSet();
  57: 
  58:   AttributeSet parent;
  59: 
  60:   /**
  61:    * Looks in this set and, if not found, later looks in the parent set. Calls
  62:    * toString(), allowing to pass as HTML.Attribute, as String to this method.
  63:    *
  64:    * @param _key A key to search for a value.
  65:    * @return The value, if one is defined.
  66:    */
  67:   public Object getAttribute(Object _key)
  68:   {
  69:     Object v = super.getAttribute(_key);
  70:     if (v != null || _key == null)
  71:       return v;
  72: 
  73:     Object key = _key.toString().toLowerCase();
  74: 
  75:     v = super.getAttribute(key);
  76:     if (v != null)
  77:       return v;
  78: 
  79:     key = HTML.getAttributeKey((String) key);
  80:     v = super.getAttribute(key);
  81:     if (v != null)
  82:       return v;
  83: 
  84:     if (parent != null)
  85:       return parent.getAttribute(key);
  86:     else
  87:       return null;
  88:   }
  89: 
  90:   /**
  91:    * The name set must return HTML.Attribute and not a string,
  92:    * where applicable.
  93:    */
  94:   public Enumeration getAttributeNames()
  95:   {
  96:     // Replace the string keys by HTML.attribute, where applicable
  97:     final Enumeration enumeration = super.getAttributeNames();
  98: 
  99:     return new Enumeration()
 100:     {
 101:       public boolean hasMoreElements()
 102:       {
 103:         return enumeration.hasMoreElements();
 104:       }
 105: 
 106:       public Object nextElement()
 107:       {
 108:         Object key = enumeration.nextElement();
 109:         if (key instanceof String)
 110:           {
 111:             HTML.Attribute hKey = HTML.getAttributeKey((String) key);
 112:             if (hKey != null)
 113:               return hKey;
 114:           }
 115:         return key;
 116:       }
 117:     };
 118:   }
 119: 
 120:   /**
 121:    * Set the parent set, containing the default values.
 122:    *
 123:    * @param a_parent
 124:    */
 125:   public void setResolveParent(AttributeSet a_parent)
 126:   {
 127:     parent = a_parent;
 128:   }
 129: 
 130:   /**
 131:    * Get the parent set, containing the default values.
 132:    *
 133:    * @return the parent, used to resolve the attributes.
 134:    */
 135:   public AttributeSet getResolveParent()
 136:   {
 137:     return parent;
 138:   }
 139: 
 140:   /**
 141:    * Add the attribute to this attribute set.
 142:    *
 143:    * @param key Attribute key (if string, it will be case insensitive)
 144:    * @param value Attribute value
 145:    */
 146:   public void addAttribute(Object key, Object value)
 147:   {
 148:     if (key instanceof String)
 149:       super.addAttribute(((String) key).toLowerCase(), value);
 150:     else
 151:       super.addAttribute(key, value);
 152:   }
 153: 
 154:   /**
 155:    * Copy attributes. The returned copy does not longer contains the extended
 156:    * features, needed to participate in the HTML parsing. The returned set may
 157:    * not be mutable.
 158:    */
 159:   public AttributeSet copyAttributes()
 160:   {
 161:     if (getAttributeCount() <= 8)
 162:       // For the small size, typical in HTML tags, the direct iteration is
 163:       // faster than more complex algorithms.
 164:       return new SmallHtmlAttributeSet(this);
 165:     else
 166:       return (AttributeSet) clone();
 167:   }
 168: 
 169:   /**
 170:    * Returns a clone of the attribute set.
 171:    *
 172:    * @return A clone of the attribute set.
 173:    */
 174:   public Object clone()
 175:   {
 176:     htmlAttributeSet set = new htmlAttributeSet();
 177:     set.addAttributes(this);
 178:     AttributeSet parent = getResolveParent();
 179:     if (parent != null)
 180:       set.setResolveParent(parent);
 181:     return set;
 182:   }
 183: }