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