1 package net.sf.uitags.js; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import net.sf.uitags.util.ArrayUtils; 13 14 final class JsFiles { 15 private static final Log log = LogFactory.getLog(JsFiles.class); 16 17 private static final String DEBUG_FILE_NAME = "util/excludes/initDebug.js"; 18 private static final List EMPTY_LIST = new ArrayList (); 19 20 private Suites suites; 21 private FileFinder fileFinder; 22 private List customFileNames = EMPTY_LIST; 23 private boolean inDebugMode = false; 24 25 28 JsFiles(Suites suites, FileFinder fileFinder) { 29 this.suites = suites; 30 this.fileFinder = fileFinder; 31 } 32 33 void setCustomFileNames(String customFileNames) { 34 if (customFileNames == null) { 35 this.customFileNames = EMPTY_LIST; 36 } 37 else { 38 String [] fileNamesAsArray = ArrayUtils.toArrayOfTrimmed(customFileNames); 39 this.customFileNames = Arrays.asList(fileNamesAsArray); 40 } 41 } 42 43 void setInDebugMode(boolean inDebugMode) { 44 this.inDebugMode = inDebugMode; 45 } 46 47 String getContents() throws IOException { 48 StringBuffer buffer = new StringBuffer (); 49 50 addDebugOnlyContentsIfNecessary(buffer); 51 addContentsFromSuites(buffer); 52 addCustomContents(buffer); 53 54 return buffer.toString(); 55 } 56 57 62 long getLastModified() { 63 return 64 Math.max(getTimestampFromDebugFile(), 65 Math.max(getLatestTimestampFromFilesInSuites(), 66 getLatestTimestampFromCustomJsFiles())); 67 } 68 69 private long getTimestampFromDebugFile() { 70 if (this.fileFinder.supportsFilesNotFromSuites() && this.inDebugMode) { 71 try { 72 return this.fileFinder.getLastModified(DEBUG_FILE_NAME); 73 } 74 catch (IOException e) { 75 return 0; 76 } 77 } 78 79 return 0; 80 } 81 82 private long getLatestTimestampFromFilesInSuites() { 83 long latestTimestamp = 0; 84 85 for (Iterator i = this.suites.getFileNames().iterator(); i.hasNext(); ) { 86 String fileName = (String ) i.next(); 87 try { 88 latestTimestamp = Math.max( 89 this.fileFinder.getLastModified(fileName), latestTimestamp); 90 } 91 catch (IOException e) { 92 if (log.isWarnEnabled()) { 94 log.warn("Error trying to get last modified of " + fileName + "."); 95 } 96 } 97 } 98 99 return latestTimestamp; 100 } 101 102 private long getLatestTimestampFromCustomJsFiles() { 103 if (this.fileFinder.supportsFilesNotFromSuites()) { 104 long latestTimestamp = 0; 105 106 for (Iterator i = this.customFileNames.iterator(); i.hasNext(); ) { 107 String fileName = (String ) i.next(); 108 try { 109 latestTimestamp = Math.max( 110 this.fileFinder.getLastModified(fileName), latestTimestamp); 111 } 112 catch (IOException e) { 113 if (log.isWarnEnabled()) { 115 log.warn("Error trying to get last modified of " + fileName + "."); 116 } 117 } 118 } 119 120 return latestTimestamp; 121 } 122 123 return 0; 124 } 125 126 private void addDebugOnlyContentsIfNecessary(StringBuffer buffer) 127 throws IOException { 128 if (this.fileFinder.supportsFilesNotFromSuites() && this.inDebugMode) { 129 buffer.append(this.fileFinder.readContents(DEBUG_FILE_NAME)); 130 } 131 } 132 133 private void addContentsFromSuites(StringBuffer buffer) 134 throws IOException { 135 for (Iterator i = this.suites.getFileNames().iterator(); i.hasNext(); ) { 136 String fileName = (String ) i.next(); 137 buffer.append(this.fileFinder.readContents(fileName)); 138 } 139 } 140 141 private void addCustomContents(StringBuffer buffer) 142 throws IOException { 143 if (this.fileFinder.supportsFilesNotFromSuites()) { 144 for (Iterator i = this.customFileNames.iterator(); i.hasNext(); ) { 145 String fileName = (String ) i.next(); 146 buffer.append(this.fileFinder.readContents(fileName)); 147 } 148 } 149 } 150 } 151 | Popular Tags |