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.modules.xml.lib;
042:
043: import java.io.Reader;
044: import java.io.StringReader;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047: import java.io.EOFException;
048:
049: import javax.swing.text.Document;
050:
051: import org.xml.sax.InputSource;
052:
053: import org.netbeans.modules.xml.text.TextEditorSupport;
054: import org.openide.ErrorManager;
055: import org.openide.util.Lookup;
056: import java.net.URL;
057:
058: import org.openide.filesystems.FileObject;
059: import org.openide.loaders.DataObject;
060:
061: /**
062: * Set of static methods converting misc data representations.
063: *
064: * @author Petr Kuzel
065: * @version 0.9
066: */
067: public final class Convertors {
068:
069: /**
070: * @return current state of Document as string
071: */
072: public static String documentToString(final Document doc) {
073:
074: if (doc == null)
075: throw new NullPointerException();
076:
077: final String[] str = new String[1];
078:
079: // safely take the text from the document
080: Runnable run = new Runnable() {
081: public void run() {
082: try {
083: str[0] = doc.getText(0, doc.getLength());
084: } catch (javax.swing.text.BadLocationException e) {
085: // impossible
086: e.printStackTrace();
087: }
088: }
089: };
090:
091: doc.render(run);
092: return str[0];
093:
094: }
095:
096: /**
097: * @return InputSource, a callie SHOULD set systemId if available
098: */
099: public static InputSource documentToInputSource(Document doc) {
100:
101: if (doc == null)
102: throw new NullPointerException();
103:
104: String text = documentToString(doc);
105: Reader reader = new StringReader(text);
106:
107: // our specifics property
108: String system = (String) doc
109: .getProperty(TextEditorSupport.PROP_DOCUMENT_URL);
110:
111: // try Swing general property
112: if (system == null) {
113: Object obj = doc
114: .getProperty(Document.StreamDescriptionProperty);
115: if (obj instanceof DataObject) {
116: try {
117: DataObject dobj = (DataObject) obj;
118: FileObject fo = dobj.getPrimaryFile();
119: URL url = fo.getURL();
120: system = url.toExternalForm();
121: } catch (IOException io) {
122: ErrorManager emgr = Lookup.getDefault().lookup(
123: ErrorManager.class);
124: emgr.notify(io);
125: }
126: } else {
127: ErrorManager emgr = Lookup.getDefault().lookup(
128: ErrorManager.class);
129: emgr.log("XML:Convertors:Unknown stream description:"
130: + obj);
131: }
132: }
133:
134: // set something, some parsers are nervous if no system id
135: if (system == null) {
136: system = "XML/Convertors/documentToInputSource()"; //NOI18N
137: }
138:
139: InputSource in = new InputSource(system); // NOI18N
140: in.setCharacterStream(reader);
141: return in;
142: }
143:
144: /**
145: * Wrap reader into buffered one and start reading returning
146: * String as a EOF is reached.
147: */
148: public static String readerToString(Reader reader)
149: throws IOException {
150:
151: BufferedReader fastReader = new BufferedReader(reader);
152: StringBuffer buf = new StringBuffer(1024);
153: try {
154: for (int i = fastReader.read(); i >= 0; i = fastReader
155: .read()) {
156: buf.append((char) i);
157: }
158: } catch (EOFException eof) {
159: //expected
160: }
161:
162: return buf.toString();
163: }
164:
165: }
|