KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > test > TestEngineContextProvider


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.api.debugger.jpda.test;
21
22 import java.net.URISyntaxException JavaDoc;
23 import org.netbeans.spi.debugger.jpda.SourcePathProvider;
24 import org.netbeans.spi.debugger.ContextProvider;
25 import org.netbeans.api.debugger.Session;
26
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.util.*;
30 import java.net.URL JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.io.File JavaDoc;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileStateInvalidException;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.JarFileSystem;
37
38
39 /**
40  * An engine context suited for automatic test environment.
41  *
42  * @author Maros Sandor
43  */

44 public class TestEngineContextProvider extends SourcePathProvider {
45
46     private String JavaDoc sourceRoot = System.getProperty ("test.dir.src");
47     private String JavaDoc home = System.getProperty("user.home");
48     private String JavaDoc[] originalSourceRoots = new String JavaDoc[] {
49                                 sourceRoot, home
50                             };
51     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
52
53     
54     public TestEngineContextProvider (ContextProvider ctxProvider) {
55     // this.session = (Session) ctxProvider.lookupFirst (null, Session.class);
56
for (int i = 0; i < originalSourceRoots.length; i++) {
57             while (originalSourceRoots[i].endsWith(File.separator)) {
58                 originalSourceRoots[i] = originalSourceRoots[i].substring(0, originalSourceRoots[i].length() - 1);
59             }
60         }
61     }
62
63     /**
64      * Translates a relative path (java/lang/Thread.java) to url.
65      *
66      * @param relativePath a relative path (java/lang/Thread.java)
67      * @return url
68      */

69     public String JavaDoc getURL (String JavaDoc relativePath, boolean global) {
70         String JavaDoc url = sourceRoot + relativePath;
71         if (new File JavaDoc (url).exists ()) return url;
72         else System.out.println("WARNIG: No URL for '"+relativePath+"', sourceRoot = '"+sourceRoot+"'.");
73         return null;
74     }
75
76
77     /**
78      * Returns relative path for given url.
79      *
80      * @param url a url of resource file
81      * @param directorySeparator a directory separator character
82      * @param includeExtension whether the file extension should be included
83      * in the result
84      *
85      * @return relative path
86      */

87     public String JavaDoc getRelativePath (
88         String JavaDoc url,
89         char directorySeparator,
90         boolean includeExtension
91     ) {
92         if (!url.startsWith (sourceRoot)) return null;
93         url = url.substring (sourceRoot.length ());
94         if (!includeExtension) {
95             int i = url.lastIndexOf ('.');
96             if (i > 0)
97                 url = url.substring (0, i);
98         }
99         url = url.replace ('/', directorySeparator);
100         return url;
101     }
102
103     /**
104      * Returns set of original source roots.
105      *
106      * @return set of original source roots
107      */

108     public String JavaDoc[] getOriginalSourceRoots () {
109         return originalSourceRoots;
110     }
111
112     /**
113      * Returns array of source roots.
114      *
115      * @return array of source roots
116      */

117     public String JavaDoc[] getSourceRoots () {
118         return originalSourceRoots;
119     }
120
121     /**
122      * Sets array of source roots.
123      *
124      * @param sourceRoots a new array of sourceRoots
125      */

126     public void setSourceRoots (String JavaDoc[] sourceRoots) {
127     }
128
129     public String JavaDoc getSourceRoot(String JavaDoc url) {
130         try {
131             url = new URL JavaDoc(url).toString();
132         } catch (MalformedURLException JavaDoc ex) {
133             ex.printStackTrace();
134         }
135         for (int i = 0; i < originalSourceRoots.length; i++) {
136             String JavaDoc sourceRoot = originalSourceRoots[i];
137             File JavaDoc sourceRootFile = new File JavaDoc(sourceRoot);
138             sourceRootFile = FileUtil.normalizeFile(sourceRootFile);
139             FileObject fileObject = FileUtil.toFileObject(sourceRootFile);
140             String JavaDoc rootURL;
141             try {
142                 if (fileObject == null) {
143                     try {
144                         rootURL = sourceRootFile.toURI().toURL().toString();
145                     } catch (MalformedURLException JavaDoc ex) {
146                         ex.printStackTrace();
147                         continue;
148                     }
149                 } else {
150                     rootURL = fileObject.getURL().toString();
151                 }
152                 if (url.startsWith(rootURL)) {
153                     File JavaDoc f = null;
154                     if (fileObject != null) {
155                         if (fileObject.getFileSystem () instanceof JarFileSystem)
156                             f = ((JarFileSystem) fileObject.getFileSystem ()).getJarFile ();
157                         else
158                             f = FileUtil.toFile (fileObject);
159                     } else {
160                         f = sourceRootFile;
161                     }
162                     if (f != null) {
163                         return f.getAbsolutePath ();
164                     }
165                 }
166             } catch (FileStateInvalidException ex) {
167                 // Invalid source root - skip
168
}
169         }
170         return null; // not found
171
}
172     
173     /**
174      * Adds property change listener.
175      *
176      * @param l new listener.
177      */

178     public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
179     }
180
181     /**
182      * Removes property change listener.
183      *
184      * @param l removed listener.
185      */

186     public void removePropertyChangeListener (
187         PropertyChangeListener JavaDoc l
188     ) {
189     }
190 }
191
Popular Tags