Skip to main content
summaryrefslogtreecommitdiffstats
blob: 261a9c2b4721eaf77d0cd5d10f604bba0f30e574 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.help.internal.criteria;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.help.ICriteria;

import org.eclipse.help.internal.HelpPlugin;

/**
 * A class represents one criterion, which has the name and values
 *
 * @since 3.5
 */
public class CriterionResource {

	private String criterionName;
	private List criterionValues;

	public CriterionResource(String criterionName){
		this(criterionName, null);
	}

	public CriterionResource (String criterionName, List criterionValues){
		this.criterionName = criterionName;
		this.criterionValues = new ArrayList();
		if(null != criterionValues) {
			this.addCriterionValues(criterionValues);
		}
	}

	public String getCriterionName(){
		return this.criterionName;
	}

	public List getCriterionValues(){
		return this.criterionValues;
	}

	public void addCriterionValue(String criterionValue){
		if(null != criterionValue && 0 != criterionValue.length() && !criterionValues.contains(criterionValue)){
			criterionValues.add(criterionValue);
		}
	}

	public void addCriterionValues(List criterionValues){
		for(Iterator iterator = criterionValues.iterator(); iterator.hasNext();){
			String criterionValue = (String) iterator.next();
			this.addCriterionValue(criterionValue);
		}
	}

	public static CriterionResource[] toCriterionResource(ICriteria[] criteriaElements) {
		List criteriaList = new ArrayList();
		outer: for (int i = 0; i < criteriaElements.length; ++i) {
			String elementName = criteriaElements[i].getName();
			String elementValue = criteriaElements[i].getValue();
			if (null != elementName && 0 != elementName.length() && null != elementValue
					&& 0 != elementValue.length()) {
				if (HelpPlugin.getCriteriaManager().isSupportedCriterion(elementName)) {
					elementName = elementName.toLowerCase();
					StringTokenizer tokenizer = new StringTokenizer(elementValue, ","); //$NON-NLS-1$
					List values = new ArrayList();
					while (tokenizer.hasMoreTokens()) {
						values.add(tokenizer.nextToken().trim());
					}
					for(int j = 0; j < criteriaList.size(); ++j){
						CriterionResource criterion = (CriterionResource) criteriaList.get(j);
						if(elementName.equals(criterion.getCriterionName())){
							criterion.addCriterionValues(values);
							continue outer;
						}
					}
				    CriterionResource criterionResource = new CriterionResource(elementName, values);
				    criteriaList.add(criterionResource);
				}
			}
		}
		CriterionResource[] criteria = new CriterionResource[criteriaList.size()];
		criteriaList.toArray(criteria);
		return criteria;
	}

}

Back to the top