~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/xsl/api/test/unit/src/org/netbeans/modules/xslt/model/impl/TestCatalogModel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
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.
 
27
 *
 
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.
 
38
 */
 
39
 
 
40
package org.netbeans.modules.xslt.model.impl;
 
41
 
 
42
import java.io.File;
 
43
import java.io.FileInputStream;
 
44
import java.io.IOException;
 
45
import java.net.URI;
 
46
import java.util.HashMap;
 
47
import java.util.Map;
 
48
import javax.swing.text.Document;
 
49
 
 
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;
 
65
 
 
66
/**
 
67
 * @author ads
 
68
 */
 
69
 
 
70
public class TestCatalogModel extends CatalogWriteModelImpl {
 
71
 
 
72
    private TestCatalogModel( File file ) throws IOException {
 
73
        super(file);
 
74
    }
 
75
 
 
76
    public static TestCatalogModel getDefault() {
 
77
        return INSTANCE;
 
78
    }
 
79
 
 
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(
 
84
                    DataObject.class);
 
85
            SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class);
 
86
            if (save != null)
 
87
                save.save();
 
88
            FileObject fo = (FileObject) source.getLookup().lookup(
 
89
                    FileObject.class);
 
90
            fo.delete();
 
91
        }
 
92
        nl.refreshResourceFile();
 
93
        return getSchemaModel(nl.getLocationURI());
 
94
    }*/
 
95
 
 
96
    public ModelSource createTestModelSource( FileObject fo, boolean editable )
 
97
            throws CatalogModelException
 
98
    {
 
99
        final DataObject dobj;
 
100
        final CatalogModel catalogModel = createCatalogModel(fo);
 
101
        try {
 
102
            dobj = DataObject.find(fo);
 
103
        }
 
104
        catch (DataObjectNotFoundException ex) {
 
105
            throw new CatalogModelException(ex);
 
106
        }
 
107
        Lookup lookup = Lookups.proxy(new Lookup.Provider() {
 
108
 
 
109
            public Lookup getLookup() {
 
110
                return Lookups
 
111
                        .fixed(new Object[] { dobj.getPrimaryFile(),
 
112
                                getDocument(dobj.getPrimaryFile()), dobj,
 
113
                                catalogModel });
 
114
            }
 
115
        });
 
116
        return new ModelSource(lookup, editable);
 
117
    }
 
118
    
 
119
    public XslModel getXslModel( URI locationURI , boolean fresh ) 
 
120
        throws CatalogModelException 
 
121
    {
 
122
        ModelSource source = INSTANCE.getModelSource(locationURI);
 
123
        XslModelFactory factory = XslModelFactory.XslModelFactoryAccess.getFactory();
 
124
        XslModelFactoryImpl impl = (XslModelFactoryImpl) factory;
 
125
        XslModel model;
 
126
        if ( fresh ) {
 
127
            model = impl.createFreshModel(source);
 
128
        }
 
129
        else {
 
130
            model = impl.getModel(source);
 
131
        }
 
132
        return model;
 
133
    }
 
134
 
 
135
    public XslModel getXslModel( URI locationURI ) throws CatalogModelException {
 
136
        return getXslModel(locationURI, false);
 
137
    }
 
138
 
 
139
    protected CatalogModel createCatalogModel( FileObject fo )
 
140
            throws CatalogModelException
 
141
    {
 
142
        return getDefault();
 
143
    }
 
144
 
 
145
    /**
 
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.
 
150
     */
 
151
    protected ModelSource createModelSource( final FileObject thisFileObj,
 
152
            boolean editable ) throws CatalogModelException
 
153
    {
 
154
        assert thisFileObj != null : "Null file object.";
 
155
        final CatalogModel catalogModel = createCatalogModel(thisFileObj);
 
156
        final DataObject dobj;
 
157
        try {
 
158
            dobj = DataObject.find(thisFileObj);
 
159
        }
 
160
        catch (DataObjectNotFoundException ex) {
 
161
            throw new CatalogModelException(ex);
 
162
        }
 
163
        Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
 
164
 
 
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 });
 
171
            }
 
172
        });
 
173
        return new ModelSource(proxyLookup, editable);
 
174
    }
 
175
 
 
176
    private Document getDocument( FileObject fo ) {
 
177
        Document result = null;
 
178
        if (documentPooling) {
 
179
            result = documentPool().get(fo);
 
180
        }
 
181
        if (result != null)
 
182
            return result;
 
183
        try {
 
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());
 
189
            fis.read(buffer);
 
190
            fis.close();
 
191
            String str = new String(buffer);
 
192
            result.insertString(0, str, null);
 
193
 
 
194
        }
 
195
        catch (Exception e) {
 
196
            return null;
 
197
        }
 
198
        if (documentPooling) {
 
199
            documentPool().put(fo, result);
 
200
        }
 
201
        return result;
 
202
    }
 
203
 
 
204
    private static void initCatalogFile() throws Exception {
 
205
        /*
 
206
         * for (NamespaceLocation nl : NamespaceLocation.values()) {
 
207
         * INSTANCE.addNamespace(nl); }
 
208
         */
 
209
    }
 
210
 
 
211
    private Map<FileObject, Document> documentPool() {
 
212
        if (myFileToDocumentMap == null) {
 
213
            myFileToDocumentMap = new HashMap<FileObject, Document>();
 
214
        }
 
215
        return myFileToDocumentMap;
 
216
    }
 
217
 
 
218
    public void setDocumentPooling( boolean v ) {
 
219
        documentPooling = v;
 
220
        if (!documentPooling) {
 
221
            clearDocumentPool();
 
222
        }
 
223
    }
 
224
 
 
225
    public void clearDocumentPool() {
 
226
        myFileToDocumentMap = null;
 
227
    }
 
228
 
 
229
    private static TestCatalogModel INSTANCE = null;
 
230
 
 
231
    private boolean documentPooling = true;
 
232
 
 
233
    private Map<FileObject, Document> myFileToDocumentMap;
 
234
 
 
235
    static {
 
236
        CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
 
237
        try {
 
238
            INSTANCE = new TestCatalogModel(Utils
 
239
                    .getTempDir("xslttest/catalog"));
 
240
            FileObject catalogFO = INSTANCE.getCatalogFileObject();
 
241
            File catFile = FileUtil.toFile(catalogFO);
 
242
            catFile.deleteOnExit();
 
243
            initCatalogFile();
 
244
        }
 
245
        catch (Exception ex) {
 
246
            ex.printStackTrace();
 
247
        }
 
248
    }
 
249
}