Source for gnu.xml.dom.html2.DomHTMLElement

   1: /* DomHTMLElement.java --
   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: package gnu.xml.dom.html2;
  39: 
  40: import gnu.xml.dom.DomDOMException;
  41: import gnu.xml.dom.DomElement;
  42: import gnu.xml.dom.DomEvent;
  43: import org.w3c.dom.DOMException;
  44: import org.w3c.dom.NamedNodeMap;
  45: import org.w3c.dom.Node;
  46: import org.w3c.dom.events.UIEvent;
  47: import org.w3c.dom.html2.HTMLElement;
  48: 
  49: /**
  50:  * Abstract implementation of an HTML element node.
  51:  *
  52:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  53:  */
  54: public abstract class DomHTMLElement
  55:   extends DomElement
  56:   implements HTMLElement
  57: {
  58: 
  59:   protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI,
  60:                            String name)
  61:   {
  62:     super(owner, namespaceURI, name);
  63:   }
  64: 
  65:   /**
  66:    * Returns the value of the specified attribute.
  67:    * The attribute name is case insensitive.
  68:    */
  69:   protected String getHTMLAttribute(String name)
  70:   {
  71:     if (hasAttributes())
  72:       {
  73:         NamedNodeMap attrs = getAttributes();
  74:         int len = attrs.getLength();
  75:         for (int i = 0; i < len; i++)
  76:           {
  77:             Node attr = attrs.item(i);
  78:             String attrName = attr.getLocalName();
  79:             if (attrName == null)
  80:               {
  81:                 attrName = attr.getNodeName();
  82:               }
  83:             if (attrName.equalsIgnoreCase(name))
  84:               {
  85:                 return attr.getNodeValue();
  86:               }
  87:           }
  88:       }
  89:     return "";
  90:   }
  91: 
  92:   protected int getIntHTMLAttribute(String name)
  93:   {
  94:     String value = getHTMLAttribute(name);
  95:     if (value == null)
  96:       {
  97:         return -1;
  98:       }
  99:     try
 100:       {
 101:         return Integer.parseInt(value);
 102:       }
 103:     catch (NumberFormatException e)
 104:       {
 105:         return -1;
 106:       }
 107:   }
 108: 
 109:   protected boolean getBooleanHTMLAttribute(String name)
 110:   {
 111:     String value = getHTMLAttribute(name);
 112:     return value != null;
 113:   }
 114: 
 115:   /**
 116:    * Sets the value of the specified attribute.
 117:    * The attribute name is case insensitive.
 118:    */
 119:   protected void setHTMLAttribute(String name, String value)
 120:   {
 121:     Node attr;
 122:     NamedNodeMap attrs = getAttributes();
 123:     int len = attrs.getLength();
 124:     for (int i = 0; i < len; i++)
 125:       {
 126:         attr = attrs.item(i);
 127:         String attrName = attr.getLocalName();
 128:         if (attrName == null)
 129:           {
 130:             attrName = attr.getNodeName();
 131:           }
 132:         if (attrName.equalsIgnoreCase(name))
 133:           {
 134:             if (value != null)
 135:               {
 136:                 attr.setNodeValue(value);
 137:               }
 138:             else
 139:               {
 140:                 attrs.removeNamedItem(attr.getNodeName());
 141:               }
 142:             return;
 143:           }
 144:       }
 145:     if (value != null)
 146:       {
 147:         // Create a new attribute
 148:         DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument();
 149:         // XXX namespace URI for attribute?
 150:         attr = doc.createAttribute(name);
 151:         attr.setNodeValue(value);
 152:       }
 153:   }
 154: 
 155:   protected void setIntHTMLAttribute(String name, int value)
 156:   {
 157:     setHTMLAttribute(name, Integer.toString(value));
 158:   }
 159: 
 160:   protected void setBooleanHTMLAttribute(String name, boolean value)
 161:   {
 162:     setHTMLAttribute(name, value ? name : null);
 163:   }
 164: 
 165:   /**
 166:    * Returns the first parent element with the specified name.
 167:    */
 168:   protected Node getParentElement(String name)
 169:   {
 170:     for (Node parent = getParentNode(); parent != null;
 171:          parent = parent.getParentNode())
 172:       {
 173:         String parentName = parent.getLocalName();
 174:         if (parentName == null)
 175:           {
 176:               parentName = parent.getNodeName();
 177:           }
 178:         if (name.equalsIgnoreCase(parentName))
 179:           {
 180:             return parent;
 181:           }
 182:       }
 183:     return null;
 184:   }
 185: 
 186:   /**
 187:    * Returns the first child element with the specified name.
 188:    */
 189:   protected Node getChildElement(String name)
 190:   {
 191:     for (Node child = getFirstChild(); child != null;
 192:          child = child.getNextSibling())
 193:       {
 194:         String childName = child.getLocalName();
 195:         if (childName == null)
 196:           {
 197:             childName = child.getLocalName();
 198:           }
 199:         if (name.equalsIgnoreCase(childName))
 200:           {
 201:             return child;
 202:           }
 203:       }
 204:     return null;
 205:   }
 206: 
 207:   /**
 208:    * Returns the index of this element among elements of the same name,
 209:    * relative to its parent.
 210:    */
 211:   protected int getIndex()
 212:   {
 213:     int index = 0;
 214:     Node parent = getParentNode();
 215:     if (parent != null)
 216:       {
 217:         for (Node ctx = parent.getFirstChild(); ctx != null;
 218:              ctx = ctx.getNextSibling())
 219:           {
 220:             if (ctx == this)
 221:               {
 222:                 return index;
 223:               }
 224:             index++;
 225:           }
 226:       }
 227:     throw new DomDOMException(DOMException.NOT_FOUND_ERR);
 228:   }
 229: 
 230:   protected void dispatchUIEvent(String name)
 231:   {
 232:     UIEvent event = new DomEvent.DomUIEvent(name);
 233:     dispatchEvent(event);
 234:   }
 235: 
 236:   public String getId()
 237:   {
 238:     return getHTMLAttribute("id");
 239:   }
 240: 
 241:   public void setId(String id)
 242:   {
 243:     setHTMLAttribute("id", id);
 244:   }
 245: 
 246:   public String getTitle()
 247:   {
 248:     return getHTMLAttribute("title");
 249:   }
 250: 
 251:   public void setTitle(String title)
 252:   {
 253:     setHTMLAttribute("title", title);
 254:   }
 255: 
 256:   public String getLang()
 257:   {
 258:     return getHTMLAttribute("lang");
 259:   }
 260: 
 261:   public void setLang(String lang)
 262:   {
 263:     setHTMLAttribute("lang", lang);
 264:   }
 265: 
 266:   public String getDir()
 267:   {
 268:     return getHTMLAttribute("dir");
 269:   }
 270: 
 271:   public void setDir(String dir)
 272:   {
 273:     setHTMLAttribute("dir", dir);
 274:   }
 275: 
 276:   public String getClassName()
 277:   {
 278:     return getHTMLAttribute("class");
 279:   }
 280: 
 281:   public void setClassName(String className)
 282:   {
 283:     setHTMLAttribute("class", className);
 284:   }
 285: 
 286: }