001: package vqwiki.file;
002:
003: import java.io.File;
004: import java.util.Collection;
005: import java.util.TreeSet;
006:
007: import vqwiki.AbstractSearchEngine;
008: import vqwiki.WikiBase;
009: import vqwiki.WikiException;
010: import vqwiki.servlets.WikiServlet;
011: import vqwiki.utils.TextFileFilter;
012: import vqwiki.utils.Utilities;
013:
014: /**
015: * Very Quick Wiki - WikiWikiWeb clone
016: * Copyright (C) 2001-2002 Gareth Cronin
017: *
018: * This is the search engine for the default file system. There are some idiosyncracies
019: * in order to keep compatibility with the pre-virtual-wiki implementations:
020: * Whenever the default virtual wiki with the context "jsp" is accessed, the path is
021: * altered so the files are put/read to/from the root of the Wiki home directory as
022: * with earlier versions.
023: *
024: *This program is free software; you can redistribute it and/or modify
025: *it under the terms of the latest version of the GNU Lesser General
026: *Public License as published by the Free Software Foundation;
027: *
028: *This program is distributed in the hope that it will be useful,
029: *but WITHOUT ANY WARRANTY; without even the implied warranty of
030: *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
031: *GNU Lesser General Public License for more details.
032: *
033: *You should have received a copy of the GNU Lesser General Public License
034: *along with this program (gpl.txt); if not, write to the Free Software
035: *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
036: *
037: *
038: */
039: public class FileSearchEngine extends AbstractSearchEngine {
040:
041: /** A pointer to self. Used for the singleton pattern */
042: private static FileSearchEngine instance = null;
043:
044: /**
045: * Create the new FileSearchEnginge. The constructor is private, so that
046: * it cannot be instanciated from outside thsi class. Use getInstance()
047: * instead.
048: *
049: * @throws java.lang.Exception
050: */
051: private FileSearchEngine() throws Exception {
052: }
053:
054: /**
055: * Create an instance of the FileSearchEngine
056: * @return a reference to the only FileSearchEngine existing
057: * @throws java.lang.Exception
058: */
059: public static synchronized FileSearchEngine getInstance()
060: throws Exception {
061: if (instance == null) {
062: instance = new FileSearchEngine();
063: instance.initSearchEngine(WikiServlet.getCurrentContext());
064: }
065: return instance;
066: }
067:
068: /**
069: * find the base directory to use for a virtual wiki
070: * @param virtualWiki the virtual wiki to find the base directory for
071: * @return base directory for a particular virtual wiki
072: */
073: protected String dir(String virtualWiki) {
074: return FileHandler.fileBase(virtualWiki);
075: }
076:
077: /**
078: * Return a list of all topic names for one given virtual Wiki
079: *
080: * @param virtualWiki The virtual viki, for which the topic names are requested
081: *
082: * @return A List of Topic Names (Collection of Strings).
083: *
084: * @throws Exception Exception during search
085: * @throws WikiException Virtual wiki cannot be found
086: */
087: public Collection getAllTopicNames(String virtualWiki)
088: throws Exception {
089: Collection all = new TreeSet();
090: if (virtualWiki.equals(WikiBase.DEFAULT_VWIKI)) {
091: virtualWiki = "";
092: }
093: File file = new File(dir(virtualWiki));
094: if (!file.exists()) {
095: throw new WikiException("Directory can not be accessed: "
096: + file);
097: }
098: File[] files = file.listFiles(new TextFileFilter());
099: for (int i = 0; i < files.length; i++) {
100: String fileName = Utilities.decodeSafeFileName(files[i]
101: .getName());
102: all.add(fileName.substring(0, fileName.length()
103: - FileHandler.EXT.length()));
104: }
105: return all;
106: }
107:
108: /**
109: * Get the filename of a topic file.
110: * @see vqwiki.AbstractSearchEngine#getFilename(java.lang.String, java.lang.String)
111: */
112: protected String getFilename(String currentWiki, String topic) {
113: return FileHandler.getPathFor(currentWiki,
114: topic + FileHandler.EXT).getName();
115: }
116: }
|