1 package org.netbeans.modules.websvc.wsitmodelext.util; 2 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.net.URI ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import java.util.logging.Logger ; 18 import javax.swing.text.Document ; 19 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl; 20 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl; 21 import org.netbeans.modules.xml.schema.model.SchemaModel; 22 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 23 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 24 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory; 25 import org.netbeans.modules.xml.xam.locator.CatalogModel; 26 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 27 import org.netbeans.modules.xml.xam.ModelSource; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.DataObjectNotFoundException; 32 import org.openide.util.Lookup; 33 import org.openide.util.lookup.Lookups; 34 35 39 40 public class TestCatalogModel extends CatalogWriteModelImpl{ 41 private TestCatalogModel(File file) throws IOException { 42 super(file); 43 } 44 45 static TestCatalogModel singletonCatMod = null; 46 public static TestCatalogModel getDefault(){ 47 if (singletonCatMod == null){ 48 CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true; 49 try { 50 singletonCatMod = new TestCatalogModel(new File (System.getProperty("java.io.tmpdir"))); 51 FileObject catalogFO = singletonCatMod.getCatalogFileObject(); 52 File catFile = FileUtil.toFile(catalogFO); 53 catFile.deleteOnExit(); 54 initCatalogFile(); 55 } catch (Exception ex) { 56 ex.printStackTrace(); 57 return null; 58 } 59 } 60 return singletonCatMod; 61 } 62 63 protected Document getDocument(FileObject fo){ 64 Document result = null; 65 if (documentPooling) { 66 result = documentPool().get(fo); 67 } 68 if (result != null) return result; 69 try { 70 71 File file = FileUtil.toFile(fo); 72 FileInputStream fis = new FileInputStream (file); 73 byte buffer[] = new byte[fis.available()]; 74 result = new org.netbeans.editor.BaseDocument( 75 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 76 result.remove(0, result.getLength()); 77 fis.read(buffer); 78 fis.close(); 79 String str = new String (buffer); 80 result.insertString(0,str,null); 81 82 } catch (Exception dObjEx) { 83 return null; 84 } 85 if (documentPooling) { 86 documentPool().put(fo, result); 87 } 88 return result; 89 } 90 91 96 protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{ 97 assert thisFileObj != null : "Null file object."; 98 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 99 final DataObject dobj; 100 try { 101 dobj = DataObject.find(thisFileObj); 102 } catch (DataObjectNotFoundException ex) { 103 throw new CatalogModelException(ex); 104 } 105 Lookup proxyLookup = Lookups.proxy( 106 new Lookup.Provider() { 107 public Lookup getLookup() { 108 Document document = null; 109 Logger l = Logger.getLogger(getClass().getName()); 110 document = getDocument(thisFileObj); 111 return Lookups.fixed(new Object [] { 112 thisFileObj, 113 document, 114 dobj, 115 catalogModel 116 }); 117 } 118 } 119 ); 120 return new ModelSource(proxyLookup, editable); 121 } 122 123 protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 124 return getDefault(); 125 } 126 127 public ModelSource createTestModelSource(FileObject thisFileObj, boolean readOnly) throws CatalogModelException{ 128 Lookup lookup = Lookups.fixed(new Object []{ 129 thisFileObj, 130 getDocument(thisFileObj), 131 createCatalogModel(thisFileObj) 132 }); 133 return new ModelSource(lookup, readOnly); 134 } 135 136 public void addNamespace(NamespaceLocation nl) throws Exception { 137 this.addURI(nl.getLocationURI(), nl.getResourceURI()); 138 } 139 140 public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception { 141 return getSchemaModel(nl.getLocationURI()); 142 } 143 144 public SchemaModel getSchemaModel(URI lcationURI) throws Exception { 145 SchemaModel model = SchemaModelFactory.getDefault().getModel( 146 singletonCatMod.getModelSource(lcationURI)); 147 if (! documentPooling) { 148 model.sync(); } 150 return model; 151 } 152 153 private Map <FileObject,Document > fileToDocumentMap; 154 private Map <FileObject,Document > documentPool() { 155 if (fileToDocumentMap == null) { 156 fileToDocumentMap = new HashMap <FileObject,Document >(); 157 } 158 return fileToDocumentMap; 159 } 160 private boolean documentPooling = false; 161 162 public void setDocumentPooling(boolean v) { 163 documentPooling = v; 164 if (! documentPooling) { 165 fileToDocumentMap = null; 166 } 167 } 168 169 private static void initCatalogFile() throws Exception { 170 TestCatalogModel instance = singletonCatMod; 171 for (NamespaceLocation nl:NamespaceLocation.values()) { 172 instance.addNamespace(nl); 173 } 174 } 175 176 public WSDLModel getWSDLModel(URI locationURI) throws Exception { 177 ModelSource source = getDefault().getModelSource(locationURI); 178 WSDLModel model = WSDLModelFactory.getDefault().getModel(source); 179 return model; 180 } 181 182 public WSDLModel getWSDLModel(NamespaceLocation nl) throws Exception { 183 nl.refreshResourceFile(); 184 return getWSDLModel(nl.getLocationURI()); 185 } 186 187 public String toString(){ 188 return "TestCatalogModel"+super.toString(); 189 } 190 } 191 192 | Popular Tags |