1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
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 ****************************************************************/
19
20
21
22 package org.apache.james.transport.matchers;
23
24 import java.io.IOException;
25
26 import org.apache.oro.text.regex.MalformedPatternException;
27 import javax.mail.MessagingException;
28
29 /**
30 * Initializes RegexMatcher with regular expressions from a file.
31 *
32 */
33 public class FileRegexMatcher extends GenericRegexMatcher {
34
35 /**
36 * @see org.apache.james.transport.matchers.GenericRegexMatcher#init()
37 */
38 public void init() throws MessagingException {
39 java.io.RandomAccessFile patternSource = null;
40 try {
41 patternSource = new java.io.RandomAccessFile(getCondition(), "r");
42 int lines = 0;
43 while(patternSource.readLine() != null) lines++;
44 patterns = new Object[lines][2];
45 patternSource.seek(0);
46 for (int i = 0; i < lines; i++) {
47 String line = patternSource.readLine();
48 patterns[i][0] = line.substring(0, line.indexOf(':'));
49 patterns[i][1] = line.substring(line.indexOf(':')+1);
50 }
51 compile(patterns);
52
53 }
54 catch (java.io.FileNotFoundException fnfe) {
55 throw new MessagingException("Could not locate patterns.", fnfe);
56 }
57 catch (java.io.IOException ioe) {
58 throw new MessagingException("Could not read patterns.", ioe);
59 }
60 catch(MalformedPatternException mp) {
61 throw new MessagingException("Could not initialize regex patterns", mp);
62 } finally {
63 if (patternSource != null) {
64 // close the file
65 try {
66 patternSource.close();
67 } catch (IOException e) {
68 // just ignore on close
69 }
70 }
71 }
72 }
73 }