001: // SampleLabelService.java
002: // $Id: SampleLabelService.java,v 1.8 2000/08/16 21:37:43 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.pics;
007:
008: import java.io.File;
009: import java.io.PrintStream;
010:
011: import java.net.URL;
012: import java.net.MalformedURLException;
013: import java.util.Vector;
014:
015: /**
016: * The internal representation of a LabelService.
017: * A LabelService is an object which should be able to deliver labels for any
018: * URL. This implementation doesn't use any fancy database (it should), it uses
019: * the file system as a Database, in fact.
020: * <p>Each service is assigned a directory, and for each requested labels, this
021: * directory is looked up for the appropriate URL. So if you want to label
022: * <strong>http://www.w3.org/pub/WWW</strong> you have to create, under
023: * this service directory a file named
024: * <strong>http/www.w3.org/pub/WWW/label</strong>. To label its Overview.html
025: * document define the
026: * <strong>http/www.w3.org/pub/WWW/Overview.html-label</strong> file.
027: * The label itself is the content of the file.
028: */
029:
030: public class SampleLabelService implements LabelServiceInterface {
031: File directory = null; // This service directory
032: String name = null; // This service name (URL)
033:
034: /**
035: * Get his service directory.
036: */
037:
038: private final File getDirectory() {
039: return directory;
040: }
041:
042: /**
043: * Slashify a path component.
044: * Turn the slashes in the given URL into appropriate file name separator
045: * from the underlying OS.
046: * @param path The path to slashify.
047: * @return A String properly slashified.
048: */
049:
050: private String slashify(String path) {
051: String separator = System.getProperty("file.separator");
052: if (separator.equals("/"))
053: return path;
054: if (separator.length() != 1)
055: throw new RuntimeException(this .getClass().getName()
056: + " invalid separator length !");
057: return path.replace('/', '\\');
058: }
059:
060: /**
061: * Filify an URL.
062: * This methods takes an URL as input, and returns an uniq File object
063: * relative to the given SampleLabelService directory.
064: * @param u The URL to filify.
065: * @param generic Filify for generic labels if <strong>true</strong>.
066: */
067:
068: public File filify(URL u, boolean generic) {
069: File file = null;
070: file = new File(getDirectory(), u.getProtocol());
071: file = new File(file, u.getHost());
072: if ((u.getPort() != 80) && (u.getPort() != -1))
073: file = new File(file, new Integer(u.getPort()).toString());
074: if ((u.getFile() != null) && (!u.getFile().equals("/"))) {
075: String part = (generic ? u.getFile().substring(1) + ".gen"
076: : u.getFile().substring(1));
077: file = new File(file, slashify(part));
078: } else if (generic) {
079: file = new File(file.getParent(), file.getName() + ".gen");
080: }
081: if (PICS.debug())
082: System.out.println("Label for " + u + " in [" + file + "]");
083: return file;
084: }
085:
086: /**
087: * Get this service name.
088: * @return A String instance, being the service name.
089: */
090:
091: public String getName() {
092: return name;
093: }
094:
095: /**
096: * Dump this service description into the given buffer.
097: * @param into The StringBuffer to dump the service to.
098: */
099:
100: public void dump(StringBuffer into, int format) {
101: into.append(" \"" + getName() + "\"");
102: }
103:
104: /**
105: * Get the specific label for the given URL.
106: * @param url The URL whose label is searched.
107: */
108:
109: public LabelInterface getSpecificLabel(URL url) {
110: File flabel = filify(url, false);
111: if (flabel.exists()) {
112: SampleLabel l = null;
113: try {
114: l = new SampleLabel(flabel);
115: } catch (InvalidLabelException e) {
116: return null;
117: }
118: return l;
119: }
120: return null;
121: }
122:
123: /**
124: * Get the most speicific generic label for an URL.
125: * @param url The URL whose generic label is to be retreived.
126: * @return An object conforming to the LabelInterface, or
127: * <strong>null</strong> if none was found.
128: */
129:
130: // This is awfully slow, I need a real database for a real implementation
131: public LabelInterface getGenericLabel(URL url) {
132: try {
133: while (true) {
134: File flabel = filify(url, true);
135: if (flabel.exists()) {
136: SampleLabel l = null;
137: try {
138: l = new SampleLabel(flabel);
139: } catch (InvalidLabelException e) {
140: return null;
141: }
142: return l;
143: }
144: String file = url.getFile();
145: if ((file == null) || file.equals("/"))
146: return null;
147: String parent = url.getFile();
148: if (parent.length() - 1 == parent.lastIndexOf("/"))
149: parent = parent.substring(0, parent.length() - 2);
150: parent = parent.substring(0, parent.lastIndexOf("/"));
151: if (parent.length() == 0)
152: parent = "/";
153: url = new URL(url.getProtocol(), url.getHost(), url
154: .getPort(), parent);
155: }
156: } catch (MalformedURLException e) {
157: e.printStackTrace();
158: return null;
159: }
160: }
161:
162: /**
163: * Get the tree labels for the given URL.
164: * @param url The URL whose tree labels are to be retreived.
165: * @return An array of SampleLabel, each comforming to the LabelInterface.
166: */
167:
168: // This is awfully slow, I need a real database for a real implementation
169: public LabelInterface[] getTreeLabels(URL url) {
170: File labels = filify(url, false);
171: if (!labels.isDirectory())
172: return null;
173: String files[] = labels.list();
174: if (files == null)
175: return null;
176: // Include the label for the queried URL itself first:
177: LabelInterface myself = getGenericLabel(url);
178: Vector v = new Vector(files.length);
179: if (myself != null)
180: v.addElement(myself);
181: // Get the other labels:
182: for (int i = 0; i < files.length; i++) {
183: SampleLabel l = null;
184: try {
185: l = new SampleLabel(new File(labels, files[i]));
186: } catch (InvalidLabelException e) {
187: l = null;
188: }
189: if (l != null)
190: v.addElement(l);
191: }
192: if (v.size() > 0) {
193: SampleLabel ls[] = new SampleLabel[v.size()];
194: v.copyInto(ls);
195: return ls;
196: } else {
197: return null;
198: }
199: }
200:
201: /**
202: * Get the generic tree labels for given URL.
203: * @param url The URL whose tree labels are to be retreived.
204: * @return An array of SampleLabel, each of which conforms to the
205: * LabelInterface.
206: */
207:
208: // This is awfully slow, I need a real database for a real implementation
209: public LabelInterface[] getGenericTreeLabels(URL url) {
210: File labels = filify(url, false);
211: if (!labels.isDirectory())
212: return null;
213: String files[] = labels.list();
214: if (files == null)
215: return null;
216: // Include the label for the queried URL itself first:
217: LabelInterface myself = getGenericLabel(url);
218: Vector v = new Vector(files.length);
219: if (myself != null)
220: v.addElement(myself);
221: // Get the other labels.
222: floop: for (int i = 0; i < files.length; i++) {
223: if (!files[i].endsWith(".gen"))
224: continue floop;
225: SampleLabel l = null;
226: try {
227: l = new SampleLabel(new File(labels, files[i]));
228: } catch (InvalidLabelException e) {
229: l = null;
230: }
231: if (l != null)
232: v.addElement(l);
233: }
234: if (v.size() > 0) {
235: SampleLabel ls[] = new SampleLabel[v.size()];
236: v.copyInto(ls);
237: return ls;
238: } else {
239: return null;
240: }
241: }
242:
243: public SampleLabelService(SampleLabelBureau b, String name)
244: throws UnknownServiceException {
245: this .name = name;
246: try {
247: this .directory = new File(b.getIdentifier());
248: this .directory = filify(new URL(name), false);
249: } catch (MalformedURLException e) {
250: throw new UnknownServiceException(name);
251: }
252: if (PICS.debug())
253: System.out.println("LabelService for " + name + " is in "
254: + directory.getAbsolutePath());
255: if ((!this .directory.exists())
256: || (!this .directory.isDirectory()))
257: throw new UnknownServiceException(name);
258: }
259:
260: }
|