001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.refactoring;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStreamReader;
047: import java.io.OutputStreamWriter;
048: import java.io.PrintWriter;
049: import java.io.StringWriter;
050: import org.netbeans.api.java.project.JavaProjectConstants;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectUtils;
053: import org.netbeans.api.project.SourceGroup;
054: import org.netbeans.api.project.Sources;
055: import org.netbeans.modules.refactoring.api.Problem;
056: import org.openide.ErrorManager;
057: import org.openide.filesystems.FileLock;
058: import org.openide.filesystems.FileObject;
059:
060: public class Utility {
061:
062: private static final ErrorManager err = ErrorManager.getDefault()
063: .getInstance("org.netbeans.modules.j2ee.refactoring"); // NOI18N
064:
065: /** Creates a new instance of Utility */
066: private Utility() {
067: }
068:
069: public static Problem addProblemsToEnd(Problem where, Problem what) {
070: Problem whereCopy = where;
071: err.log("Where: " + where);
072: err.log("What: " + what);
073: if (what != null) {
074: if (where == null) {
075: whereCopy = what;
076: } else {
077: while (where.getNext() != null) {
078: where = where.getNext();
079: }
080: err.log("Last where: " + where);
081: while (what != null) {
082: Problem elem = what;
083: err.log("Elem: " + elem);
084: where.setNext(elem);
085: where = where.getNext();
086: what = what.getNext();
087: }
088: }
089: }
090: err.log("wherecopy return: " + whereCopy);
091: return whereCopy;
092: }
093:
094: /**
095: * Creates full class name from package name and simple class name
096: * @param pkg package name
097: * @param simpleName simple class name
098: * @return full class name
099: */
100: public static String getClassName(String pkg,
101: final String simpleName) {
102: return (pkg == null || pkg.length() == 0 ? "" : pkg + ".")
103: + simpleName; // NOI18N
104: }
105:
106: static void writeFileFromString(FileObject fileObject,
107: String content) {
108: if (content == null) {
109: return;
110: }
111: FileLock lock = null;
112: PrintWriter writer = null;
113: try {
114: lock = fileObject.lock();
115: writer = new PrintWriter(new OutputStreamWriter(fileObject
116: .getOutputStream(lock), "UTF-8")); // NOI18N
117: writer.print(content);
118:
119: } catch (IOException exc) {
120: //TODO
121: } finally {
122: if (writer != null) {
123: writer.close();
124: }
125: if (lock != null) {
126: lock.releaseLock();
127: }
128: }
129:
130: }
131:
132: static String readFileIntoString(FileObject fileObject) {
133: BufferedReader reader = null;
134: String content = null;
135: try {
136: StringWriter writer = new StringWriter();
137: reader = new BufferedReader(new InputStreamReader(
138: fileObject.getInputStream(), "UTF-8")); // NOI18N
139: int chr = reader.read();
140: while (chr != -1) {
141: writer.write(chr);
142: chr = reader.read();
143: }
144: content = writer.toString();
145: } catch (IOException exc) {
146: //TODO
147: } finally {
148: if (reader != null) {
149: try {
150: reader.close();
151: } catch (IOException x) {
152: // ignore
153: }
154: }
155: }
156: return content;
157: }
158:
159: public static final FileObject findMetaInfServices(Project project) {
160: Sources srcs = ProjectUtils.getSources(project);
161: SourceGroup[] grps = srcs
162: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES);
163: for (int i = 0; i < grps.length; i++) {
164: FileObject fo = grps[i].getRootFolder().getFileObject(
165: "META-INF/services"); //NOI18N
166: if (fo != null) {
167: return fo;
168: }
169: }
170: grps = srcs
171: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
172: for (int i = 0; i < grps.length; i++) {
173: FileObject fo = grps[i].getRootFolder().getFileObject(
174: "META-INF/services"); //NOI18N
175: if (fo != null) {
176: return fo;
177: }
178: }
179: return null;
180: }
181:
182: }
|