KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > 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. You can obtain a copy of the License at
5  * http://www.netbeans.org/cddl.html or http://www.netbeans.org/cddl.txt. When
6  * distributing Covered Code, include this CDDL Header Notice in each file and
7  * include the License file at http://www.netbeans.org/cddl.txt. If applicable,
8  * add the following below the CDDL Header, with the fields enclosed by brackets []
9  * replaced by your own identifying information: "Portions Copyrighted [year]
10  * [name of copyright owner]" The Original Software is NetBeans. The Initial
11  * Developer of the Original Software is Sun Microsystems, Inc. Portions
12  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
13  */

14
15 package org.netbeans.modules.xslt.model.impl;
16
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24
25 import org.netbeans.editor.BaseDocument;
26 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
27 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
28 import org.netbeans.modules.xml.text.syntax.XMLKit;
29 import org.netbeans.modules.xml.xam.locator.CatalogModel;
30 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
31 import org.netbeans.modules.xml.xam.ModelSource;
32 import org.netbeans.modules.xslt.model.XslModel;
33 import org.netbeans.modules.xslt.model.spi.XslModelFactory;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.DataObjectNotFoundException;
38 import org.openide.util.Lookup;
39 import org.openide.util.lookup.Lookups;
40
41 /**
42  * @author ads
43  */

44
45 public class TestCatalogModel extends CatalogWriteModelImpl {
46
47     private TestCatalogModel( File JavaDoc file ) throws IOException JavaDoc {
48         super(file);
49     }
50
51     public static TestCatalogModel getDefault() {
52         return INSTANCE;
53     }
54
55     /*public XslModel getXslModel( NamespaceLocation nl ) throws Exception {
56         if (nl.getResourceFile().exists()) {
57             ModelSource source = INSTANCE.getModelSource(nl.getLocationURI());
58             DataObject dobj = (DataObject) source.getLookup().lookup(
59                     DataObject.class);
60             SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class);
61             if (save != null)
62                 save.save();
63             FileObject fo = (FileObject) source.getLookup().lookup(
64                     FileObject.class);
65             fo.delete();
66         }
67         nl.refreshResourceFile();
68         return getSchemaModel(nl.getLocationURI());
69     }*/

70
71     public ModelSource createTestModelSource( FileObject fo, boolean editable )
72             throws CatalogModelException
73     {
74         final DataObject dobj;
75         final CatalogModel catalogModel = createCatalogModel(fo);
76         try {
77             dobj = DataObject.find(fo);
78         }
79         catch (DataObjectNotFoundException ex) {
80             throw new CatalogModelException(ex);
81         }
82         Lookup lookup = Lookups.proxy(new Lookup.Provider() {
83
84             public Lookup getLookup() {
85                 return Lookups
86                         .fixed(new Object JavaDoc[] { dobj.getPrimaryFile(),
87                                 getDocument(dobj.getPrimaryFile()), dobj,
88                                 catalogModel });
89             }
90         });
91         return new ModelSource(lookup, editable);
92     }
93     
94     public XslModel getXslModel( URI JavaDoc locationURI , boolean fresh )
95         throws CatalogModelException
96     {
97         ModelSource source = INSTANCE.getModelSource(locationURI);
98         XslModelFactory factory = XslModelFactory.XslModelFactoryAccess.getFactory();
99         XslModelFactoryImpl impl = (XslModelFactoryImpl) factory;
100         XslModel model;
101         if ( fresh ) {
102             model = impl.createFreshModel(source);
103         }
104         else {
105             model = impl.getModel(source);
106         }
107         return model;
108     }
109
110     public XslModel getXslModel( URI JavaDoc locationURI ) throws CatalogModelException {
111         return getXslModel(locationURI, false);
112     }
113
114     protected CatalogModel createCatalogModel( FileObject fo )
115             throws CatalogModelException
116     {
117         return getDefault();
118     }
119
120     /**
121      * This method could be overridden by the Unit testcase to return a special
122      * ModelSource object for a FileObject with custom impl of classes added to
123      * the lookup. This is optional if both getDocument(FO) and
124      * createCatalogModel(FO) are overridden.
125      */

126     protected ModelSource createModelSource( final FileObject thisFileObj,
127             boolean editable ) throws CatalogModelException
128     {
129         assert thisFileObj != null : "Null file object.";
130         final CatalogModel catalogModel = createCatalogModel(thisFileObj);
131         final DataObject dobj;
132         try {
133             dobj = DataObject.find(thisFileObj);
134         }
135         catch (DataObjectNotFoundException ex) {
136             throw new CatalogModelException(ex);
137         }
138         Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
139
140             public Lookup getLookup() {
141                 Document JavaDoc document = null;
142                 document = getDocument(thisFileObj);
143                 return Lookups.fixed(new Object JavaDoc[] {
144                         FileUtil.toFile(thisFileObj), thisFileObj, document,
145                         dobj, catalogModel });
146             }
147         });
148         return new ModelSource(proxyLookup, editable);
149     }
150
151     private Document JavaDoc getDocument( FileObject fo ) {
152         Document JavaDoc result = null;
153         if (documentPooling) {
154             result = documentPool().get(fo);
155         }
156         if (result != null)
157             return result;
158         try {
159             File JavaDoc file = FileUtil.toFile(fo);
160             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
161             byte buffer[] = new byte[fis.available()];
162             result = new BaseDocument(XMLKit.class, false);
163             result.remove(0, result.getLength());
164             fis.read(buffer);
165             fis.close();
166             String JavaDoc str = new String JavaDoc(buffer);
167             result.insertString(0, str, null);
168
169         }
170         catch (Exception JavaDoc e) {
171             return null;
172         }
173         if (documentPooling) {
174             documentPool().put(fo, result);
175         }
176         return result;
177     }
178
179     private static void initCatalogFile() throws Exception JavaDoc {
180         /*
181          * for (NamespaceLocation nl : NamespaceLocation.values()) {
182          * INSTANCE.addNamespace(nl); }
183          */

184     }
185
186     private Map JavaDoc<FileObject, Document JavaDoc> documentPool() {
187         if (myFileToDocumentMap == null) {
188             myFileToDocumentMap = new HashMap JavaDoc<FileObject, Document JavaDoc>();
189         }
190         return myFileToDocumentMap;
191     }
192
193     public void setDocumentPooling( boolean v ) {
194         documentPooling = v;
195         if (!documentPooling) {
196             clearDocumentPool();
197         }
198     }
199
200     public void clearDocumentPool() {
201         myFileToDocumentMap = null;
202     }
203
204     private static TestCatalogModel INSTANCE = null;
205
206     private boolean documentPooling = true;
207
208     private Map JavaDoc<FileObject, Document JavaDoc> myFileToDocumentMap;
209
210     static {
211         CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
212         try {
213             INSTANCE = new TestCatalogModel(Utils
214                     .getTempDir("xslttest/catalog"));
215             FileObject catalogFO = INSTANCE.getCatalogFileObject();
216             File JavaDoc catFile = FileUtil.toFile(catalogFO);
217             catFile.deleteOnExit();
218             initCatalogFile();
219         }
220         catch (Exception JavaDoc ex) {
221             ex.printStackTrace();
222         }
223     }
224 }
225
Popular Tags