Coverage Report - net.sf.jolene.factories.DocumentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentFactory
51%
18/35
25%
3/12
0
 
 1  
 /**
 2  
  * Created by IntelliJ IDEA.
 3  
  * User: DanH
 4  
  * Date: Aug 9, 2005
 5  
  * Time: 6:50:28 PM
 6  
  */
 7  
 package net.sf.jolene.factories;
 8  
 
 9  
 import net.sf.jolene.constants.Prefs;
 10  
 import net.sf.jolene.dom.Document;
 11  
 import org.apache.log4j.LogManager;
 12  
 import org.apache.log4j.Logger;
 13  
 
 14  
 import java.io.IOException;
 15  
 import java.io.File;
 16  
 import java.io.FileNotFoundException;
 17  
 import java.util.HashMap;
 18  
 import java.util.Map;
 19  
 
 20  
 
 21  
 /**
 22  
  * DocumentFactory is a Singleton used to perform cacheing of documents. Documents added to the cache
 23  
  * are not parsed again. Instead they are cloned which is faster.
 24  
  *
 25  
  * @author Dan Howard
 26  
  */
 27  
 public class DocumentFactory {
 28  
 
 29  1
     private static final Logger log = LogManager.getLogger(DocumentFactory.class);
 30  
 
 31  
     private static final int DEFAULT_CACHE_SIZE = 32;
 32  
 
 33  1
     private static DocumentFactory instance = new DocumentFactory();
 34  
 
 35  
     private Map<String, Document> documents;
 36  
 
 37  
     /**
 38  
      * Gets the factory instance.
 39  
      *
 40  
      * @return DocumentFactory instance.
 41  
      */
 42  
     public static DocumentFactory getInstance() {
 43  1
         return instance;
 44  
     }
 45  
 
 46  1
     private DocumentFactory() {
 47  
         int cacheSize;
 48  
         try {
 49  1
             cacheSize = Integer.parseInt(Prefs.CacheSize.getValue());
 50  0
         } catch (NumberFormatException e) {
 51  0
             cacheSize = DEFAULT_CACHE_SIZE;
 52  0
             log.warn("NumberFormatException : " + e.getMessage() + " cache size set to 32");
 53  1
         }
 54  1
         documents = new HashMap<String, Document>(cacheSize);
 55  1
     }
 56  
 
 57  
     /**
 58  
      * Returns a new document instance using the full filename + timestamp as the factory key.
 59  
      *
 60  
      * @param docFileName full path and name to the file to get.
 61  
      * @return Document the document object from the cache.
 62  
      * @throws IOException if any IOException occurs.
 63  
      */
 64  
     public Document getDocument(String docFileName) throws IOException {
 65  1
         log.debug("getDocument: " + docFileName);
 66  
 
 67  1
         boolean cacheEnabled = Boolean.parseBoolean(Prefs.DocumentCaching.getValue());
 68  
 
 69  1
         log.debug("getDocument: filename: Cache enabled? " + cacheEnabled);
 70  
 
 71  1
         if (cacheEnabled) {
 72  1
             File file = new File(docFileName);
 73  1
             if (! file.exists()) {
 74  0
                 throw new FileNotFoundException(docFileName + " not found!");
 75  
             }
 76  1
             String key = docFileName + " - " + file.lastModified();
 77  1
             if (!documents.containsKey(key)) {
 78  1
                 documents.put(key, new Document(docFileName));
 79  
             }
 80  1
             return documents.get(key).clone();
 81  
         }
 82  
 
 83  0
         return new Document(docFileName);
 84  
     }
 85  
 
 86  
     /**
 87  
      * Returns a new document instance using the full filename + timestamp as the factory key.
 88  
      *
 89  
      * @param docFileName full path and name to the file to get.
 90  
      * @param contextPath aids in finding the document's real path from the uri.
 91  
      * @param uri         uri to the document.
 92  
      * @return Document   the document object from the cache.
 93  
      * @throws IOException if ever an IOException occurs.
 94  
      */
 95  
     public Document getDocument(String docFileName, String contextPath, String uri) throws IOException {
 96  0
         log.debug("getDocument (with cp): " + docFileName + " cp: " + contextPath);
 97  
 
 98  0
         boolean cacheEnabled = Boolean.parseBoolean(Prefs.DocumentCaching.getValue());
 99  0
         log.debug("getDocument: filename, contextPath, uri: Cache enabled? " + cacheEnabled);
 100  
 
 101  0
         if (cacheEnabled) {
 102  0
             File file = new File(docFileName);
 103  0
             if (! file.exists()) {
 104  0
                 throw new FileNotFoundException(docFileName + " not found!");
 105  
             }
 106  0
             String key = docFileName + " - " + file.lastModified();
 107  0
             if (!documents.containsKey(key)) {
 108  0
                 documents.put(key, new Document(docFileName, contextPath, uri));
 109  
             }
 110  0
             return documents.get(key).clone();
 111  
 
 112  
         }
 113  0
         return new Document(docFileName, contextPath, uri);
 114  
     }
 115  
 
 116  
 
 117  
 }