summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/BasicProblemPreference.java
blob: fd146b0cc711ba79ae3828537bb3e3fc1d050657 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia 
 * 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:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.param;

import java.io.File;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.regex.Pattern;

/**
 * Preference representing a problem preference of a basic type.
 * 
 * @see IProblemPreferenceDescriptor.PreferenceType for types.
 * 
 */
public class BasicProblemPreference extends AbstractProblemPreference {
	protected Object value;
	private PreferenceType type = PreferenceType.TYPE_STRING;

	public PreferenceType getType() {
		return type;
	}

	/**
	 * Set preferene type
	 * 
	 * @param type
	 */
	public void setType(PreferenceType type) {
		if (type == null)
			throw new NullPointerException("Type cannot be null"); //$NON-NLS-1$
		this.type = type;
	}

	/**
	 * Generate an info with given key and label
	 * 
	 * @param key
	 *        - property id (use in actual property hash of a checker)
	 * @param label
	 *        - label to be shown to user
	 * @param type
	 *        - parameter type
	 */
	public BasicProblemPreference(String key, String label, PreferenceType type) {
		this(key, label);
		setType(type);
	}

	/**
	 * Generate an info with given key and label
	 * 
	 * @param key
	 *        - property id (use in actual property hash of a checker)
	 * @param label
	 *        - label to be shown to user
	 */
	public BasicProblemPreference(String key, String label) {
		setKey(key);
		setLabel(label);
	}

	@Override
	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public Object getValue() {
		return value;
	}

	public String exportValue() {
		Pattern pat = Pattern.compile("^[A-Za-z0-9._-]+$"); //$NON-NLS-1$
		String x = String.valueOf(getValue());
		if (pat.matcher(x).find() == false)
			return escape(x);
		return x;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.cdt.codan.core.param.IProblemPreferenceValue#importValue(
	 * java.lang.String)
	 */
	@Override
	public void importValue(String str) {
		if (str.startsWith("\"")) //$NON-NLS-1$
			str = unescape(str);
		switch (getType()) {
			case TYPE_STRING:
				setValue(str);
				break;
			case TYPE_INTEGER:
				setValue(Integer.parseInt(str));
				break;
			case TYPE_BOOLEAN:
				setValue(Boolean.valueOf(str));
				break;
			case TYPE_FILE:
				setValue(new File(str));
				break;
			default:
				throw new IllegalArgumentException(getType()
						+ " is not supported for basic type"); //$NON-NLS-1$
		}
	}

	@Override
	public String toString() {
		return "(" + type + ")" + getKey() + ((value == null) ? "" : "=" + value); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
	}

	@Override
	public void importValue(StreamTokenizer tokenizer) {
		try {
			tokenizer.nextToken();
			String val = tokenizer.sval;
			importValue(val);
		} catch (IOException e) {
			new IllegalArgumentException(e);
		}
	}
}