Path: blob/master/src/java.xml/share/classes/org/w3c/dom/Attr.java
40948 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* This file is available under and governed by the GNU General Public26* License version 2 only, as published by the Free Software Foundation.27* However, the following notice accompanied the original version of this28* file and, per its terms, should not be removed:29*30* Copyright (c) 2004 World Wide Web Consortium,31*32* (Massachusetts Institute of Technology, European Research Consortium for33* Informatics and Mathematics, Keio University). All Rights Reserved. This34* work is distributed under the W3C(r) Software License [1] in the hope that35* it will be useful, but WITHOUT ANY WARRANTY; without even the implied36* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.37*38* [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-2002123139*/4041package org.w3c.dom;4243/**44* The <code>Attr</code> interface represents an attribute in an45* <code>Element</code> object. Typically the allowable values for the46* attribute are defined in a schema associated with the document.47* <p><code>Attr</code> objects inherit the <code>Node</code> interface, but48* since they are not actually child nodes of the element they describe, the49* DOM does not consider them part of the document tree. Thus, the50* <code>Node</code> attributes <code>parentNode</code>,51* <code>previousSibling</code>, and <code>nextSibling</code> have a52* <code>null</code> value for <code>Attr</code> objects. The DOM takes the53* view that attributes are properties of elements rather than having a54* separate identity from the elements they are associated with; this should55* make it more efficient to implement such features as default attributes56* associated with all elements of a given type. Furthermore,57* <code>Attr</code> nodes may not be immediate children of a58* <code>DocumentFragment</code>. However, they can be associated with59* <code>Element</code> nodes contained within a60* <code>DocumentFragment</code>. In short, users and implementors of the61* DOM need to be aware that <code>Attr</code> nodes have some things in62* common with other objects inheriting the <code>Node</code> interface, but63* they also are quite distinct.64* <p>The attribute's effective value is determined as follows: if this65* attribute has been explicitly assigned any value, that value is the66* attribute's effective value; otherwise, if there is a declaration for67* this attribute, and that declaration includes a default value, then that68* default value is the attribute's effective value; otherwise, the69* attribute does not exist on this element in the structure model until it70* has been explicitly added. Note that the <code>Node.nodeValue</code>71* attribute on the <code>Attr</code> instance can also be used to retrieve72* the string version of the attribute's value(s).73* <p> If the attribute was not explicitly given a value in the instance74* document but has a default value provided by the schema associated with75* the document, an attribute node will be created with76* <code>specified</code> set to <code>false</code>. Removing attribute77* nodes for which a default value is defined in the schema generates a new78* attribute node with the default value and <code>specified</code> set to79* <code>false</code>. If validation occurred while invoking80* <code>Document.normalizeDocument()</code>, attribute nodes with81* <code>specified</code> equals to <code>false</code> are recomputed82* according to the default attribute values provided by the schema. If no83* default value is associate with this attribute in the schema, the84* attribute node is discarded.85* <p>In XML, where the value of an attribute can contain entity references,86* the child nodes of the <code>Attr</code> node may be either87* <code>Text</code> or <code>EntityReference</code> nodes (when these are88* in use; see the description of <code>EntityReference</code> for89* discussion).90* <p>The DOM Core represents all attribute values as simple strings, even if91* the DTD or schema associated with the document declares them of some92* specific type such as tokenized.93* <p>The way attribute value normalization is performed by the DOM94* implementation depends on how much the implementation knows about the95* schema in use. Typically, the <code>value</code> and96* <code>nodeValue</code> attributes of an <code>Attr</code> node initially97* returns the normalized value given by the parser. It is also the case98* after <code>Document.normalizeDocument()</code> is called (assuming the99* right options have been set). But this may not be the case after100* mutation, independently of whether the mutation is performed by setting101* the string value directly or by changing the <code>Attr</code> child102* nodes. In particular, this is true when <a href='http://www.w3.org/TR/2004/REC-xml-20040204#dt-charref'>character103* references</a> are involved, given that they are not represented in the DOM and they104* impact attribute value normalization. On the other hand, if the105* implementation knows about the schema in use when the attribute value is106* changed, and it is of a different type than CDATA, it may normalize it107* again at that time. This is especially true of specialized DOM108* implementations, such as SVG DOM implementations, which store attribute109* values in an internal form different from a string.110* <p>The following table gives some examples of the relations between the111* attribute value in the original document (parsed attribute), the value as112* exposed in the DOM, and the serialization of the value:113* <table class="striped">114* <caption>Examples of the Original, Normalized and Serialized Values </caption>115* <thead>116* <tr>117* <th scope="col">Examples</th>118* <th scope="col">Parsed119* attribute value</th>120* <th scope="col">Initial <code>Attr.value</code></th>121* <th scope="col">Serialized attribute value</th>122* </tr>123* </thead>124* <tbody>125* <tr>126* <th scope="row" valign='top' rowspan='1' colspan='1'>127* Character reference</th>128* <td valign='top' rowspan='1' colspan='1'>129* <pre>"x&#178;=5"</pre>130* </td>131* <td valign='top' rowspan='1' colspan='1'>132* <pre>"x²=5"</pre>133* </td>134* <td valign='top' rowspan='1' colspan='1'>135* <pre>"x&#178;=5"</pre>136* </td>137* </tr>138* <tr>139* <th scope="row" valign='top' rowspan='1' colspan='1'>Built-in140* character entity</th>141* <td valign='top' rowspan='1' colspan='1'>142* <pre>"y&lt;6"</pre>143* </td>144* <td valign='top' rowspan='1' colspan='1'>145* <pre>"y<6"</pre>146* </td>147* <td valign='top' rowspan='1' colspan='1'>148* <pre>"y&lt;6"</pre>149* </td>150* </tr>151* <tr>152* <th scope="row" valign='top' rowspan='1' colspan='1'>Literal newline between</th>153* <td valign='top' rowspan='1' colspan='1'>154* <pre>155* "x=5&#10;y=6"</pre>156* </td>157* <td valign='top' rowspan='1' colspan='1'>158* <pre>"x=5 y=6"</pre>159* </td>160* <td valign='top' rowspan='1' colspan='1'>161* <pre>"x=5&#10;y=6"</pre>162* </td>163* </tr>164* <tr>165* <th scope="row" valign='top' rowspan='1' colspan='1'>Normalized newline between</th>166* <td valign='top' rowspan='1' colspan='1'>167* <pre>"x=5168* y=6"</pre>169* </td>170* <td valign='top' rowspan='1' colspan='1'>171* <pre>"x=5 y=6"</pre>172* </td>173* <td valign='top' rowspan='1' colspan='1'>174* <pre>"x=5 y=6"</pre>175* </td>176* </tr>177* <tr>178* <th scope="row" valign='top' rowspan='1' colspan='1'>Entity <code>e</code> with literal newline</th>179* <td valign='top' rowspan='1' colspan='1'>180* <pre>181* <!ENTITY e '...&#10;...'> [...]> "x=5&e;y=6"</pre>182* </td>183* <td valign='top' rowspan='1' colspan='1'><em>Dependent on Implementation and Load Options</em></td>184* <td valign='top' rowspan='1' colspan='1'><em>Dependent on Implementation and Load/Save Options</em></td>185* </tr>186* </tbody>187* </table>188* <p>See also the <a href='http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407'>Document Object Model (DOM) Level 3 Core Specification</a>.189*/190public interface Attr extends Node {191/**192* Returns the name of this attribute. If <code>Node.localName</code> is193* different from <code>null</code>, this attribute is a qualified name.194*/195public String getName();196197/**198* <code>True</code> if this attribute was explicitly given a value in199* the instance document, <code>false</code> otherwise. If the200* application changed the value of this attribute node (even if it ends201* up having the same value as the default value) then it is set to202* <code>true</code>. The implementation may handle attributes with203* default values from other schemas similarly but applications should204* use <code>Document.normalizeDocument()</code> to guarantee this205* information is up-to-date.206*/207public boolean getSpecified();208209/**210* On retrieval, the value of the attribute is returned as a string.211* Character and general entity references are replaced with their212* values. See also the method <code>getAttribute</code> on the213* <code>Element</code> interface.214* <br>On setting, this creates a <code>Text</code> node with the unparsed215* contents of the string, i.e. any characters that an XML processor216* would recognize as markup are instead treated as literal text. See217* also the method <code>Element.setAttribute()</code>.218* <br> Some specialized implementations, such as some [<a href='http://www.w3.org/TR/2003/REC-SVG11-20030114/'>SVG 1.1</a>]219* implementations, may do normalization automatically, even after220* mutation; in such case, the value on retrieval may differ from the221* value on setting.222*/223public String getValue();224/**225* On retrieval, the value of the attribute is returned as a string.226* Character and general entity references are replaced with their227* values. See also the method <code>getAttribute</code> on the228* <code>Element</code> interface.229* <br>On setting, this creates a <code>Text</code> node with the unparsed230* contents of the string, i.e. any characters that an XML processor231* would recognize as markup are instead treated as literal text. See232* also the method <code>Element.setAttribute()</code>.233* <br> Some specialized implementations, such as some [<a href='http://www.w3.org/TR/2003/REC-SVG11-20030114/'>SVG 1.1</a>]234* implementations, may do normalization automatically, even after235* mutation; in such case, the value on retrieval may differ from the236* value on setting.237* @exception DOMException238* NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.239*/240public void setValue(String value)241throws DOMException;242243/**244* The <code>Element</code> node this attribute is attached to or245* <code>null</code> if this attribute is not in use.246* @since 1.4, DOM Level 2247*/248public Element getOwnerElement();249250/**251* The type information associated with this attribute. While the type252* information contained in this attribute is guarantee to be correct253* after loading the document or invoking254* <code>Document.normalizeDocument()</code>, <code>schemaTypeInfo</code>255* may not be reliable if the node was moved.256* @since 1.5, DOM Level 3257*/258public TypeInfo getSchemaTypeInfo();259260/**261* Returns whether this attribute is known to be of type ID (i.e. to262* contain an identifier for its owner element) or not. When it is and263* its value is unique, the <code>ownerElement</code> of this attribute264* can be retrieved using the method <code>Document.getElementById</code>265* . The implementation could use several ways to determine if an266* attribute node is known to contain an identifier:267* <ul>268* <li> If validation269* occurred using an XML Schema [<a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>]270* while loading the document or while invoking271* <code>Document.normalizeDocument()</code>, the post-schema-validation272* infoset contributions (PSVI contributions) values are used to273* determine if this attribute is a schema-determined ID attribute using274* the <a href='http://www.w3.org/TR/2003/REC-xptr-framework-20030325/#term-sdi'>275* schema-determined ID</a> definition in [<a href='http://www.w3.org/TR/2003/REC-xptr-framework-20030325/'>XPointer</a>]276* .277* </li>278* <li> If validation occurred using a DTD while loading the document or279* while invoking <code>Document.normalizeDocument()</code>, the infoset <b>[type definition]</b> value is used to determine if this attribute is a DTD-determined ID280* attribute using the <a href='http://www.w3.org/TR/2003/REC-xptr-framework-20030325/#term-ddi'>281* DTD-determined ID</a> definition in [<a href='http://www.w3.org/TR/2003/REC-xptr-framework-20030325/'>XPointer</a>]282* .283* </li>284* <li> from the use of the methods <code>Element.setIdAttribute()</code>,285* <code>Element.setIdAttributeNS()</code>, or286* <code>Element.setIdAttributeNode()</code>, i.e. it is an287* user-determined ID attribute;288* <p ><b>Note:</b> XPointer framework (see section 3.2 in [<a href='http://www.w3.org/TR/2003/REC-xptr-framework-20030325/'>XPointer</a>]289* ) consider the DOM user-determined ID attribute as being part of the290* XPointer externally-determined ID definition.291* </li>292* <li> using mechanisms that293* are outside the scope of this specification, it is then an294* externally-determined ID attribute. This includes using schema295* languages different from XML schema and DTD.296* </li>297* </ul>298* <br> If validation occurred while invoking299* <code>Document.normalizeDocument()</code>, all user-determined ID300* attributes are reset and all attribute nodes ID information are then301* reevaluated in accordance to the schema used. As a consequence, if302* the <code>Attr.schemaTypeInfo</code> attribute contains an ID type,303* <code>isId</code> will always return true.304* @since 1.5, DOM Level 3305*/306public boolean isId();307308}309310311