source: trunk/gli/src/org/greenstone/gatherer/util/Utility.java@ 5153

Last change on this file since 5153 was 5153, checked in by jmt12, 21 years ago

Fix 203B143

  • Property svn:keywords set to Author Date Id Revision
File size: 40.9 KB
Line 
1package org.greenstone.gatherer.util;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.awt.*;
39import java.io.*;
40import java.net.*;
41import java.util.*;
42import javax.swing.*;
43import javax.swing.tree.*;
44import org.apache.xerces.parsers.*;
45import org.apache.xml.serialize.*;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.util.HTMLStringTokenizer;
48import org.w3c.dom.*;
49import org.xml.sax.*;
50/** To provide a library of common methods, in a static context, for use in the Gatherer.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3b
53 */
54public class Utility {
55 static final public Dimension BUTTON_SIZE = new Dimension(160, 35);
56 static final public Dimension LABEL_SIZE = new Dimension(125, 35);
57 /** The default size of a gatherer progress bar, in either the download view or the build view. */
58 static final public Dimension PROGRESS_BAR_SIZE = new Dimension(580,65);
59 /** The number of kilobytes to use as a io buffer. */
60 static final public int FACTOR = 1;
61 /** The size of the io buffer, calculated as FACTOR * 1024. */
62 static final public int BUFFER_SIZE = FACTOR * 1024;
63 /** Definition of an important directory name, in this case the archive directory for the collection. */
64 static final public String ARCHIVE_DIR = "archives" + File.separator;
65 /** Definition of an important directory name, in this case the base dir, or the working directory of the Gatherer. */
66 static final public String BASE_DIR = System.getProperty("user.dir") + File.separator;
67 /** Definition of an important directory name, in this case the building directory for the collection. */
68 static final public String BUILD_DIR = "building" + File.separator;
69 /** Definition of an important directory name, in this case the public web cache for the Gatherer. */
70 static final public String CACHE_DIR = BASE_DIR + "cache" + File.separator;
71 static final public String CFG_COLLECTIONMETA_COLLECTIONNAME = "collectionmeta collectionname";
72 static final public String CFG_COLLECTIONMETA_COLLECTIONEXTRA = "collectionmeta collectionextra";
73 static final public String CFG_COLLECTIONMETA_ICONCOLLECTION = "collectionmeta iconcollection";
74 static final public String CFG_CLASSIFY = "classify";
75 static final public String CFG_CLASSIFY_BUTTONNAME = "-buttonname";
76 static final public String CFG_CLASSIFY_HFILE = "-hfile";
77 static final public String CFG_CLASSIFY_METADATA = "-metadata";
78 static final public String CFG_CLASSIFY_SORT = "-sort";
79 static final public String CFG_CREATOR = "creator";
80 static final public String CFG_FORMAT = "format";
81 static final public String CFG_MAINTAINER = "maintainer";
82 /** Definition of an important directory name, in this case the parent directory of all the collections in the gsdl. */
83 static final public String COL_DIR = "collect" + File.separator;
84 static final public String COLLECTION_DEMO = "greenstone demo";
85 static final public String COLLECTION_DEMO_DIRECTORY = "demo" + File.separator;
86 static final public String COLLECTION_DLS = "Development Library Subset";
87 static final public String COLLECTION_DLS_DIRECTORY = "dls" + File.separator;
88 static final public String COLLECTION_TREE = "Collection";
89 /** Definition of an important directory name, in this case the file the collection configuration is expect to be in. */
90 static final public String CONFIG_DIR = "etc" + File.separator + "collect.cfg";
91 /** The default file name for the urls missing any file. */
92 static final public String DEFAULT_FILE = "index.html";
93 static final public String DEFAULT_NAMESPACE = "gsp";
94 /** The default protocol header for those urls missing any protocol. */
95 static final public String DEFAULT_PROTOCOL = "http://";
96 /** The default dictionary to load. */
97 static final public String DICTIONARY = "dictionary";
98 static final public String DLS_MDS = "dls.mds";
99 static final public String ENGLISH_VALUE = "en";
100 /** Definition of an important directory name, in this case the etc (or extra information) directory for the collection. */
101 static final public String ETC_DIR = "etc" + File.separator;
102 static final public String EXTRACTED_METADATA_NAMESPACE = "ex";
103 /** The location of the default greenstone metadata file. */
104 static final public String GREENSTONEDIRECTORYMETADATA_TEMPLATE = "xml/metadata.xml";
105 /** Definition of an important directory name, in this case the private web cache directory for the collection. */
106 static final public String GCACHE_DIR = "cache" + File.separator;
107 static final public String GLI_ARCHIVE = "GLI.jar";
108 /** Definition of an important directory name, in this case the location of help documentation. */
109 static final public String HELP_DIR = BASE_DIR + "help" + File.separator;
110 /** Definition of an important directory name, in this case the import directory for the collection. */
111 static final public String IMPORT_DIR = "import" + File.separator;
112 /** Definition of an important directory name, in this case the index directory for the collection. */
113 static final public String INDEX_DIR = "index" + File.separator;
114 static final public String LANGUAGE_ATTRIBUTE = "language";
115 /** Definition of an important directory name, in this case the log directory for the collection. */
116 static final public String LOG_DIR = "log" + File.separator;
117 /** Definition of an important directory name, in this case the location of the expected collection metadata sets.. */
118 static final public String META_DIR = "metadata" + File.separator; // Col. Copy
119 /** Definition of an important directory name, in this case the location of the default metadata sets. */
120 static final public String METADATA_DIR = BASE_DIR + "metadata" + File.separator;
121 /** The location the gatherer expects to find metadata set information. */
122 static final public String METADATA_SET_TEMPLATE = "xml/template.mds";
123 static final public String METADATA_VALUE_TEMPLATE = "xml/template.mdv";
124 static final public String METADATA_XML = "metadata.xml";
125 static final public String NAME_ELEMENT = "Name";
126 /** The default name of the perl executable under unix. */
127 static final public String PERL_EXECUTABLE_UNIX = "perl";
128 /** The default name of the perl executable under windows. */
129 static final public String PERL_EXECUTABLE_WINDOWS = "Perl.exe";
130 /** The default profile file */
131 static final public String PROFILE_TEMPLATE = "xml/protemp.xml";
132 /** The name of the Gatherer. */
133 static final public String PROGRAM_NAME = "Greenstone Librarian Interface";
134 /** The current version of the Gatherer. */
135 static final public String PROGRAM_VERSION = "ver 2.0";
136 /** Definition of an important directory name, in this case the location of the recycled files location. */
137 static final public String RECYCLE = BASE_DIR + "recycle" + File.separator;
138 /** Definition of an important directory name, in this case the location of image and other resources. */
139 static final public String RES_DIR = BASE_DIR + "resource" + File.separator;
140 static final public String SERVER_EXE = "server.exe";
141 /** Definition of an important directory name, in this case the location of opening (or welcome) screen html. */
142 static final public String WELCOME_DIR = BASE_DIR + "welcome" + File.separator;
143 static final public String WORKSPACE_TREE = "Workspace";
144 static final public String XML_DIRECTORY = "xml" + File.separator;
145 // These are out of alphabetic order to avoid forward reference error.
146 /** The default icon to produce a 'help-icon' sized blank space before a menu entry. */
147 static final public ImageIcon BLANK_ICON = new ImageIcon(ClassLoader.getSystemResource("images/blank.gif"));
148 /** The default error icon image. */
149 static final public ImageIcon ERROR_ICON = new ImageIcon(ClassLoader.getSystemResource("images/error.gif"));
150 static final public ImageIcon HELP_ICON = new ImageIcon(ClassLoader.getSystemResource("images/help.gif"));
151 /** The image for a toggle button whose state is 'on'. */
152 static final public ImageIcon ON_ICON = new ImageIcon(ClassLoader.getSystemResource("images/check.gif"));
153 /** The image for a toggle button whose state is 'off'. */
154 static final public ImageIcon OFF_ICON = new ImageIcon(ClassLoader.getSystemResource("images/cross.gif"));
155 /** Method to turn a file from with the system file tree into a tree path for insertion into a tree.
156 * @param file The <strong>File</strong> whose tree path you are attempting to discover.
157 * @param in_col A <i>boolean</i> indicating whether we are looking for a file within a collection of not. If <i>true</i> then the tree paths head in the collection name, and no element in the path refers to the import directory. Otherwise the paths head will be one of the system roots and all traversed file locations will exist in the path.
158 * @return A <strong>TreePath</strong> which traverses the file system tree to the specified file.
159 */
160 public static TreePath createTreePath(File file, boolean in_col) {
161 TreePath path = null;
162 // Get the absolute path of the file.
163 String abs_path = file.getAbsolutePath();
164 while(file != null) {
165 // If we are looking for a node within our collection, we expect
166 // its path from root to be <col_name>/... without any higher
167 // details and without gimport. So if we encounter a gimport we
168 // skip to its parent, add that, then return.
169 if(in_col && file.getName().equals("gimport")) {
170 file = file.getParentFile();
171 if(path == null) {
172 path = new TreePath(file.getName());
173 }
174 else {
175 path = path.pathByAddingChild(file.getName());
176 }
177 file = null;
178 }
179 else {
180 if(path == null) {
181 path = new TreePath(file.getName());
182 }
183 else {
184 path = path.pathByAddingChild(file.getName());
185 }
186 file = file.getParentFile();
187 }
188 }
189 // Unfortunately we've created the path in reverse order so we have to
190 // reverse it.
191 Object temp[] = new Object[path.getPathCount()];
192 for(int i = 0; i < temp.length; i++) {
193 temp[(temp.length - 1) - i] = path.getPathComponent(i);
194 }
195 return new TreePath(temp);
196 }
197 /** Takes a rfc2616 'safe' String and translates it back into its 'unsafe' form. Basically the native c wget decode_string() function, but without pointer stuff. If searches through the String looking for the pattern %xy where x and y are hexidecimal digits and where xy maps to a character.<BR> If x or y are not hexidecimal or % is followed by a \0 then the pattern is left as is.
198 * @param encoded The url-safe <strong>String</strong> to be decoded.
199 * @return The decoded <strong>String</strong>.
200 */
201 public static String decodeString(String encoded) {
202 String decoded = "";
203 for(int i = 0; i < encoded.length(); i++) {
204 if(encoded.charAt(i) == '%') {
205 if(hexidecimal(encoded.charAt(i+1)) != -1
206 && hexidecimal(encoded.charAt(i+2)) != -1) {
207 char unsafe_chr = (char)
208 ((hexidecimal(encoded.charAt(i+1)) * 16) +
209 hexidecimal(encoded.charAt(i+2)));
210 decoded = decoded + unsafe_chr;
211 i = i + 2;
212 }
213 }
214 else {
215 decoded = decoded + encoded.charAt(i);
216 }
217 }
218 return decoded;
219 }
220 /** It turns out that in Java you have to make sure a directory is empty before you delete it (much like unix I suppose), and so just like unix I'll have to set up a recursive delete function.
221 * @param file The <strong>File</strong> you want to delete.
222 * @return A <i>boolean</i> which is <i>true</i> if the file specified was successfully deleted, <i>false</i> otherwise.
223 */
224 static public boolean delete(File file) {
225 boolean result = true;
226 // If files a directory, delete files children.
227 if(file.isDirectory()) {
228 File files[] = file.listFiles();
229 for(int i = 0; files != null && result && i < files.length; i++) {
230 result = delete(files[i]);
231 }
232 }
233 if(result) {
234 // Delete file.
235 return file.delete();
236 }
237 return result;
238 }
239 /** Generate a depth first enumeration of a tree. */
240 static public EnumeratedVector depthFirstEnumeration(TreeNode node, EnumeratedVector result) {
241 result.add(node);
242 for(int i = 0; i < node.getChildCount(); i++) {
243 depthFirstEnumeration(node.getChildAt(i), result);
244 }
245 return result;
246 }
247 /** Encodes a string of text so its safe to use in a Greenstone configuration file. Esentially replaces newlines with their escaped form.
248 * @param raw The <strong>String</strong> before encoding.
249 * @return A <strong>String</strong> which is safe to write to the configuration file.
250 */
251 static final private char AMPERSTAMP_CHAR = '&';
252 static final private char ESCAPE_CHAR = '\\';
253 static final private char GREATER_THAN_CHAR = '>';
254 static final private char LESS_THAN_CHAR = '<';
255 static final private char NEWLINE_CHAR = '\n';
256 static final private char QUOTE_CHAR = '\'';
257 static final private char SPEECH_CHAR = '\"';
258 static final private String ENCODED_AMPERSTAMP_STR = "&amp;";
259 static final private String ENCODED_GREATER_THAN_STR = "&gt;";
260 static final private String ENCODED_LESS_THAN_STR = "&lt;";
261 static final private String ENCODED_SPEECH_STR = "&quot;";
262 static final private String ESCAPED_NEWLINE_STR = "\\n";
263
264 /** Decodes a string of text so its safe to use in a Greenstone configuration file. Esentially replaces "\n" with a newline.
265 * @param raw The <strong>String</strong> before decoding, read from the configuration file..
266 * @return A <strong>String</strong> ready to be placed in a component.
267 */
268 static public String decodeGreenstone(String raw) {
269 raw = raw.replaceAll("&apos;", "\'");
270 raw = raw.replaceAll("&gt;", ">");
271 raw = raw.replaceAll("&lt;", "<");
272 raw = raw.replaceAll("&quot;", "\"");
273 raw = raw.replaceAll("&#39;", "\'");
274 raw = raw.replaceAll("\\\\n", "\n");
275 return raw;
276 }
277
278 static public String encodeGreenstone(String raw) {
279 // Once again regex fails to provide the power necessary for me to change strings. What I need to do is replace "<" and ">" with "&lt;" and "&gt;", and replace "\<" and "\>" with "<" and ">".
280 StringBuffer processed = new StringBuffer();
281 int index = 0;
282 while(index < raw.length()) {
283 char c = raw.charAt(index);
284 switch(c) {
285 // Replace a normal new line character with "\n"
286 case NEWLINE_CHAR:
287 processed.append(ESCAPED_NEWLINE_STR);
288 break;
289 // Replace "\<" with "<", or with "\&lt;" if this is for XML. Similar requirements for "\>".
290 case ESCAPE_CHAR:
291 if(index + 1 < raw.length()) {
292 char d = raw.charAt(index + 1);
293 if(d == LESS_THAN_CHAR) {
294 processed.append(LESS_THAN_CHAR);
295 index++;
296 break;
297 }
298 else if(d == GREATER_THAN_CHAR) {
299 processed.append(GREATER_THAN_CHAR);
300 index++;
301 break;
302 }
303 }
304 // I have no idea how this would happen, but I better watch for it anyway
305 processed.append(c);
306 break;
307 // Replace "<" with "&lt;"
308 case LESS_THAN_CHAR:
309 processed.append(ENCODED_LESS_THAN_STR);
310 break;
311 // Replace ">" with "&gt;"
312 case GREATER_THAN_CHAR:
313 processed.append(ENCODED_GREATER_THAN_STR);
314 break;
315 default:
316 processed.append(c);
317 }
318 index++;
319 }
320 return processed.toString();
321 }
322 /** When retrieve text for, or from the collect.cfg file it may contain characters that can't go into a DOM such as "<" and ">". We also might already have encoded versions "&lt;" and "&gt;". Thus we must encode the former, and double encode the latter. */
323 static public String encodeXML(String raw) {
324 StringBuffer processed = new StringBuffer();
325 int index = 0;
326 while(index < raw.length()) {
327 char c = raw.charAt(index);
328 switch(c) {
329 case GREATER_THAN_CHAR:
330 processed.append(ENCODED_GREATER_THAN_STR);
331 break;
332 case LESS_THAN_CHAR:
333 processed.append(ENCODED_LESS_THAN_STR);
334 break;
335 case AMPERSTAMP_CHAR:
336 processed.append(ENCODED_AMPERSTAMP_STR);
337 break;
338 default:
339 processed.append(c);
340 }
341 index++;
342 }
343 return processed.toString();
344 }
345
346 /** Using this method we can request that a certain document be written, as valid XML, to a certain output stream. This makes use of the Xerces Serialization suite, which should in no way be confused with the usual method of Serialization used by Java. */
347 static public boolean export(Document document, String filename) {
348 return export(document, new File(filename));
349 }
350
351 static public boolean export(Document document, File file) {
352 try {
353 OutputStream os = new FileOutputStream(file);
354 // Create an output format for our document.
355 OutputFormat f = new OutputFormat(document);
356 f.setIndenting(true);
357 f.setLineWidth(0);
358 f.setPreserveSpace(false);
359 // Create the necessary writer stream for serialization.
360 OutputStreamWriter osw = new OutputStreamWriter(os);
361 Writer w = new BufferedWriter(osw);
362 // Generate a new serializer from the above.
363 XMLSerializer s = new XMLSerializer(w, f);
364 s.asDOMSerializer();
365 // Finally serialize the document to file.
366 s.serialize(document);
367 // And close.
368 os.close();
369 return true;
370 }
371 // A file not found exception is most likely thrown because the directory the metadata.xml file is attempting to be written to no longer has any files in it. I'll add a test in GDMDocument to test for this, but if it still happens ignore it (a non-existant directory can't really have metadata added to it any way.
372 catch (FileNotFoundException fnf_exception) {
373 if(!file.getName().endsWith(METADATA_XML)) {
374 fnf_exception.printStackTrace();
375 return false;
376 }
377 return true;
378 }
379 catch (IOException ioe) {
380 ioe.printStackTrace();
381 return false;
382 }
383 }
384
385 /** Given a starting directory, searches for the collect.cfg file and returns it if found.
386 * @return The collect.cfg File or null if not found.
387 */
388 static final public File findConfigFile(File start) {
389 if(start == null) {
390 return null;
391 }
392 // See if the collect.cfg files here.
393 File collect_cfg = new File(start, "collect.cfg");
394 if(collect_cfg.exists()) {
395 return collect_cfg;
396 }
397 // Search for the existance of collect.cfg in a etc directory.
398 File etc_dir = new File(start, "etc" + File.separator + "collect.cfg");
399 if(etc_dir.exists()) {
400 return etc_dir;
401 }
402 // Otherwise search this directories parent if its not null.
403 return findConfigFile(start.getParentFile());
404 }
405
406 /** Convert a long, detailing the length of a file in bytes, into a nice human readable string using b, kb, Mb and Gb. */
407 static final public String BYTE_SUFFIX = " b";
408 static final public long GIGABYTE = 1024000000l;
409 static final public String GIGABYTE_SUFFIX = " Gb";
410 static final public long KILOBYTE = 1024l;
411 static final public String KILOBYTE_SUFFIX = " kb";
412 static final public long MEGABYTE = 1024000l;
413 static final public String MEGABYTE_SUFFIX = " mb";
414 static final public String formatFileLength(long length) {
415 StringBuffer result = new StringBuffer("");
416 float number = 0f;
417 String suffix = null;
418 // Determine the floating point number and the suffix (radix) used.
419 if(length >= GIGABYTE) {
420 number = (float) length / (float) GIGABYTE;
421 suffix = GIGABYTE_SUFFIX;
422 }
423 else if(length >= MEGABYTE) {
424 number = (float) length / (float) MEGABYTE;
425 suffix = MEGABYTE_SUFFIX;
426 }
427 else if(length >= KILOBYTE) {
428 number = (float) length / (float) KILOBYTE;
429 suffix = KILOBYTE_SUFFIX;
430 }
431 else {
432 number = (float) length;
433 suffix = BYTE_SUFFIX;
434 }
435 // Create the formatted string remembering to round the number to 2.d.p. To do this copy everything in the number string from the start to the first occurance of '.' then copy two more digits. Finally search for and print anything that appears after (and including) the optional 'E' delimter.
436 String number_str = Float.toString(number);
437 char number_char[] = number_str.toCharArray();
438 int pos = 0;
439 // Print the characters up to the '.'
440 while(number_char != null && pos < number_char.length && number_char[pos] != '.') {
441 result.append(number_char[pos]);
442 pos++;
443 }
444 if(pos < number_char.length) {
445 // Print the '.' and at most two characters after it
446 result.append(number_char[pos]);
447 pos++;
448 for(int i = 0; i < 2 && pos < number_char.length; i++, pos++) {
449 result.append(number_char[pos]);
450 }
451 // Search through the remaining string for 'E'
452 while(pos < number_char.length && number_char[pos] != 'E') {
453 pos++;
454 }
455 // If we still have string then we found an E. Copy the remaining string.
456 while(pos < number_char.length) {
457 result.append(number_char[pos]);
458 pos++;
459 }
460 }
461 // Add suffix
462 result.append(suffix);
463 // Done
464 return result.toString();
465 }
466
467 /** This method formats a given string, using HTML markup, so its width does not exceed the given width and its appearance if justified.
468 * @param text The <strong>String</strong> requiring formatting.
469 * @param width The maximum width per line as an <i>int</i>.
470 * @return A <strong>String</strong> formatted so as to have no line longer than the specified width.
471 * TODO Currently HTML formatting tags are simply removed from the text, as the effects of spreading HTML tags over a break are undetermined. To solve this we need to associate tags with a certain text token so if it gets broken on to the next line the tags go with it, or if the tags cover a sequence of words that are broken we need to close then reopen the tags. However all this is a major task and well beyond anything I have time to 'muck-round' on.
472 */
473 static public String formatHTMLWidth(String text, int width) {
474 HTMLStringTokenizer html = new HTMLStringTokenizer(text);
475 int current_width = 0;
476 int threshold = width / 2;
477 Stack lines = new Stack();
478 String line = "";
479 while(html.hasMoreTokens()) {
480 String token = html.nextToken();
481 while(token != null) {
482 if(html.isTag()) {
483 // Insert smart HTML tag code here.
484 token = null;
485 }
486 else {
487 // If the token is bigger than two thirds width, before we've even started break it down.
488 if(current_width + 1 + token.length() > width && token.length() > threshold) {
489 String prefix = token.substring(0, width - 1 - current_width);
490 token = token.substring(prefix.length());
491 if(current_width == 0) {
492 line = line + prefix;
493 }
494 else {
495 line = line + " " + prefix;
496 }
497 lines.push(line);
498 line = "";
499 current_width = 0;
500 }
501 // If adding the next token would push us over the maximum line width.
502 else if(current_width + 1 + token.length() > width) {
503 line = space(line, width, current_width);
504 lines.push(line);
505 line = token;
506 current_width = token.length();
507 token = null;
508 }
509 // Otherwise we should be able to just add the token, give or take.
510 else {
511 if(current_width == 0) {
512 line = line + token;
513 current_width = token.length();
514 }
515 else {
516 // Special case for standard punctuation which may exist after a tag like so:
517 // My name is <scratchy>Slim Shady</scratchy>. <-- Annoying punctuation.
518 if(token.equals(".") || token.equals(",") || token.equals("!") || token.equals("?")) {
519 line = line + token;
520 current_width = current_width + 1;
521 }
522 else {
523 line = line + " " + token;
524 current_width = current_width + 1 + token.length();
525 }
526 }
527 token = null;
528 }
529 }
530 }
531 }
532 String result = line;
533 while(!lines.empty()) {
534 result = (String)lines.pop() + "<BR>" + result;
535 }
536 // Replace ' ' with "&nbsp;"
537 boolean tag = false;
538 int pos = 0;
539 while(pos < result.length()) {
540 if(result.charAt(pos) == '<') {
541 tag = true;
542 }
543 else if(result.charAt(pos) == '>') {
544 tag = false;
545 }
546 else if(result.charAt(pos) == ' ' && !tag) {
547 String prefix = result.substring(0, pos);
548 String suffix = result.substring(pos + 1);
549 result = prefix + "&nbsp;" + suffix;
550 }
551 pos++;
552 }
553 result = "<HTML>" + result + "</HTML>";
554 return result;
555 }
556 /** Format the given filename path string so that it is no longer than the given width. If it is wider replace starting directories with ...
557 * @param key The key <strong>String</Strong> used to retrieve a phrase from the dictionary for this item.
558 * @param raw The raw filename path <strong>String</strong>.
559 * @param width The maximum width as an <i>int</i>.
560 * @return A path <strong>String</strong> no longer than width.
561 */
562 static public String formatPath(String key, String raw, int width) {
563 JLabel label = new JLabel(Gatherer.dictionary.get(key, raw));
564 int position = -1;
565 while(label.getPreferredSize().width > width && (position = raw.indexOf(File.separator)) != -1) {
566 raw = "..." + raw.substring(position + 1);
567 label.setText(Gatherer.dictionary.get(key, raw));
568 }
569 if(raw.indexOf(File.separator) == -1 && raw.startsWith("...")) {
570 raw = raw.substring(3);
571 }
572 return raw;
573 }
574
575 /** Method which constructs the archive directory given a certain collection.
576 * @param col_dir The location of the collection directory as a <strong>String</strong>.
577 * @return The location of the given collections archive directory, also as a <strong>String</strong>.
578 */
579 static public String getArchiveDir(String gsdl_path, String col_name) {
580 return gsdl_path + File.separator + COL_DIR + col_name + File.separator + ARCHIVE_DIR;
581 }
582 /** Method which constructs the build directory given a certain collection.
583 * @param col_dir The location of the collection directory as a <strong>String</strong>.
584 * @return The location of the given collections build directory, also as a <strong>String</strong>.
585 */
586 static public String getBuildDir(String col_dir) {
587 if(col_dir == null) {
588 return BASE_DIR + BUILD_DIR;
589 }
590 return col_dir + BUILD_DIR;
591 }
592 /** Builds the private cache dir by appending col_dir and 'cache'.
593 * @param col_dir A String representing the directory path of the current collection.
594 * @return A String representing the path to the private file cache within the current collection.
595 */
596 public static String getCacheDir(String col_dir) {
597 return col_dir + GCACHE_DIR;
598 }
599 /** Method which constructs the collection directory for Greenstone.
600 * @param gsdl_path The location of the gsdl installation directory as a <strong>String</strong>.
601 * @return The location of the collection directory, also as a <strong>String</strong>.
602 */
603 public static String getCollectionDir(String gsdl_path) {
604 return gsdl_path + COL_DIR;
605 }
606 /** Method which constructs the configuration file given a certain collection.
607 * @param col_dir The location of the collection directory as a <strong>String</strong>.
608 * @return The location of the given collections configuration file, also as a <strong>String</strong>.
609 */
610 static public String getConfigDir(String col_dir) {
611 return col_dir + CONFIG_DIR;
612 }
613
614 static public String getDateString() {
615 Calendar current = Calendar.getInstance();
616 String day_name = null;
617 switch(current.get(Calendar.DAY_OF_WEEK)) {
618 case Calendar.MONDAY: day_name = "Mon"; break;
619 case Calendar.TUESDAY: day_name = "Tue"; break;
620 case Calendar.WEDNESDAY: day_name = "Wed"; break;
621 case Calendar.THURSDAY: day_name = "Thu"; break;
622 case Calendar.FRIDAY: day_name = "Fri"; break;
623 case Calendar.SATURDAY: day_name = "Sat"; break;
624 case Calendar.SUNDAY: day_name = "Sun"; break;
625 default: day_name = "";
626 }
627 String month_name = null;
628 switch(current.get(Calendar.MONTH)) {
629 case Calendar.JANUARY: month_name = "Jan"; break;
630 case Calendar.FEBRUARY: month_name = "Feb"; break;
631 case Calendar.MARCH: month_name = "Mar"; break;
632 case Calendar.APRIL: month_name = "Apr"; break;
633 case Calendar.MAY: month_name = "May"; break;
634 case Calendar.JUNE: month_name = "Jun"; break;
635 case Calendar.JULY: month_name = "Jul"; break;
636 case Calendar.AUGUST: month_name = "Aug"; break;
637 case Calendar.SEPTEMBER: month_name = "Sep"; break;
638 case Calendar.OCTOBER: month_name = "Oct"; break;
639 case Calendar.NOVEMBER: month_name = "Nov"; break;
640 case Calendar.DECEMBER: month_name = "Dec"; break;
641 default: month_name = "";
642 }
643 int day = current.get(Calendar.DAY_OF_MONTH);
644 int hour = current.get(Calendar.HOUR_OF_DAY);
645 int minute = current.get(Calendar.MINUTE);
646 int second = current.get(Calendar.SECOND);
647 int year = current.get(Calendar.YEAR);
648
649 return day_name + " " + month_name + " " + day + " " + year + " " + Utility.pad(String.valueOf(hour), 2, '0', true) + ":" + Utility.pad(String.valueOf(minute), 2, '0', true) + ":" + Utility.pad(String.valueOf(second), 2, '0', true);
650 }
651
652 /** Retrieves and formats the depth field of the config file to four characters.
653 * @param length The length of the desired string as an <i>int</i>.
654 * @return A <strong>String</strong> representation of the mirroring depth padded to length characters.
655 */
656 public static String getDepthString(int length) {
657 return pad("" + Gatherer.self.config.getInt("mirroring.depth", false), length);
658 }
659 /** Method which constructs the etc directory given a certain collection.
660 * @param col_dir The location of the collection directory as a <strong>String</strong>.
661 * @return The location of the given collections etc directory, also as a <strong>String</strong>.
662 */
663 public static String getEtcDir(String col_dir) {
664 return col_dir + ETC_DIR;
665 }
666 /** Method to retrieve an image icon with the given filename found in classpath or the resouces directory.
667 * @return The specified <strong>ImageIcon</strong>, or an error image replacement if no such images exists.
668 */
669 static public ImageIcon getImage(String filename) {
670 ImageIcon image = null;
671 try {
672 image = new ImageIcon(ClassLoader.getSystemResource("images/" + Gatherer.dictionary.get("Version") + "/" + filename));
673 }
674 catch(NullPointerException exception) {
675 image = new ImageIcon(ClassLoader.getSystemResource("images/" + filename));
676 }
677 if(image == null) {
678 image = ERROR_ICON;
679 }
680 return image;
681 }
682
683 /** Method which constructs the import directory given a certain collection.
684 * @param col_dir The location of the collection directory as a <strong>String</strong>.
685 * @return The location of the given collections import directory, also as a <strong>String</strong>.
686 */
687 public static String getImportDir(String col_dir) {
688 return col_dir + IMPORT_DIR;
689 }
690 /** Method which constructs the index directory given a certain collection.
691 * @param col_dir The location of the collection directory as a <strong>String</strong>.
692 * @return The location of the given collections index directory, also as a <strong>String</strong>.
693 */
694 static public String getIndexDir(String col_dir) {
695 return col_dir + INDEX_DIR;
696 }
697 /** Method which constructs the log directory given a certain collection.
698 * @param col_dir The location of the collection directory as a <strong>String</strong>.
699 * @return The location of the given collections log directory, also as a <strong>String</strong>.
700 */
701 public static String getLogDir(String col_dir) {
702 return col_dir + LOG_DIR;
703 }
704 /** Determine this machines name.
705 * @return The name as a <strong>String</strong>.
706 */
707 static public String getMachineName() {
708 try {
709 return InetAddress.getLocalHost().getHostName();
710 }
711 catch(UnknownHostException ex) {
712 }
713 return "Unknown Machine";
714 }
715 /** Method which constructs the metadata directory given a certain collection.
716 * @param col_dir The location of the collection directory as a <strong>String</strong>.
717 * @return The location of the given collections metadata directory, also as a <strong>String</strong>.
718 */
719 static public String getMetadataDir(String col_dir) {
720 return col_dir + META_DIR;
721 }
722
723
724 static public File getRecycleDirectory() {
725 return new File(RECYCLE);
726 }
727
728 /** Determine whether a character is a hexidecimal one.
729 * @param chr The <i>char</i> in question.
730 * @return An <i>int</i> representing the value of the hexidecimal character or -1 if not a hexidecimal.
731 */
732 public static int hexidecimal(char chr) {
733 switch(chr) {
734 case '0':
735 return 0;
736 case '1':
737 return 1;
738 case '2':
739 return 2;
740 case '3':
741 return 3;
742 case '4':
743 return 4;
744 case '5':
745 return 5;
746 case '6':
747 return 6;
748 case '7':
749 return 7;
750 case '8':
751 return 8;
752 case '9':
753 return 9;
754 case 'A':
755 return 10;
756 case 'B':
757 return 11;
758 case 'C':
759 return 12;
760 case 'D':
761 return 13;
762 case 'E':
763 return 14;
764 case 'F':
765 return 15;
766 default:
767 return -1;
768 }
769 }
770
771 /** A string is a valid hierarchy index if it matches '[0-9](\.[0-9])*' */
772 static public boolean isIndex(String raw) {
773 boolean result = true;
774 for(int i = 0; result && i < raw.length(); i++) {
775 char c = raw.charAt(i);
776 if(Character.isDigit(c) || (c == '.' && (i != 0 || i != raw.length() - 1))) {
777 // Valid index
778 }
779 else {
780 result = false;
781 }
782 }
783 return result;
784 }
785
786 /** Method to determine if the host system is Microsoft Windows based.
787 * @return A <i>boolean</i> which is <i>true</i> if the platform is Windows, <i>false</i> otherwise.
788 */
789 public static boolean isWindows() {
790 Properties props = System.getProperties();
791 String os_name = props.getProperty("os.name","");
792 if(os_name.startsWith("Windows")) {
793 return true;
794 }
795 return false;
796 }
797 /** Takes a string and a desired length and pads out the string to the length by adding spaces to the left.
798 * @param str The target <strong>String</strong> that needs to be padded.
799 * @param length The desired length of the string as an <i>int</i>.
800 * @return A <strong>String</strong> made from appending space characters with the string until it has a length equal to length.
801 */
802 public static String pad(String str, int length) {
803 return pad(str, length, ' ', true);
804 }
805 public static String pad(String str_raw, int length, char fill, boolean end) {
806 StringBuffer str = new StringBuffer(str_raw);
807 while(str.length() < length) {
808 if(end) {
809 str.insert(0, fill);
810 }
811 else {
812 str.append(fill);
813 }
814 }
815 return str.toString();
816 }
817
818 /** Parse in a xml document from a given filename. Note that this filename may need to be resolved by the class loader, especially for template files within a jar. */
819 static public Document parse(String filename, boolean use_classloader) {
820 File file = null;
821 if(use_classloader) {
822 try {
823 URL url = ClassLoader.getSystemResource(filename);
824 file = new File(URLDecoder.decode(url.getFile(), "UTF-8"));
825 url = null;
826 }
827 catch (Exception error) {
828 // Most likely file name.
829 file = new File("classes" + File.separator + filename);
830 //Gatherer.printStackTrace(error);
831 }
832 }
833 if(file == null) {
834 file = new File(filename);
835 }
836 return parse(file, true);
837 }
838 /** Parse in a xml document from a given file. */
839 static public Document parse(File file) {
840 return parse(file, true);
841 }
842 /** Parse in a xml document from a given file. */
843 static public Document parse(File file, boolean noisey) {
844 Document document = null;
845 try {
846 FileInputStream fis = new FileInputStream(file);
847 InputStreamReader isr = new InputStreamReader(fis);
848 Reader r = new BufferedReader(isr);
849 InputSource isc = new InputSource(r);
850 DOMParser parser = new DOMParser();
851 parser.setFeature("http://xml.org/sax/features/validation", false);
852 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
853 // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
854 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
855 parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
856 parser.parse(isc);
857 document = parser.getDocument();
858 isr.close();
859 fis.close();
860 parser = null;
861 isc = null;
862 r = null;
863 isr = null;
864 fis = null;
865 file = null;
866 }
867 catch (Exception error) {
868 if(noisey) {
869 error.printStackTrace();
870 Gatherer.printStackTrace(error);
871 }
872 }
873 return document;
874 }
875
876 /** Method to spread out a line of text so that is is justified to the given width, by attempting to widen white-spacing in a balanced way.
877 * @param original The <strong>String</strong> to justify.
878 * @param width The desired width as an <i>int</i>.
879 * @param current_width An <i>int</i> representing the current width of the string, which takes into account special characters.
880 * @return The newly justified <strong>String</strong>.
881 */
882 static public String space(String original, int width, int current_width) {
883 // Strip trailing whitespace.
884 while(original.charAt(original.length() - 1) == ' ') {
885 original = original.substring(0, original.length() - 2);
886 }
887 int diff = width - current_width;
888 // Now add diff spaces, one at each existing space.
889 int pos = 0;
890 while(diff > 0) {
891 if(pos == original.length()) {
892 pos = 0;
893 }
894 if(original.charAt(pos) == ' ') {
895 // Insert a space.
896 String prefix = original.substring(0, pos);
897 String suffix = original.substring(pos);
898 original = prefix + " " + suffix;
899 pos = pos + 2;
900 diff--;
901 }
902 pos++;
903 }
904 return original;
905 }
906 /** Method to strip new lines and extra spaces from a string. Used to restore text that has been mangled into width formatted blocks by the DOM parser.
907 * @param raw The <strong>Strong</strong> containing the mangled text.
908 * @return A <strong>String</strong> with new lines and extra spaces removed.
909 */
910 static public String stripNL(String raw_str) {
911 byte raw[] = raw_str.getBytes();
912 byte formatted[] = new byte[raw.length];
913 byte previous = '\0';
914 int j = 0;
915 for(int i = 0; i < raw.length; i++) {
916 if(raw[i] == '\n') {
917 // Skip new lines.
918 }
919 else if(raw[i] == '\t') {
920 // Skip tabs.
921 }
922 else if(raw[i] == ' ' && raw[i] == previous) {
923 // Skip erroneous whitespace.
924 }
925 else {
926 formatted[j] = raw[i];
927 j++;
928 }
929 previous = raw[i];
930 }
931 byte finish[] = new byte[j];
932 System.arraycopy(formatted, 0, finish, 0, j);
933 return new String(finish);
934 }
935 /** Trims the string text to the length specified removing end characters and adding if necessary.
936 * @param text A <strong>String</strong> which you wish to ensure is shorter than length.
937 * @param length An <i>int</i> specifying the strings maximum length after which its trimmed.
938 * @return The trimmed <strong>String</strong>.
939 */
940 public static String trim(String text, int length) {
941 if(text.length() > length) {
942 text = text.substring(0, length);
943 text = text + "...";
944 }
945 return text;
946 }
947
948 static public String trimCenter(String text, int length) {
949 if(text.length() > length) {
950 int half = (length - 3) / 2;
951 StringBuffer temp = new StringBuffer(text.substring(0, half));
952 temp.append("...");
953 temp.append(text.substring(text.length() - half));
954 text = temp.toString();
955 }
956 return text;
957 }
958 /** This method checks to see what registered file system root directorys are mounted, and returns only accessible ones. The exception is removable media drives (in particular floppy-disk drives) which will throw all sorts of error if we test them here. Instead they are assumed to be always accessible, but a test is conducted at the time you attempt to map them to test for actual accessibility (then at least the errors are thrown after the user tries to initiate the mapping of the drive which has no disk in it).
959 * @param roots A <strong>File[]</strong> containing all of the file system roots registered on this system.
960 * @return A filtered <strong>File[]</strong> containing only those drives that are accessible and/or are floppy-disk media drives.
961 */
962 public static File[] validateDrives(File roots[]) {
963 Vector valid = new Vector();
964 for(int i = 0; i < roots.length; i++) {
965 String name = roots[i].getAbsolutePath();
966 name = name.toLowerCase();
967 if(!name.startsWith("a:") && !name.startsWith("b:")) {
968 valid.add(roots[i]);
969 }
970 }
971 roots = new File[valid.size()];
972 for(int i = 0; i < roots.length; i++) {
973 roots[i] = (File)valid.get(i);
974 }
975 return roots;
976 }
977}
Note: See TracBrowser for help on using the repository browser.