1 19 20 package org.netbeans.modules.xml.retriever.catalog.impl; 21 22 import java.io.BufferedInputStream ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.StyledDocument ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 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 ; 43 import org.w3c.dom.ls.DOMImplementationLS ; 44 import org.w3c.dom.ls.LSInput ; 45 import org.w3c.dom.ls.LSResourceResolver ; 46 47 51 public class LSResourceResolverImpl implements LSResourceResolver { 52 53 54 public LSResourceResolverImpl() { 55 } 56 57 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURIStr) { 58 if((systemId == null) || (systemId.trim().length() <=0 )) 60 return null; 61 URI systemIdURI = null; 62 try { 63 systemIdURI = new URI (systemId); 64 } catch (URISyntaxException ex) { 65 return null; 66 } 67 68 FileObject baseFO = null; 69 CatalogModel depRez = null; 71 try { 72 baseFO = getFileObject(baseURIStr); 73 depRez = getResolver(baseFO); 74 } catch (CatalogModelException ex) { 75 return null; 76 } catch (IOException 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 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 FileObject resultFob = (FileObject) resultMS.getLookup().lookup(FileObject.class); 98 if(resultFob == null) 99 return null; 100 101 File resultFile = FileUtil.toFile(resultFob); 103 if(resultFile == null) 104 return null; 105 106 URI resultURI = resultFile.toURI(); 108 109 110 DOMImplementation domImpl = null; 112 try { 113 114 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 115 } catch (ParserConfigurationException ex) { 116 return null; 117 } 118 DOMImplementationLS dols = (DOMImplementationLS ) domImpl.getFeature("LS","3.0"); 119 LSInput lsi = dols.createLSInput(); 120 InputStream 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 baseURIStr) throws IOException { 129 if(baseURIStr == null) 130 return null; 131 URI baseURI = null; 132 try { 133 baseURI = new URI (baseURIStr); 134 } catch (URISyntaxException ex) { 135 IOException ioe = new IOException (); 136 ioe.initCause(ex); 137 throw ioe; 138 } 139 if(baseURI.isAbsolute()){ 140 if(baseURI.getScheme().equalsIgnoreCase("file")){ File baseFile = null; 142 try{ 143 baseFile = new File (baseURI); 144 }catch(Exception e){ 145 IOException ioe = new IOException (); 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 e){ 154 IOException ioe = new IOException (); 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 getFileStreamFromDocument(File 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 sd = null; 185 try { 186 sd = thisDocumentEditorCookie.openDocument(); 187 } catch (IOException ex) { 188 return null; 189 } 190 if(sd == null) 191 return null; 192 String docContent = null; 193 try { 194 docContent = sd.getText(0, sd.getLength()); 195 } catch (BadLocationException ex) { 196 return null; 197 } 198 if(docContent == null) 199 return null; 200 BufferedInputStream bis = new BufferedInputStream (new 201 ByteArrayInputStream (docContent.getBytes())); 202 return bis; 203 }else{ 204 return null; 206 } 207 } 208 return null; 209 } 210 211 212 213 214 215 } 216 | Popular Tags |