AttrImpl.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.xml.dom; 18 19import org.w3c.dom.Attr; 20import org.w3c.dom.DOMException; 21import org.w3c.dom.Element; 22import org.w3c.dom.Node; 23 24/** 25 * Provides a straightforward implementation of the corresponding W3C DOM 26 * interface. The class is used internally only, thus only notable members that 27 * are not in the original interface are documented (the W3C docs are quite 28 * extensive). Hope that's ok. 29 * <p> 30 * Some of the fields may have package visibility, so other classes belonging to 31 * the DOM implementation can easily access them while maintaining the DOM tree 32 * structure. 33 */ 34public class AttrImpl extends NodeImpl implements Attr { 35 36 // Maintained by ElementImpl. 37 ElementImpl ownerElement; 38 39 private boolean namespaceAware; 40 41 private String namespaceURI; 42 43 private String localName; 44 45 private String prefix; 46 47 private String value; 48 49 AttrImpl(DocumentImpl document, String namespaceURI, String qualifiedName) { 50 super(document); 51 52 namespaceAware = true; 53 this.namespaceURI = namespaceURI; 54 55 if (qualifiedName == null || "".equals(qualifiedName)) { 56 throw new DOMException(DOMException.NAMESPACE_ERR, qualifiedName); 57 } 58 59 int prefixSeparator = qualifiedName.lastIndexOf(":"); 60 if (prefixSeparator != -1) { 61 setPrefix(qualifiedName.substring(0, prefixSeparator)); 62 qualifiedName = qualifiedName.substring(prefixSeparator + 1); 63 } 64 65 localName = qualifiedName; 66 67 if ("".equals(localName)) { 68 throw new DOMException(DOMException.NAMESPACE_ERR, localName); 69 } 70 71 if ("xmlns".equals(localName) && !"http://www.w3.org/2000/xmlns/".equals(namespaceURI)) { 72 throw new DOMException(DOMException.NAMESPACE_ERR, localName); 73 } 74 75 if (!document.isXMLIdentifier(localName)) { 76 throw new DOMException(DOMException.INVALID_CHARACTER_ERR, localName); 77 } 78 79 value = ""; 80 } 81 82 AttrImpl(DocumentImpl document, String name) { 83 super(document); 84 85 this.namespaceAware = false; 86 87 int prefixSeparator = name.lastIndexOf(":"); 88 if (prefixSeparator != -1) { 89 String prefix = name.substring(0, prefixSeparator); 90 String localName = name.substring(prefixSeparator + 1); 91 92 if (!document.isXMLIdentifier(prefix) || !document.isXMLIdentifier(localName)) { 93 throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name); 94 } 95 } else { 96 if (!document.isXMLIdentifier(name)) { 97 throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name); 98 } 99 } 100 101 this.localName = name; 102 } 103 104 @Override 105 public String getLocalName() { 106 return namespaceAware ? localName : null; 107 } 108 109 public String getName() { 110 return (prefix != null ? prefix + ":" : "") + localName; 111 } 112 113 @Override 114 public String getNamespaceURI() { 115 return namespaceURI; 116 } 117 118 @Override 119 public String getNodeName() { 120 return getName(); 121 } 122 123 public short getNodeType() { 124 return Node.ATTRIBUTE_NODE; 125 } 126 127 @Override 128 public String getNodeValue() { 129 return getValue(); 130 } 131 132 public Element getOwnerElement() { 133 return ownerElement; 134 } 135 136 @Override 137 public String getPrefix() { 138 return prefix; 139 } 140 141 public boolean getSpecified() { 142 return value != null; 143 } 144 145 public String getValue() { 146 return value; 147 } 148 149 @Override 150 public void setNodeValue(String value) throws DOMException { 151 setValue(value); 152 } 153 154 @Override 155 public void setPrefix(String prefix) { 156 if (!namespaceAware) { 157 throw new DOMException(DOMException.NAMESPACE_ERR, prefix); 158 } 159 160 if (prefix != null) { 161 if (namespaceURI == null || !document.isXMLIdentifier(prefix) || "xmlns".equals(prefix)) { 162 throw new DOMException(DOMException.NAMESPACE_ERR, prefix); 163 } 164 165 if ("xml".equals(prefix) && !"http://www.w3.org/XML/1998/namespace".equals(namespaceURI)) { 166 throw new DOMException(DOMException.NAMESPACE_ERR, prefix); 167 } 168 } 169 170 this.prefix = prefix; 171 } 172 173 public void setValue(String value) throws DOMException { 174 this.value = value; 175 } 176 177} 178