KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > plugin > java > reflect > JarFileContext


1 /*====================================================================
2
3 Objectweb Browser Framework
4 Copyright (C) 2000-2003 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.browser.plugin.java.reflect;
28
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34
35 import org.objectweb.util.browser.api.Context;
36 import org.objectweb.util.browser.api.Entry;
37 import org.objectweb.util.browser.api.Wrapper;
38 import org.objectweb.util.browser.core.api.ContextContainer;
39 import org.objectweb.util.browser.core.naming.DefaultEntry;
40 import org.objectweb.util.browser.core.naming.DefaultName;
41
42 /**
43  *
44  *
45  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
46  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
47  *
48  * @version 0.1
49  */

50 public class JarFileContext
51     implements Wrapper, Context {
52
53     //==================================================================
54
//
55
// Internal states.
56
//
57
//==================================================================
58

59     protected JarFileStructure jarFile_;
60
61     //==================================================================
62
//
63
// No constructor.
64
//
65
//==================================================================
66

67     //==================================================================
68
//
69
// Internal methods.
70
//
71
//==================================================================
72

73     /**
74      * Replace all the occurences of the given string with the given replacement.
75      * @param original The original String to modify.
76      * @param to_replace The substring of the string to search and replace.
77      * @param new_value The replacement String.
78      * @return The modified String.
79      */

80     protected String JavaDoc replaceAll(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
81         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
82         int indexBegin = 0;
83         int index = original.indexOf(to_replace, indexBegin);
84         while (index != -1) {
85             newString.append(original.substring(indexBegin, index));
86             newString.append(new_value);
87             indexBegin = index + to_replace.length();
88             index = original.indexOf(to_replace, indexBegin);
89         }
90         newString.append(original.substring(indexBegin));
91         return newString.toString();
92     }
93
94     protected ContextContainer getContext(ContextContainer contextContainer, String JavaDoc packageName){
95         if(packageName!=null && !packageName.equals("")) {
96             String JavaDoc currentPackage = null;
97             String JavaDoc otherPackage = null;
98             if(packageName.lastIndexOf(".")!=-1){
99                 currentPackage = packageName.substring(0,packageName.indexOf("."));
100                 otherPackage = packageName.substring(packageName.indexOf(".")+1);
101             } else {
102                 currentPackage = packageName;
103             }
104             if(currentPackage!=null) {
105                 ContextContainer cc = (ContextContainer)contextContainer.getLocalValue(currentPackage);
106                 if(cc == null) {
107                     cc = new PackageContextContainer();
108                     contextContainer.addEntry(currentPackage, cc);
109                 }
110                 if(otherPackage!=null)
111                     return getContext(cc, otherPackage);
112                 else
113                     return cc;
114             } else
115                 return contextContainer;
116         }
117         return contextContainer;
118     }
119
120     protected void addClass(ContextContainer contextContainer, Class JavaDoc theClass) {
121         String JavaDoc packageName = theClass.getPackage().getName();
122         ContextContainer cc = getContext(contextContainer, packageName);
123         cc.addEntry(theClass.getName().substring(theClass.getName().lastIndexOf(".") + 1),theClass);
124     }
125
126     //==================================================================
127
//
128
// Public methods for Context interface.
129
//
130
//==================================================================
131

132     public Entry JavaDoc[] getEntries(){
133         URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc(new URL JavaDoc[]{jarFile_.getJarURL()});
134         ContextContainer cc = new PackageContextContainer();
135         Vector JavaDoc elements = new Vector JavaDoc();
136         Enumeration JavaDoc entries = jarFile_.getJarFile().entries();
137         while(entries.hasMoreElements()) {
138             JarEntry JavaDoc entry = (JarEntry JavaDoc)entries.nextElement();
139             if(!entry.isDirectory()){
140                 try {
141                     Class JavaDoc c = Class.forName(replaceAll(entry.getName().substring(0, entry.getName().lastIndexOf('.')),"/","."),true,classLoader);
142                     addClass(cc, c);
143                     //elements.add(new DefaultEntry(c,new DefaultName(entry.getName()),this));
144
} catch (ClassNotFoundException JavaDoc e) {
145                     // Ignore
146
}
147             }
148         }
149         Entry JavaDoc[] theEntries = cc.getEntries();
150         for (int i = 0; i < theEntries.length; i++) {
151             elements.add(new DefaultEntry(theEntries[i].getValue(),new DefaultName(theEntries[i].getName().toString()),this));
152         }
153         return (Entry JavaDoc[])elements.toArray(new Entry JavaDoc[0]);
154     }
155
156     //==================================================================
157
//
158
// Public methods for Wrapper interface.
159
//
160
//==================================================================
161

162     public void setWrapped(Object JavaDoc object){
163         jarFile_ = (JarFileStructure)object;
164     }
165
166     public Object JavaDoc getWrapped(){
167         return jarFile_;
168     }
169
170 }
171     
172
Popular Tags