001: // RepositoryFinder.java
002: // $Id: RepositoryFinder.java,v 1.6 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.serialization.ResourceDescription;
011: import org.w3c.tools.resources.serialization.AttributeDescription;
012: import org.w3c.tools.resources.serialization.xml.XMLSerializer;
013:
014: import org.w3c.tools.resources.AttributeHolder;
015:
016: import java.io.BufferedReader;
017: import java.io.BufferedWriter;
018: import java.io.File;
019: import java.io.FileReader;
020: import java.io.FileWriter;
021: import java.io.FilenameFilter;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.ObjectInputStream;
025: import java.io.PrintStream;
026: import java.io.Reader;
027: import java.io.Serializable;
028: import java.io.Writer;
029:
030: import org.apache.oro.text.regex.Perl5Matcher;
031: import org.apache.oro.text.regex.Perl5Compiler;
032: import org.apache.oro.text.regex.Pattern;
033: import org.apache.oro.text.regex.MalformedPatternException;
034:
035: import java.util.Hashtable;
036:
037: /**
038: * <p>The finder that handles a given repository file.
039: */
040:
041: public class RepositoryFinder {
042:
043: /**
044: * The relative directory name of the repository in the store
045: */
046: protected String repository_dir = null;
047:
048: /**
049: * The repository name in the repository directory
050: */
051: protected String repository_name = null;
052:
053: /**
054: * The store directory (relative to current path)
055: */
056: protected String store_dir = null;
057:
058: /**
059: * A resource lister instance that handles the repository resources
060: */
061: protected ResourceLister rl = null;
062:
063: protected File file = null;
064: protected Serializer serializer = null;
065: private Hashtable containers = null;
066: private AttributeHolder[] holders = null;
067: private Reader reader = null;
068: private Writer writer = null;
069:
070: protected static Perl5Matcher pmatcher = new Perl5Matcher();
071: protected static Perl5Compiler pcompiler = new Perl5Compiler();
072:
073: /**
074: * Initializes the finder.
075: * opens the file in the right directory, adds a resource lister
076: * and a XML serializer, finds the containers.
077: * @param storedir the store directory path
078: * @param repname the repository filename.
079: */
080: public RepositoryFinder(String storedir, String repname)
081: throws InvalidStoreException {
082:
083: // System.out.println("I am repository finder for" +repname);
084: int slash = repname.indexOf('/', 0);
085: if (slash != -1) {
086: this .repository_dir = repname.substring(0, slash);
087: this .repository_name = repname.substring(slash + 1, repname
088: .length());
089: } else {
090: this .repository_dir = "";
091: this .repository_name = repname;
092: }
093: this .store_dir = storedir;
094: this .rl = new ResourceLister();
095: this .containers = new Hashtable();
096:
097: serializer = new org.w3c.tools.resources.serialization.xml.XMLSerializer();
098: File file = new File(store_dir + "/" + repository_dir,
099: repository_name);
100: // System.out.println("reading repository "+repository_name+"...\n");
101:
102: try {
103: this .reader = new BufferedReader(new FileReader(file));
104: this .holders = serializer.readAttributeHolders(reader);
105: for (int i = 0; i < holders.length; i++) {
106: // rl.printAttributeHolder(holders[i]);
107: if (rl.getKeyFromHolder(holders[i]) != null) {
108: // store containers in a dedicated hashtable with key
109: containers.put(rl.getKeyFromHolder(holders[i]),
110: holders[i]);
111: }
112: }
113: } catch (IOException ex) {
114: System.out.println("Unable to read repository");
115: throw new InvalidStoreException("inexistent repository");
116: }
117: }
118:
119: /**
120: * find a given resource
121: * @param identifier the name of the resource to find
122: * @return an AttributeHolder.
123: */
124: public AttributeHolder getAttributeHolder(String identifier) {
125: for (int i = 0; i < holders.length; i++) {
126: if (rl.getIdentFromHolder(holders[i]).compareTo(identifier) == 0) {
127: return holders[i];
128: }
129: }
130: return null;
131: }
132:
133: public AttributeHolder[] getAttributeHolders() {
134: return holders;
135: }
136:
137: public String getRep() {
138: return (repository_dir + "/" + repository_name);
139: }
140:
141: /**
142: * write changes into a tmp file and then commit to the real file
143: */
144: public void writeHolders() {
145: File tmp = new File(store_dir + "/" + repository_dir,
146: repository_name + ".tmp");
147: File bak = new File(store_dir + "/" + repository_dir,
148: repository_name + ".bak");
149:
150: File orig = new File(store_dir + "/" + repository_dir,
151: repository_name);
152:
153: try {
154: this .writer = new BufferedWriter(new FileWriter(tmp));
155: // this.writer = new OutputStreamWriter( out );
156: } catch (Exception ex) {
157: ex.printStackTrace();
158: throw new RuntimeException(
159: "Unable to open temp rep for write");
160: }
161: try {
162: serializer.writeResources(holders, writer);
163: } catch (Exception ex) {
164: ex.printStackTrace();
165: throw new RuntimeException("Unable to write repository");
166: }
167:
168: if (!orig.renameTo(bak)) {
169: System.out.println("unable to rename " + orig + " to "
170: + bak);
171: } else {
172: if (!tmp.renameTo(orig)) {
173: bak.renameTo(orig);
174: System.out.println("unable to rename " + tmp + " to "
175: + orig);
176: } else {
177: // System.out.println("Repository saved");
178: }
179: }
180:
181: }
182: }
|