KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > impl > LSResourceResolverImpl


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.modules.xml.retriever.catalog.impl;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.StyledDocument JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
34 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
35 import org.netbeans.modules.xml.xam.locator.CatalogModel;
36 import org.netbeans.modules.xml.xam.ModelSource;
37 import org.openide.cookies.EditorCookie;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.w3c.dom.DOMImplementation JavaDoc;
43 import org.w3c.dom.ls.DOMImplementationLS JavaDoc;
44 import org.w3c.dom.ls.LSInput JavaDoc;
45 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
46
47 /**
48  *
49  * @author girix
50  */

51 public class LSResourceResolverImpl implements LSResourceResolver JavaDoc {
52     
53     /** Creates a new instance of LSResourceResolverImpl */
54     public LSResourceResolverImpl() {
55     }
56     
57     public LSInput JavaDoc resolveResource(String JavaDoc type, String JavaDoc namespaceURI, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc baseURIStr) {
58         //check for sanity of the systemID
59
if((systemId == null) || (systemId.trim().length() <=0 ))
60             return null;
61         URI JavaDoc systemIdURI = null;
62         try {
63             systemIdURI = new URI JavaDoc(systemId);
64         } catch (URISyntaxException JavaDoc ex) {
65             return null;
66         }
67         
68         FileObject baseFO = null;
69         //get the resolver object
70
CatalogModel depRez = null;
71         try {
72             baseFO = getFileObject(baseURIStr);
73             depRez = getResolver(baseFO);
74         } catch (CatalogModelException ex) {
75             return null;
76         } catch (IOException JavaDoc ex) {
77             return null;
78         }
79         if(depRez == null)
80             return null;
81         ModelSource baseMS = null;
82         try {
83             baseMS = org.netbeans.modules.xml.retriever.catalog.Utilities.createModelSource(baseFO, false);
84         } catch (CatalogModelException ex) {
85         }
86         //get the model source from it
87
ModelSource resultMS = null;
88         try {
89             resultMS = depRez.getModelSource(systemIdURI, baseMS);
90         } catch (CatalogModelException ex) {
91             return null;
92         }
93         if(resultMS == null)
94             return null;
95         
96         //get file object
97
FileObject resultFob = (FileObject) resultMS.getLookup().lookup(FileObject.class);
98         if(resultFob == null)
99             return null;
100         
101         //get file
102
File JavaDoc resultFile = FileUtil.toFile(resultFob);
103         if(resultFile == null)
104             return null;
105         
106         //get URI out of file
107
URI JavaDoc resultURI = resultFile.toURI();
108         
109         
110         //create LSInput object
111
DOMImplementation JavaDoc domImpl = null;
112         try {
113             
114             domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
115         } catch (ParserConfigurationException JavaDoc ex) {
116             return null;
117         }
118         DOMImplementationLS JavaDoc dols = (DOMImplementationLS JavaDoc) domImpl.getFeature("LS","3.0");
119         LSInput JavaDoc lsi = dols.createLSInput();
120         InputStream JavaDoc is = getFileStreamFromDocument(resultFile);
121         if(is != null)
122             lsi.setByteStream(is);
123         lsi.setSystemId(resultURI.toString());
124         return lsi;
125     }
126     
127     
128     private FileObject getFileObject(String JavaDoc baseURIStr) throws IOException JavaDoc{
129         if(baseURIStr == null)
130             return null;
131         URI JavaDoc baseURI = null;
132         try {
133             baseURI = new URI JavaDoc(baseURIStr);
134         } catch (URISyntaxException JavaDoc ex) {
135             IOException JavaDoc ioe = new IOException JavaDoc();
136             ioe.initCause(ex);
137             throw ioe;
138         }
139         if(baseURI.isAbsolute()){
140             if(baseURI.getScheme().equalsIgnoreCase("file")){ //NOI18N
141
File JavaDoc baseFile = null;
142                 try{
143                     baseFile = new File JavaDoc(baseURI);
144                 }catch(Exception JavaDoc e){
145                     IOException JavaDoc ioe = new IOException JavaDoc();
146                     ioe.initCause(e);
147                     throw ioe;
148                 }
149                 baseFile = FileUtil.normalizeFile(baseFile);
150                 FileObject baseFileObject = null;
151                 try{
152                     baseFileObject = FileUtil.toFileObject(baseFile);
153                 }catch(Exception JavaDoc e){
154                     IOException JavaDoc ioe = new IOException JavaDoc();
155                     ioe.initCause(e);
156                     throw ioe;
157                 }
158                 return baseFileObject;
159             }
160         }
161         return null;
162     }
163     
164     
165     private CatalogModel getResolver(FileObject baseFileObject) throws CatalogModelException{
166         if(baseFileObject != null)
167             return CatalogWriteModelFactory.getInstance().getCatalogWriteModelForProject(baseFileObject);
168         return null;
169     }
170     
171     private InputStream JavaDoc getFileStreamFromDocument(File JavaDoc resultFile) {
172         FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(resultFile));
173         if(fo != null){
174             DataObject dobj = null;
175             try {
176                 dobj = DataObject.find(fo);
177             } catch (DataObjectNotFoundException ex) {
178                 return null;
179             }
180             if(dobj.isModified()){
181                 EditorCookie thisDocumentEditorCookie = (EditorCookie)dobj.getCookie(EditorCookie.class);
182                 if(thisDocumentEditorCookie == null)
183                     return null;
184                 StyledDocument JavaDoc sd = null;
185                 try {
186                     sd = thisDocumentEditorCookie.openDocument();
187                 } catch (IOException JavaDoc ex) {
188                     return null;
189                 }
190                 if(sd == null)
191                     return null;
192                 String JavaDoc docContent = null;
193                 try {
194                     docContent = sd.getText(0, sd.getLength());
195                 } catch (BadLocationException JavaDoc ex) {
196                     return null;
197                 }
198                 if(docContent == null)
199                     return null;
200                 BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new
201                         ByteArrayInputStream JavaDoc(docContent.getBytes()));
202                 return bis;
203             }else{
204                 //return null so that the validator will use normal file path to access doc
205
return null;
206             }
207         }
208         return null;
209     }
210     
211     
212     
213     
214     
215 }
216
Popular Tags