1 14 15 package org.netbeans.modules.xslt.model.impl; 16 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 import java.io.IOException ; 20 import java.net.URI ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import javax.swing.text.Document ; 24 25 import org.netbeans.editor.BaseDocument; 26 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl; 27 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl; 28 import org.netbeans.modules.xml.text.syntax.XMLKit; 29 import org.netbeans.modules.xml.xam.locator.CatalogModel; 30 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 31 import org.netbeans.modules.xml.xam.ModelSource; 32 import org.netbeans.modules.xslt.model.XslModel; 33 import org.netbeans.modules.xslt.model.spi.XslModelFactory; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 import org.openide.util.Lookup; 39 import org.openide.util.lookup.Lookups; 40 41 44 45 public class TestCatalogModel extends CatalogWriteModelImpl { 46 47 private TestCatalogModel( File file ) throws IOException { 48 super(file); 49 } 50 51 public static TestCatalogModel getDefault() { 52 return INSTANCE; 53 } 54 55 70 71 public ModelSource createTestModelSource( FileObject fo, boolean editable ) 72 throws CatalogModelException 73 { 74 final DataObject dobj; 75 final CatalogModel catalogModel = createCatalogModel(fo); 76 try { 77 dobj = DataObject.find(fo); 78 } 79 catch (DataObjectNotFoundException ex) { 80 throw new CatalogModelException(ex); 81 } 82 Lookup lookup = Lookups.proxy(new Lookup.Provider() { 83 84 public Lookup getLookup() { 85 return Lookups 86 .fixed(new Object [] { dobj.getPrimaryFile(), 87 getDocument(dobj.getPrimaryFile()), dobj, 88 catalogModel }); 89 } 90 }); 91 return new ModelSource(lookup, editable); 92 } 93 94 public XslModel getXslModel( URI locationURI , boolean fresh ) 95 throws CatalogModelException 96 { 97 ModelSource source = INSTANCE.getModelSource(locationURI); 98 XslModelFactory factory = XslModelFactory.XslModelFactoryAccess.getFactory(); 99 XslModelFactoryImpl impl = (XslModelFactoryImpl) factory; 100 XslModel model; 101 if ( fresh ) { 102 model = impl.createFreshModel(source); 103 } 104 else { 105 model = impl.getModel(source); 106 } 107 return model; 108 } 109 110 public XslModel getXslModel( URI locationURI ) throws CatalogModelException { 111 return getXslModel(locationURI, false); 112 } 113 114 protected CatalogModel createCatalogModel( FileObject fo ) 115 throws CatalogModelException 116 { 117 return getDefault(); 118 } 119 120 126 protected ModelSource createModelSource( final FileObject thisFileObj, 127 boolean editable ) throws CatalogModelException 128 { 129 assert thisFileObj != null : "Null file object."; 130 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 131 final DataObject dobj; 132 try { 133 dobj = DataObject.find(thisFileObj); 134 } 135 catch (DataObjectNotFoundException ex) { 136 throw new CatalogModelException(ex); 137 } 138 Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() { 139 140 public Lookup getLookup() { 141 Document document = null; 142 document = getDocument(thisFileObj); 143 return Lookups.fixed(new Object [] { 144 FileUtil.toFile(thisFileObj), thisFileObj, document, 145 dobj, catalogModel }); 146 } 147 }); 148 return new ModelSource(proxyLookup, editable); 149 } 150 151 private Document getDocument( FileObject fo ) { 152 Document result = null; 153 if (documentPooling) { 154 result = documentPool().get(fo); 155 } 156 if (result != null) 157 return result; 158 try { 159 File file = FileUtil.toFile(fo); 160 FileInputStream fis = new FileInputStream (file); 161 byte buffer[] = new byte[fis.available()]; 162 result = new BaseDocument(XMLKit.class, false); 163 result.remove(0, result.getLength()); 164 fis.read(buffer); 165 fis.close(); 166 String str = new String (buffer); 167 result.insertString(0, str, null); 168 169 } 170 catch (Exception e) { 171 return null; 172 } 173 if (documentPooling) { 174 documentPool().put(fo, result); 175 } 176 return result; 177 } 178 179 private static void initCatalogFile() throws Exception { 180 184 } 185 186 private Map <FileObject, Document > documentPool() { 187 if (myFileToDocumentMap == null) { 188 myFileToDocumentMap = new HashMap <FileObject, Document >(); 189 } 190 return myFileToDocumentMap; 191 } 192 193 public void setDocumentPooling( boolean v ) { 194 documentPooling = v; 195 if (!documentPooling) { 196 clearDocumentPool(); 197 } 198 } 199 200 public void clearDocumentPool() { 201 myFileToDocumentMap = null; 202 } 203 204 private static TestCatalogModel INSTANCE = null; 205 206 private boolean documentPooling = true; 207 208 private Map <FileObject, Document > myFileToDocumentMap; 209 210 static { 211 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 212 try { 213 INSTANCE = new TestCatalogModel(Utils 214 .getTempDir("xslttest/catalog")); 215 FileObject catalogFO = INSTANCE.getCatalogFileObject(); 216 File catFile = FileUtil.toFile(catalogFO); 217 catFile.deleteOnExit(); 218 initCatalogFile(); 219 } 220 catch (Exception ex) { 221 ex.printStackTrace(); 222 } 223 } 224 } 225 | Popular Tags |