KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > refactoring > Utility


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.apisupport.refactoring;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
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"); // NOI18N
42

43     /** Creates a new instance of Utility */
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     /**
74      * Creates full class name from package name and simple class name
75      * @param pkg package name
76      * @param simpleName simple class name
77      * @return full class name
78      */

79     public static String JavaDoc getClassName(String JavaDoc pkg, final String JavaDoc simpleName) {
80         return (pkg == null || pkg.length() == 0 ? "" : pkg + ".") + simpleName; // NOI18N
81
}
82     
83     static void writeFileFromString(FileObject fileObject, String JavaDoc content) {
84         if (content == null) {
85             return;
86         }
87         FileLock lock = null;
88         PrintWriter JavaDoc writer = null;
89         try {
90             lock = fileObject.lock();
91             writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(fileObject.getOutputStream(lock), "UTF-8")); // NOI18N
92
writer.print(content);
93             
94         } catch (IOException JavaDoc exc) {
95             //TODO
96
} finally {
97             if (writer != null) {
98                 writer.close();
99             }
100             if (lock != null) {
101                 lock.releaseLock();
102             }
103         }
104         
105     }
106     
107     static String JavaDoc readFileIntoString(FileObject fileObject) {
108         BufferedReader JavaDoc reader = null;
109         String JavaDoc content = null;
110         try {
111             StringWriter JavaDoc writer =new StringWriter JavaDoc();
112             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fileObject.getInputStream(), "UTF-8")); // NOI18N
113
int chr = reader.read();
114             while (chr != -1) {
115                 writer.write(chr);
116                 chr = reader.read();
117             }
118             content = writer.toString();
119         } catch (IOException JavaDoc exc) {
120             //TODO
121
} finally {
122             if (reader != null) {
123                 try {
124                     reader.close();
125                 } catch (IOException JavaDoc x) {
126                     // ignore
127
}
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"); //NOI18N
138
if (fo != null) {
139                 return fo;
140             }
141         }
142         return null;
143     }
144
145 }
146
Popular Tags