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.pfixxml;
021:
022: import java.io.IOException;
023:
024: import javax.xml.transform.TransformerException;
025:
026: import org.xml.sax.SAXException;
027:
028: import de.schlund.pfixxml.resources.FileResource;
029: import de.schlund.pfixxml.targets.SPCache;
030: import de.schlund.pfixxml.targets.SPCacheFactory;
031: import de.schlund.pfixxml.util.XsltVersion;
032:
033: /**
034: * IncludeDocumentFactory.java
035: *
036: *
037: * Created: 20021029
038: *
039: * @author <a href="mailto:haecker@schlund.de">Joerg Haecker</a>
040: *
041: *
042: * This class realises the factory and the singleton pattern. It is responsible to
043: * create and store objects of type {@link IncludeDocument} in a {@link SPCache}
044: * cache created by {@link SPCacheFactory}. If a requested IncludeDocument is found
045: * in the cache it will be returned, otherwise it will be created and stored in the cache.
046: * If the source file of a requested IncludeDocument is newer then the one stored in the cache,
047: * it will be recreated and be stored in the cache.
048: */
049: public class IncludeDocumentFactory {
050:
051: // private static Logger LOG = Logger.getLogger(IncludeDocumentFactory.class);
052: private static IncludeDocumentFactory instance = new IncludeDocumentFactory();
053: private SPCache<String, IncludeDocument> cache = SPCacheFactory
054: .getInstance().getDocumentCache();
055:
056: private IncludeDocumentFactory() {
057: }
058:
059: public static IncludeDocumentFactory getInstance() {
060: return instance;
061: }
062:
063: // FIXME! Don't do the whole method synchronized!!
064: public synchronized IncludeDocument getIncludeDocument(
065: XsltVersion xsltVersion, FileResource path, boolean mutable)
066: throws SAXException, IOException, TransformerException {
067: //TODO: change method signature (create multiple methods) to reflect mutable vs. immutable document creation
068: if (xsltVersion == null && !mutable)
069: throw new IllegalArgumentException(
070: "XsltVersion has to be specified to create a immutable document.");
071: IncludeDocument includeDocument = null;
072: String key = getKey(xsltVersion, path, mutable);
073:
074: if (!isDocumentInCache(key)
075: || isDocumentInCacheObsolete(path, key)) {
076: includeDocument = new IncludeDocument();
077: includeDocument.createDocument(xsltVersion, path, mutable);
078: cache.setValue(key, includeDocument);
079: } else {
080: includeDocument = (IncludeDocument) cache.getValue(key);
081: }
082: return includeDocument;
083: }
084:
085: private boolean isDocumentInCache(String key) {
086: return cache.getValue(key) != null ? true : false;
087: }
088:
089: private String getKey(XsltVersion xsltVersion, FileResource path,
090: boolean mutable) {
091: return mutable ? path.toURI().toString() + "_mutable" : path
092: .toURI().toString()
093: + "_imutable" + "_" + xsltVersion;
094: }
095:
096: private boolean isDocumentInCacheObsolete(FileResource path,
097: String newkey) {
098: long savedTime = ((IncludeDocument) cache.getValue(newkey))
099: .getModTime();
100: return path.lastModified() > savedTime ? true : false;
101: }
102:
103: public void reset() {
104: SPCacheFactory.getInstance().reset();
105: cache = SPCacheFactory.getInstance().getDocumentCache();
106: }
107:
108: }
|