KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui;
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 javax.swing.text.Document JavaDoc;
29 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
30 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
31 import org.netbeans.modules.xml.schema.model.SchemaModel;
32 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
33 import org.netbeans.modules.xml.xam.locator.CatalogModel;
34 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
35 import org.netbeans.modules.xml.xam.ModelSource;
36 import org.openide.cookies.SaveCookie;
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     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
51         super(file);
52     }
53     
54     static TestCatalogModel singletonCatMod = null;
55     public static TestCatalogModel getDefault(){
56         if (singletonCatMod == null){
57             CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
58             try {
59                 singletonCatMod = new TestCatalogModel(Util.getTempDir("schematest/catalog"));
60                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
61                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
62                 catFile.deleteOnExit();
63                 initCatalogFile();
64             } catch (Exception JavaDoc ex) {
65                 ex.printStackTrace();
66                 return null;
67             }
68         }
69         return singletonCatMod;
70     }
71     
72     
73     /**
74      * This method could be overridden by the Unit testcase to return a special
75      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
76      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
77      */

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