1 19 20 package org.netbeans.modules.websvc.editor.completion; 21 22 import java.awt.Image ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 import java.util.TreeMap ; 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 46 public class FileAttributeSupport { 47 48 private static String FILE_PROTOCOL_TEXT = "file://"; 49 50 static List completionResults(int offset, BaseDocument doc, String valuePart, boolean requiresFileProtocolPrefix) { 51 List <CompletionQuery.ResultItem> res = new ArrayList <CompletionQuery.ResultItem>(); 52 53 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 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 valuePart = valuePart.substring(FILE_PROTOCOL_TEXT.length()); 75 } else { 76 if(!valueQuoted) return Collections.EMPTY_LIST; 78 } 79 80 if(valuePart.startsWith("/")) { 82 84 87 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,"completion of absolute paths hasn't been implemented yet!"); 88 return Collections.EMPTY_LIST; 89 } 90 91 String path = ""; String fileNamePart = valuePart; 93 int lastSlash = valuePart.lastIndexOf('/'); 94 if (lastSlash == 0) { 95 path = "/"; fileNamePart = valuePart.substring(1); 97 } else if (lastSlash > 0) { path = valuePart.substring(0, lastSlash); 99 fileNamePart = (lastSlash == valuePart.length())? "": valuePart.substring(lastSlash+1); } 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 String normalizedPath = resolveRelativeURL("/"+orig.getPath(), path); 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("/") && ((path.length() == 0 && fileNamePart.length() == 0) || (path.lastIndexOf("../")+3 == path.length()))){ res.add(0, new WSResultItem.PackageResultItem("../")); } 129 } 130 } catch (FileStateInvalidException ex) { 131 } catch (IllegalArgumentException ex) { 133 } 135 int itemOffset = offset - valuePart.length() + lastSlash + 1; int itemLength = fileNamePart.length(); 137 138 139 Iterator 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 <CompletionQuery.ResultItem> files(FileObject folder, String prefix) { 150 ArrayList <CompletionQuery.ResultItem> res = new ArrayList <CompletionQuery.ResultItem>(); 151 TreeMap <String ,CompletionQuery.ResultItem> resFolders = new TreeMap <String ,CompletionQuery.ResultItem>(); 152 TreeMap <String ,CompletionQuery.ResultItem> resFiles = new TreeMap <String ,CompletionQuery.ResultItem>(); 153 154 Enumeration files = folder.getChildren(false); 155 while (files.hasMoreElements()) { 156 FileObject file = (FileObject)files.nextElement(); 157 String 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 icon = getIcon(file); 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 getIcon(FileObject fo) { 175 Image 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 191 private static String resolveRelativeURL(String relativeTo, String url) { 192 String result; 194 if (url.startsWith("/")) { result = "/"; url = url.substring(1); 197 } else { 198 if ((relativeTo == null) || (!relativeTo.startsWith("/"))) throw new IllegalArgumentException (); 201 relativeTo = resolveRelativeURL(null, relativeTo); 202 int lastSlash = relativeTo.lastIndexOf('/'); 203 if (lastSlash == -1) 204 throw new IllegalArgumentException (); 205 result = relativeTo.substring(0, lastSlash + 1); 206 } 207 208 StringTokenizer st = new StringTokenizer (url, "/", true); while(st.hasMoreTokens()) { 211 String tok = st.nextToken(); 212 if (tok.equals("/")) { if (!result.endsWith("/")) result = result + "/"; } else 217 if (tok.equals("")) ; else 220 if (tok.equals(".")) ; else 223 if (tok.equals("..")) { String withoutSlash = result.substring(0, result.length() - 1); 225 int ls = withoutSlash.lastIndexOf("/"); if (ls != -1) 227 result = withoutSlash.substring(0, ls + 1); 228 } else { 229 result = result + tok; 231 } 232 } 234 return result; 236 } 237 } 238 | Popular Tags |