587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.metadata.ant.conditions;
|
|
19 |
|
|
20 |
|
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.List;
|
|
23 |
import java.util.regex.Matcher;
|
|
24 |
import java.util.regex.Pattern;
|
|
25 |
|
|
26 |
import org.apache.tools.ant.BuildException;
|
|
27 |
import org.apache.tools.ant.taskdefs.condition.Condition;
|
628
|
28 |
import org.apache.tools.ant.types.DataType;
|
587
|
29 |
|
|
30 |
import com.nokia.helium.metadata.ant.types.MetaDataFilter;
|
|
31 |
import com.nokia.helium.metadata.ant.types.MetaDataFilterSet;
|
628
|
32 |
import com.nokia.helium.metadata.ant.types.SeverityEnum;
|
587
|
33 |
|
|
34 |
/**
|
|
35 |
* This class implements a Ant Condition which report true if it finds the given
|
628
|
36 |
* input string is matched against the given filter of given severity
|
587
|
37 |
*
|
|
38 |
* Example:
|
|
39 |
* <pre>
|
|
40 |
* <target name="test-metadata-regex">
|
|
41 |
* <au:assertTrue>
|
|
42 |
* <hlm:metadataRegexTest severity="ERROR" string="Error:">
|
|
43 |
* <metadatafilterset refid="filterset.sbs"/>
|
|
44 |
* </hlm:metadataRegexTest>
|
|
45 |
* </au:assertTrue>
|
|
46 |
* </target>
|
|
47 |
* </pre>
|
|
48 |
*
|
628
|
49 |
* The condition will eval as true if the string is matched with any of the pattern in the given severity
|
587
|
50 |
*
|
|
51 |
* @ant.type name="metadataRegexTest" category="Metadata"
|
|
52 |
*/
|
628
|
53 |
public class MetaDataRegexTestCondition extends DataType implements Condition {
|
587
|
54 |
|
628
|
55 |
private SeverityEnum severity;
|
587
|
56 |
private String string;
|
|
57 |
|
|
58 |
private List<MetaDataFilterSet> filterSets = new ArrayList<MetaDataFilterSet>();
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Sets which severity to be searched.
|
|
62 |
*
|
|
63 |
* @param severity
|
|
64 |
* @ant.required
|
|
65 |
*/
|
628
|
66 |
public void setSeverity(SeverityEnum severity) {
|
587
|
67 |
this.severity = severity;
|
|
68 |
}
|
|
69 |
/**
|
|
70 |
* Sets which string to be matched against regular expression.
|
|
71 |
*
|
|
72 |
* @param string
|
|
73 |
* @ant.required
|
|
74 |
*/
|
|
75 |
public void setString(String string) {
|
|
76 |
this.string = string;
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Helper function to add the created filter
|
|
81 |
* @param filter to be added to the filterset
|
|
82 |
*/
|
|
83 |
public void add(MetaDataFilterSet filterSet) {
|
|
84 |
if (filterSet != null) {
|
|
85 |
filterSets.add(filterSet);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
/**
|
|
92 |
* This method iterates through the regular expression patterns and match the input string against them.
|
628
|
93 |
* @return true if the string is matched with any of the pattern in the given severity, false otherwise.
|
587
|
94 |
*/
|
|
95 |
public boolean eval() {
|
628
|
96 |
if (this.severity == null) {
|
588
|
97 |
throw new BuildException("'severity' attribute is not defined");
|
628
|
98 |
}
|
|
99 |
if (this.string == null || (this.string != null && this.string.isEmpty())) {
|
588
|
100 |
throw new BuildException("'string' attribute is not defined");
|
628
|
101 |
}
|
588
|
102 |
for (MetaDataFilterSet set : filterSets) {
|
|
103 |
for (MetaDataFilter filter : set.getAllFilters()) {
|
|
104 |
Pattern pattern = filter.getPattern();
|
|
105 |
Matcher matcher = pattern.matcher(this.string);
|
|
106 |
if (matcher.matches()) {
|
628
|
107 |
return severity.getSeverity().equals(filter.getSeverity());
|
588
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
return false;
|
587
|
112 |
}
|
|
113 |
}
|