KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > xml > parsers > DocumentInputSource


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
20 package org.netbeans.api.xml.parsers;
21
22 import java.io.*;
23 import java.net.URL JavaDoc;
24 import java.util.*;
25
26 import javax.swing.text.Document JavaDoc;
27
28 import org.xml.sax.InputSource JavaDoc;
29
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.util.Lookup;
34
35 /**
36  * Integrate NetBeans widely used Swing's {@link Document} with SAX API's.
37  * Let it look like {@link InputSource}.
38  *
39  * @author Petr Kuzel
40  */

41 public final class DocumentInputSource extends InputSource JavaDoc {
42
43     private final Document JavaDoc doc;
44      
45     /**
46      * Creates new instance of <code>DocumentInputSource</code>. Client should
47      * set system ID if available otherwise default one is derived.
48      * @param doc Swing document used to be wrapped
49      * @see #getSystemId()
50      */

51     public DocumentInputSource(Document JavaDoc doc) {
52         this.doc = doc;
53     }
54
55     // inherit JavaDoc
56
public Reader getCharacterStream() {
57         String JavaDoc text = documentToString(doc);
58         return new StringReader(text);
59     }
60
61     /**
62      * This <code>InputSource</code> is backended by Swing's <code>Document</code>.
63      * Consequently its character stream is read-only, it
64      * always reads content of associted <code>Document</code>.
65      */

66     public final void setCharacterStream(Reader reader) {
67         // do nothing
68
}
69
70     /**
71      * Get InputSource system ID. Use ordered logic:
72      * <ul>
73      * <li>use client's <code>setSystemId()</code>, or
74      * <li>try to derive it from <code>Document</code>
75      * <p>e.g. look at <code>Document.StreamDescriptionProperty</code> for
76      * {@link DataObject} and use URL of its primary file.
77      * </ul>
78      * @return entity system Id or <code>null</code>
79      */

80     public String JavaDoc getSystemId() {
81         
82         String JavaDoc system = super.getSystemId();;
83         
84         // XML module specifics property, promote into this API
85
// String system = (String) doc.getProperty(TextEditorSupport.PROP_DOCUMENT_URL);
86

87         if (system == null) {
88             Object JavaDoc obj = doc.getProperty(Document.StreamDescriptionProperty);
89             if (obj instanceof DataObject) {
90                 try {
91                         DataObject dobj = (DataObject) obj;
92                         FileObject fo = dobj.getPrimaryFile();
93                         URL JavaDoc url = fo.getURL();
94                         system = url.toExternalForm();
95                 } catch (IOException io) {
96                     ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
97                     emgr.notify(io);
98                 }
99             } else {
100                 ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
101                 emgr.log("XML:DocumentInputSource:Unknown stream description:" + obj);
102             }
103         }
104         
105         return system;
106     }
107         
108     
109     /**
110      * @return current state of Document as string
111      */

112     private static String JavaDoc documentToString(final Document JavaDoc doc) {
113         
114         final String JavaDoc[] str = new String JavaDoc[1];
115
116         // safely take the text from the document
117
Runnable JavaDoc run = new Runnable JavaDoc() {
118             public void run () {
119                 try {
120                     str[0] = doc.getText(0, doc.getLength());
121                 } catch (javax.swing.text.BadLocationException JavaDoc e) {
122                     // impossible
123
e.printStackTrace();
124                 }
125             }
126         };
127
128         doc.render(run);
129         return str[0];
130         
131     }
132     
133     /**
134      * For debugging purposes only.
135      */

136     public String JavaDoc toString() {
137         return "DocumentInputSource SID:" + getSystemId();
138     }
139 }
140
Popular Tags