1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 import java.net.URI ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import java.util.logging.Logger ; 38 import javax.swing.text.Document ; 39 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl; 40 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl; 41 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 42 import org.netbeans.modules.xml.xam.ModelSource; 43 import org.netbeans.modules.xml.xam.locator.CatalogModel; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.util.Lookup; 49 import org.openide.util.lookup.Lookups; 50 51 55 56 public class TestCatalogModel extends CatalogWriteModelImpl { 57 58 private TestCatalogModel(File file) throws IOException { 59 super(file); 60 } 61 62 static TestCatalogModel singletonCatMod = null; 63 public static TestCatalogModel getDefault(){ 64 if (singletonCatMod == null){ 65 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 66 try { 67 File catalogFolder = new File ("c:/wsdlui"); 70 catalogFolder.createNewFile(); 71 singletonCatMod = new TestCatalogModel(catalogFolder); 72 FileObject catalogFO = singletonCatMod.getCatalogFileObject(); 73 File catFile = FileUtil.toFile(catalogFO); 74 catFile.deleteOnExit(); 75 } catch (Exception ex) { 77 ex.printStackTrace(); 78 return null; 79 } 80 } 81 return singletonCatMod; 82 } 83 84 85 86 91 protected ModelSource createModelSource(FileObject thisFileObj, boolean editable) throws CatalogModelException{ 92 assert thisFileObj != null : "Null file object."; 93 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 94 final DataObject dobj; 95 try { 96 dobj = DataObject.find(thisFileObj); 97 } catch (DataObjectNotFoundException ex) { 98 throw new CatalogModelException(ex); 99 } 100 Lookup proxyLookup = Lookups.proxy( 101 new Lookup.Provider() { 102 public Lookup getLookup() { 103 Document document = null; 104 Logger l = Logger.getLogger(getClass().getName()); 105 document = getDocument(dobj.getPrimaryFile()); 106 return Lookups.fixed(new Object [] { 107 dobj.getPrimaryFile(), 108 document, 109 dobj, 110 catalogModel 111 }); 112 } 113 } 114 ); 115 return new ModelSource(proxyLookup, editable); 116 } 117 118 private Document getDocument(FileObject fo){ 119 Document result = null; 120 if (documentPooling) { 121 result = documentPool().get(fo); 122 } 123 if (result != null) return result; 124 try { 125 126 File file = FileUtil.toFile(fo); 127 FileInputStream fis = new FileInputStream (file); 128 byte buffer[] = new byte[fis.available()]; 129 result = new org.netbeans.editor.BaseDocument( 130 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 131 result.remove(0, result.getLength()); 132 fis.read(buffer); 133 fis.close(); 134 String str = new String (buffer); 135 result.insertString(0,str,null); 136 137 } catch (Exception dObjEx) { 138 return null; 139 } 140 if (documentPooling) { 141 documentPool().put(fo, result); 142 } 143 return result; 144 } 145 146 protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 147 return getDefault(); 148 } 149 150 public ModelSource createTestModelSource(FileObject fo, boolean editable) throws CatalogModelException{ 151 final DataObject dobj; 152 final CatalogModel catalogModel = createCatalogModel(fo); 153 try { 154 dobj = DataObject.find(fo); 155 } catch (DataObjectNotFoundException ex) { 156 throw new CatalogModelException(ex); 157 } 158 Lookup lookup = Lookups.proxy(new Lookup.Provider() { 159 public Lookup getLookup() { 160 return Lookups.fixed(new Object [] { 161 dobj.getPrimaryFile(), 162 getDocument(dobj.getPrimaryFile()), 163 dobj, 164 catalogModel 165 }); 166 } 167 } ); 168 return new ModelSource(lookup, editable); 169 } 170 171 private Map <FileObject,Document > fileToDocumentMap; 172 private Map <FileObject,Document > documentPool() { 173 if (fileToDocumentMap == null) { 174 fileToDocumentMap = new HashMap <FileObject,Document >(); 175 } 176 return fileToDocumentMap; 177 } 178 private boolean documentPooling = true; 179 180 public void setDocumentPooling(boolean v) { 181 documentPooling = v; 182 if (! documentPooling) { 183 clearDocumentPool(); 184 } 185 } 186 187 public void clearDocumentPool() { 188 fileToDocumentMap = null; 189 } 190 191 public String toString(){ 192 return "TestCatalogModel"+super.toString(); 193 } 194 195 } 196 197 | Popular Tags |