KickJava   Java API By Example, From Geeks To Geeks.

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

19
20 /*
21  * TestCatalogModel.java
22  *
23  * Created on April 2, 2006, 10:41 AM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.URI JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import javax.swing.text.Document JavaDoc;
39 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
40 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
41 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
42 import org.netbeans.modules.xml.xam.ModelSource;
43 import org.netbeans.modules.xml.xam.locator.CatalogModel;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.util.Lookup;
49 import org.openide.util.lookup.Lookups;
50
51 /**
52  *
53  * @author girix
54  */

55
56 public class TestCatalogModel extends CatalogWriteModelImpl {
57         
58     private TestCatalogModel(File JavaDoc file) throws IOException JavaDoc{
59         super(file);
60     }
61     
62     static TestCatalogModel singletonCatMod = null;
63     public static TestCatalogModel getDefault(){
64         if (singletonCatMod == null){
65             CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
66             try {
67                 //singletonCatMod = new TestCatalogModel(new File(System.getProperty("java.io.tmpdir")));
68
//temporary for testing use system temp dir
69
File JavaDoc catalogFolder = new File JavaDoc("c:/wsdlui");
70                 catalogFolder.createNewFile();
71                 singletonCatMod = new TestCatalogModel(catalogFolder);
72                 FileObject catalogFO = singletonCatMod.getCatalogFileObject();
73                 File JavaDoc catFile = FileUtil.toFile(catalogFO);
74                 catFile.deleteOnExit();
75                 //initCatalogFile();
76
} catch (Exception JavaDoc ex) {
77                 ex.printStackTrace();
78                 return null;
79             }
80         }
81         return singletonCatMod;
82     }
83     
84     
85     
86     /**
87      * This method could be overridden by the Unit testcase to return a special
88      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
89      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
90      */

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