KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > SourceAttachmentManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.*;
14 import java.util.*;
15
16 import org.eclipse.core.runtime.*;
17
18 public class SourceAttachmentManager {
19     private Hashtable entries;
20     private static final String JavaDoc KEY_PLATFORM_PATH = "platform-path"; //$NON-NLS-1$
21

22     public static class SourceAttachmentEntry {
23         private IPath entryPath;
24         private IPath attachmentPath;
25         private IPath attachmentRootPath;
26         
27         public SourceAttachmentEntry(IPath entryPath, IPath attachmentPath, IPath attachmentRootPath) {
28             this.entryPath = entryPath;
29             this.attachmentPath = attachmentPath;
30             this.attachmentRootPath = attachmentRootPath;
31         }
32         
33         public IPath getEntryPath() {
34             return entryPath;
35         }
36         public IPath getAttachmentPath() {
37             return attachmentPath;
38         }
39         
40         public IPath getAttachmentRootPath() {
41             return attachmentRootPath;
42         }
43     }
44     /**
45      * Constructor for SourceAttachementManager.
46      */

47     public SourceAttachmentManager() {
48         entries = new Hashtable();
49         initialize();
50     }
51     
52     public boolean isEmpty() {
53         return entries.isEmpty();
54     }
55     
56     public SourceAttachmentEntry findEntry(IPath entryPath) {
57         return (SourceAttachmentEntry)entries.get(entryPath);
58     }
59     
60     public void addEntry(IPath libraryPath, IPath attachmentPath, IPath attachmentRootPath) {
61         entries.put(libraryPath, new SourceAttachmentEntry(libraryPath, attachmentPath, attachmentRootPath));
62     }
63     
64     private String JavaDoc getFileName() {
65         IPath stateLocation = PDECore.getDefault().getStateLocation();
66         IPath stateFile = stateLocation.append("sourceAttachements.properties"); //$NON-NLS-1$
67
return stateFile.toOSString();
68     }
69
70     private void initialize() {
71         String JavaDoc fileName = getFileName();
72         Properties properties = new Properties();
73         try {
74             FileInputStream fis = new FileInputStream(fileName);
75             properties.load(fis);
76             parseProperties(properties);
77             fis.close();
78         }
79         catch (IOException e) {
80         }
81     }
82     
83     private void parseProperties(Properties properties) {
84         String JavaDoc platformPath = properties.getProperty(KEY_PLATFORM_PATH);
85         if (platformPath==null) return;
86         IPath oldPlatformPath = new Path(platformPath);
87         IPath currentPlatformPath = ExternalModelManager.getEclipseHome();
88         // If the saved entries are for a different platform path,
89
// discard them.
90
if (oldPlatformPath.equals(currentPlatformPath)==false) return;
91         for (Enumeration keys = properties.keys(); keys.hasMoreElements();) {
92             String JavaDoc key = (String JavaDoc)keys.nextElement();
93             if (key.startsWith("entry.")) //$NON-NLS-1$
94
parseEntryProperty(properties.getProperty(key));
95         }
96     }
97     
98     private void parseEntryProperty(String JavaDoc value) {
99         int semi = value.indexOf(';');
100         
101         String JavaDoc library = value.substring(0, semi);
102         String JavaDoc paths = value.substring(semi+1);
103         
104         semi = paths.indexOf(";"); //$NON-NLS-1$
105

106         String JavaDoc att, attRoot=null;
107         if (semi!= -1) {
108             att = paths.substring(0, semi);
109             attRoot = paths.substring(semi+1);
110         }
111         else
112             att = paths;
113         addEntry(new Path(library), new Path(att), attRoot!=null?new Path(attRoot):null);
114     }
115     
116     public void save() {
117         String JavaDoc fileName = getFileName();
118         Properties properties = new Properties();
119         IPath platformPath = ExternalModelManager.getEclipseHome();
120         properties.setProperty(KEY_PLATFORM_PATH, platformPath.toOSString());
121         
122         int i=0;
123         for (Enumeration keys=entries.keys(); keys.hasMoreElements();) {
124             IPath entryPath = (IPath)keys.nextElement();
125             SourceAttachmentEntry entry = (SourceAttachmentEntry)entries.get(entryPath);
126             String JavaDoc library = entry.getEntryPath().toOSString();
127             String JavaDoc value;
128             if (entry.getAttachmentRootPath()!=null)
129                 value = library+";"+entry.getAttachmentPath().toOSString()+";"+entry.getAttachmentRootPath().toOSString(); //$NON-NLS-1$ //$NON-NLS-2$
130
else
131                 value = library+";"+entry.getAttachmentPath().toOSString(); //$NON-NLS-1$
132
i++;
133             properties.setProperty("entry."+i, value); //$NON-NLS-1$
134
}
135         try {
136             FileOutputStream fos = new FileOutputStream(fileName);
137             properties.store(fos, "User-defined source attachments"); //$NON-NLS-1$
138
fos.flush();
139             fos.close();
140         }
141         catch (IOException e) {
142         }
143     }
144 }
145
Popular Tags