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 |
|
|
19 |
package com.nokia.helium.metadata.ant.types;
|
|
20 |
|
628
|
21 |
import java.io.File;
|
|
22 |
import java.io.FileNotFoundException;
|
|
23 |
import java.io.FileReader;
|
|
24 |
import java.util.Collection;
|
|
25 |
import java.util.Vector;
|
|
26 |
|
587
|
27 |
import org.apache.tools.ant.BuildException;
|
|
28 |
import org.apache.tools.ant.Project;
|
628
|
29 |
import org.apache.tools.ant.types.DataType;
|
|
30 |
|
587
|
31 |
import fmpp.models.CsvSequence;
|
628
|
32 |
import fmpp.util.StringUtil.ParseException;
|
|
33 |
import freemarker.template.TemplateModelException;
|
587
|
34 |
import freemarker.template.TemplateSequenceModel;
|
|
35 |
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This type contains set of filters to be used by the metadatarecord. Two options:
|
|
39 |
* <pre>
|
|
40 |
* Example 1:
|
|
41 |
* <metadatafilterset id="common" filterfile="filters.csv" />
|
|
42 |
*
|
|
43 |
* Example 2:
|
|
44 |
* <metadatafilterset id="common"/>
|
628
|
45 |
* <metadatafilter severity="error" regex="^make(?:\[\d+\])?:\s+.*\s+not\s+remade" description="make error" />
|
|
46 |
* <metadatafilter severity="error"regex="" description="" />
|
587
|
47 |
* <metadatafilterset/>
|
|
48 |
*
|
|
49 |
* Example 3:
|
|
50 |
* <metadatafilterset refid="common" />
|
|
51 |
* </pre>
|
|
52 |
* @ant.task name="metadatafilterset" category="Metadata"
|
|
53 |
*/
|
628
|
54 |
public class MetaDataFilterSet extends DataType implements MetaDataFilterCollection {
|
|
55 |
private Vector<MetaDataFilterCollection> filterCollections = new Vector<MetaDataFilterCollection>();
|
|
56 |
private File filterFile;
|
|
57 |
private boolean initialized;
|
587
|
58 |
|
|
59 |
/**
|
|
60 |
* Helper function called by ant to set the FilterFile
|
|
61 |
* @param FilterFile the csv file used by the filterset
|
|
62 |
*/
|
628
|
63 |
public void setFilterFile(File file) throws Exception {
|
587
|
64 |
filterFile = file;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Helper function called to get FilterFile.
|
|
69 |
* @return filterfile used by this filterset
|
|
70 |
*/
|
628
|
71 |
public File getFilterFile() {
|
587
|
72 |
return filterFile;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Helper function called to get filterlist.
|
|
77 |
* FilterSet can contain nested filterset, individual filter.
|
|
78 |
* Individual filter is created as nestedFilterSet and appended so that
|
|
79 |
* the precedence is maintained.
|
|
80 |
* @return All filters
|
|
81 |
*/
|
628
|
82 |
public Collection<MetaDataFilter> getAllFilters() {
|
|
83 |
// Shall we treat current object as a reference?
|
|
84 |
if (this.isReference()) {
|
|
85 |
if (filterFile != null) {
|
|
86 |
throw new BuildException("You cannot use the 'filterFile' in reference object.");
|
|
87 |
}
|
|
88 |
if (!filterCollections.isEmpty()) {
|
|
89 |
throw new BuildException("You cannot have nested filters when using a reference object.");
|
|
90 |
}
|
|
91 |
Object filterSetObject = this.getRefid().getReferencedObject();
|
|
92 |
if (filterSetObject != null && filterSetObject instanceof MetaDataFilterCollection) {
|
|
93 |
Collection<MetaDataFilter> allFilters = ((MetaDataFilterCollection)filterSetObject).getAllFilters();
|
|
94 |
checkInvalidFilters(allFilters);
|
|
95 |
return allFilters;
|
|
96 |
} else {
|
|
97 |
throw new BuildException("Filterset object is not instance of MetaDataFilterCollection");
|
|
98 |
}
|
|
99 |
} else {
|
|
100 |
if (!initialized) {
|
|
101 |
if (filterFile != null) {
|
|
102 |
addDataFromCSVFile();
|
|
103 |
}
|
|
104 |
initialized = true;
|
|
105 |
}
|
|
106 |
Collection<MetaDataFilter> allFilters = new Vector<MetaDataFilter>();
|
|
107 |
// Add any nested filterCollection
|
|
108 |
for (MetaDataFilterCollection filterCollection : filterCollections) {
|
|
109 |
allFilters.addAll(filterCollection.getAllFilters());
|
|
110 |
}
|
|
111 |
checkInvalidFilters(allFilters);
|
587
|
112 |
return allFilters;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Helper function called to remove any invalid filters
|
|
118 |
* @return only the valid filters
|
|
119 |
*/
|
628
|
120 |
private void checkInvalidFilters(Collection<MetaDataFilter> filterList) {
|
|
121 |
int count = 0;
|
|
122 |
for (MetaDataFilter filter : filterList) {
|
|
123 |
SeverityEnum.Severity severity = filter.getSeverity();
|
587
|
124 |
String regEx = filter.getRegex();
|
628
|
125 |
if (severity == null || regEx == null) {
|
|
126 |
log("Invalid filter found at " + filter.getLocation().toString(), Project.MSG_ERR);
|
|
127 |
count++;
|
587
|
128 |
}
|
|
129 |
}
|
628
|
130 |
if (count > 0) {
|
|
131 |
throw new BuildException("Invalid filter have been found. Please check your configuration.");
|
|
132 |
}
|
587
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Helper function called by ant to create the new filter
|
|
137 |
*/
|
|
138 |
public MetaDataFilter createMetaDataFilter() {
|
|
139 |
MetaDataFilter filter = new MetaDataFilter();
|
|
140 |
add(filter);
|
|
141 |
return filter;
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Helper function called by ant to create the new filter
|
|
146 |
*/
|
|
147 |
public MetaDataFilterSet createMetaDataFilterSet() {
|
|
148 |
MetaDataFilterSet filterSet = new MetaDataFilterSet();
|
|
149 |
add(filterSet);
|
|
150 |
return filterSet;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Helper function to add the created filter
|
|
155 |
* @param filter to be added to the filterset
|
|
156 |
*/
|
628
|
157 |
public void add(MetaDataFilterCollection filterCollection) {
|
|
158 |
if (filterCollection != null) {
|
|
159 |
filterCollections.add(filterCollection);
|
587
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Helper function to add the filters from the csv files
|
|
165 |
* @param csv file path from which the filters needs to be added.
|
|
166 |
*/
|
628
|
167 |
private void addDataFromCSVFile() {
|
587
|
168 |
CsvSequence csvs = new CsvSequence();
|
|
169 |
csvs.setSeparator(',');
|
|
170 |
try {
|
628
|
171 |
csvs.load(new FileReader(filterFile));
|
|
172 |
int size = 0;
|
|
173 |
size = csvs.size();
|
|
174 |
for (int i = 0; i < size; i++) {
|
|
175 |
TemplateSequenceModel model = (TemplateSequenceModel)csvs.get(i);
|
|
176 |
int modelSize = model.size();
|
|
177 |
if (modelSize != 3 ) {
|
|
178 |
throw new BuildException("Metadata CSV file filter file format is invalid. It model must have 3 column, it currently has " + size);
|
|
179 |
}
|
|
180 |
MetaDataFilter filter = new MetaDataFilter();
|
|
181 |
SeverityEnum severity = new SeverityEnum();
|
|
182 |
severity.setValue(model.get(0).toString());
|
|
183 |
filter.setSeverity(severity);
|
|
184 |
filter.setRegex(model.get(1).toString());
|
|
185 |
filter.setDescription(model.get(2).toString());
|
|
186 |
filterCollections.add(filter);
|
|
187 |
}
|
|
188 |
} catch (FileNotFoundException fex) {
|
|
189 |
throw new BuildException(fex.getMessage(), fex);
|
|
190 |
} catch (ParseException pex) {
|
|
191 |
throw new BuildException(pex.getMessage(), pex);
|
587
|
192 |
} catch (java.io.IOException iex) {
|
628
|
193 |
throw new BuildException(iex.getMessage(), iex);
|
|
194 |
} catch (TemplateModelException e) {
|
|
195 |
throw new BuildException(e.getMessage(), e);
|
588
|
196 |
}
|
587
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
|