1 19 20 package org.netbeans.modules.xml.axi; 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.retriever.catalog.impl.CatalogFileWrapperDOMImpl; 31 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl; 32 import org.netbeans.modules.xml.schema.model.SchemaModel; 33 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 34 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 35 import org.netbeans.modules.xml.xam.ModelSource; 36 import org.netbeans.modules.xml.xam.locator.CatalogModel; 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 51 private TestCatalogModel(File file) throws IOException { 52 super(file); 53 } 54 55 static TestCatalogModel singletonCatMod = null; 56 public static TestCatalogModel getDefault(){ 57 if (singletonCatMod == null){ 58 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 59 try { 60 singletonCatMod = new TestCatalogModel(new File (System.getProperty("java.io.tmpdir"))); 61 FileObject catalogFO = singletonCatMod.getCatalogFileObject(); 62 File catFile = FileUtil.toFile(catalogFO); 63 catFile.deleteOnExit(); 64 } catch (Exception ex) { 66 ex.printStackTrace(); 67 return null; 68 } 69 } 70 return singletonCatMod; 71 } 72 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 165 public SchemaModel getSchemaModel(URI lcationURI) throws Exception { 166 SchemaModel model = SchemaModelFactory.getDefault().getModel( 167 singletonCatMod.getModelSource(lcationURI)); 168 if (! documentPooling) { 169 model.sync(); } 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 public String toString(){ 195 return "TestCatalogModel"+super.toString(); 196 } 197 198 public AXIModel getAXIModel(FileObject thisFileObj) throws CatalogModelException, IOException { 199 ModelSource source = createModelSource(thisFileObj, true); 200 SchemaModel model = SchemaModelFactory.getDefault().getModel(source); 201 return AXIModelFactory.getDefault().getModel(model); 202 } 203 } 204 205 | Popular Tags |