KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > util > TestCatalogModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.completion.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.modules.xml.axi.AXIModel;
31 import org.netbeans.modules.xml.axi.AXIModelFactory;
32 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
33 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
34 import org.netbeans.modules.xml.schema.model.SchemaModel;
35 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
36 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
37 import org.netbeans.modules.xml.xam.ModelSource;
38 import org.netbeans.modules.xml.xam.locator.CatalogModel;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.DataObjectNotFoundException;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.Lookups;
45
46 /**
47  *
48  * @author girix
49  */

50
51 public class TestCatalogModel extends CatalogWriteModelImpl {
52         
53     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
54         super(file);
55     }
56     
57     static TestCatalogModel singletonCatMod = null;
58     public static TestCatalogModel getDefault(){
59         if (singletonCatMod == null){
60             CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
61             try {
62                 singletonCatMod = new TestCatalogModel(new File JavaDoc(System.getProperty("java.io.tmpdir")));
63                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
64                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
65                 catFile.deleteOnExit();
66                 //initCatalogFile();
67
} catch (Exception JavaDoc ex) {
68                 ex.printStackTrace();
69                 return null;
70             }
71         }
72         return singletonCatMod;
73     }
74     
75     /**
76      * This method could be overridden by the Unit testcase to return a special
77      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
78      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
79      */

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