View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.p2elv2.functions;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.gwe.p2elv2.PFunction;
24  import org.gwe.p2elv2.PStatementContext;
25  import org.gwe.p2elv2.PVarValueSpace;
26  import org.gwe.utils.rex.REXUtils;
27  
28  /**
29   * @author Marco Ruiz
30   * @since Aug 11, 2008
31   */
32  public class PFRegExp extends PFunction {
33  
34  	public PFRegExp() { super("regExp"); }
35  	
36      public PVarValueSpace calculateValues(List<String> params, PStatementContext ctx) {
37      	PVarValueSpace result = new PVarValueSpace();
38      	
39      	Params par = new Params(params);
40      	if (params.size() < 4) return result;
41      	
42  	    String[] regexps = new String[]{par.prefixExp, par.matchExp, par.suffixExp};
43  		List<List<String>> fieldSources = REXUtils.findRepeatsStrong(par.source, regexps, true);
44  		
45  		if (par.matchIndex < 0) {
46  		    for (List<String> fldSource : fieldSources) 
47  		    	result.add(fldSource.get(1));
48  		} else {
49  			String resVal = (fieldSources.size() > par.matchIndex) ? fieldSources.get(par.matchIndex).get(1) : "";
50  			result.add(resVal);
51  		}
52      	
53  	    return result;
54      }
55  
56  	public boolean isSingleValue(List<String> params) {
57  		return new Params(params).matchIndex >= 0;
58  	}
59  
60  	class Params {
61      	private int matchIndex = -1;
62      	private String source;
63      	private String prefixExp;
64      	private String matchExp;
65      	private String suffixExp;
66      	
67      	public Params(List<String> params) {
68      		try {
69      			matchIndex = Integer.parseInt(safelyGet(params, 0));
70      		} catch(Exception e) {}
71      		source        = safelyGet(params, 1);
72      		prefixExp     = safelyGet(params, 2);
73      		matchExp      = safelyGet(params, 3);
74      		suffixExp     = safelyGet(params, 4);
75      	}
76      	
77      	private String safelyGet(List<String> params, int index) {
78      		return (params.size() > index) ? params.get(index) : "";
79      	}
80      }
81  
82      public static void main(String[] args) {
83  /*  
84  		$regexp(/home/user/data/file, /, [^/]*, /)                                      = home, user, data, file
85  		$regexp(/home/user/data/file, /, [^/]*, $)                                      = file
86  		$regexp(cmd --aIndex 1 --bIndex 2 --other 4 --cIndex 3, \s*--, [^\s]*, Index)   = a, b, c
87  		$regexp(http://host/path, ://, [^/]*, /)                                        = host
88  */    	
89  
90      	testCase("");
91      	testCase("-1");
92      	testCase("0");
93      	testCase("1");
94      	testCase("2");
95      	testCase("3");
96      	testCase("4");
97      	testCase("5");
98      }
99  
100 	private static void testCase(String maxStr) {
101 	    testParameters(maxStr, "/home/user/data/file", "/", "[^/]*");
102     	testParameters(maxStr, "/home/user/data/file", "/", "[^/]*", "$");
103     	testParameters(maxStr, "cmd --aIndex 1 --bIndex 2 --other 4 --cIndex 3", "\\s*--", "[^\\s]*", "Index");
104     	testParameters(maxStr, "http://host/path", "://", "[^/]*", "/");
105 		System.out.println();
106     }
107     
108     private static void testParameters(String... params) {
109     	PFRegExp testObj = new PFRegExp();
110     	PVarValueSpace values = testObj.calculateValues(new ArrayList<String>(Arrays.asList(params)), null);
111 		System.out.println(values);
112     }
113 }
114