View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
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    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.io.IOException;
18  import java.io.ObjectInputStream;
19  
20  import org.htmlunit.SgmlPage;
21  import org.w3c.dom.Attr;
22  import org.w3c.dom.TypeInfo;
23  
24  /**
25   * An attribute of an element. Attributes are stored in {@link HtmlElement},
26   * but the XPath engine expects attributes to be in a {@link DomNode}.
27   *
28   * @author Denis N. Antonioli
29   * @author David K. Taylor
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  public class DomAttr extends DomNamespaceNode implements Attr {
34  
35      private String value_;
36      private boolean specified_;
37  
38      /**
39       * Instantiate a new attribute.
40       *
41       * @param page the page that the attribute belongs to
42       * @param namespaceURI the namespace that defines the attribute name (maybe {@code null})
43       * @param qualifiedName the name of the attribute
44       * @param value the value of the attribute
45       * @param specified {@code true} if this attribute was explicitly given a value in the source document,
46       *        or if the application changed the value of the attribute
47       */
48      public DomAttr(final SgmlPage page, final String namespaceURI, final String qualifiedName, final String value,
49          final boolean specified) {
50          super(namespaceURI, qualifiedName, page);
51          value_ = normalizeValue(value);
52          specified_ = specified;
53      }
54  
55      // still done for backward compatibility, see DomElement.ATTRIBUTE_VALUE_EMPTY
56      @SuppressWarnings("deprecation")
57      private static String normalizeValue(final String value) {
58          if (value != null && value.isEmpty()) {
59              return DomElement.ATTRIBUTE_VALUE_EMPTY;
60          }
61          return value;
62      }
63  
64      private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
65          ois.defaultReadObject();
66          // the deserialized empty string is a new instance
67          value_ = normalizeValue(value_);
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public short getNodeType() {
75          return ATTRIBUTE_NODE;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String getNodeName() {
83          return getName();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String getNodeValue() {
91          return getValue();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getName() {
99          return getQualifiedName();
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String getValue() {
107         return value_;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void setNodeValue(final String value) {
115         setValue(value);
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void setValue(final String value) {
123         value_ = normalizeValue(value);
124         specified_ = true;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public DomElement getOwnerElement() {
132         return (DomElement) getParentNode();
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public boolean getSpecified() {
140         return specified_;
141     }
142 
143     /**
144      * {@inheritDoc}
145      * Not yet implemented.
146      */
147     @Override
148     public TypeInfo getSchemaTypeInfo() {
149         throw new UnsupportedOperationException("DomAttr.getSchemaTypeInfo is not yet implemented.");
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public boolean isId() {
157         return DomElement.ID_ATTRIBUTE.equals(getNodeName());
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public String toString() {
165         return getClass().getSimpleName() + "[name=" + getNodeName() + " value=" + getNodeValue() + "]";
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public String getCanonicalXPath() {
173         final DomNode parent = getParentNode();
174         if (parent == null) {
175             return "/@" + getName();
176         }
177         return parent.getCanonicalXPath() + "/@" + getName();
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public String getTextContent() {
185         return getNodeValue();
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public void setTextContent(final String textContent) {
193         final DomElement owner = getOwnerElement();
194         final boolean mappedElement = owner != null
195                 && getOwnerDocument() instanceof HtmlPage
196                 && (DomElement.NAME_ATTRIBUTE.equals(getName()) || DomElement.ID_ATTRIBUTE.equals(getName()));
197 
198         final HtmlPage htmlPage = mappedElement ? (HtmlPage) getPage() : null;
199 
200         if (mappedElement) {
201             htmlPage.removeMappedElement(owner, false, false);
202         }
203         setValue(textContent);
204         if (mappedElement) {
205             htmlPage.addMappedElement(owner, false);
206         }
207     }
208 }