View Javadoc
1   /*
2    * Copyright 2009-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.admin.service;
17  
18  import java.io.Serializable;
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  
22  import org.springframework.util.StringUtils;
23  
24  /**
25   * @author Dave Syer
26   * 
27   */
28  @SuppressWarnings("serial")
29  public class FileInfo implements Comparable<FileInfo>, Serializable {
30  
31  	private final static String TIMESTAMP_PATTERN = ".*\\.[0-9]*\\.[0-9]*";
32  
33  	private final String timestamp;
34  
35  	private final String path;
36  
37  	private final String shortPath;
38  
39  	private final boolean local;
40  
41  	public FileInfo(String path) {
42  		this(path, null, true);
43  	}
44  
45  	public FileInfo(String path, String timestamp, boolean local) {
46  		super();
47  		this.path = path.replace("\\", "/");
48  		this.shortPath = extractPath(path, timestamp);
49  		this.timestamp = extractTimestamp(path, timestamp);
50  		this.local = local;
51  	}
52  	
53  	public FileInfo shortPath() {
54  		FileInfo info = new FileInfo(shortPath, timestamp, local);
55  		return info;
56  	}
57  	
58  	public String getPattern() {
59  		if (path.matches(TIMESTAMP_PATTERN)) {
60  			return path;
61  		}
62  		String extension = StringUtils.getFilenameExtension(path);
63  		String prefix = extension == null ? path : path.substring(0, path.length() - extension.length() - 1);
64  		if (prefix.matches(TIMESTAMP_PATTERN)) {
65  			return path;
66  		}
67  		return prefix + ".*.*" + (extension==null ? "" : "." + extension);
68  	}
69  
70  	private String extractPath(String path, String timestamp) {
71  		if (path.matches(TIMESTAMP_PATTERN)) {
72  			return path.substring(0, path.length() - 16);
73  		}
74  		String extension = StringUtils.getFilenameExtension(path);
75  		String prefix = extension == null ? path : path.substring(0, path.length() - extension.length() - 1);
76  		if (prefix.matches(TIMESTAMP_PATTERN)) {
77  			return prefix.substring(0, prefix.length() - 16) + "." + extension;
78  		}
79  		return path;
80  	}
81  
82  	private String extractTimestamp(String path, String timestamp) {
83  		if (timestamp != null) {
84  			return timestamp;
85  		}
86  		if (path.matches(TIMESTAMP_PATTERN)) {
87  			return path.substring(path.length() - 15, path.length());
88  		}
89  		String extension = StringUtils.getFilenameExtension(path);
90  		String prefix = extension == null ? path : path.substring(0, path.length() - extension.length() - 1);
91  		if (prefix.matches(TIMESTAMP_PATTERN)) {
92  			return prefix.substring(prefix.length() - 15, prefix.length());
93  		}
94  		return new SimpleDateFormat("yyyyMMdd.HHmmss").format(new Date());
95  	}
96  
97  	/**
98  	 * @return the local
99  	 */
100 	public boolean isLocal() {
101 		return local;
102 	}
103 
104 	/**
105 	 * @return the locator
106 	 */
107 	public String getTimestamp() {
108 		return timestamp;
109 	}
110 
111 	public String getPath() {
112 		return path;
113 	}
114 
115 	public String getFileName() {
116 		if (path.matches(TIMESTAMP_PATTERN)) {
117 			return path;
118 		}
119 		String extension = StringUtils.getFilenameExtension(path);
120 		String prefix = extension == null ? path : path.substring(0, path.length() - extension.length() - 1);
121 		if (prefix.matches(TIMESTAMP_PATTERN)) {
122 			return path;
123 		}
124 		return prefix + getSuffix() + (extension == null ? "" : "." + extension);
125 	}
126 
127 	private String getSuffix() {
128 		return "." + timestamp;
129 	}
130 
131 	public int compareTo(FileInfo o) {
132 		return shortPath.equals(o.shortPath) ? -timestamp.compareTo(o.timestamp) : path.compareTo(o.path);
133 	}
134 
135 	public String toString() {
136 		return "FileInfo [path=" + path + ", timestamp=" + timestamp + ", local=" + local + "]";
137 	}
138 
139 }