KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > filesystems > XMLEnvironmentProvider


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.filesystems;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.xml.sax.*;
26 import org.xml.sax.helpers.*;
27
28 import org.openide.cookies.InstanceCookie;
29 import org.openide.loaders.*;
30 import org.openide.filesystems.*;
31 import org.openide.util.*;
32 import org.openide.util.lookup.*;
33 import org.openide.xml.*;
34 import org.openide.*;
35
36 /**
37  * Default implementation providing Environment for XMLDataObject according to its
38  * public ID.
39  *
40  * <p>
41  * Subclass instance of this must be registered as <tt>FileEntityResolver</tt>
42  * dictates and must implement <tt>Environment.Provider</tt> interface.
43  *
44  * <p>
45  * It forever caches provided environment!
46  *
47  * @see org.netbeans.core.xml.FileEntityResolver
48  * @author Petr Kuzel
49  * @version
50  */

51 class XMLEnvironmentProvider extends SharedClassObject {
52
53     private static final long serialVersionUID = 18947L;
54     
55     private static transient Map<FileObject, Lookup> envs = new HashMap<FileObject, Lookup>(11); // 11 expected number of modules using this
56

57     // Environment.Provider ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58

59     /** Returns a lookup that represents environment.
60      * @return the lookup
61      */

62     public final Lookup getEnvironment(DataObject obj){
63         
64         // the obj check is done by core FileEntityResolver that calls us
65

66         // we want to create just one instance per FileObject
67

68         FileObject file = obj.getPrimaryFile();
69         Lookup lookup = envs.get(file);
70         if (lookup == null) {
71             lookup = createLookup(obj);
72             envs.put(file, lookup);
73         }
74         return lookup;
75         
76     }
77     
78     /**
79      * It is called exactly once per DataObject.
80      *
81      * @return content of assigned Lookup
82      */

83     protected InstanceContent createInstanceContent(DataObject obj) {
84         return new InstanceContent();
85     }
86     
87     /**
88      * It is called exactly once per DataObject.
89      *
90      * @return Lookup containing <tt>createInstanceContent()</tt>
91      */

92     protected Lookup createLookup(DataObject obj) {
93         InstanceContent ic = createInstanceContent(obj);
94         Lookup lookup = new AbstractLookup(ic);
95         if (lookup.lookup(InstanceCookie.class) == null) {
96             Exceptions.printStackTrace(new IllegalStateException JavaDoc()); // instance cookie required
97
}
98         return lookup;
99     }
100             
101 }
102
Popular Tags