001    /*
002     * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons-sandbox//xmlio/src/java/org/apache/commons/xmlio/out/XMLStringWriter.java,v 1.1 2004/10/08 11:56:20 ozeigermann Exp $
003     * $Revision: 155476 $
004     * $Date: 2005-02-26 13:31:24 +0000 (Sat, 26 Feb 2005) $
005     *
006     * ====================================================================
007     *
008     * Copyright 2004 The Apache Software Foundation 
009     *
010     * Licensed under the Apache License, Version 2.0 (the "License");
011     * you may not use this file except in compliance with the License.
012     * You may obtain a copy of the License at
013     *
014     *     http://www.apache.org/licenses/LICENSE-2.0
015     *
016     * Unless required by applicable law or agreed to in writing, software
017     * distributed under the License is distributed on an "AS IS" BASIS,
018     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019     * See the License for the specific language governing permissions and
020     * limitations under the License.
021     *
022     */
023    
024    package org.apache.commons.xmlio.out;
025    
026    import java.io.IOException;
027    import java.io.StringWriter;
028    
029    /**
030     * This is a convenience class for writing XML to a string. As
031     * no IOExceptions will occur this class catches them for you
032     * doing nothing. Call {@link #toString} to finally get your string.
033     * As constructor for {@link XMLWriter} already needs writer call
034     * {@link #create} to get your objects instead of consructor.
035     *
036     */
037    public class XMLStringWriter extends XMLWriter {
038    
039        /** Creates a new <code>XMLStringWriter</code> objects. */
040        public static XMLStringWriter create() {
041            return new XMLStringWriter(new StringWriter());
042        }
043    
044        private StringWriter sw;
045    
046        private XMLStringWriter(StringWriter sw) {
047            super(sw);
048            this.sw = sw;
049        }
050    
051        /** Gets the string representation of your written XML. */
052        public String toString() {
053            try {
054                flush();
055            } catch (IOException ioe) {
056                // won't happen...
057            }
058            sw.flush();
059            return sw.toString();
060        }
061    
062        public void writeXMLDeclaration() {
063            try {
064                super.writeXMLDeclaration();
065            } catch (IOException ioe) {
066                // won't happen...
067            }
068        }
069    
070        public void writeProlog(String prolog) {
071            try {
072                super.writeProlog(prolog);
073            } catch (IOException ioe) {
074                // won't happen...
075            }
076        }
077    
078        public void writeNl() {
079            try {
080                super.writeNl();
081            } catch (IOException ioe) {
082                // won't happen...
083            }
084        }
085    
086        public void writeComment(String comment) {
087            try {
088                super.writeComment(comment);
089            } catch (IOException ioe) {
090                // won't happen...
091            }
092        }
093    
094        public void writePI(String target, String data) {
095            try {
096                super.writePI(target, data);
097            } catch (IOException ioe) {
098                // won't happen...
099            }
100        }
101    
102        public void writeStartTag(String startTag, boolean nl) {
103            try {
104                super.writeStartTag(startTag, nl);
105            } catch (IOException ioe) {
106                // won't happen...
107            }
108        }
109    
110        public void writeStartTag(String startTag) {
111            try {
112                super.writeStartTag(startTag);
113            } catch (IOException ioe) {
114                // won't happen...
115            }
116        }
117    
118        public void writeEndTag(String endTag, boolean nl) {
119            try {
120                super.writeEndTag(endTag, nl);
121            } catch (IOException ioe) {
122                // won't happen...
123            }
124        }
125    
126        public void writeEndTag(String endTag) {
127            try {
128                super.writeEndTag(endTag);
129            } catch (IOException ioe) {
130                // won't happen...
131            }
132        }
133    
134        public void writeEmptyElement(String emptyTag, boolean nl) {
135            try {
136                super.writeEmptyElement(emptyTag, nl);
137            } catch (IOException ioe) {
138                // won't happen...
139            }
140        }
141    
142        public void writeEmptyElement(String emptyTag) {
143            try {
144                super.writeEmptyElement(emptyTag);
145            } catch (IOException ioe) {
146                // won't happen...
147            }
148        }
149    
150        public void writeCData(String cData) {
151            try {
152                super.writeCData(cData);
153            } catch (IOException ioe) {
154                // won't happen...
155            }
156        }
157    
158        public void writePCData(String pcData) {
159            try {
160                super.writePCData(pcData);
161            } catch (IOException ioe) {
162                // won't happen...
163            }
164        }
165    
166        public void writeElementWithCData(String startTag, String cData, String endTag) {
167            try {
168                super.writeElementWithCData(startTag, cData, endTag);
169            } catch (IOException ioe) {
170                // won't happen...
171            }
172        }
173    
174        public void writeElementWithPCData(String startTag, String pcData, String endTag) {
175            try {
176                super.writeElementWithPCData(startTag, pcData, endTag);
177            } catch (IOException ioe) {
178                // won't happen...
179            }
180        }
181    
182    }