View Javadoc

1   package org.apache.velocity.anakia;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  
25  import org.jdom.Element;
26  import org.jdom.output.XMLOutputter;
27  import org.jdom.output.Format;
28  
29  /**
30   * This class extends XMLOutputter in order to provide
31   * a way to walk an Element tree into a String.
32   *
33   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
34   * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
35   * @version $Id: OutputWrapper.java 463298 2006-10-12 16:10:32Z henning $
36   */
37  public class OutputWrapper extends XMLOutputter
38  {
39      /**
40       * Empty constructor
41       */
42      public OutputWrapper()
43      {
44      }
45  
46      /**
47       * @param f
48       */
49      public OutputWrapper(Format f)
50      {
51          super(f);
52      }
53  
54      /**
55       * This method walks an Element tree into a String. The cool
56       * thing about it is that it will strip off the first Element.
57       * For example, if you have:
58       * <p>
59       * &lt;td&gt; foo &lt;strong&gt;bar&lt;/strong&gt; ack &lt;/td&gt;
60       * </p>
61       * It will output
62       * <p>
63       *  foo &lt;strong&gt;bar&lt;/strong&gt; ack &lt;/td&gt;
64       * </p>
65       * @param element
66       * @param strip
67       * @return The output string.
68       */
69      public String outputString(Element element, boolean strip)
70      {
71          StringWriter buff = new StringWriter();
72  
73          try
74          {
75              outputElementContent(element, buff);
76          }
77          catch (IOException e)
78          {
79          }
80          return buff.toString();
81      }
82  }