2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
24
* Contributor(s): The Original Software is NetBeans. The Initial
25
* Developer of the Original Software is Sun Microsystems, Inc. Portions
26
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
28
* If you wish your version of this file to be governed by only the CDDL
29
* or only the GPL Version 2, indicate your decision by adding
30
* "[Contributor] elects to include this software in this distribution
31
* under the [CDDL or GPL Version 2] license." If you do not indicate a
32
* single choice of license, a recipient has the option to distribute
33
* your version of this file under either the CDDL, the GPL Version 2 or
34
* to extend the choice of license to its licensees as provided above.
35
* However, if you add GPL Version 2 code and therefore, elected the GPL
36
* Version 2 license, then the option applies only if the new code is
37
* made subject to such option by the copyright holder.
40
package org.netbeans.modules.xslt.model.impl;
43
import java.io.FileInputStream;
44
import java.io.IOException;
46
import java.util.HashMap;
48
import javax.swing.text.Document;
50
import org.netbeans.editor.BaseDocument;
51
import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
52
import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
53
import org.netbeans.modules.xml.text.syntax.XMLKit;
54
import org.netbeans.modules.xml.xam.locator.CatalogModel;
55
import org.netbeans.modules.xml.xam.locator.CatalogModelException;
56
import org.netbeans.modules.xml.xam.ModelSource;
57
import org.netbeans.modules.xslt.model.XslModel;
58
import org.netbeans.modules.xslt.model.spi.XslModelFactory;
59
import org.openide.filesystems.FileObject;
60
import org.openide.filesystems.FileUtil;
61
import org.openide.loaders.DataObject;
62
import org.openide.loaders.DataObjectNotFoundException;
63
import org.openide.util.Lookup;
64
import org.openide.util.lookup.Lookups;
70
public class TestCatalogModel extends CatalogWriteModelImpl {
72
private TestCatalogModel( File file ) throws IOException {
76
public static TestCatalogModel getDefault() {
80
/*public XslModel getXslModel( NamespaceLocation nl ) throws Exception {
81
if (nl.getResourceFile().exists()) {
82
ModelSource source = INSTANCE.getModelSource(nl.getLocationURI());
83
DataObject dobj = (DataObject) source.getLookup().lookup(
85
SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class);
88
FileObject fo = (FileObject) source.getLookup().lookup(
92
nl.refreshResourceFile();
93
return getSchemaModel(nl.getLocationURI());
96
public ModelSource createTestModelSource( FileObject fo, boolean editable )
97
throws CatalogModelException
99
final DataObject dobj;
100
final CatalogModel catalogModel = createCatalogModel(fo);
102
dobj = DataObject.find(fo);
104
catch (DataObjectNotFoundException ex) {
105
throw new CatalogModelException(ex);
107
Lookup lookup = Lookups.proxy(new Lookup.Provider() {
109
public Lookup getLookup() {
111
.fixed(new Object[] { dobj.getPrimaryFile(),
112
getDocument(dobj.getPrimaryFile()), dobj,
116
return new ModelSource(lookup, editable);
119
public XslModel getXslModel( URI locationURI , boolean fresh )
120
throws CatalogModelException
122
ModelSource source = INSTANCE.getModelSource(locationURI);
123
XslModelFactory factory = XslModelFactory.XslModelFactoryAccess.getFactory();
124
XslModelFactoryImpl impl = (XslModelFactoryImpl) factory;
127
model = impl.createFreshModel(source);
130
model = impl.getModel(source);
135
public XslModel getXslModel( URI locationURI ) throws CatalogModelException {
136
return getXslModel(locationURI, false);
139
protected CatalogModel createCatalogModel( FileObject fo )
140
throws CatalogModelException
146
* This method could be overridden by the Unit testcase to return a special
147
* ModelSource object for a FileObject with custom impl of classes added to
148
* the lookup. This is optional if both getDocument(FO) and
149
* createCatalogModel(FO) are overridden.
151
protected ModelSource createModelSource( final FileObject thisFileObj,
152
boolean editable ) throws CatalogModelException
154
assert thisFileObj != null : "Null file object.";
155
final CatalogModel catalogModel = createCatalogModel(thisFileObj);
156
final DataObject dobj;
158
dobj = DataObject.find(thisFileObj);
160
catch (DataObjectNotFoundException ex) {
161
throw new CatalogModelException(ex);
163
Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
165
public Lookup getLookup() {
166
Document document = null;
167
document = getDocument(thisFileObj);
168
return Lookups.fixed(new Object[] {
169
FileUtil.toFile(thisFileObj), thisFileObj, document,
170
dobj, catalogModel });
173
return new ModelSource(proxyLookup, editable);
176
private Document getDocument( FileObject fo ) {
177
Document result = null;
178
if (documentPooling) {
179
result = documentPool().get(fo);
184
File file = FileUtil.toFile(fo);
185
FileInputStream fis = new FileInputStream(file);
186
byte buffer[] = new byte[fis.available()];
187
result = new BaseDocument(XMLKit.class, false);
188
result.remove(0, result.getLength());
191
String str = new String(buffer);
192
result.insertString(0, str, null);
195
catch (Exception e) {
198
if (documentPooling) {
199
documentPool().put(fo, result);
204
private static void initCatalogFile() throws Exception {
206
* for (NamespaceLocation nl : NamespaceLocation.values()) {
207
* INSTANCE.addNamespace(nl); }
211
private Map<FileObject, Document> documentPool() {
212
if (myFileToDocumentMap == null) {
213
myFileToDocumentMap = new HashMap<FileObject, Document>();
215
return myFileToDocumentMap;
218
public void setDocumentPooling( boolean v ) {
220
if (!documentPooling) {
225
public void clearDocumentPool() {
226
myFileToDocumentMap = null;
229
private static TestCatalogModel INSTANCE = null;
231
private boolean documentPooling = true;
233
private Map<FileObject, Document> myFileToDocumentMap;
236
CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
238
INSTANCE = new TestCatalogModel(Utils
239
.getTempDir("xslttest/catalog"));
240
FileObject catalogFO = INSTANCE.getCatalogFileObject();
241
File catFile = FileUtil.toFile(catalogFO);
242
catFile.deleteOnExit();
245
catch (Exception ex) {
246
ex.printStackTrace();