KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > api > DbgSourceMap


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

19
20 package org.netbeans.modules.scripting.php.dbginterface.api;
21
22 import java.net.URL JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30
31 /**
32  *
33  * @author marcow
34  */

35 public class DbgSourceMap {
36     private String JavaDoc serverBaseDir;
37     private URL JavaDoc serverBaseUrl;
38     private FileObject sourceRoot;
39     private boolean serverIsOnWindows;
40     private Map JavaDoc<String JavaDoc,DataObject> sourceFileMap =
41             Collections.synchronizedMap(new WeakHashMap JavaDoc<String JavaDoc,DataObject>());
42     
43     /** Creates a new instance of DbgSourceMap */
44     public DbgSourceMap(URL JavaDoc serverBaseUrl, String JavaDoc serverBaseDir, FileObject sourceRoot) {
45         this.serverBaseUrl = serverBaseUrl;
46         this.serverBaseDir = serverBaseDir;
47         this.sourceRoot= sourceRoot;
48         this.serverIsOnWindows = serverBaseDir.contains("\\");
49     }
50     
51     public FileObject getSourceRoot() {
52         return sourceRoot;
53     }
54     
55     public String JavaDoc getServerBaseDir() {
56         return serverBaseDir;
57     }
58     
59     public DataObject mapToSourceFileDataObject(String JavaDoc sourceFile) {
60         DataObject ret = null;
61         
62         if(null == sourceFile || sourceFile.trim().length() == 0) {
63             throw new IllegalArgumentException JavaDoc("sourceFile parameter cannot be null or blank");
64         }
65
66         if (!sourceFile.startsWith(serverBaseDir)) {
67             throw new IllegalArgumentException JavaDoc("sourceFile: " + sourceFile + " outside serverBaseDir: " + serverBaseDir);
68         }
69
70         ret = sourceFileMap.get(sourceFile);
71         
72         if (ret != null) {
73             return ret;
74         }
75         
76         String JavaDoc s = sourceFile.substring(serverBaseDir.length());
77         
78         if (s.startsWith("/") || s.startsWith("\\")) {
79             s = s.substring(1);
80         }
81         
82         
83         FileObject fo = sourceRoot.getFileObject(s);
84         
85         try {
86             ret = DataObject.find(fo);
87             sourceFileMap.put(sourceFile, ret);
88         }
89         catch (DataObjectNotFoundException donfe) {
90             ErrorManager.getDefault().notify(ErrorManager.WARNING, donfe);
91         }
92         
93         return ret;
94     }
95
96     public String JavaDoc mapToServerPath(FileObject fo) {
97         if (fo == null) {
98             return null;
99         }
100         
101         if (fo.equals(sourceRoot)) {
102             return serverBaseDir;
103         }
104         
105         FileObject parent = fo.getParent();
106         
107         while (parent != null && !parent.equals(sourceRoot)) {
108             parent = parent.getParent();
109         }
110         
111         if (parent == null) {
112                 // This fileObject does not live within our sourceRoot.
113
return null;
114         }
115         
116         String JavaDoc relSourceFile = fo.getPath().substring(sourceRoot.getPath().length());
117         String JavaDoc targetFile = serverBaseDir + relSourceFile;
118
119         if (serverIsOnWindows) {
120             targetFile = targetFile.replace('/', '\\');
121         }
122         
123         return targetFile;
124    }
125 }
126
Popular Tags