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