View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.xml;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.XMLOutput;
20  import org.apache.commons.jelly.xpath.XPathTagSupport;
21  
22  import org.xml.sax.SAXException;
23  
24  /***
25   * A tag which outputs a comment to the underlying XMLOutput based on the
26   * contents of its body.
27   *
28   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 155420 $
30   */
31  public class CommentTag extends XPathTagSupport {
32  
33      private String text;
34  
35      public CommentTag() {
36      }
37  
38      // Tag interface
39      //-------------------------------------------------------------------------
40      public void doTag(XMLOutput output) throws JellyTagException {
41          String text = getText();
42          if (text == null) {
43              text = getBodyText(false);
44          }
45          char[] ch = text.toCharArray();
46          try {
47              output.comment(ch, 0, ch.length);
48          } catch (SAXException e) {
49              throw new JellyTagException(e);
50          }
51      }
52  
53      // Properties
54      //-------------------------------------------------------------------------
55      /***
56       * Returns the text.
57       * @return String
58       */
59      public String getText() {
60          return text;
61      }
62  
63      /***
64       * Sets the comment text. If no text is specified then the body of the tag
65       * is used instead.
66       *
67       * @param text The comment text to use
68       */
69      public void setText(String text) {
70          this.text = text;
71      }
72  
73  }