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 package org.apache.james.jspf.impl; 21 22 23 import org.apache.james.jspf.parser.TermDefinition; 24 25 import java.util.regex.Pattern; 26 27 /** 28 * Default implementation for the TermDefinition. 29 * This implementation try to retrieve the definition looking up a 30 * static REGEX field in the term class. 31 */ 32 public class DefaultTermDefinition implements TermDefinition { 33 34 private Pattern pattern; 35 36 private Class termDef; 37 38 private int matchSize = 0; 39 40 public DefaultTermDefinition(Class tClass) throws IllegalArgumentException, 41 SecurityException, IllegalAccessException, NoSuchFieldException { 42 String pString = (String) tClass.getField("REGEX").get(null); 43 pattern = Pattern.compile(pString); 44 termDef = tClass; 45 calcGroups(pString); 46 } 47 48 /** 49 * This method should be done differently. We currently don't hanlde the 50 * escaping at all. 51 * 52 * @param pString 53 */ 54 private void calcGroups(String pString) { 55 int i = 0; 56 int c = 0; 57 while (true) { 58 int p1 = pString.indexOf("(", i); 59 int p2 = pString.indexOf("(?:", i); 60 if (p1 < 0) 61 break; 62 if (p1 != p2) 63 c++; 64 i = p1 + 1; 65 } 66 matchSize = c; 67 } 68 69 /** 70 * @see org.apache.james.jspf.parser.TermDefinition#getPattern() 71 */ 72 public Pattern getPattern() { 73 return pattern; 74 } 75 76 /** 77 * @see org.apache.james.jspf.parser.TermDefinition#getTermDef() 78 */ 79 public Class getTermDef() { 80 return termDef; 81 } 82 83 /** 84 * @see org.apache.james.jspf.parser.TermDefinition#getMatchSize() 85 */ 86 public int getMatchSize() { 87 return matchSize; 88 } 89 }