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:
042: package org.netbeans.api.xml.parsers;
043:
044: import java.io.*;
045: import java.net.URL;
046: import java.util.*;
047:
048: import javax.swing.text.Document;
049:
050: import org.xml.sax.InputSource;
051:
052: import org.openide.ErrorManager;
053: import org.openide.filesystems.FileObject;
054: import org.openide.loaders.DataObject;
055: import org.openide.util.Lookup;
056:
057: /**
058: * Integrate NetBeans widely used Swing's {@link Document} with SAX API's.
059: * Let it look like {@link InputSource}.
060: *
061: * @author Petr Kuzel
062: */
063: public final class DocumentInputSource extends InputSource {
064:
065: private final Document doc;
066:
067: /**
068: * Creates new instance of <code>DocumentInputSource</code>. Client should
069: * set system ID if available otherwise default one is derived.
070: * @param doc Swing document used to be wrapped
071: * @see #getSystemId()
072: */
073: public DocumentInputSource(Document doc) {
074: this .doc = doc;
075: }
076:
077: // inherit JavaDoc
078: public Reader getCharacterStream() {
079: String text = documentToString(doc);
080: return new StringReader(text);
081: }
082:
083: /**
084: * This <code>InputSource</code> is backended by Swing's <code>Document</code>.
085: * Consequently its character stream is read-only, it
086: * always reads content of associted <code>Document</code>.
087: */
088: public final void setCharacterStream(Reader reader) {
089: // do nothing
090: }
091:
092: /**
093: * Get InputSource system ID. Use ordered logic:
094: * <ul>
095: * <li>use client's <code>setSystemId()</code>, or
096: * <li>try to derive it from <code>Document</code>
097: * <p>e.g. look at <code>Document.StreamDescriptionProperty</code> for
098: * {@link DataObject} and use URL of its primary file.
099: * </ul>
100: * @return entity system Id or <code>null</code>
101: */
102: public String getSystemId() {
103:
104: String system = super .getSystemId();
105: ;
106:
107: // XML module specifics property, promote into this API
108: // String system = (String) doc.getProperty(TextEditorSupport.PROP_DOCUMENT_URL);
109:
110: if (system == null) {
111: Object obj = doc
112: .getProperty(Document.StreamDescriptionProperty);
113: if (obj instanceof DataObject) {
114: try {
115: DataObject dobj = (DataObject) obj;
116: FileObject fo = dobj.getPrimaryFile();
117: URL url = fo.getURL();
118: system = url.toExternalForm();
119: } catch (IOException io) {
120: ErrorManager emgr = (ErrorManager) Lookup
121: .getDefault().lookup(ErrorManager.class);
122: emgr.notify(io);
123: }
124: } else {
125: ErrorManager emgr = (ErrorManager) Lookup.getDefault()
126: .lookup(ErrorManager.class);
127: emgr
128: .log("XML:DocumentInputSource:Unknown stream description:"
129: + obj);
130: }
131: }
132:
133: return system;
134: }
135:
136: /**
137: * @return current state of Document as string
138: */
139: private static String documentToString(final Document doc) {
140:
141: final String[] str = new String[1];
142:
143: // safely take the text from the document
144: Runnable run = new Runnable() {
145: public void run() {
146: try {
147: str[0] = doc.getText(0, doc.getLength());
148: } catch (javax.swing.text.BadLocationException e) {
149: // impossible
150: e.printStackTrace();
151: }
152: }
153: };
154:
155: doc.render(run);
156: return str[0];
157:
158: }
159:
160: /**
161: * For debugging purposes only.
162: */
163: public String toString() {
164: return "DocumentInputSource SID:" + getSystemId();
165: }
166: }
|