1 19 20 package org.netbeans.modules.xml.wsdl.model.extensions; 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 80 protected ModelSource createModelSource(final 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(thisFileObj); 95 return Lookups.fixed(new Object [] { 96 thisFileObj, 97 document, 98 dobj, 99 catalogModel 100 }); 101 } 102 } 103 ); 104 return new ModelSource(proxyLookup, editable); 105 } 106 107 public void clearDocumentPool() { 108 fileToDocumentMap = null; 109 } 110 111 private Document getDocument(FileObject fo) { 112 Document result = null; 113 if (documentPooling) { 114 result = documentPool().get(fo); 115 } 116 if (result != null) return result; 117 118 FileInputStream fis = null; 119 try { 120 File file = FileUtil.toFile(fo); 121 fis = new FileInputStream (file); 122 byte buffer[] = new byte[fis.available()]; 123 result = new org.netbeans.editor.BaseDocument( 124 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 125 result.remove(0, result.getLength()); 126 fis.read(buffer); 127 String str = new String (buffer); 128 result.insertString(0,str,null); 129 130 } catch (Exception dObjEx) { 131 return null; 132 } finally { 133 try { if (fis != null) fis.close(); } catch(IOException ioe) {} 134 } 135 if (documentPooling) { 136 documentPool().put(fo, result); 137 } 138 return result; 139 } 140 141 protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 142 return getDefault(); 143 } 144 145 public ModelSource createTestModelSource(FileObject thisFileObj, boolean readOnly) throws CatalogModelException{ 146 Lookup lookup = Lookups.fixed(new Object []{ 147 thisFileObj, 148 getDocument(thisFileObj), 149 createCatalogModel(thisFileObj) 150 }); 151 return new ModelSource(lookup, readOnly); 152 } 153 154 public void addNamespace(NamespaceLocation nl) throws Exception { 155 this.addURI(nl.getLocationURI(), nl.getResourceURI()); 156 } 157 158 public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception { 159 nl.refreshResourceFile(); 160 return getSchemaModel(nl.getLocationURI()); 161 } 162 163 public SchemaModel getSchemaModel(URI lcationURI) throws Exception { 164 SchemaModel model = SchemaModelFactory.getDefault().getModel( 165 singletonCatMod.getModelSource(lcationURI)); 166 if (! documentPooling) { 167 model.sync(); } 169 return model; 170 } 171 172 private Map <FileObject,Document > fileToDocumentMap; 173 private Map <FileObject,Document > documentPool() { 174 if (fileToDocumentMap == null) { 175 fileToDocumentMap = new HashMap <FileObject,Document >(); 176 } 177 return fileToDocumentMap; 178 } 179 private boolean documentPooling = false; 180 181 public void setDocumentPooling(boolean v) { 182 documentPooling = v; 183 if (! documentPooling) { 184 fileToDocumentMap = null; 185 } 186 } 187 188 private static void initCatalogFile() throws Exception { 189 TestCatalogModel instance = singletonCatMod; 190 for (NamespaceLocation nl:NamespaceLocation.values()) { 191 instance.addNamespace(nl); 192 } 193 } 194 195 public WSDLModel getWSDLModel(URI locationURI) throws Exception { 196 ModelSource source = getDefault().getModelSource(locationURI); 197 WSDLModel model = WSDLModelFactory.getDefault().getModel(source); 198 return model; 199 } 200 201 public WSDLModel getWSDLModel(NamespaceLocation nl) throws Exception { 202 nl.refreshResourceFile(); 203 return getWSDLModel(nl.getLocationURI()); 204 } 205 206 public String toString(){ 207 return "TestCatalogModel"+super.toString(); 208 } 209 } 210 211 | Popular Tags |