OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / xml / dom / AttrImpl.java
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
17 package org.apache.harmony.xml.dom;
18
19 import org.w3c.dom.Attr;
20 import org.w3c.dom.DOMException;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.TypeInfo;
24
25 /**
26  * Provides a straightforward implementation of the corresponding W3C DOM
27  * interface. The class is used internally only, thus only notable members that
28  * are not in the original interface are documented (the W3C docs are quite
29  * extensive). Hope that's ok.
30  * <p>
31  * Some of the fields may have package visibility, so other classes belonging to
32  * the DOM implementation can easily access them while maintaining the DOM tree
33  * structure.
34  */
35 public final class AttrImpl extends NodeImpl implements Attr {
36
37     // Maintained by ElementImpl.
38     ElementImpl ownerElement;
39     boolean isId;
40
41     boolean namespaceAware;
42     String namespaceURI;
43     String prefix;
44     String localName;
45
46     private String value;
47
48     AttrImpl(DocumentImpl document, String namespaceURI, String qualifiedName) {
49         super(document);
50
51         setNameNS(this, namespaceURI, qualifiedName);
52         value = "";
53     }
54
55     AttrImpl(DocumentImpl document, String name) {
56         super(document);
57
58         this.namespaceAware = false;
59
60         int prefixSeparator = name.lastIndexOf(":");
61         if (prefixSeparator != -1) {
62             String prefix = name.substring(0, prefixSeparator);
63             String localName = name.substring(prefixSeparator + 1);
64
65             if (!DocumentImpl.isXMLIdentifier(prefix) || !DocumentImpl.isXMLIdentifier(localName)) {
66                 throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name);
67             }
68         } else {
69             if (!DocumentImpl.isXMLIdentifier(name)) {
70                 throw new DOMException(DOMException.INVALID_CHARACTER_ERR, name);
71             }
72         }
73
74         this.localName = name;
75     }
76
77     @Override
78     public String getLocalName() {
79         return namespaceAware ? localName : null;
80     }
81
82     public String getName() {
83         return prefix != null
84                 ? prefix + ":" + localName
85                 : localName;
86     }
87
88     @Override
89     public String getNamespaceURI() {
90         return namespaceURI;
91     }
92
93     @Override
94     public String getNodeName() {
95         return getName();
96     }
97
98     public short getNodeType() {
99         return Node.ATTRIBUTE_NODE;
100     }
101
102     @Override
103     public String getNodeValue() {
104         return getValue();
105     }
106
107     public Element getOwnerElement() {
108         return ownerElement;
109     }
110
111     @Override
112     public String getPrefix() {
113         return prefix;
114     }
115
116     public boolean getSpecified() {
117         return value != null;
118     }
119
120     public String getValue() {
121         return value;
122     }
123
124     @Override
125     public void setPrefix(String prefix) {
126         this.prefix = validatePrefix(prefix, namespaceAware, namespaceURI);
127     }
128
129     public void setValue(String value) throws DOMException {
130         this.value = value;
131     }
132
133     public TypeInfo getSchemaTypeInfo() {
134         // TODO: populate this when we support XML Schema
135         return NULL_TYPE_INFO;
136     }
137
138     public boolean isId() {
139         return isId;
140     }
141 }