KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > Env


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

19
20 package org.netbeans.modules.settings;
21
22 import java.io.IOException JavaDoc;
23
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.FileSystem;
26 import org.openide.filesystems.Repository;
27 import org.openide.loaders.DataObject;
28 import org.openide.loaders.Environment;
29 import org.openide.util.Lookup;
30
31 /** A provider for .settings files of a certain DTD.
32  * It creates a suitable convertor according to {@link #EA_CONVERTOR}.
33  *
34  * @author Jan Pokorsky
35  */

36 public final class Env implements Environment.Provider {
37     /** file attribute containing convertor object. Usage
38      * <code>&lt;attr name="settings.convertor" methodvalue="org.netbeans.modules.settings.XMLPropertiesConvertor.create"/>
39      * </code>
40      */

41     public final static String JavaDoc EA_CONVERTOR = "settings.convertor"; //NOI18N
42
/** file attribute containing path to the provider. Used by
43      * InstanceDataObject.create or upgrade algorithm. Usage
44      * <code>&lt;attr name="settings.providerPath" stringvalue="xml/lookups/NetBeans/DTD_XML_Properties_1_0.instance"/>
45      * </code>
46      */

47     public final static String JavaDoc EA_PROVIDER_PATH = "settings.providerPath"; // NOI18N
48
/** file attribute containing PUBLIC attribute of xml header. Usage
49      * <code>&lt;attr name="hint.originalPublicID" stringvalue="-//NetBeans//DTD XML Properties 1.0//EN"/>
50      * </code>
51      */

52     public final static String JavaDoc EA_PUBLICID = "hint.originalPublicID"; // NOI18N
53
/** file attribute containnig class name of the setting object. Usage
54      * <code>&lt;attr name="settings.instanceClass" stringvalue="org.netbeans.modules.foo.Foo"/>
55      * </code>
56      */

57     public final static String JavaDoc EA_INSTANCE_CLASS_NAME = "settings.instanceClass"; //NOI18N
58
/** file attribute containnig class name and subclass names of the setting object. Use the
59      * attribute for performance reasons. Usage
60      * <code>&lt;attr name="settings.instanceOf" stringvalue="org.netbeans.modules.foo.Foo[, ...]"/>
61      * </code>
62      */

63     public final static String JavaDoc EA_INSTANCE_OF = "settings.instanceOf"; //NOI18N
64
/** file attribute containnig the setting object. Usage
65      * <code>&lt;attr name="settings.instanceCreate" newvalue="org.netbeans.modules.foo.Foo"/>
66      * </code> or
67      * <code>&lt;attr name="settings.instanceCreate" methodvalue="org.netbeans.modules.foo.Foo.create"/>
68      * </code>
69      */

70     public final static String JavaDoc EA_INSTANCE_CREATE = "settings.instanceCreate"; //NOI18N
71
/** file attribute determining whether the registration works also for subclasses of the registering
72      * class. Use of this attribute is optional. The default is false, the value must be boolean, example:
73      * <code>&lt;attr name="settings.subclasses" boolvalue="true"/&gt;</code>
74      */

75     public static final String JavaDoc EA_SUBCLASSES = "settings.subclasses"; // NOI18N
76

77     private final FileObject providerFO;
78     
79     /** create Environment.Provider */
80     public static Environment.Provider create(FileObject fo) {
81         return new Env(fo);
82     }
83     
84     private Env(FileObject fo) {
85         providerFO = fo;
86     }
87     
88     public Lookup getEnvironment(DataObject dobj) {
89         if (!(dobj instanceof org.openide.loaders.InstanceDataObject)) return Lookup.EMPTY;
90         InstanceProvider icp = new InstanceProvider(dobj, providerFO);
91         return icp.getLookup();
92     }
93     
94     /** parse file attribute
95      * @param attr String value can be null; used delimiter is ","
96      * @return set of items
97      */

98     public static java.util.Set JavaDoc<String JavaDoc> parseAttribute(Object JavaDoc attr) {
99         if (attr != null && attr instanceof String JavaDoc) {
100             java.util.StringTokenizer JavaDoc s =
101                 new java.util.StringTokenizer JavaDoc((String JavaDoc) attr, ","); //NOI18N
102
java.util.Set JavaDoc<String JavaDoc> set = new java.util.HashSet JavaDoc<String JavaDoc>(10);
103             while (s.hasMoreTokens()) {
104                 set.add(s.nextToken().trim());
105             }
106             return set;
107         } else {
108             return java.util.Collections.emptySet();
109         }
110     }
111     
112     /** look up appropriate provider according to clazz */
113     public static FileObject findProvider(Class JavaDoc clazz) throws IOException JavaDoc {
114         String JavaDoc prefix = "xml/memory/"; //NOI18N
115
FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
116         FileObject memContext = sfs.findResource(prefix);
117         if (memContext == null) throw new java.io.FileNotFoundException JavaDoc("SFS/xml/memory/"); //NOI18N
118
Class JavaDoc c = clazz;
119         while (c != null) {
120             String JavaDoc name = c.getName().replace('.', '/');
121             String JavaDoc convertorPath = new StringBuffer JavaDoc(200).append(prefix).
122                 append(name).toString(); // NOI18N
123
FileObject fo = sfs.findResource(convertorPath);
124             if (fo != null) {
125                 String JavaDoc providerPath = (String JavaDoc) fo.getAttribute(EA_PROVIDER_PATH);
126                 if (providerPath != null) {
127                     if (c.equals(clazz)) {
128                         return sfs.findResource(providerPath);
129                     } else {
130                         // check the special subclasses attribute
131
Object JavaDoc inheritAttribute = fo.getAttribute(EA_SUBCLASSES);
132                         if (inheritAttribute instanceof Boolean JavaDoc) {
133                             boolean subclasses = ((Boolean JavaDoc)inheritAttribute).booleanValue();
134                             if (subclasses) {
135                                 return sfs.findResource(providerPath);
136                             }
137                         }
138                     }
139                 }
140             }
141             c = c.getSuperclass();
142         }
143         return null;
144     }
145     
146     private static String JavaDoc xmlLookupsPrefix = "xml/lookups"; // NOI18N
147
private static String JavaDoc xmlEntitiesPrefix = "xml/entities"; // NOI18N
148

149     /** find an entity registration according to passed provider
150      * @param provider provider file object
151      * @return entity file object
152      */

153     public static FileObject findEntityRegistration(FileObject provider) {
154         String JavaDoc filename = provider.getPath();
155         int i = filename.lastIndexOf('.');
156         if (i != -1 && i > filename.lastIndexOf('/')) {
157             filename = filename.substring(0, i);
158         }
159         String JavaDoc resource = xmlEntitiesPrefix +
160             filename.substring(xmlLookupsPrefix.length(), filename.length());
161         
162         return Repository.getDefault().getDefaultFileSystem().findResource(resource);
163     }
164 }
165
Popular Tags