KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > TestCatalogModel


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

9
10 package org.netbeans.modules.xml.schema.model;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.logging.Level JavaDoc;
19 import javax.swing.text.Document JavaDoc;
20 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
21 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
22 import org.netbeans.modules.xml.xam.locator.CatalogModel;
23 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
24 import org.netbeans.modules.xml.xam.ModelSource;
25 import org.openide.cookies.SaveCookie;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30 import org.openide.util.Lookup;
31 import org.openide.util.lookup.Lookups;
32
33 /**
34  *
35  * @author girix
36  */

37
38 public class TestCatalogModel extends CatalogWriteModelImpl{
39     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
40         super(file);
41     }
42     
43     static TestCatalogModel singletonCatMod = null;
44     public static TestCatalogModel getDefault(){
45         if (singletonCatMod == null){
46             CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
47             try {
48                 singletonCatMod = new TestCatalogModel(Util.getTempDir("schematest/catalog"));
49                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
50                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
51                 catFile.deleteOnExit();
52                 initCatalogFile();
53             } catch (Exception JavaDoc ex) {
54                 ex.printStackTrace();
55                 return null;
56             }
57         }
58         return singletonCatMod;
59     }
60     
61     
62     /**
63      * This method could be overridden by the Unit testcase to return a special
64      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
65      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
66      */

67     protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
68         assert thisFileObj != null : "Null file object.";
69         final CatalogModel catalogModel = createCatalogModel(thisFileObj);
70         final DataObject dobj;
71         try {
72             dobj = DataObject.find(thisFileObj);
73         } catch (DataObjectNotFoundException ex) {
74             throw new CatalogModelException(ex);
75         }
76         Lookup proxyLookup = Lookups.proxy(
77                 new Lookup.Provider() {
78             public Lookup getLookup() {
79                 return Lookups.fixed(new Object JavaDoc[] {
80                     FileUtil.toFile(thisFileObj),
81                     thisFileObj,
82                     getDocument(dobj.getPrimaryFile()),
83                     dobj,
84                     catalogModel
85                 });
86             }
87         }
88         );
89         return new ModelSource(proxyLookup, editable);
90     }
91     
92     private Document JavaDoc getDocument(FileObject fo){
93         Document JavaDoc result = null;
94         if (documentPooling) {
95             result = documentPool().get(fo);
96         }
97         if (result != null) return result;
98         try {
99             
100             File JavaDoc file = FileUtil.toFile(fo);
101             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
102             byte buffer[] = new byte[fis.available()];
103             result = new org.netbeans.editor.BaseDocument(
104                     org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
105             result.remove(0, result.getLength());
106             fis.read(buffer);
107             fis.close();
108             String JavaDoc str = new String JavaDoc(buffer);
109             result.insertString(0,str,null);
110             
111         } catch (Exception JavaDoc dObjEx) {
112             return null;
113         }
114         if (documentPooling) {
115             documentPool().put(fo, result);
116         }
117         return result;
118     }
119     
120     protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{
121         return getDefault();
122     }
123     
124     public ModelSource createTestModelSource(FileObject fo, boolean editable) throws CatalogModelException{
125         final DataObject dobj;
126         final CatalogModel catalogModel = createCatalogModel(fo);
127         try {
128             dobj = DataObject.find(fo);
129         } catch (DataObjectNotFoundException ex) {
130             throw new CatalogModelException(ex);
131         }
132         Lookup lookup = Lookups.proxy(new Lookup.Provider() {
133             public Lookup getLookup() {
134                         return Lookups.fixed(new Object JavaDoc[] {
135                             dobj.getPrimaryFile(),
136                             getDocument(dobj.getPrimaryFile()),
137                             dobj,
138                             catalogModel
139                         });
140             }
141         } );
142         return new ModelSource(lookup, editable);
143     }
144     
145     private static void initCatalogFile() throws Exception JavaDoc {
146         for (NamespaceLocation nl : NamespaceLocation.values()) {
147             singletonCatMod.addNamespace(nl);
148         }
149     }
150     
151     public void addNamespace(NamespaceLocation nl) throws Exception JavaDoc {
152         this.addURI(nl.getLocationURI(), nl.getResourceURI());
153     }
154     
155     public SchemaModel getSchemaModel(NamespaceLocation nl) throws Exception JavaDoc {
156         if (nl.getResourceFile().exists()) {
157             ModelSource source = singletonCatMod.getModelSource(nl.getLocationURI());
158             DataObject dobj = (DataObject) source.getLookup().lookup(DataObject.class);
159             SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class);
160             if (save != null) save.save();
161             FileObject fo = (FileObject) source.getLookup().lookup(FileObject.class);
162             fo.delete();
163         }
164         nl.refreshResourceFile();
165         return getSchemaModel(nl.getLocationURI());
166     }
167     
168     public SchemaModel getSchemaModel(URI JavaDoc lcationURI) throws Exception JavaDoc {
169         ModelSource source = singletonCatMod.getModelSource(lcationURI);
170         SchemaModel model = SchemaModelFactory.getDefault().getModel(source);
171         return model;
172     }
173     
174     private Map JavaDoc<FileObject,Document JavaDoc> fileToDocumentMap;
175     private Map JavaDoc<FileObject,Document JavaDoc> documentPool() {
176         if (fileToDocumentMap == null) {
177             fileToDocumentMap = new HashMap JavaDoc<FileObject,Document JavaDoc>();
178         }
179         return fileToDocumentMap;
180     }
181     private boolean documentPooling = true;
182     
183     public void setDocumentPooling(boolean v) {
184         documentPooling = v;
185         if (! documentPooling) {
186             clearDocumentPool();
187         }
188     }
189
190     public void clearDocumentPool() {
191         fileToDocumentMap = null;
192     }
193 }
194
195
Popular Tags