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