KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > 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.axi;
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.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
31 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
32 import org.netbeans.modules.xml.schema.model.SchemaModel;
33 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
34 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
35 import org.netbeans.modules.xml.xam.ModelSource;
36 import org.netbeans.modules.xml.xam.locator.CatalogModel;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.util.Lookup;
42 import org.openide.util.lookup.Lookups;
43
44 /**
45  *
46  * @author girix
47  */

48
49 public class TestCatalogModel extends CatalogWriteModelImpl {
50         
51     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
52         super(file);
53     }
54     
55     static TestCatalogModel singletonCatMod = null;
56     public static TestCatalogModel getDefault(){
57         if (singletonCatMod == null){
58             CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
59             try {
60                 singletonCatMod = new TestCatalogModel(new File JavaDoc(System.getProperty("java.io.tmpdir")));
61                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
62                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
63                 catFile.deleteOnExit();
64                 //initCatalogFile();
65
} catch (Exception JavaDoc ex) {
66                 ex.printStackTrace();
67                 return null;
68             }
69         }
70         return singletonCatMod;
71     }
72     
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 void addNamespace(NamespaceLocation nl) throws Exception {
161
// this.addURI(nl.getLocationURI(), nl.getResourceURI());
162
// }
163
//
164

165     public SchemaModel getSchemaModel(URI JavaDoc lcationURI) throws Exception JavaDoc {
166         SchemaModel model = SchemaModelFactory.getDefault().getModel(
167                 singletonCatMod.getModelSource(lcationURI));
168         if (! documentPooling) {
169             model.sync(); // resync to restored to origin content
170
}
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     public String JavaDoc toString(){
195         return "TestCatalogModel"+super.toString();
196     }
197         
198     public AXIModel getAXIModel(FileObject thisFileObj) throws CatalogModelException, IOException JavaDoc {
199         ModelSource source = createModelSource(thisFileObj, true);
200         SchemaModel model = SchemaModelFactory.getDefault().getModel(source);
201         return AXIModelFactory.getDefault().getModel(model);
202     }
203 }
204
205
Popular Tags