1 19 package org.netbeans.modules.websvc.wsitconf.util; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.net.URI ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.logging.Logger ; 28 import javax.swing.text.Document ; 29 import org.netbeans.modules.websvc.wsitconf.util.TestCatalogModel; 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 protected Document getDocument(FileObject fo){ 75 Document result = null; 76 if (documentPooling) { 77 result = documentPool().get(fo); 78 } 79 if (result != null) return result; 80 try { 81 82 File file = FileUtil.toFile(fo); 83 FileInputStream fis = new FileInputStream (file); 84 byte buffer[] = new byte[fis.available()]; 85 result = new org.netbeans.editor.BaseDocument( 86 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 87 result.remove(0, result.getLength()); 88 fis.read(buffer); 89 fis.close(); 90 String str = new String (buffer); 91 result.insertString(0,str,null); 92 93 } catch (Exception dObjEx) { 94 return null; 95 } 96 if (documentPooling) { 97 documentPool().put(fo, result); 98 } 99 return result; 100 } 101 102 107 @Override 108 protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{ 109 assert thisFileObj != null : "Null file object."; 110 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 111 final DataObject dobj; 112 try { 113 dobj = DataObject.find(thisFileObj); 114 } catch (DataObjectNotFoundException ex) { 115 throw new CatalogModelException(ex); 116 } 117 Lookup proxyLookup = Lookups.proxy( 118 new Lookup.Provider() { 119 public Lookup getLookup() { 120 Document document = null; 121 Logger l = Logger.getLogger(getClass().getName()); 122 document = getDocument(thisFileObj); 123 return Lookups.fixed(new Object [] { 124 thisFileObj, 125 document, 126 dobj, 127 catalogModel 128 }); 129 } 130 } 131 ); 132 return new ModelSource(proxyLookup, editable); 133 } 134 135 @Override 136 protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 137 return getDefault(); 138 } 139 140 public ModelSource createTestModelSource(FileObject thisFileObj, boolean readOnly) throws CatalogModelException{ 141 Lookup lookup = Lookups.fixed(new Object []{ 142 thisFileObj, 143 getDocument(thisFileObj), 144 createCatalogModel(thisFileObj) 145 }); 146 return new ModelSource(lookup, readOnly); 147 } 148 149 public void addNamespace(NamespaceLocation nl) throws Exception { 150 this.addURI(nl.getLocationURI(), nl.getResourceURI()); 151 } 152 153 public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception { 154 return getSchemaModel(nl.getLocationURI()); 155 } 156 157 public SchemaModel getSchemaModel(URI lcationURI) throws Exception { 158 SchemaModel model = SchemaModelFactory.getDefault().getModel( 159 singletonCatMod.getModelSource(lcationURI)); 160 if (! documentPooling) { 161 model.sync(); } 163 return model; 164 } 165 166 private Map <FileObject,Document > fileToDocumentMap; 167 private Map <FileObject,Document > documentPool() { 168 if (fileToDocumentMap == null) { 169 fileToDocumentMap = new HashMap <FileObject,Document >(); 170 } 171 return fileToDocumentMap; 172 } 173 private boolean documentPooling = false; 174 175 public void setDocumentPooling(boolean v) { 176 documentPooling = v; 177 if (! documentPooling) { 178 fileToDocumentMap = null; 179 } 180 } 181 182 private static void initCatalogFile() throws Exception { 183 TestCatalogModel instance = singletonCatMod; 184 for (NamespaceLocation nl:NamespaceLocation.values()) { 185 instance.addNamespace(nl); 186 } 187 } 188 189 public WSDLModel getWSDLModel(URI locationURI) throws Exception { 190 ModelSource source = getDefault().getModelSource(locationURI); 191 WSDLModel model = WSDLModelFactory.getDefault().getModel(source); 192 return model; 193 } 194 195 public WSDLModel getWSDLModel(NamespaceLocation nl) throws Exception { 196 nl.refreshResourceFile(); 197 return getWSDLModel(nl.getLocationURI()); 198 } 199 200 @Override 201 public String toString(){ 202 return "TestCatalogModel"+super.toString(); 203 } 204 } 205 206 | Popular Tags |