/plugins/ProjectViewer/tags/pv_2_1_3_5/projectviewer/persist/OldConfigLoader.java

# · Java · 255 lines · 149 code · 45 blank · 61 comment · 41 complexity · ba7eaca3af453a1c9668ed85f0284c2b MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.persist;
  20. //{{{ Imports
  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.util.Stack;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.Properties;
  29. import org.gjt.sp.util.Log;
  30. import org.gjt.sp.jedit.jEdit;
  31. import projectviewer.vpt.VPTNode;
  32. import projectviewer.vpt.VPTFile;
  33. import projectviewer.vpt.VPTProject;
  34. import projectviewer.vpt.VPTRoot;
  35. import projectviewer.vpt.VPTDirectory;
  36. import projectviewer.ProjectPlugin;
  37. import projectviewer.ProjectManager;
  38. //}}}
  39. /**
  40. * Class that takes care of loading old config files into the new virtual-tree
  41. * object style. Since saving is always done in the new way, there's no need
  42. * to worry about saving in the old format.
  43. *
  44. * @author Marcelo Vanzin
  45. * @version $Id: OldConfigLoader.java 6295 2004-12-06 02:50:27Z vanza $
  46. */
  47. public final class OldConfigLoader {
  48. //{{{ Constants
  49. private static final String OLD_CONFIG_FILE = "projects.properties";
  50. private static final String OLD_PRJ_CONFIG_DIR = "projects" + File.separator;
  51. private static final String OLD_PRJ_CONFIG_FILE = "files.properties";
  52. //}}}
  53. /** Private constructor. No instances! */
  54. private OldConfigLoader() { }
  55. //{{{ load(ProjectManager) method
  56. /** Loads the old configuration data into the new object style. */
  57. public static void load(ProjectManager manager) throws IOException {
  58. File oldPropsFile = new File(ProjectPlugin.getResourcePath(OLD_CONFIG_FILE));
  59. String oldFileList = ProjectPlugin.getResourcePath(OLD_PRJ_CONFIG_FILE);
  60. if (!oldPropsFile.exists()) {
  61. oldPropsFile = new File(jEdit.getSettingsDirectory(), "projectviewer/projects.properties");
  62. if (!oldPropsFile.exists())
  63. return;
  64. oldFileList = jEdit.getSettingsDirectory() + File.separator + "projectviewer/files.properties";
  65. }
  66. Properties pList = loadProperties(oldPropsFile.getAbsolutePath(), true);
  67. Properties oldProps = null;
  68. int counter = 1;
  69. String prjName = pList.getProperty( "project." + counter );
  70. while ( prjName != null ) {
  71. String root = pList.getProperty( "project." + counter + ".root" );
  72. VPTProject p = new VPTProject(prjName);
  73. if ( root != null ) {
  74. if ( oldProps == null ) {
  75. oldProps = loadProperties(oldFileList, true);
  76. }
  77. p.setRootPath(root);
  78. loadProjectFromProps(p, oldProps, counter);
  79. } else {
  80. loadProjectFromFile(p, counter);
  81. }
  82. manager.addProject(p, VPTRoot.getInstance());
  83. prjName = pList.getProperty( "project." + ( ++counter ) );
  84. }
  85. // TODO: cleanup old config after conversion?
  86. } //}}}
  87. //{{{ loadProjectFromFile(VPTProject, int) method
  88. /**
  89. * Loads the project data from the file located in the old project
  90. * file directory.
  91. *
  92. * @param p The project where to load the data.
  93. * @param idx The index of the properties file to load.
  94. */
  95. private static void loadProjectFromFile(VPTProject p, int idx) {
  96. Properties props = null;
  97. try {
  98. props = loadProperties(OLD_PRJ_CONFIG_DIR + "project" + idx + ".properties", false);
  99. } catch (IOException ioe) {
  100. Log.log(Log.ERROR,OldConfigLoader.class,ioe);
  101. }
  102. if (props == null) return;
  103. HashMap paths = new HashMap();
  104. // ensures that the path does not have a trailing '/'
  105. p.setRootPath(new File(props.getProperty("root")).getAbsolutePath());
  106. String tmp = props.getProperty("webroot");
  107. if (tmp != null && !tmp.equals("http://<projecturl>"))
  108. p.setURL(tmp);
  109. if (props.get("folderTreeState") != null) {
  110. p.setProperty("projectviewer.folder_tree_state", (String) props.get("folderTreeState"));
  111. }
  112. for (Iterator it = props.keySet().iterator(); it.hasNext(); ) {
  113. String key = (String) it.next();
  114. if (key.startsWith("file")) {
  115. File f = new File(props.getProperty(key));
  116. VPTNode parent = ensureDirAdded(p, f.getParent(), paths);
  117. VPTFile vf = new VPTFile(f);
  118. p.registerFile(vf);
  119. parent.add(vf);
  120. } else if (key.startsWith("open_files")) {
  121. p.addOpenFile(props.getProperty(key));
  122. } else if (key.equals("buildfile")) {
  123. p.setProperty("antelope.buildfile", props.getProperty(key));
  124. }
  125. }
  126. } //}}}
  127. //{{{ loadProjectFromProps(VPTProject, Properties) method
  128. /**
  129. * Loads the project from the given properties. Keys are expected to be
  130. * in the reeealy old format (pre-1.0.3), when all projects were stored
  131. * in the same file.
  132. */
  133. private static void loadProjectFromProps(VPTProject p, Properties props, int idx) {
  134. int counter = 1;
  135. String prefix = "file.";
  136. String suffix = ".project." + idx;
  137. String fileName = props.getProperty(prefix + counter + suffix);
  138. HashMap paths = new HashMap();
  139. while (fileName != null) {
  140. File f = new File(fileName);
  141. VPTNode parent = ensureDirAdded(p, f.getParent(), paths);
  142. VPTFile vf = new VPTFile(f);
  143. p.registerFile(vf);
  144. parent.add(vf);
  145. fileName = props.getProperty(prefix + (++counter) + suffix);
  146. }
  147. } //}}}
  148. //{{{ ensureDirAdded(VPTProject, HashMap)
  149. /**
  150. * Ensure that the given directory and all ancestors up to the root
  151. * of the project are added to the project.
  152. *
  153. * @param p The project.
  154. * @param path The path to the directory to check.
  155. * @param dirs A HashMap containing the already added dirs (key = path).
  156. * @return The VPTNode representing the given directory.
  157. */
  158. private static VPTNode ensureDirAdded(VPTProject p, String path, HashMap dirs) {
  159. if (path.equals(p.getRootPath())) {
  160. return p;
  161. }
  162. if (dirs.get(path) != null) {
  163. return (VPTNode) dirs.get(path);
  164. }
  165. Stack toAdd = new Stack();
  166. VPTNode dir = new VPTDirectory(new File(path));
  167. dirs.put(path, dir);
  168. toAdd.push(dir);
  169. VPTNode where = null;
  170. String parent = new File(path).getParent();
  171. while (where == null) {
  172. if (dirs.get(parent) != null) {
  173. where = (VPTNode) dirs.get(parent);
  174. } else {
  175. if (parent.equals(p.getRootPath())) {
  176. where = p;
  177. } else {
  178. dir = new VPTDirectory(new File(parent));
  179. dirs.put(parent, dir);
  180. toAdd.push(dir);
  181. parent = new File(parent).getParent();
  182. }
  183. }
  184. }
  185. while (!toAdd.isEmpty()) {
  186. VPTNode o = (VPTNode) toAdd.pop();
  187. where.add(o);
  188. where = o;
  189. }
  190. return where;
  191. } //}}}
  192. //{{{ loadProperties(String, boolean) method
  193. /** Load the specified properties file. */
  194. private static Properties loadProperties(String fileName, boolean abs) throws IOException {
  195. Properties props = new Properties();
  196. InputStream in = null;
  197. try {
  198. if (abs) {
  199. in = new FileInputStream(fileName);
  200. } else {
  201. in = ProjectPlugin.getResourceAsStream(fileName);
  202. }
  203. if ( in != null )
  204. props.load(in);
  205. return props;
  206. } finally {
  207. if (in != null) in.close();
  208. }
  209. } //}}}
  210. }