KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > TagFileCatalog


1 /*
2  * TagFileCatalog.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: TagFileCatalog.java,v 1.1.1.1 2002/09/24 16:08:01 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Date JavaDoc;
31
32 public final class TagFileCatalog
33 {
34     private File tagfileDir;
35     private File catalogFile;
36     private ArrayList JavaDoc entries = new ArrayList JavaDoc();
37
38     public TagFileCatalog(File tagfileDir)
39     {
40         this.tagfileDir = tagfileDir;
41         catalogFile = File.getInstance(tagfileDir, "catalog");
42     }
43
44     public synchronized void addEntry(File dir, File tagfile, Mode mode)
45     {
46         String JavaDoc directoryPath = dir.canonicalPath();
47         String JavaDoc modeName = mode.toString();
48         for (int i = entries.size()-1; i >= 0; i--) {
49             CatalogEntry entry = getEntry(i);
50             if (entry.directoryPath.equals(directoryPath)) {
51                 if (entry.modeName == null) {
52                     entry.tagfileName = tagfile.getName();
53                     entry.modeName = modeName;
54                     return;
55                 } else if (entry.modeName.equals(modeName)) {
56                     entry.tagfileName = tagfile.getName();
57                     return;
58                 }
59             }
60         }
61         // Not found.
62
entries.add(new CatalogEntry(directoryPath, modeName, tagfile.getName()));
63     }
64
65     public synchronized boolean containsTagFileName(String JavaDoc name)
66     {
67         for (int i = entries.size()-1; i >= 0; i--) {
68             CatalogEntry entry = getEntry(i);
69             if (entry.tagfileName.equals(name))
70                 return true;
71         }
72         // Not found.
73
return false;
74     }
75
76     public synchronized File getTagFile(File dir, Mode mode)
77     {
78         Debug.assertTrue(mode != null);
79         String JavaDoc directoryPath = dir.canonicalPath();
80         String JavaDoc modeName = mode.toString();
81         for (int i = entries.size()-1; i >= 0; i--) {
82             CatalogEntry entry = getEntry(i);
83             if (directoryPath.equals(entry.directoryPath) && modeName.equals(entry.modeName))
84                 return File.getInstance(tagfileDir, entry.tagfileName);
85         }
86         // Not found.
87
return null;
88     }
89
90     public synchronized void update()
91     {
92         boolean changed = false;
93         for (int i = entries.size()-1; i >= 0; i--) {
94             CatalogEntry entry = getEntry(i);
95             File file = File.getInstance(tagfileDir, entry.tagfileName);
96             if (!file.exists()) {
97                 entries.remove(i);
98                 changed = true;
99             }
100         }
101         if (changed)
102             save();
103     }
104
105     public synchronized void load()
106     {
107         try {
108             if (catalogFile.isFile()) {
109                 BufferedReader JavaDoc reader =
110                     new BufferedReader JavaDoc(new InputStreamReader JavaDoc(catalogFile.getInputStream()));
111                 String JavaDoc s;
112                 while ((s = reader.readLine()) != null) {
113                     if (s.trim().startsWith("#"))
114                         continue;
115                     int i = s.indexOf('\t');
116                     if (i < 0) {
117                         // Old or invalid format.
118
Log.error("TagFileCatalog.load old or invalid format");
119                         entries.clear();
120                         break;
121                     }
122                     String JavaDoc tagFileName = s.substring(0, i);
123                     String JavaDoc remaining = s.substring(i+1).trim();
124                     i = remaining.indexOf('\t');
125                     if (i < 0) {
126                         // Invalid format.
127
Log.error("TagFileCatalog.load invalid format");
128                         entries.clear();
129                         break;
130                     }
131                     String JavaDoc modeName = remaining.substring(0, i);
132                     String JavaDoc directoryPath = remaining.substring(i+1).trim();
133                     entries.add(new CatalogEntry(directoryPath, modeName, tagFileName));
134                 }
135                 reader.close();
136             }
137         }
138         catch (IOException JavaDoc e) {
139             Log.error(e);
140         }
141     }
142
143     public synchronized void save()
144     {
145         try {
146             BufferedWriter JavaDoc writer =
147                 new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(catalogFile.getOutputStream()));
148             writer.write("# " + new Date JavaDoc().toString() + '\n');
149             final int limit = entries.size();
150             for (int i = 0; i < limit; i++) {
151                 CatalogEntry entry = getEntry(i);
152                 writer.write(entry.tagfileName);
153                 writer.write('\t');
154                 writer.write(entry.modeName);
155                 writer.write('\t');
156                 writer.write(entry.directoryPath);
157                 writer.write('\n');
158             }
159             writer.flush();
160             writer.close();
161         }
162         catch (IOException JavaDoc e) {
163             Log.error(e);
164         }
165     }
166
167     private final CatalogEntry getEntry(int i)
168     {
169         return (CatalogEntry) entries.get(i);
170     }
171
172     private static class CatalogEntry
173     {
174         final String JavaDoc directoryPath;
175         String JavaDoc modeName;
176         String JavaDoc tagfileName;
177
178         CatalogEntry(String JavaDoc directoryPath, String JavaDoc modeName, String JavaDoc tagfileName)
179         {
180             this.directoryPath = directoryPath;
181             this.modeName = modeName;
182             this.tagfileName = tagfileName;
183         }
184
185         public String JavaDoc toString()
186         {
187             return tagfileName + " " + directoryPath + " " + modeName;
188         }
189     }
190 }
191
Popular Tags