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