001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2003 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.project;
053:
054: import java.io.File;
055: import java.io.FileOutputStream;
056: import java.io.FileInputStream;
057: import java.util.Properties;
058: import java.util.List;
059: import java.util.ArrayList;
060: import org.acm.seguin.util.FileSettings;
061:
062: /**
063: * Hold data on the known projects.
064: *
065: *@author Mike Atkinson
066: *@created August 29, 2003
067: *@since jrefactory 2.8.01
068: *@version $Id: Project.java,v 1.6 2003/10/10 16:45:00 mikeatkinson Exp $
069: */
070: public class Project {
071: private static Project[] knownProjects = new Project[0];
072: private static Project currentProject = null;
073:
074: public static Project getCurrentProject() {
075: return currentProject;
076: }
077:
078: public static String getCurrentProjectName() {
079: if (currentProject == null) {
080: return null;
081: }
082: return currentProject.projectName;
083: }
084:
085: public static void setCurrentProject(Project project) {
086: currentProject = project;
087: }
088:
089: public static void loadProjects() {
090: List projects = new ArrayList();
091: File projectsRoot = getProjectsRoot();
092: File[] files = projectsRoot.listFiles();
093: for (int i = 0; i < files.length; i++) {
094: if (files[i].isDirectory()) {
095: File projFile = new File(files[i], "proj.settings");
096: Project proj = new Project(files[i].getName());
097: }
098: }
099: if (projects.size() > 0) {
100: knownProjects = (Project[]) projects
101: .toArray(new Project[projects.size()]);
102: } else {
103: knownProjects = new Project[] { new Project("default") };
104: }
105: }
106:
107: public static void storeProjects() {
108: for (int i = 0; i < knownProjects.length; i++) {
109: try {
110: knownProjects[i].settings.save();
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114: }
115: }
116:
117: private static File getProjectsRoot() {
118: File root = FileSettings.getRefactorySettingsRoot();
119: File projDir = new File(root, "projects");
120: if (!projDir.exists()) {
121: projDir.mkdir();
122: }
123: return projDir;
124: }
125:
126: public static Project[] getProjects() {
127: return knownProjects;
128: }
129:
130: public static Project getProject(String name) {
131: for (int i = 0; i < knownProjects.length; i++) {
132: if (knownProjects[i].projectName.equals(name)) {
133: return knownProjects[i];
134: }
135: }
136: return null;
137: }
138:
139: public static Project createProject(String name) {
140: Project project = getProject(name);
141: if (project == null) {
142: project = new Project(name);
143: Project[] pa = new Project[knownProjects.length + 1];
144: System.arraycopy(knownProjects, 0, pa, 0,
145: knownProjects.length);
146: pa[knownProjects.length] = project;
147: knownProjects = pa;
148: }
149: return project;
150: }
151:
152: private FileSettings settings = null;
153: private String projectName = null;
154:
155: private Project(String projectName) {
156: this .projectName = projectName;
157: settings = FileSettings.getSettings(projectName, "Refactory",
158: "proj");
159: if (projectName == null || projectName.length() == 0
160: || "default".equals(projectName)) {
161: if (!settings.isLocalProperty("CLASSPATH")) {
162: settings.setString("CLASSPATH", ".");
163: settings.setString("BASE_DIR", "");
164: }
165: }
166: /*
167: File projectsRoot = getProjectsRoot();
168: File projDir = new File(projectsRoot, projectName);
169: if (!projDir.exists()) {
170: projDir.mkdir();
171: }
172: File projFile = new File(projDir, "proj.settings");
173: if (!projFile.exists()) {
174: try {
175: projFile.createNewFile();
176: settings = new FileSettings(projFile);
177: setProperty("CLASSPATH", ".");
178: setProperty("BASE_DIR", "");
179: } catch (java.io.IOException e) {
180: e.printStackTrace();
181: }
182: if (!"default".equals(name)) {
183: File root = FileSettings.getRefactorySettingsRoot();
184: File prettyFile = new File(projDir, "pretty.settings");
185: try {
186: java.io.PrintWriter pw = new java.io.PrintWriter(new java.io.FileWriter(prettyFile));
187: pw.println("# project "+name+" pretty.settings");
188: pw.close();
189: } catch (java.io.IOException e) {
190: }
191: }
192: } else {
193: settings = new FileSettings(projFile);
194: }
195: */
196: }
197:
198: public String getProperty(String propName) {
199: //System.out.println("Project.getProperty("+propName+")");
200: if (settings != null) {
201: return settings.getProperty(propName);
202: }
203: return null;
204: }
205:
206: public String getProperty(String propName, String defaultValue) {
207: //System.out.println("Project.getProperty("+propName+","+defaultValue+")");
208: try {
209: if (settings != null) {
210: return settings.getProperty(propName, defaultValue);
211: }
212: } catch (org.acm.seguin.util.SettingNotFoundException e) {
213: }
214: return defaultValue;
215: }
216:
217: public void setProperty(String propName, String value) {
218: settings.getProperty(propName, value);
219: }
220:
221: public String getClassPath() {
222: return getProperty("CLASSPATH", "");
223: }
224:
225: public void setClassPath(String classpath) {
226: setProperty("CLASSPATH", classpath);
227: }
228:
229: public String getBaseDir() {
230: return getProperty("BASE_DIR", "");
231: }
232:
233: public void setBaseDir(String baseDir) {
234: setProperty("BASE_DIR", baseDir);
235: }
236:
237: /**
238: * Logs a message through the project object if one has been provided.
239: *
240: * @param message The message to log.
241: * Should not be <code>null</code>.
242: *
243: * @param priority The logging priority of the message.
244: */
245: protected void log(String message) {
246: System.out.println(message);
247: }
248:
249: public File resolveFile(String name) throws ProjectException {
250: return new File(name);
251: }
252: }
|