KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > xdm > 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.web.jsf.xdm.model;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import javax.swing.text.Document JavaDoc;
18 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
19 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
20 import org.netbeans.modules.xml.xam.locator.CatalogModel;
21 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
22 import org.netbeans.modules.xml.xam.ModelSource;
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.loaders.DataObject;
26 import org.openide.loaders.DataObjectNotFoundException;
27 import org.openide.util.Lookup;
28 import org.openide.util.lookup.Lookups;
29
30 /**
31  *
32  * @author girix
33  */

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

64     protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
65         assert thisFileObj != null : "Null file object.";
66         final CatalogModel catalogModel = createCatalogModel(thisFileObj);
67         final DataObject dobj;
68         try {
69             dobj = DataObject.find(thisFileObj);
70         } catch (DataObjectNotFoundException ex) {
71             throw new CatalogModelException(ex);
72         }
73         Lookup proxyLookup = Lookups.proxy(
74                 new Lookup.Provider() {
75             public Lookup getLookup() {
76                 Document JavaDoc document = null;
77                 document = getDocument(thisFileObj);
78                 return Lookups.fixed(new Object JavaDoc[] {
79                     FileUtil.toFile(thisFileObj),
80                     thisFileObj,
81                     document,
82                     dobj,
83                     catalogModel
84                 });
85             }
86         }
87         );
88         return new ModelSource(proxyLookup, editable);
89     }
90     
91     private Document JavaDoc getDocument(FileObject fo){
92         Document JavaDoc result = null;
93         if (documentPooling) {
94             result = documentPool().get(fo);
95         }
96         if (result != null) return result;
97         try {
98             
99             File JavaDoc file = FileUtil.toFile(fo);
100             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
101             byte buffer[] = new byte[fis.available()];
102             result = new org.netbeans.editor.BaseDocument(
103                     org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
104             result.remove(0, result.getLength());
105             fis.read(buffer);
106             fis.close();
107             String JavaDoc str = new String JavaDoc(buffer);
108             result.insertString(0,str,null);
109             
110         } catch (Exception JavaDoc dObjEx) {
111             return null;
112         }
113         if (documentPooling) {
114             documentPool().put(fo, result);
115         }
116         return result;
117     }
118     
119     protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{
120         return getDefault();
121     }
122     
123     public ModelSource createTestModelSource(FileObject fo, boolean editable) throws CatalogModelException{
124         final DataObject dobj;
125         final CatalogModel catalogModel = createCatalogModel(fo);
126         try {
127             dobj = DataObject.find(fo);
128         } catch (DataObjectNotFoundException ex) {
129             throw new CatalogModelException(ex);
130         }
131         Lookup lookup = Lookups.proxy(new Lookup.Provider() {
132             public Lookup getLookup() {
133                         return Lookups.fixed(new Object JavaDoc[] {
134                             dobj.getPrimaryFile(),
135                             getDocument(dobj.getPrimaryFile()),
136                             dobj,
137                             catalogModel
138                         });
139             }
140         } );
141         return new ModelSource(lookup, editable);
142     }
143     
144     private static void initCatalogFile() throws Exception JavaDoc {
145     }
146     
147     private Map JavaDoc<FileObject,Document JavaDoc> fileToDocumentMap;
148     private Map JavaDoc<FileObject,Document JavaDoc> documentPool() {
149         if (fileToDocumentMap == null) {
150             fileToDocumentMap = new HashMap JavaDoc<FileObject,Document JavaDoc>();
151         }
152         return fileToDocumentMap;
153     }
154     private boolean documentPooling = true;
155     
156     public void setDocumentPooling(boolean v) {
157         documentPooling = v;
158         if (! documentPooling) {
159             clearDocumentPool();
160         }
161     }
162
163     public void clearDocumentPool() {
164         fileToDocumentMap = null;
165     }
166 }
167
168
Popular Tags