1 19 20 package org.netbeans.modules.xml.wsdl.refactoring; 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.wsdl.model.WSDLModel; 35 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory; 36 import org.netbeans.modules.xml.xam.locator.CatalogModel; 37 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 38 import org.netbeans.modules.xml.xam.ModelSource; 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 private TestCatalogModel(File file) throws IOException { 53 super(file); 54 } 55 56 static TestCatalogModel singletonCatMod = null; 57 public static TestCatalogModel getDefault(){ 58 if (singletonCatMod == null){ 59 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 60 try { 61 singletonCatMod = new TestCatalogModel(new File (System.getProperty("java.io.tmpdir"))); 62 FileObject catalogFO = singletonCatMod.getCatalogFileObject(); 63 File catFile = FileUtil.toFile(catalogFO); 64 catFile.deleteOnExit(); 65 initCatalogFile(); 66 } catch (Exception ex) { 67 ex.printStackTrace(); 68 return null; 69 } 70 } 71 return singletonCatMod; 72 } 73 74 75 76 81 protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{ 82 assert thisFileObj != null : "Null file object."; 83 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 84 final DataObject dobj; 85 try { 86 dobj = DataObject.find(thisFileObj); 87 } catch (DataObjectNotFoundException ex) { 88 throw new CatalogModelException(ex); 89 } 90 Lookup proxyLookup = Lookups.proxy( 91 new Lookup.Provider() { 92 public Lookup getLookup() { 93 Document document = null; 94 Logger l = Logger.getLogger(getClass().getName()); 95 document = getDocument(thisFileObj); 96 return Lookups.fixed(new Object [] { 97 thisFileObj, 98 document, 99 dobj, 100 catalogModel 101 }); 102 } 103 } 104 ); 105 return new ModelSource(proxyLookup, editable); 106 } 107 108 109 private Document getDocument(FileObject fo) { 110 Document result = null; 111 if (documentPooling) { 112 result = documentPool().get(fo); 113 } 114 if (result != null) return result; 115 116 FileInputStream fis = null; 117 try { 118 File file = FileUtil.toFile(fo); 119 fis = new FileInputStream (file); 120 byte buffer[] = new byte[fis.available()]; 121 result = new org.netbeans.editor.BaseDocument( 122 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 123 result.remove(0, result.getLength()); 124 fis.read(buffer); 125 String str = new String (buffer); 126 result.insertString(0,str,null); 127 128 } catch (Exception dObjEx) { 129 return null; 130 } finally { 131 try { if (fis != null) fis.close(); } catch(IOException ioe) {} 132 } 133 if (documentPooling) { 134 documentPool().put(fo, result); 135 } 136 return result; 137 } 138 139 protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 140 return getDefault(); 141 } 142 143 public ModelSource createTestModelSource(FileObject thisFileObj, boolean readOnly) throws CatalogModelException{ 144 Lookup lookup = Lookups.fixed(new Object []{ 145 thisFileObj, 146 getDocument(thisFileObj), 147 createCatalogModel(thisFileObj) 148 }); 149 return new ModelSource(lookup, readOnly); 150 } 151 152 public void addNamespace(NamespaceLocation nl) throws Exception { 153 this.addURI(nl.getLocationURI(), nl.getResourceURI()); 154 } 155 156 public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception { 157 nl.refreshResourceFile(); 158 return getSchemaModel(nl.getLocationURI()); 159 } 160 161 public SchemaModel getSchemaModel(URI lcationURI) throws Exception { 162 SchemaModel model = SchemaModelFactory.getDefault().getModel( 163 singletonCatMod.getModelSource(lcationURI)); 164 if (! documentPooling) { 165 model.sync(); } 167 return model; 168 } 169 170 private Map <FileObject,Document > fileToDocumentMap; 171 private Map <FileObject,Document > documentPool() { 172 if (fileToDocumentMap == null) { 173 fileToDocumentMap = new HashMap <FileObject,Document >(); 174 } 175 return fileToDocumentMap; 176 } 177 private boolean documentPooling = false; 178 179 public void setDocumentPooling(boolean v) { 180 documentPooling = v; 181 if (! documentPooling) { 182 fileToDocumentMap = null; 183 } 184 } 185 186 private static void initCatalogFile() throws Exception { 187 TestCatalogModel instance = singletonCatMod; 188 for (NamespaceLocation nl:NamespaceLocation.values()) { 189 instance.addNamespace(nl); 190 } 191 } 192 193 public WSDLModel getWSDLModel(URI locationURI) throws Exception { 194 ModelSource source = getDefault().getModelSource(locationURI); 195 WSDLModel model = WSDLModelFactory.getDefault().getModel(source); 196 return model; 197 } 198 199 public WSDLModel getWSDLModel(NamespaceLocation nl) throws Exception { 200 nl.refreshResourceFile(); 201 return getWSDLModel(nl.getLocationURI()); 202 } 203 204 public String toString(){ 205 return "TestCatalogModel"+super.toString(); 206 } 207 } 208 209 | Popular Tags |