KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > test > 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.retriever.catalog.test;
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.Logger 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.CatalogModelException;
23 import org.netbeans.modules.xml.xam.ModelSource;
24 import org.netbeans.modules.xml.xam.locator.CatalogModel;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.loaders.DataObject;
28 import org.openide.loaders.DataObjectNotFoundException;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.Lookups;
31
32 /**
33  *
34  * @author girix
35  */

36
37 public class TestCatalogModel extends CatalogWriteModelImpl {
38         
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(new File JavaDoc(System.getProperty("java.io.tmpdir")));
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     /**
64      * This method could be overridden by the Unit testcase to return a special
65      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
66      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
67      */

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