KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > lib > 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.modules.xml.tools.lib;
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 import org.netbeans.tax.io.*;
36
37 /**
38  * Set of static methods converting misc data representations.
39  *
40  * @author Petr Kuzel
41  * @version 0.9
42  */

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

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

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

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

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

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

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