001: package org.openi.feeds;
002:
003: import java.io.File;
004: import java.io.FileNotFoundException;
005: import java.io.IOException;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011:
012: import org.apache.log4j.Logger;
013: import org.openi.xml.BeanStorage;
014: import org.springframework.util.AntPathMatcher;
015:
016: public class FeedsUtil {
017: private static Logger logger = Logger.getLogger(FeedsUtil.class);
018:
019: public static FeedsMetaData[] readMetaDataObjects(int startPos,
020: List metaDataFiles) {
021: List metaDatas = new ArrayList();
022: BeanStorage beanStorage = new BeanStorage();
023: for (Iterator iter = metaDataFiles.iterator(); iter.hasNext();) {
024: String path = (String) iter.next();
025: try {
026: FeedsMetaData mtdt = (FeedsMetaData) beanStorage
027: .restoreBeanFromFile(path);
028: //override path since metadata's path can be invalid if user move the feeds
029: String feedpath = path.substring(0, path.length()
030: - ".metadata".length());
031: feedpath = feedpath.substring(startPos);
032: Map attr = mtdt.getAttributes();
033: if (attr == null) {
034: attr = new HashMap();
035: mtdt.setAttributes(attr);
036: }
037: feedpath = feedpath.replace('\\', '/');
038: attr.put("path", feedpath);
039: metaDatas.add(mtdt);
040: } catch (IndexOutOfBoundsException ex) {
041: logger.error("Invalid path :" + path, ex);
042: } catch (Throwable ex) {
043: logger.error("error! corrupted metadata :" + path, ex);
044: }
045: }
046: return (FeedsMetaData[]) metaDatas
047: .toArray(new FeedsMetaData[metaDatas.size()]);
048: }
049:
050: public static FeedsMetaData restoreMetaData(String feedPath)
051: throws Exception {
052: File feedFile = new File(feedPath);
053: if (feedFile.exists() && feedFile.isFile()) {
054: File metaDataFile = new File(feedFile.getAbsolutePath()
055: + ".metadata");
056: if (metaDataFile.exists()) {
057: BeanStorage beanStorage = new BeanStorage();
058: return (FeedsMetaData) beanStorage
059: .restoreBeanFromFile(metaDataFile
060: .getAbsolutePath());
061: } else {
062: throw new FileNotFoundException(
063: "Metadata file for feed'" + feedPath
064: + "' does not exist");
065: }
066: } else {
067: throw new FileNotFoundException("Invalid feed file '"
068: + feedPath + "'");
069: }
070:
071: }
072:
073: public static void persistMetaData(String feedFile,
074: FeedsMetaData metaData) throws Exception {
075: String metaDataFile = feedFile + ".metadata";
076: BeanStorage beanStorage = new BeanStorage();
077: beanStorage.saveBeanToFile(metaDataFile, metaData);
078: }
079:
080: public static void listMetaDataFiles(File dir, List metaDataList) {
081: File files[] = dir.listFiles();
082: for (int i = 0; i < files.length; i++) {
083: if (files[i].isDirectory())
084: listMetaDataFiles(files[i], metaDataList);
085: else if (files[i].getName().endsWith(".metadata")) {
086: try {
087: metaDataList.add(files[i].getCanonicalPath());
088: } catch (IOException e) {
089: logger.error("inaccessible file :"
090: + files[i].getAbsolutePath());
091: }
092: }
093: }
094: }
095:
096: /**
097: * recusrively build tree map of all files
098: * @param startPos position of the relative path on full cannonical path
099: * @param file
100: * @param treemap
101: */
102: public static void buildTreeMapOfAllFeeds(int startPos, File file,
103: Map treemap, String entensions) {
104: String relativePath = "";
105: try {
106: relativePath = file.getCanonicalPath().substring(startPos);
107: relativePath = relativePath.replace('\\', '/');
108: } catch (IOException e) {
109: logger.error("could not build file list of +"
110: + file.getAbsolutePath());
111: }
112: if (file.isDirectory()) {
113: Map me = null;
114: if ("".equals(relativePath)) {
115: me = treemap;
116: } else {
117: me = new HashMap();
118: me.put("type", "dir");
119: treemap.put(relativePath, me);
120: }
121: File files[] = file.listFiles();
122: for (int i = 0; i < files.length; i++) {
123: buildTreeMapOfAllFeeds(startPos, files[i], me,
124: entensions);
125: }
126: } else {
127: int extindex = file.getName().lastIndexOf(".");
128: if (extindex != -1) {
129: String ext = file.getName().substring(extindex);
130: if (entensions.indexOf(ext.toLowerCase()) != -1) {
131: Map me = new HashMap();
132: treemap.put(relativePath, me);
133: me.put("type", "file");
134: me.put("size", String.valueOf(file.length()));
135: me.put("modified", String.valueOf(file
136: .lastModified()));
137: }
138: }
139: }
140: }
141:
142: public static void recurseFolders(int startPos, File file,
143: Map treemap, String pattern, boolean listFiles) {
144: String relativePath = "";
145: try {
146: relativePath = file.getCanonicalPath().substring(startPos);
147: relativePath = relativePath.replace('\\', '/');
148: } catch (IOException e) {
149: logger.error("could not build file list of +"
150: + file.getAbsolutePath());
151: }
152: if (pattern != null && !"".equals(pattern)) {
153: AntPathMatcher matcher = new AntPathMatcher();
154: if (!matcher.match(pattern, relativePath))
155: return;
156: }
157: if (file.isDirectory()) {
158: Map me = null;
159: if ("".equals(relativePath)) {
160: me = treemap;
161: } else {
162: me = new HashMap();
163: me.put("type", "dir");
164: treemap.put(relativePath, me);
165: }
166: File files[] = file.listFiles();
167: for (int i = 0; i < files.length; i++) {
168: recurseFolders(startPos, files[i], me, pattern,
169: listFiles);
170: }
171: } else if (listFiles) {
172: int extindex = file.getName().lastIndexOf(".");
173: if (extindex != -1) {
174: Map me = new HashMap();
175: treemap.put(relativePath, me);
176: me.put("type", "file");
177: me.put("size", String.valueOf(file.length()));
178: me.put("modified", String.valueOf(file.lastModified()));
179:
180: }
181: }
182: }
183:
184: public static String resolvePathWithBaseDir(String baseDir,
185: String path) {
186: path = path.replace('\\', '/');
187: if (baseDir == null || "".equals(baseDir))
188: return path;
189:
190: baseDir = baseDir.replace('\\', '/');
191: if (!(baseDir.endsWith("/") || path.startsWith("/")))
192: return baseDir + "/" + path;
193: else
194: return baseDir + path;
195: }
196:
197: public static Map listFolders(int startPos, File file,
198: boolean foldersOnly, String filterPattern) {
199: Map treemap = new HashMap();
200: recurseFolders(startPos, file, treemap, filterPattern,
201: foldersOnly == false);
202: return treemap;
203: }
204: }
|