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.Comment;
21  
22  /**
23   * Wrapper for the DOM node Comment.
24   *
25   * @author Karel Kolman
26   * @author Ahmed Ashour
27   */
28  public class DomComment extends DomCharacterData implements Comment {
29  
30      /** The symbolic node name. */
31      public static final String NODE_NAME = "#comment";
32  
33      /**
34       * Creates an instance of DomComment.
35       *
36       * @param page the Page that contains this element
37       * @param data the string data held by this node
38       */
39      public DomComment(final SgmlPage page, final String data) {
40          super(page, data);
41      }
42  
43      /**
44       * {@inheritDoc}
45       * @return the node type constant, in this case {@link org.w3c.dom.Node#COMMENT_NODE}
46       */
47      @Override
48      public short getNodeType() {
49          return COMMENT_NODE;
50      }
51  
52      /**
53       * @return the node name, in this case {@link #NODE_NAME}
54       */
55      @Override
56      public String getNodeName() {
57          return NODE_NAME;
58      }
59  
60      /**
61       * Recursively write the XML data for the node tree starting at <code>node</code>.
62       *
63       * @param indent white space to indent child nodes
64       * @param printWriter writer where child nodes are written
65       */
66      @Override
67      protected void printXml(final String indent, final PrintWriter printWriter) {
68          printWriter.print(indent);
69          printWriter.print("<!--");
70          printWriter.print(getData());
71          printWriter.print("-->");
72          printChildrenAsXml(indent, printWriter);
73      }
74  
75      /**
76       * Returns a simple string representation to facilitate debugging.
77       * @return a simple string representation
78       */
79      @Override
80      public String toString() {
81          return asXml();
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void setPrefix(final String prefix) {
89          // Empty.
90      }
91  }