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