Source for gnu.xml.dom.DomNsNode

   1: /* DomNsNode.java -- 
   2:    Copyright (C) 1999,2000,2001,2004 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;
  39: 
  40: import javax.xml.XMLConstants;
  41: import org.w3c.dom.DOMException;
  42: 
  43: /**
  44:  * <p> Abstract implemention of namespace support.  This facilitates
  45:  * sharing code for attribute and element nodes.
  46:  *
  47:  * @author David Brownell 
  48:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  49:  */
  50: public abstract class DomNsNode
  51:   extends DomNode
  52: {
  53:   
  54:   private String name;
  55:   private String namespace;
  56:   private String prefix;
  57:   String localName;
  58:   
  59:   /**
  60:    * Constructs a node associated with the specified document, and
  61:    * with the specified namespace information.
  62:    *
  63:    * @param owner The document with which this entity is associated
  64:    * @param namespaceURI Combined with the local part of the name,
  65:    *    this identifies a type of element or attribute; may be null.
  66:    *  If this is the empty string, it is reassigned as null so that
  67:    *  applications only need to test that case.
  68:    * @param name Name of this node, which may include a prefix
  69:    */
  70:   // package private
  71:   DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name)
  72:   {
  73:     super(nodeType, owner);
  74:     setNodeName(name);
  75:     setNamespaceURI(namespaceURI);
  76:   }
  77: 
  78:   /**
  79:    * <b>DOM L1</b>
  80:    * Returns the node's name, including any namespace prefix.
  81:    */
  82:   public final String getNodeName()
  83:   {
  84:     return name;
  85:   }
  86: 
  87:   final void setNodeName(String name)
  88:   {
  89:     this.name = name.intern();
  90:     int index = name.indexOf(':');
  91:     if (index == -1)
  92:       {
  93:         prefix = null;
  94:         localName = this.name;
  95:       }
  96:     else
  97:       {
  98:         prefix = name.substring(0, index).intern();
  99:         localName = name.substring(index + 1).intern();
 100:       }
 101:   }
 102:   
 103:   /**
 104:    * <b>DOM L2</b>
 105:    * Returns the node's namespace URI
 106:    * <em>or null</em> if the node name is not namespace scoped.
 107:    */
 108:   public final String getNamespaceURI()
 109:   {
 110:     return namespace;
 111:   }
 112: 
 113:   final void setNamespaceURI(String namespaceURI)
 114:   {
 115:     if ("".equals(namespaceURI))
 116:       {
 117:         namespaceURI = null;
 118:       }
 119:     namespace = (namespaceURI == null) ? null : namespaceURI.intern();
 120:   }
 121:   
 122:   /**
 123:    * <b>DOM L2</b>
 124:    * Returns any prefix part of the node's name (before any colon).
 125:    */
 126:   public final String getPrefix()
 127:   {
 128:     return prefix;
 129:   }
 130: 
 131:   /**
 132:    * <b>DOM L2</b>
 133:    * Assigns the prefix part of the node's name (before any colon).
 134:    */
 135:   public final void setPrefix(String prefix)
 136:   {
 137:     if (readonly)
 138:       {
 139:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 140:       }
 141: 
 142:     if (prefix == null)
 143:       {
 144:         name = localName;
 145:         return;
 146:       }
 147:     else if (namespace == null)
 148:       {
 149:         throw new DomDOMException(DOMException.NAMESPACE_ERR,
 150:                                   "can't set prefix, node has no namespace URI",
 151:                                   this, 0);
 152:       }
 153: 
 154:     DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion()));
 155:     if (prefix.indexOf (':') != -1)
 156:       {
 157:         throw new DomDOMException(DOMException.NAMESPACE_ERR,
 158:                                   "illegal prefix " + prefix, this, 0);
 159:       }
 160: 
 161:     if (XMLConstants.XML_NS_PREFIX.equals(prefix)
 162:         && !XMLConstants.XML_NS_URI.equals(namespace))
 163:       {
 164:         throw new DomDOMException(DOMException.NAMESPACE_ERR,
 165:                                   "xml namespace is always " +
 166:                                   XMLConstants.XML_NS_URI, this, 0);
 167:       }
 168: 
 169:     if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
 170:       {
 171:         if (namespace != null || getNodeType() != ATTRIBUTE_NODE)
 172:           {
 173:             throw new DomDOMException(DOMException.NAMESPACE_ERR,
 174:                                       "xmlns attribute prefix is reserved",
 175:                                       this, 0);
 176:           }
 177:       }
 178:     else if (getNodeType () == ATTRIBUTE_NODE
 179:              && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
 180:                  name.startsWith("xmlns:")))
 181:       {
 182:         throw new DomDOMException(DOMException.NAMESPACE_ERR,
 183:                                   "namespace declarations can't change names",
 184:                                   this, 0);
 185:       }
 186: 
 187:     this.prefix = prefix.intern();
 188:   }
 189: 
 190:   /**
 191:    * <b>DOM L2</b>
 192:    * Returns the local part of the node's name (after any colon).
 193:    */
 194:   public final String getLocalName()
 195:   {
 196:     return localName;
 197:   }
 198:   
 199: }