KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > parser > ParsingSupport


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.tax.parser;
20
21 import java.io.StringReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25
26 import javax.swing.text.Document JavaDoc;
27
28 import org.xml.sax.InputSource JavaDoc;
29
30 import org.openide.nodes.*; //CookieFactory
31
import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33
34 import org.netbeans.tax.TreeException;
35 import org.netbeans.tax.TreeDocumentRoot;
36
37 /**
38  * Provides support for parsing differnt sources. Subclasses must implement
39  * accordingly <code>parse(InputSource)</code> method.
40  *
41  * @author Petr Kuzel
42  * @version 0.9
43  */

44 public abstract class ParsingSupport {
45         
46     /**
47      * Parse DataObject returning its model or null (on parse failure).
48      */

49     public abstract TreeDocumentRoot parse(InputSource JavaDoc in) throws IOException JavaDoc, TreeException;
50
51     
52     /**
53      * Converts to InputSource and pass it.
54      */

55     protected TreeDocumentRoot parse(TreeDocumentRoot doc) throws IOException JavaDoc, TreeException {
56         
57         return doc;
58         
59 // InputSource in = new InputSource();
60
// in.setCharacterStream(new TreeReader(doc));
61
// return parse(in);
62
}
63
64     
65     /**
66      * Converts to InputSource and pass it.
67      */

68     protected TreeDocumentRoot parse(final Document JavaDoc doc) throws IOException JavaDoc, TreeException {
69         
70         InputSource JavaDoc in = new InputSource JavaDoc();
71         
72         // safely take the text from the document
73
//??? what about DocumentReader
74

75         final String JavaDoc[] str = new String JavaDoc[1];
76         
77         Runnable JavaDoc run = new Runnable JavaDoc() {
78             public void run () {
79                 try {
80                     str[0] = doc.getText(0, doc.getLength());
81                 } catch (javax.swing.text.BadLocationException JavaDoc e) {
82                     // impossible
83
e.printStackTrace();
84                 }
85             }
86         };
87
88         doc.render(run);
89         in.setCharacterStream(new StringReader JavaDoc(str[0]));
90         return parse(in);
91     }
92     
93     
94     /**
95      * Converts to InputSource and pass it.
96      */

97     protected TreeDocumentRoot parse(FileObject fo) throws IOException JavaDoc, TreeException{
98         
99         try {
100             URL JavaDoc url = fo.getURL();
101             InputSource JavaDoc in = new InputSource JavaDoc(url.toExternalForm()); //!!! we could try ti get encoding from MIME content type
102
in.setByteStream(fo.getInputStream());
103             return parse(in);
104             
105         } catch (IOException JavaDoc ex) {
106             ErrorManager emgr = ErrorManager.getDefault();
107             emgr.annotate(ex, Util.THIS.getString("MSG_can_not_create_URL"));
108             emgr.notify(ex);
109         }
110         return null;
111     }
112     
113 }
114
Popular Tags