001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.lucefix;
021:
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.Vector;
029:
030: import org.apache.lucene.document.Document;
031: import org.xml.sax.InputSource;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.XMLReader;
034: import org.xml.sax.helpers.XMLReaderFactory;
035:
036: import de.schlund.pfixcore.lucefix.Tripel.Type;
037: import de.schlund.pfixxml.resources.FileResource;
038: import de.schlund.pfixxml.resources.ResourceUtil;
039:
040: /**
041: * @author schuppi
042: * @date Jun 14, 2005
043: */
044: public class DocumentCache {
045: private Map<String, Document> cache;
046:
047: // private static Logger LOG = Logger.getLogger(DocumentCache.class);
048:
049: public DocumentCache() {
050: cache = new HashMap<String, Document>();
051: }
052:
053: public Document getDocument(Tripel tripel)
054: throws FileNotFoundException, IOException, SAXException {
055: return getDocument(tripel.getPath(), tripel.getType());
056: }
057:
058: public Document getDocument(String path, Type type)
059: throws FileNotFoundException, IOException, SAXException {
060: // look in cache
061: // LOG.debug("looking for " + path);
062: Document retval = lookup(path);
063: if (retval != null) {
064: found++;
065: } else {
066: missed++;
067: String filename = stripAddition(path);
068: // file was not scanned
069: flush();
070: Collection<Document> newest = DocumentCache
071: .getDocumentsFromFileAsCollection(ResourceUtil
072: .getFileResourceFromDocroot(filename));
073: for (Iterator<Document> iter = newest.iterator(); iter
074: .hasNext();) {
075: Document element = iter.next();
076: if (type != Type.EDITORUPDATE) {
077: cache.put(element.get("path"), element);
078: }
079: if (path.equals(element.get("path"))) {
080: retval = element;
081: break;
082: }
083: }
084: }
085: return retval;
086: }
087:
088: /**
089: * @param path
090: * @return
091: */
092: private static String stripAddition(String path) {
093: int letzterSlash = path.lastIndexOf("/");
094: int vorletzterSlash = path.lastIndexOf("/", letzterSlash - 1);
095: return path.substring(0, vorletzterSlash);
096: }
097:
098: /**
099: * @param path
100: * @return
101: */
102: private Document lookup(String path) {
103: return (Document) cache.get(path);
104: }
105:
106: public boolean remove(Document doc) {
107: if (doc == null)
108: return false;
109: return cache.remove(doc.get("path")) != null;
110: }
111:
112: public Collection<Document> getRest() {
113: return cache.values();
114: }
115:
116: public void flush() {
117: cache.clear();
118: }
119:
120: private static Collection<Document> getDocumentsFromFileAsCollection(
121: FileResource f) throws FileNotFoundException, IOException,
122: SAXException {
123: XMLReader xmlreader = XMLReaderFactory.createXMLReader();
124: IncludeFileHandler handler = new IncludeFileHandler(f);
125: xmlreader.setContentHandler(handler);
126: xmlreader.setDTDHandler(handler);
127: xmlreader.setEntityResolver(handler);
128: xmlreader.setErrorHandler(handler);
129: xmlreader.setProperty(
130: "http://xml.org/sax/properties/lexical-handler",
131: handler);
132: try {
133: xmlreader.parse(new InputSource(f.getInputStream()));
134: } catch (Exception e) {
135: // org.apache.log4j.Logger.getLogger(DocumentCache.class).warn("bad xml: " + f);
136: return new Vector<Document>();
137: }
138: return handler.getScannedDocumentsAsVector();
139: }
140:
141: // statistic stuff
142: private int found = 0, missed = 0;
143:
144: protected void resetStatistic() {
145: found = missed = 0;
146: }
147:
148: protected int getFound() {
149: return found;
150: }
151:
152: protected int getMissed() {
153: return missed;
154: }
155:
156: }
|