001: // StoreFinder.java
002: // $Id: StoreFinder.java,v 1.7 2002/02/04 17:28:12 cbournez Exp $
003: // (c) COPYRIGHT MIT and INRIA, 2002.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.offline.browse;
007:
008: import org.w3c.tools.resources.serialization.Serializer;
009: import org.w3c.tools.resources.Resource;
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.serialization.ResourceDescription;
012: import org.w3c.tools.resources.serialization.AttributeDescription;
013: import org.w3c.tools.resources.serialization.xml.XMLSerializer;
014: import org.w3c.tools.resources.AttributeHolder;
015:
016: import org.apache.oro.text.regex.Perl5Matcher;
017: import org.apache.oro.text.regex.Perl5Compiler;
018: import org.apache.oro.text.regex.Pattern;
019: import org.apache.oro.text.regex.MalformedPatternException;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.FilenameFilter;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.ObjectInputStream;
028: import java.io.PrintStream;
029: import java.io.Reader;
030: import java.io.Serializable;
031: import java.io.Writer;
032:
033: import java.lang.String;
034:
035: import java.util.Hashtable;
036: import java.util.Stack;
037:
038: /**
039: * <p>The finder that handles a given jigsaw store
040: */
041:
042: public class StoreFinder {
043:
044: public static final String ROOT_REP = "root.xml";
045: public static final String INDEX_F = "index.xml";
046:
047: /**
048: * The store directory (relative to current path)
049: */
050: public String store_dir = null;
051:
052: /**
053: * The server name (e.g. http-server)
054: */
055: public String server_name = null;
056:
057: /**
058: * A resource lister instance that handles the repository resources
059: */
060: protected ResourceLister rl = null;
061:
062: protected Serializer serializer = null;
063:
064: /* Hashtable that associates key/repository */
065: protected Hashtable index_table = new Hashtable();
066:
067: /* Hashtable that associates repository/RepositoryFinder */
068: protected Hashtable finders = new Hashtable();
069:
070: /* Current stack (for recursive actions) */
071: private Stack cur_stack = new Stack();
072:
073: /* Resource & repository stack */
074: private Stack res_stack = new Stack(); // the resource stack
075: private Stack rep_stack = new Stack(); // the equivalent repository stack
076:
077: /**
078: * The working repository
079: */
080: private String working_rep = null;
081:
082: protected static Perl5Matcher pmatcher = new Perl5Matcher();
083: protected static Perl5Compiler pcompiler = new Perl5Compiler();
084:
085: /**
086: * Initializes the finder
087: */
088: public StoreFinder(String store_dir, String server_name) {
089: this .store_dir = store_dir;
090: this .server_name = server_name;
091: this .rl = new ResourceLister();
092: }
093:
094: /**
095: * Reads the index file of the store.
096: * opens the file and initialize index_table, launch one finder for
097: * each repository.
098: */
099: public void readStoreIndex() throws IOException {
100: serializer = new org.w3c.tools.resources.serialization.xml.XMLSerializer();
101: File index = new File(store_dir, server_name + "-" + INDEX_F);
102: System.out.println("Reading index file...");
103:
104: try {
105: Reader reader = new BufferedReader(new FileReader(index));
106: AttributeHolder[] atth = serializer
107: .readAttributeHolders(reader);
108:
109: for (int i = 0; i < atth.length; i++) {
110: // rl.printAttributeHolder(atth[i]);
111: Integer key = rl.getKeyFromHolder(atth[i]);
112: if (key != null) {
113: String rep = rl.getRepositoryFromHolder(atth[i]);
114: index_table.put(key, rep);
115: if (!finders.containsKey(rep)) {
116: RepositoryFinder rf = new RepositoryFinder(
117: store_dir, rep);
118: finders.put(rep, rf);
119: }
120: }
121: }
122: } catch (InvalidStoreException e) {
123: System.out.println("Invalid store");
124: throw new IOException();
125: } catch (IOException e) {
126: System.out.println("can't read index file");
127: throw new IOException();
128: }
129: }
130:
131: /**
132: * Reads the root file of the store.
133: * opens the file and initialize working repository and stacks,
134: * lists all the store recurively.
135: */
136: public void readStoreRoot() throws IOException {
137:
138: serializer = new org.w3c.tools.resources.serialization.xml.XMLSerializer();
139: File root = new File(store_dir, ROOT_REP);
140:
141: /* initialize the working repository and stack to root rep */
142: working_rep = ROOT_REP;
143: res_stack.push(ROOT_REP);
144: RepositoryFinder rf = (RepositoryFinder) finders.get(ROOT_REP);
145: rep_stack.push(rf);
146:
147: /* read the attribute holders (=resources) */
148: System.out.println("reading root file...\n");
149: Reader reader = new BufferedReader(new FileReader(root));
150: AttributeHolder[] atth = serializer
151: .readAttributeHolders(reader);
152: for (int i = 0; i < atth.length; i++) {
153: // print the resource name
154: System.out.println(rl.getIdentFromHolder(atth[i]));
155: Integer key = rl.getKeyFromHolder(atth[i]);
156: if (key != null) {
157: // get the repository where the container is, from the key
158: String repository = (String) index_table.get(key);
159: //System.out.println("container: "+key+"is in: "+repository);
160:
161: // read the container recursively
162: if (repository.compareTo(ROOT_REP) != 0) {
163: recursiveReadContainer(repository, 1);
164: }
165: }
166: }
167: }
168:
169: /**
170: * Recursive read of containers
171: * @param repname the repository file name
172: * @param deep recursivity depth level.
173: */
174: private void recursiveReadContainer(String repname, int deep) {
175: // System.out.println("recurse in repository "+repname);
176: RepositoryFinder rf = (RepositoryFinder) finders.get(repname);
177: AttributeHolder[] atth = rf.getAttributeHolders();
178: // calculate display tabbing from depth
179: String depthtab = "";
180: for (int j = 0; j < deep; j++) {
181: depthtab = depthtab + "\t";
182: }
183: // print and recurse if a key is found (container)
184: for (int i = 0; i < atth.length; i++) {
185: System.out.println(depthtab + "|-"
186: + rl.getIdentFromHolder(atth[i]));
187: Integer key = rl.getKeyFromHolder(atth[i]);
188: if (key != null) {
189: if (index_table.containsKey(key)) {
190: String chrep = (String) index_table.get(key);
191: // System.out.println(" recurse for "+key);
192: recursiveReadContainer(chrep, deep + 1);
193: } else {
194: System.out.println("WARNING! "
195: + rl.getIdentFromHolder(atth[i])
196: + " is a container but its key " + key
197: + " does not exist in store index");
198: }
199: }
200: }
201: }
202:
203: /**
204: * Execute an action on the store.
205: * launch a try for the given action on the current repository.
206: * @param action the action string
207: * @param identifier the target of action (a Perl5 regexp)
208: * @param recursive a boolean flag (true = recursive action).
209: */
210: public void finderAction(String action, String identifier,
211: boolean recursive) {
212: // System.out.println(action+" "+identifier);
213:
214: if (working_rep.compareTo(ROOT_REP) == 0) {
215: System.out.println(working_rep);
216: tryAction(action, working_rep, identifier, recursive, 1);
217: } else {
218: RepositoryFinder rf = (RepositoryFinder) rep_stack.peek();
219: System.out.println(working_rep);
220: tryAction(action, rf.getRep(), identifier, recursive, 1);
221: }
222: }
223:
224: /**
225: * the real try method
226: */
227: private void tryAction(String action, String repname,
228: String identifier, boolean recursive, int deep) {
229: // System.out.println("tryAction: "+action+"-"+repname +"-"+identifier);
230:
231: RepositoryFinder rf = (RepositoryFinder) finders.get(repname);
232: AttributeHolder[] atth = rf.getAttributeHolders();
233: Pattern iPattern = null;
234:
235: boolean toWrite = false;
236:
237: try {
238: iPattern = pcompiler.compile("^" + identifier + "$",
239: Perl5Compiler.DEFAULT_MASK);
240: for (int i = 0; i < atth.length; i++) {
241: cur_stack.push(rl.getIdentFromHolder(atth[i]));
242: if (pmatcher.matches(rl.getIdentFromHolder(atth[i]),
243: iPattern)) {
244: rl.performAction(action, atth[i], deep);
245: rl.performActionOnFrames(action, atth[i], deep);
246: toWrite = true;
247: Integer key = rl.getKeyFromHolder(atth[i]);
248: if (key != null && recursive == true) {
249: // System.out.println("recursive perform");
250: String chrep = (String) index_table.get(key);
251: if (chrep.compareTo(ROOT_REP) != 0) {
252: recursivePerformAction(action, chrep,
253: deep + 1);
254: }
255: }
256: // System.out.println(cur_stack.toString());
257: } else {
258: Integer key = rl.getKeyFromHolder(atth[i]);
259: if (key != null && recursive == true) {
260: String chrep = (String) index_table.get(key);
261: if (chrep.compareTo(ROOT_REP) != 0) {
262: // System.out.println("try in :"+chrep);
263: tryAction(action, chrep, identifier,
264: recursive, deep + 1);
265: }
266: }
267: }
268: cur_stack.pop();
269: if (toWrite) {
270: // System.out.println("write holders in "+rf.getRep());
271: rf.writeHolders(); // commit changes
272: toWrite = false;
273: }
274:
275: }
276: } catch (MalformedPatternException ex) {
277: System.out.println("malformed expression " + identifier);
278: }
279: }
280:
281: /* the real action, called by tryAction when target is found */
282: private void recursivePerformAction(String action, String repname,
283: int deep) {
284: // get the RepositoryFinder instance
285: RepositoryFinder rf = (RepositoryFinder) finders.get(repname);
286: // perform action and recurse
287: AttributeHolder[] atth = rf.getAttributeHolders();
288: for (int i = 0; i < atth.length; i++) {
289: rl.performAction(action, atth[i], deep);
290: rl.performActionOnFrames(action, atth[i], deep);
291: Integer key = rl.getKeyFromHolder(atth[i]);
292: // recursive call if it is a container
293: if (key != null) {
294: String chrep = (String) index_table.get(key);
295: if (chrep.compareTo(ROOT_REP) != 0)
296: recursivePerformAction(action, chrep, deep + 1);
297: }
298: }
299: // System.out.println("write holders in "+rf.getRep());
300: rf.writeHolders(); // commit changes
301: }
302:
303: /**
304: * Get the working repository
305: * @return a string that is the repository file name.
306: */
307: public String getWorkingRep() {
308: return (working_rep);
309: }
310:
311: /**
312: * Print the working repository
313: */
314: public void printWorkingRep() {
315: System.out.println(working_rep);
316: }
317:
318: /**
319: * Set the working repository
320: * @param newrep a string that is the container name or ".." to
321: * go back in the stack.
322: */
323: public void setWorkingRep(String newrep) {
324: if (newrep.compareTo("..") == 0) {
325: res_stack.pop();
326: rep_stack.pop();
327: working_rep = (String) res_stack.peek();
328: } else {
329: try {
330: RepositoryFinder rf = (RepositoryFinder) rep_stack
331: .peek();
332: AttributeHolder[] atth = rf.getAttributeHolders();
333: for (int i = 0; i < atth.length; i++) {
334: Integer key = rl.getKeyFromHolder(atth[i]);
335: if (rl.getIdentFromHolder(atth[i])
336: .compareTo(newrep) == 0
337: && key != null) {
338: working_rep = newrep;
339: res_stack.push(rl.getIdentFromHolder(atth[i]));
340: String reps = (String) index_table.get(key);
341: rep_stack.push((RepositoryFinder) finders
342: .get(reps));
343: // System.out.println(res_stack.toString());
344: }
345: }
346: if (working_rep.compareTo(newrep) != 0) {
347: System.out
348: .println("container not found or not a container");
349: }
350: } catch (java.lang.NullPointerException ex) {
351: ex.printStackTrace();
352: }
353: }
354: }
355:
356: }
|