KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > editor > completion > FileAttributeSupport


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.modules.websvc.editor.completion;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.editor.BaseDocument;
33 import org.netbeans.editor.ext.CompletionQuery;
34 import org.netbeans.modules.editor.NbEditorUtilities;
35
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.loaders.DataObject;
41
42 /**
43  *
44  * @author Marek Fukala
45  */

46 public class FileAttributeSupport {
47     
48     private static String JavaDoc FILE_PROTOCOL_TEXT = "file://";
49     
50     static List JavaDoc completionResults(int offset, BaseDocument doc, String JavaDoc valuePart, boolean requiresFileProtocolPrefix) {
51         List JavaDoc<CompletionQuery.ResultItem> res = new ArrayList JavaDoc<CompletionQuery.ResultItem>();
52         
53         //value part (we support for the CC) may be:
54
// file:///user/marek/myWSDL.xml - absolute
55
// file://conf/myWSDL.xml - relative (to the project root || to the current source?????)
56

57         boolean valueQuoted = valuePart.startsWith("\"");
58         valuePart = Utils.unquote(valuePart);
59         
60         if(requiresFileProtocolPrefix) {
61             if(FILE_PROTOCOL_TEXT.indexOf(valuePart) == 0 && FILE_PROTOCOL_TEXT.length() != valuePart.length() || valuePart.length() == 0) {
62                 //complete file://
63
WSResultItem ri = new WSResultItem.FileProtocolResultItem(valueQuoted);
64                 res.add(0, ri);
65                 ri.setSubstituteOffset(offset - valuePart.length());
66                 
67                 return res;
68             }
69             
70             if(!valuePart.startsWith(FILE_PROTOCOL_TEXT)) {
71                 return Collections.EMPTY_LIST;
72             }
73             //cut off the file:// part
74
valuePart = valuePart.substring(FILE_PROTOCOL_TEXT.length());
75         } else {
76             //must start with quote if not file:// prefix
77
if(!valueQuoted) return Collections.EMPTY_LIST;
78         }
79         
80         //test if absolute path
81
if(valuePart.startsWith("/")) {
82             //we cannot do that now
83

84             //FileUtil.!!!!!!!!!!!!!!
85
//public static FileObject toFileObject(File file) {
86

87             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,"completion of absolute paths hasn't been implemented yet!");
88             return Collections.EMPTY_LIST;
89         }
90         
91         String JavaDoc path = ""; // NOI18N
92
String JavaDoc fileNamePart = valuePart;
93         int lastSlash = valuePart.lastIndexOf('/');
94         if (lastSlash == 0) {
95             path = "/"; // NOI18N
96
fileNamePart = valuePart.substring(1);
97         } else if (lastSlash > 0) { // not a leading slash?
98
path = valuePart.substring(0, lastSlash);
99             fileNamePart = (lastSlash == valuePart.length())? "": valuePart.substring(lastSlash+1); // NOI18N
100
}
101         
102         try {
103             FileObject orig = NbEditorUtilities.getFileObject(doc);
104             if(orig == null) {
105                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot get FileObject from the edited document!");
106                 return res;
107             }
108             
109             Project project = FileOwnerQuery.getOwner(orig);
110             FileObject base = project.getProjectDirectory();
111             
112             // need to normalize fileNamePart with respect to orig
113
String JavaDoc normalizedPath = resolveRelativeURL("/"+orig.getPath(), path); // NOI18N
114
//is this absolute path?
115
if (path.startsWith("/"))
116                 normalizedPath = base.getPath() + path;
117             else
118                 normalizedPath = normalizedPath.substring(1);
119             
120             
121             FileSystem fs = orig.getFileSystem();
122             FileObject folder = fs.findResource(normalizedPath);
123             if (folder != null) {
124                 res = files(folder, fileNamePart);
125                 if (!folder.equals(base) && !path.startsWith("/") // NOI18N
126
&& ((path.length() == 0 && fileNamePart.length() == 0) || (path.lastIndexOf("../")+3 == path.length()))){ // NOI18N
127
res.add(0, new WSResultItem.PackageResultItem("../")); // NOI18N
128
}
129             }
130         } catch (FileStateInvalidException ex) {
131             // unreachable FS - disable completion
132
} catch (IllegalArgumentException JavaDoc ex) {
133             // resolving failed
134
}
135         int itemOffset = offset - valuePart.length() + lastSlash + 1; // works even with -1
136
int itemLength = fileNamePart.length();
137         
138         
139         //set substitute offset
140
Iterator JavaDoc i = res.iterator();
141         while(i.hasNext()) {
142             WSResultItem resultItem = (WSResultItem)i.next();
143             resultItem.setSubstituteOffset(itemOffset);
144         }
145         
146         return res;
147     }
148     
149     private static List JavaDoc<CompletionQuery.ResultItem> files(FileObject folder, String JavaDoc prefix) {
150         ArrayList JavaDoc<CompletionQuery.ResultItem> res = new ArrayList JavaDoc<CompletionQuery.ResultItem>();
151         TreeMap JavaDoc<String JavaDoc,CompletionQuery.ResultItem> resFolders = new TreeMap JavaDoc<String JavaDoc,CompletionQuery.ResultItem>();
152         TreeMap JavaDoc<String JavaDoc,CompletionQuery.ResultItem> resFiles = new TreeMap JavaDoc<String JavaDoc,CompletionQuery.ResultItem>();
153         
154         Enumeration JavaDoc files = folder.getChildren(false);
155         while (files.hasMoreElements()) {
156             FileObject file = (FileObject)files.nextElement();
157             String JavaDoc fname = file.getNameExt();
158             if (fname.startsWith(prefix) && !"cvs".equalsIgnoreCase(fname)) {
159                 
160                 if (file.isFolder())
161                     resFolders.put(file.getNameExt(), new WSResultItem.PackageResultItem(file.getNameExt() + "/"));
162                 else{
163                     Image JavaDoc icon = getIcon(file); //try to get image from the FO
164
resFiles.put(file.getNameExt(), new WSResultItem.FileResultItem(file.getNameExt(), icon));
165                 }
166             }
167         }
168         res.addAll(resFolders.values());
169         res.addAll(resFiles.values());
170         
171         return res;
172     }
173     
174     private static Image JavaDoc getIcon(FileObject fo) {
175         Image JavaDoc icon = null;
176         try {
177             icon = DataObject.find(fo).getNodeDelegate().getIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
178         } catch(org.openide.loaders.DataObjectNotFoundException e) {
179             e.printStackTrace(System.out);
180         }
181         return icon;
182     }
183     
184     /** Returns an absolute context URL (starting with '/') for a relative URL and base URL.
185      * @param relativeTo url to which the relative URL is related. Treated as directory iff
186      * ends with '/'
187      * @param url the relative URL by RFC 2396
188      * @exception IllegalArgumentException if url is not absolute and relativeTo
189      * can not be related to, or if url is intended to be a directory
190      */

