KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitmodelext > util > TestCatalogModel


1 package org.netbeans.modules.websvc.wsitmodelext.util;
2 /*
3  * TestCatalogModel.java
4  *
5  * Created on April 2, 2006, 10:41 AM
6  *
7  * To change this template, choose Tools | Template Manager
8  * and open the template in the editor.
9  */

10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.logging.Logger JavaDoc;
18 import javax.swing.text.Document JavaDoc;
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 /**
36  *
37  * @author girix
38  */

39
40 public class TestCatalogModel extends CatalogWriteModelImpl{
41     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
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 JavaDoc(System.getProperty("java.io.tmpdir")));
51                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
52                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
53                 catFile.deleteOnExit();
54                 initCatalogFile();
55             } catch (Exception JavaDoc ex) {
56                 ex.printStackTrace();
57                 return null;
58             }
59         }
60         return singletonCatMod;
61     }
62     
63     protected Document JavaDoc getDocument(FileObject fo){
64         Document JavaDoc result = null;
65         if (documentPooling) {
66             result = documentPool().get(fo);
67         }
68         if (result != null) return result;
69         try {
70
71             File JavaDoc file = FileUtil.toFile(fo);
72             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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 JavaDoc str = new String JavaDoc(buffer);
80             result.insertString(0,str,null);
81             
82         } catch (Exception JavaDoc dObjEx) {
83             return null;
84         }
85         if (documentPooling) {
86             documentPool().put(fo, result);
87         }
88         return result;
89     }
90     
91     /**
92      * This method could be overridden by the Unit testcase to return a special
93      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
94      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
95      */

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 JavaDoc document = null;
109                 Logger JavaDoc l = Logger.getLogger(getClass().getName());
110                 document = getDocument(thisFileObj);
111                 return Lookups.fixed(new Object JavaDoc[] {
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 JavaDoc[]{
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 JavaDoc {
137         this.addURI(nl.getLocationURI(), nl.getResourceURI());
138     }
139     
140     public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception JavaDoc {
141         return getSchemaModel(nl.getLocationURI());
142     }
143     
144     public SchemaModel getSchemaModel(URI JavaDoc lcationURI) throws Exception JavaDoc {
145         SchemaModel model = SchemaModelFactory.getDefault().getModel(
146                 singletonCatMod.getModelSource(lcationURI));
147         if (! documentPooling) {
148             model.sync(); // resync to restored to origin content
149
}
150         return model;
151     }
152     
153     private Map JavaDoc<FileObject,Document JavaDoc> fileToDocumentMap;
154     private Map JavaDoc<FileObject,Document JavaDoc> documentPool() {
155         if (fileToDocumentMap == null) {
156             fileToDocumentMap = new HashMap JavaDoc<FileObject,Document JavaDoc>();
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 JavaDoc {
170         TestCatalogModel instance = singletonCatMod;
171         for (NamespaceLocation nl:NamespaceLocation.values()) {
172             instance.addNamespace(nl);
173         }
174     }
175     
176     public WSDLModel getWSDLModel(URI JavaDoc locationURI) throws Exception JavaDoc {
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 JavaDoc {
183         nl.refreshResourceFile();
184         return getWSDLModel(nl.getLocationURI());
185     }
186     
187     public String JavaDoc toString(){
188         return "TestCatalogModel"+super.toString();
189     }
190 }
191
192
Popular Tags