001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: FilePatternMatcher.java 11751 2008-01-30 22:23:27Z mpreston $
024: */
025: package com.bostechcorp.cbesb.runtime.file;
026:
027: import java.text.SimpleDateFormat;
028: import java.util.Date;
029: import java.util.regex.Matcher;
030: import java.util.regex.Pattern;
031: import java.util.regex.PatternSyntaxException;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.oro.text.GlobCompiler;
036: import org.apache.oro.text.regex.PatternMatcher;
037: import org.apache.oro.text.regex.Perl5Matcher;
038:
039: /**
040: * FilePatternMatcher allows matching filenames using Glob style patterns.
041: * Multiple patterns can be specified and if the specified filename matches any
042: * pattern, match() method will return true;
043: *
044: * @author j.zhang
045: * @version 1.0.0
046: */
047: public class FilePatternMatcher {
048:
049: private static final Log log = LogFactory
050: .getLog(FilePatternMatcher.class);
051:
052: private static final String DATE = "{DATE}";
053:
054: private static final String TIME = "{TIME}";
055:
056: private static final String BASENAME = "{BASENAME}";
057:
058: private static final String EXT = "{EXT}";
059:
060: private static final String COUNT = "{COUNT}";
061:
062: private static final String MATCH_MODE_GLOB = "glob";
063:
064: private static final String MATCH_MODE_REGEXPR = "regexpr";
065:
066: /**
067: * Determines if the specified filename matches any of the preset patterns.
068: *
069: * @param filename
070: * The filename to match against the patterns.
071: * @return True if the filename matches at least one pattern. False
072: * otherwise.
073: */
074: // public boolean match(String filename) {
075: // for (int i = 0; i < patterns.size(); i++) {
076: // Pattern pattern = (Pattern) patterns.get(i);
077: // boolean matches = matcher.matches(filename, pattern);
078: // if (matches) {
079: // return true;
080: // }
081: // }
082: // return false;
083: // }
084: public static String processFilePattern(String filename,
085: String filePattern, int fileCount) {
086: String returnFilename = "";
087: if (filePattern != null) {
088: returnFilename = filePattern;
089: CharSequence charSequenceDate = new String(DATE);
090: CharSequence charSequenceTime = new String(TIME);
091: CharSequence charSequenceBasename = new String(BASENAME);
092: CharSequence charSequenceExt = new String(EXT);
093: CharSequence charSequenceCount = new String(COUNT);
094: CharSequence charSequenceDotExt = new String("." + EXT);
095:
096: if (filePattern.contains(charSequenceDate)) {
097: Date date = new Date();
098: SimpleDateFormat dateFormat = new SimpleDateFormat(
099: "yyyyMMdd");
100: String dateString = dateFormat.format(date);
101: returnFilename = returnFilename.replace(DATE,
102: dateString);
103: }
104: if (filePattern.contains(charSequenceTime)) {
105: Date date = new Date();
106: SimpleDateFormat dateFormat = new SimpleDateFormat(
107: "hhmmss");
108: String timeString = dateFormat.format(date);
109: returnFilename = returnFilename.replace(TIME,
110: timeString);
111: }
112: if (filePattern.contains(charSequenceBasename)) {
113: String basenameString = "";
114: int index = filename.lastIndexOf(".");
115: if (index != -1) {
116: basenameString = filename.substring(0, index);
117: returnFilename = returnFilename.replace(BASENAME,
118: basenameString);
119: } else {
120: returnFilename = returnFilename.replace(BASENAME,
121: filename);
122: }
123: }
124: if (filePattern.contains(charSequenceExt)) {
125: String extString = "";
126: int index = filename.lastIndexOf(".");
127: if (index != -1) {
128: extString = filename.substring(index + 1);
129: returnFilename = returnFilename.replace(EXT,
130: extString);
131: } else {
132: returnFilename = returnFilename.replace(
133: charSequenceDotExt, extString);
134: }
135: }
136: if (filePattern.contains(charSequenceCount)) {
137: String countString = String.valueOf(fileCount);
138: returnFilename = returnFilename.replace(COUNT,
139: countString);
140: }
141: } else {
142: returnFilename = filename;
143: }
144: return returnFilename;
145: }
146:
147: public static boolean matchPattern(String filename,
148: String filePattern, String matchMode) throws Exception {
149: // boolean b = true;
150: if (filePattern != null) {
151: if (MATCH_MODE_REGEXPR.equalsIgnoreCase(matchMode)) {
152: try {
153: Pattern pattern = Pattern.compile(filePattern);
154: Matcher matcher = pattern.matcher(filename);
155: return (matcher.matches());
156: } catch (PatternSyntaxException e) {
157: log.error("FileName: " + filename
158: + " do not matche the FilePattern:", e);
159: if (log.isDebugEnabled()) {
160: log.debug("FileName: " + filename
161: + " do not matche the FilePattern:", e);
162: }
163: }
164: }
165:
166: if (MATCH_MODE_GLOB.equalsIgnoreCase(matchMode)) {
167: GlobCompiler compiler = new GlobCompiler();
168: org.apache.oro.text.regex.Pattern pattern = compiler
169: .compile(filePattern);
170: PatternMatcher matcher = new Perl5Matcher();
171: if (matcher.matches(filename, pattern)) {
172: return true;
173: } else {
174: log.debug("FileName: " + filename
175: + " do not matche the FilePattern");
176: return false;
177: }
178: }
179: }
180: return true;
181: }
182: }
|