KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > cookies > DataObjectAdapters


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.spi.xml.cookies;
21
22 import java.io.Reader JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URISyntaxException JavaDoc;
25 import java.net.URL JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.sax.SAXSource JavaDoc;
32 import org.netbeans.api.xml.parsers.DocumentInputSource;
33 import org.netbeans.api.xml.services.UserCatalog;
34 import org.openide.cookies.EditorCookie;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileStateInvalidException;
37 import org.openide.loaders.DataObject;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.SAXNotRecognizedException JavaDoc;
42 import org.xml.sax.SAXNotSupportedException JavaDoc;
43 import org.xml.sax.XMLReader JavaDoc;
44
45 /**
46  * Adapt <code>DataObject</code> to other common XML interfaces.
47  *
48  * @author Petr Kuzel
49  * @since 0.9
50  */

51 public final class DataObjectAdapters {
52     
53     /** SAX feature: Perform namespace processing. */
54     private static final String JavaDoc SAX_FEATURES_NAMESPACES = "http://xml.org/sax/features/namespaces"; // NOI18N
55

56     /** cached SAXParserFactory instance. */
57     private static SAXParserFactory JavaDoc saxParserFactory;
58     
59     private DataObjectAdapters() {
60     }
61     
62     /**
63      * Create InputSource from DataObject. Default implementation prefers opened
64      * Swing <code>Document</code> over primary file URL.
65      * @return <code>DataObject</code> never <code>null</code>
66      */

67     public static InputSource inputSource (DataObject dataObject) {
68         if (dataObject == null) throw new NullPointerException JavaDoc();
69         return new DataObjectInputSource(dataObject);
70     }
71
72     /**
73      * Lazy evaluated wrapper.
74      */

75     private static class DataObjectInputSource extends InputSource {
76         
77         private final DataObject dataObject;
78         
79         public DataObjectInputSource (DataObject dataObject) {
80             this.dataObject = dataObject;
81         }
82                 
83         public String JavaDoc getSystemId() {
84             return DataObjectAdapters.getSystemId (dataObject);
85         }
86         
87         public Reader JavaDoc getCharacterStream() {
88
89             EditorCookie editor = (EditorCookie) dataObject.getCookie(EditorCookie.class);
90
91             if (editor != null) {
92                 Document JavaDoc doc = editor.getDocument();
93                 if (doc != null) {
94                     return new DocumentInputSource(doc).getCharacterStream();
95                 }
96             }
97             
98             return null;
99         }
100         
101     }
102
103     
104     /**
105      * Create Source from DataObject. Default implementation prefers opened
106      * Swing <code>Document</code> over primary file URL.
107      * @return <code>DataObject</code> never <code>null</code>
108      */

109     public static Source JavaDoc source (DataObject dataObject) {
110         if (dataObject == null) throw new NullPointerException JavaDoc();
111         return new DataObjectSAXSource(dataObject);
112     }
113
114     /**
115      * Lazy evaluated wrapper.
116      */

117     private static class DataObjectSAXSource extends SAXSource JavaDoc {
118         
119         private final DataObject dataObject;
120         
121         public DataObjectSAXSource(DataObject dataObject) {
122             this.dataObject = dataObject;
123         }
124         
125         public String JavaDoc getSystemId() {
126             return DataObjectAdapters.getSystemId (dataObject);
127         }
128         
129         public XMLReader JavaDoc getXMLReader() {
130             try {
131                 XMLReader JavaDoc reader = newXMLReader();
132                 reader.setEntityResolver (getEntityResolver());
133                 return reader;
134             } catch (ParserConfigurationException JavaDoc ex) {
135                 Util.THIS.debug(ex);
136             } catch (SAXNotRecognizedException JavaDoc ex) {
137                 Util.THIS.debug(ex);
138             } catch (SAXNotSupportedException JavaDoc ex) {
139                 Util.THIS.debug(ex);
140             } catch (SAXException JavaDoc ex) {
141                 Util.THIS.debug(ex);
142             }
143             return null;
144         }
145         
146         public InputSource getInputSource() {
147             return inputSource (dataObject);
148         }
149
150     } // class DataObjectSAXSource
151

152
153     /** Try to find the best URL name of <code>dataObject</code>.
154      * @return system Id of <code>dataObject</code>
155      */

156     private static String JavaDoc getSystemId (DataObject dataObject) {
157         String JavaDoc systemId = null;
158         try {
159             FileObject fileObject = dataObject.getPrimaryFile();
160             URL JavaDoc url = fileObject.getURL();
161             try {
162                 systemId = new URI JavaDoc(url.toString()).toASCIIString();
163             } catch (URISyntaxException JavaDoc ex) {
164                 // if cannot be converted to URI, return at least external form
165
// instead of returning null
166
systemId = url.toExternalForm();
167                 Util.THIS.debug(ex);
168             }
169         } catch (FileStateInvalidException exc) {
170             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (exc);
171
172             // nothing to do -> return null; //???
173
}
174         return systemId;
175     }
176
177     private static synchronized SAXParserFactory JavaDoc getSAXParserFactory () throws ParserConfigurationException JavaDoc, SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
178         if ( saxParserFactory == null ) {
179             saxParserFactory = SAXParserFactory.newInstance();
180             saxParserFactory.setFeature (SAX_FEATURES_NAMESPACES, true);
181         }
182         return saxParserFactory;
183     }
184
185     /**
186      *
187      * @throws ParserConfigurationException if a parser cannot
188      * be created which satisfies the requested configuration.
189      * @throws SAXException if a parser cannot be created which satisfies the requested configuration.
190      */

191     private static XMLReader JavaDoc newXMLReader () throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
192         SAXParser JavaDoc parser = getSAXParserFactory().newSAXParser(); //!!! it is expensive!
193
return parser.getXMLReader();
194     }
195     
196     private static EntityResolver JavaDoc getEntityResolver () {
197         UserCatalog catalog = UserCatalog.getDefault();
198         EntityResolver JavaDoc res = (catalog == null ? null : catalog.getEntityResolver());
199         return res;
200     }
201 }
202
Popular Tags