1 19 20 package org.netbeans.modules.apisupport.refactoring; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectUtils; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.modules.refactoring.api.Problem; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileObject; 37 38 39 public class Utility { 40 41 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring"); 43 44 private Utility() { 45 } 46 47 48 public static Problem addProblemsToEnd(Problem where, Problem what) { 49 Problem whereCopy = where; 50 err.log("Where: " + where); 51 err.log("What: " + what); 52 if (what != null) { 53 if (where == null) { 54 whereCopy = what; 55 } else { 56 while (where.getNext() != null) { 57 where = where.getNext(); 58 } 59 err.log("Last where: " + where); 60 while (what != null) { 61 Problem elem = what; 62 err.log("Elem: " + elem); 63 where.setNext(elem); 64 where = where.getNext(); 65 what = what.getNext(); 66 } 67 } 68 } 69 err.log("wherecopy return: " + whereCopy); 70 return whereCopy; 71 } 72 73 79 public static String getClassName(String pkg, final String simpleName) { 80 return (pkg == null || pkg.length() == 0 ? "" : pkg + ".") + simpleName; } 82 83 static void writeFileFromString(FileObject fileObject, String content) { 84 if (content == null) { 85 return; 86 } 87 FileLock lock = null; 88 PrintWriter writer = null; 89 try { 90 lock = fileObject.lock(); 91 writer = new PrintWriter (new OutputStreamWriter (fileObject.getOutputStream(lock), "UTF-8")); writer.print(content); 93 94 } catch (IOException exc) { 95 } finally { 97 if (writer != null) { 98 writer.close(); 99 } 100 if (lock != null) { 101 lock.releaseLock(); 102 } 103 } 104 105 } 106 107 static String readFileIntoString(FileObject fileObject) { 108 BufferedReader reader = null; 109 String content = null; 110 try { 111 StringWriter writer =new StringWriter (); 112 reader = new BufferedReader (new InputStreamReader (fileObject.getInputStream(), "UTF-8")); int chr = reader.read(); 114 while (chr != -1) { 115 writer.write(chr); 116 chr = reader.read(); 117 } 118 content = writer.toString(); 119 } catch (IOException exc) { 120 } finally { 122 if (reader != null) { 123 try { 124 reader.close(); 125 } catch (IOException x) { 126 } 128 } 129 } 130 return content; 131 } 132 133 public static final FileObject findMetaInfServices(Project project) { 134 Sources srcs = ProjectUtils.getSources(project); 135 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 136 for (int i = 0; i < grps.length; i++) { 137 FileObject fo = grps[i].getRootFolder().getFileObject("META-INF/services"); if (fo != null) { 139 return fo; 140 } 141 } 142 return null; 143 } 144 145 } 146 | Popular Tags |