KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > io > Convertors


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.io;
20
21 import java.io.Reader JavaDoc;
22 import java.io.StringReader JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.EOFException JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28
29 import javax.swing.text.Document JavaDoc;
30
31 import org.xml.sax.InputSource JavaDoc;
32
33 import org.netbeans.tax.TreeException;
34 import org.netbeans.tax.TreeDocumentRoot;
35
36 /**
37  * Set of static methods converting misc data representations.
38  *
39  * @author Petr Kuzel
40  * @version 0.9
41  */

42 public final class Convertors {
43
44
45     /**
46      * @return current state of Document as string
47      */

48     public static String JavaDoc documentToString (final Document JavaDoc doc) {
49         
50         final String JavaDoc[] str = new String JavaDoc[1];
51         
52         // safely take the text from the document
53
Runnable JavaDoc run = new Runnable JavaDoc () {
54             public void run () {
55                 try {
56                     str[0] = doc.getText (0, doc.getLength ());
57                 } catch (javax.swing.text.BadLocationException JavaDoc e) {
58                     // impossible
59
e.printStackTrace ();
60                 }
61             }
62         };
63         
64         doc.render (run);
65         return str[0];
66         
67     }
68     
69     /**
70      * @return InputSource, a callie SHOULD set systemId if available
71      */

72     public static InputSource JavaDoc documentToInputSource (Document JavaDoc doc) {
73         String JavaDoc text = documentToString (doc);
74         Reader JavaDoc reader = new StringReader JavaDoc (text);
75         InputSource JavaDoc in = new InputSource JavaDoc ("StringReader"); // NOI18N
76
in.setCharacterStream (reader);
77         return in;
78     }
79     
80     
81     /**
82      * Wrap reader into buffered one and start reading returning
83      * String as a EOF is reached.
84      */

85     public static String JavaDoc readerToString (Reader JavaDoc reader) throws IOException JavaDoc {
86         
87         BufferedReader JavaDoc fastReader = new BufferedReader JavaDoc (reader);
88         StringBuffer JavaDoc buf = new StringBuffer JavaDoc (1024);
89         try {
90             for (int i = fastReader.read (); i >= 0; i = fastReader.read ()) {
91                 buf.append ((char)i);
92             }
93         } catch (EOFException JavaDoc eof) {
94             //expected
95
}
96         
97         return buf.toString ();
98     }
99     
100     /*
101      *
102      */

103     public static String JavaDoc treeToString (TreeDocumentRoot doc) throws IOException JavaDoc {
104         
105         StringWriter JavaDoc out = new StringWriter JavaDoc ();
106         TreeStreamResult result = new TreeStreamResult (out);
107         TreeWriter writer = result.getWriter (doc);
108         
109         try {
110             writer.writeDocument ();
111             return out.toString ();
112         } catch (TreeException ex) {
113             throw new IOException JavaDoc ("Cannot read tree " + ex.getMessage ()); // NOI18N
114

115         } finally {
116             try {
117                 out.close ();
118             } catch (IOException JavaDoc ioex) {
119                 // do not know
120
}
121         }
122         
123     }
124     
125     public static byte[] treeToByteArray (TreeDocumentRoot doc) throws IOException JavaDoc {
126         
127         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc (1024 * 8);
128         TreeStreamResult result = new TreeStreamResult (out);
129         TreeWriter writer = result.getWriter (doc);
130         
131         try {
132             writer.writeDocument ();
133             byte[] array = out.toByteArray ();
134             return array;
135         } catch (TreeException ex) {
136             throw new IOException JavaDoc ("Cannot read tree " + ex.getMessage ()); // NOI18N
137

138         } finally {
139             try {
140                 out.close ();
141             } catch (IOException JavaDoc ioex) {
142                 // do not know
143
}
144         }
145     }
146 }
147
Popular Tags