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