View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.PrintWriter;
18  
19  import org.htmlunit.SgmlPage;
20  import org.w3c.dom.DOMException;
21  import org.w3c.dom.ProcessingInstruction;
22  
23  /**
24   * Wrapper for the DOM node ProcessingInstruction.
25   *
26   * @author Ahmed Ashour
27   */
28  public class DomProcessingInstruction extends DomNode implements ProcessingInstruction {
29  
30      private final String target_;
31      private String data_;
32  
33      /**
34       * Creates a new instance.
35       *
36       * @param page the Page that contains this element
37       * @param target the target
38       * @param data the data
39       */
40      public DomProcessingInstruction(final SgmlPage page, final String target, final String data) {
41          super(page);
42          target_ = target;
43          setData(data);
44      }
45  
46      /**
47       * {@inheritDoc}
48       * @return the node type constant, in this case {@link org.w3c.dom.Node#PROCESSING_INSTRUCTION_NODE}
49       */
50      @Override
51      public short getNodeType() {
52          return org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String getNodeName() {
60          return target_;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public String getTarget() {
68          return getNodeName();
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public String getData() {
76          return getNodeValue();
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public void setData(final String data) throws DOMException {
84          setNodeValue(data);
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void setNodeValue(final String value) {
92          data_ = value;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public String getNodeValue() {
100         return data_;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void setTextContent(final String textContent) {
108         setNodeValue(textContent);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     protected void printXml(final String indent, final PrintWriter printWriter) {
116         printWriter.print("<?");
117         printWriter.print(getTarget());
118         printWriter.print(" ");
119         printWriter.print(getData());
120         printWriter.print("?>");
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public void setPrefix(final String prefix) {
128         // Empty.
129     }
130 }