2
* TestCatalogModel.java
4
* Created on April 2, 2006, 10:41 AM
6
* To change this template, choose Tools | Template Manager
7
* and open the template in the editor.
10
package org.sample.registry.model;
13
import java.io.FileInputStream;
14
import java.io.IOException;
15
import java.util.HashMap;
17
import javax.swing.text.Document;
18
import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
19
import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
20
import org.netbeans.modules.xml.xam.locator.CatalogModel;
21
import org.netbeans.modules.xml.xam.locator.CatalogModelException;
22
import org.netbeans.modules.xml.xam.ModelSource;
23
import org.openide.filesystems.FileObject;
24
import org.openide.filesystems.FileUtil;
25
import org.openide.loaders.DataObject;
26
import org.openide.loaders.DataObjectNotFoundException;
27
import org.openide.util.Lookup;
28
import org.openide.util.lookup.Lookups;
35
public class TestCatalogModel extends CatalogWriteModelImpl{
36
private TestCatalogModel(File file) throws IOException{
40
static TestCatalogModel singletonCatMod = null;
41
public static TestCatalogModel getDefault(){
42
if (singletonCatMod == null){
43
CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
45
singletonCatMod = new TestCatalogModel(Util.getTempDir("schematest/catalog"));
46
FileObject catalogFO = singletonCatMod.getCatalogFileObject();
47
File catFile = FileUtil.toFile(catalogFO);
48
catFile.deleteOnExit();
50
} catch (Exception ex) {
55
return singletonCatMod;
60
* This method could be overridden by the Unit testcase to return a special
61
* ModelSource object for a FileObject with custom impl of classes added to the lookup.
62
* This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
64
protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
65
assert thisFileObj != null : "Null file object.";
66
final CatalogModel catalogModel = createCatalogModel(thisFileObj);
67
final DataObject dobj;
69
dobj = DataObject.find(thisFileObj);
70
} catch (DataObjectNotFoundException ex) {
71
throw new CatalogModelException(ex);
73
Lookup proxyLookup = Lookups.proxy(
74
new Lookup.Provider() {
75
public Lookup getLookup() {
76
Document document = null;
77
document = getDocument(thisFileObj);
78
return Lookups.fixed(new Object[] {
79
FileUtil.toFile(thisFileObj),
88
return new ModelSource(proxyLookup, editable);
91
private Document getDocument(FileObject fo){
92
Document result = null;
93
if (documentPooling) {
94
result = documentPool().get(fo);
96
if (result != null) return result;
99
File file = FileUtil.toFile(fo);
100
FileInputStream fis = new FileInputStream(file);
101
byte buffer[] = new byte[fis.available()];
102
result = new org.netbeans.editor.BaseDocument(
103
org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
104
result.remove(0, result.getLength());
107
String str = new String(buffer);
108
result.insertString(0,str,null);
110
} catch (Exception dObjEx) {
113
if (documentPooling) {
114
documentPool().put(fo, result);
119
protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{
123
public ModelSource createTestModelSource(FileObject fo, boolean editable) throws CatalogModelException{
124
final DataObject dobj;
125
final CatalogModel catalogModel = createCatalogModel(fo);
127
dobj = DataObject.find(fo);
128
} catch (DataObjectNotFoundException ex) {
129
throw new CatalogModelException(ex);
131
Lookup lookup = Lookups.proxy(new Lookup.Provider() {
132
public Lookup getLookup() {
133
return Lookups.fixed(new Object[] {
134
dobj.getPrimaryFile(),
135
getDocument(dobj.getPrimaryFile()),
141
return new ModelSource(lookup, editable);
144
private static void initCatalogFile() throws Exception {
147
private Map<FileObject,Document> fileToDocumentMap;
148
private Map<FileObject,Document> documentPool() {
149
if (fileToDocumentMap == null) {
150
fileToDocumentMap = new HashMap<FileObject,Document>();
152
return fileToDocumentMap;
154
private boolean documentPooling = true;
156
public void setDocumentPooling(boolean v) {
158
if (! documentPooling) {
163
public void clearDocumentPool() {
164
fileToDocumentMap = null;