191     private static String JavaDoc resolveRelativeURL(String JavaDoc relativeTo, String JavaDoc url) {
192         //System.out.println("- resolving " + url + " relative to " + relativeTo);
193
String JavaDoc result;
194         if (url.startsWith("/")) { // NOI18N
195
result = "/"; // NOI18N
196
url = url.substring(1);
197         } else {
198             // canonize relativeTo
199
if ((relativeTo == null) || (!relativeTo.startsWith("/"))) // NOI18N
200
throw new IllegalArgumentException JavaDoc();
201             relativeTo = resolveRelativeURL(null, relativeTo);
202             int lastSlash = relativeTo.lastIndexOf('/');
203             if (lastSlash == -1)
204                 throw new IllegalArgumentException JavaDoc();
205             result = relativeTo.substring(0, lastSlash + 1);
206         }
207         
208         // now url does not start with '/' and result starts with '/' and ends with '/'
209
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url, "/", true); // NOI18N
210
while(st.hasMoreTokens()) {
211             String JavaDoc tok = st.nextToken();
212             //System.out.println("token : \"" + tok + "\""); // NOI18N
213
if (tok.equals("/")) { // NOI18N
214
if (!result.endsWith("/")) // NOI18N
215
result = result + "/"; // NOI18N
216
} else
217                 if (tok.equals("")) // NOI18N
218
; // do nohing
219
else
220                     if (tok.equals(".")) // NOI18N
221
; // do nohing
222
else
223                         if (tok.equals("..")) { // NOI18N
224
String JavaDoc withoutSlash = result.substring(0, result.length() - 1);
225                 int ls = withoutSlash.lastIndexOf("/"); // NOI18N
226
if (ls != -1)
227                     result = withoutSlash.substring(0, ls + 1);
228                         } else {
229                 // some file
230
result = result + tok;
231                         }
232             //System.out.println("result : " + result); // NOI18N
233
}
234         //System.out.println("- resolved to " + result);
235
return result;
236     }
237 }
238
Popular Tags