KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > au > id > jericho > lib > html > CharStreamSourceUtil


1 // Jericho HTML Parser - Java based library for analysing and manipulating HTML
2
// Version 2.2
3
// Copyright (C) 2006 Martin Jericho
4
// http://sourceforge.net/projects/jerichohtml/
5
//
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 2.1 of the License, or (at your option) any later version.
10
// http://www.gnu.org/copyleft/lesser.html
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20

21 package au.id.jericho.lib.html;
22
23 import java.io.*;
24 import java.nio.*;
25
26 /**
27  * Contains static utility methods for manipulating the way data is retrieved from a {@link CharStreamSource} object.
28  * <p>
29  * See the documentation of the {@link CharStreamSource} class for details.
30  */

31 public final class CharStreamSourceUtil {
32     private static final int DEFAULT_ESTIMATED_MAXIMUM_OUTPUT_LENGTH=2048;
33
34     private CharStreamSourceUtil() {}
35
36     /**
37      * Returns a <code>Reader</code> that reads the output of the specified {@link CharStreamSource}.
38      * <p>
39      * The current implementation of this method simply returns <code>new StringReader(</code>{@link #toString(CharStreamSource) toString(charStreamSource)}<code>)</code>,
40      * but a future version may implement this method in a more memory efficient manner.
41      *
42      * @param charStreamSource the character stream source producing the output.
43      * @return a <code>Reader</code> that reads the output of the specified {@link CharStreamSource}.
44      */

45     public static Reader getReader(final CharStreamSource charStreamSource) {
46         return new StringReader(toString(charStreamSource));
47     }
48
49     /**
50      * Returns the output of the specified {@link CharStreamSource} as a string.
51      * <p>
52      * The current implementation of this method simply returns <code>new StringReader(</code>{@link #toString(CharStreamSource) toString(charStreamSource)}<code>)</code>,
53      * but a future version may implement this method in a more memory efficient manner.
54      *
55      * @param charStreamSource the character stream source producing the output.
56      * @return the output of the specified {@link CharStreamSource} as a string.
57      */

58     public static String JavaDoc toString(final CharStreamSource charStreamSource) {
59         long estimatedMaximumOutputLength=charStreamSource.getEstimatedMaximumOutputLength();
60         if (estimatedMaximumOutputLength==-1L) estimatedMaximumOutputLength=DEFAULT_ESTIMATED_MAXIMUM_OUTPUT_LENGTH;
61         final StringWriter writer=new StringWriter((int)(estimatedMaximumOutputLength));
62         try {
63             charStreamSource.writeTo(writer);
64         } catch (IOException ex) {throw new RuntimeException JavaDoc(ex);} // assume the IOException is not thrown explicitly by the charStreamSource.output method
65
return writer.toString();
66     }
67 }
68
Popular Tags