01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
10: * http://www.apache.org/licenses/LICENSE-2.0 *
11: * *
12: * Unless required by applicable law or agreed to in writing, *
13: * software distributed under the License is distributed on an *
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15: * KIND, either express or implied. See the License for the *
16: * specific language governing permissions and limitations *
17: * under the License. *
18: ****************************************************************/package org.apache.james.transport.matchers;
19:
20: import java.io.IOException;
21:
22: import org.apache.oro.text.regex.MalformedPatternException;
23: import org.apache.mailet.RFC2822Headers;
24: import javax.mail.MessagingException;
25:
26: /**
27: * Initializes RegexMatcher with regular expressions from a file.
28: *
29: */
30: public class FileRegexMatcher extends GenericRegexMatcher {
31: public void init() throws MessagingException {
32: java.io.RandomAccessFile patternSource = null;
33: try {
34: patternSource = new java.io.RandomAccessFile(
35: getCondition(), "r");
36: int lines = 0;
37: while (patternSource.readLine() != null)
38: lines++;
39: patterns = new Object[lines][2];
40: patternSource.seek(0);
41: for (int i = 0; i < lines; i++) {
42: String line = patternSource.readLine();
43: patterns[i][0] = line.substring(0, line.indexOf(':'));
44: patterns[i][1] = line.substring(line.indexOf(':') + 1);
45: }
46: compile(patterns);
47:
48: } catch (java.io.FileNotFoundException fnfe) {
49: throw new MessagingException("Could not locate patterns.",
50: fnfe);
51: } catch (java.io.IOException ioe) {
52: throw new MessagingException("Could not read patterns.",
53: ioe);
54: } catch (MalformedPatternException mp) {
55: throw new MessagingException(
56: "Could not initialize regex patterns", mp);
57: } finally {
58: if (patternSource != null) {
59: // close the file
60: try {
61: patternSource.close();
62: } catch (IOException e) {
63: // just ignore on close
64: }
65: }
66: }
67: }
68: }
|