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