001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax.io;
042:
043: import java.io.Reader;
044: import java.io.StringReader;
045: import java.io.StringWriter;
046: import java.io.BufferedReader;
047: import java.io.IOException;
048: import java.io.EOFException;
049: import java.io.ByteArrayOutputStream;
050:
051: import javax.swing.text.Document;
052:
053: import org.xml.sax.InputSource;
054:
055: import org.netbeans.tax.TreeException;
056: import org.netbeans.tax.TreeDocumentRoot;
057:
058: /**
059: * Set of static methods converting misc data representations.
060: *
061: * @author Petr Kuzel
062: * @version 0.9
063: */
064: public final class Convertors {
065:
066: /**
067: * @return current state of Document as string
068: */
069: public static String documentToString(final Document doc) {
070:
071: final String[] str = new String[1];
072:
073: // safely take the text from the document
074: Runnable run = new Runnable() {
075: public void run() {
076: try {
077: str[0] = doc.getText(0, doc.getLength());
078: } catch (javax.swing.text.BadLocationException e) {
079: // impossible
080: e.printStackTrace();
081: }
082: }
083: };
084:
085: doc.render(run);
086: return str[0];
087:
088: }
089:
090: /**
091: * @return InputSource, a callie SHOULD set systemId if available
092: */
093: public static InputSource documentToInputSource(Document doc) {
094: String text = documentToString(doc);
095: Reader reader = new StringReader(text);
096: InputSource in = new InputSource("StringReader"); // NOI18N
097: in.setCharacterStream(reader);
098: return in;
099: }
100:
101: /**
102: * Wrap reader into buffered one and start reading returning
103: * String as a EOF is reached.
104: */
105: public static String readerToString(Reader reader)
106: throws IOException {
107:
108: BufferedReader fastReader = new BufferedReader(reader);
109: StringBuffer buf = new StringBuffer(1024);
110: try {
111: for (int i = fastReader.read(); i >= 0; i = fastReader
112: .read()) {
113: buf.append((char) i);
114: }
115: } catch (EOFException eof) {
116: //expected
117: }
118:
119: return buf.toString();
120: }
121:
122: /*
123: *
124: */
125: public static String treeToString(TreeDocumentRoot doc)
126: throws IOException {
127:
128: StringWriter out = new StringWriter();
129: TreeStreamResult result = new TreeStreamResult(out);
130: TreeWriter writer = result.getWriter(doc);
131:
132: try {
133: writer.writeDocument();
134: return out.toString();
135: } catch (TreeException ex) {
136: throw new IOException("Cannot read tree " + ex.getMessage()); // NOI18N
137:
138: } finally {
139: try {
140: out.close();
141: } catch (IOException ioex) {
142: // do not know
143: }
144: }
145:
146: }
147:
148: public static byte[] treeToByteArray(TreeDocumentRoot doc)
149: throws IOException {
150:
151: ByteArrayOutputStream out = new ByteArrayOutputStream(1024 * 8);
152: TreeStreamResult result = new TreeStreamResult(out);
153: TreeWriter writer = result.getWriter(doc);
154:
155: try {
156: writer.writeDocument();
157: byte[] array = out.toByteArray();
158: return array;
159: } catch (TreeException ex) {
160: throw new IOException("Cannot read tree " + ex.getMessage()); // NOI18N
161:
162: } finally {
163: try {
164: out.close();
165: } catch (IOException ioex) {
166: // do not know
167: }
168: }
169: }
170: }
|