summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/AbstractProblemPreference.java
blob: 91969fcc225cf98e650d273b00af34b25a666041 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * 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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * Default implementation of problem preference. It keeps preference metadata
 * together with preference value. Some implementations may separate them.
 * 
 */
public abstract class AbstractProblemPreference implements IProblemPreference {
	/**
	 * default key for a preference
	 */
	public static final String PARAM = "params"; //$NON-NLS-1$
	private String key = PARAM;
	private String label = ""; //$NON-NLS-1$
	private String toolTip = null;
	private String uiInfo;
	private IProblemPreference parent;

	public String getLabel() {
		return label;
	}

	public String getToolTip() {
		return toolTip;
	}

	public String getKey() {
		return key;
	}

	public String getUiInfo() {
		return uiInfo;
	}

	/**
	 * Set preference key for itself
	 * 
	 * @param key
	 */
	public void setKey(String key) {
		if (key == null)
			throw new NullPointerException("key"); //$NON-NLS-1$
		if (isValidIdentifier(key))
			this.key = key;
		else
			throw new IllegalArgumentException(
					"Key must have java identifier syntax or number, i.e no dots and other funky stuff: " + key); //$NON-NLS-1$
	}

	protected boolean isValidIdentifier(String id) {
		if (id == null)
			return false;
		int n = id.length();
		if (n == 0)
			return false;
		if (id.equals("#")) //$NON-NLS-1$
			return true;
		for (int i = 0; i < n; i++)
			if (!Character.isJavaIdentifierPart(id.charAt(i)))
				return false;
		return true;
	}

	/**
	 * Sets a label for UI control
	 * 
	 * @param label
	 */
	public void setLabel(String label) {
		if (label == null)
			throw new NullPointerException("Label cannot be null"); //$NON-NLS-1$
		this.label = label;
	}

	/**
	 * Sets tooltip for ui control. Not supported now.
	 * 
	 * @param tooltip
	 */
	public void setToolTip(String tooltip) {
		this.toolTip = tooltip;
	}

	/**
	 * Sets uiinfo for ui control. Not supported now.
	 * 
	 * @param uiinfo
	 */
	public void setUiInfo(String uiinfo) {
		this.uiInfo = uiinfo;
	}

	public Object getValue() {
		throw new UnsupportedOperationException();
	}

	public void setValue(Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	/**
	 * @param str
	 * @return
	 */
	protected StreamTokenizer getImportTokenizer(String str) {
		ByteArrayInputStream st = new ByteArrayInputStream(str.getBytes());
		StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(
				st));
		tokenizer.resetSyntax();
		tokenizer.quoteChar('"');
		tokenizer.wordChars('_', '_');
		tokenizer.wordChars('-', '-');
		tokenizer.wordChars('.', '.');
		tokenizer.wordChars('0', '9');
		tokenizer.wordChars('a', 'z');
		tokenizer.wordChars('A', 'Z');
		tokenizer.wordChars(128 + 32, 255);
		tokenizer.whitespaceChars(0, ' ');
		tokenizer.commentChar('/');
		return tokenizer;
	}

	public IProblemPreference getParent() {
		return parent;
	}

	/**
	 * @param parent
	 *        the parent to set
	 */
	public void setParent(IProblemPreference parent) {
		this.parent = parent;
	}

	public String getQualifiedKey() {
		if (parent == null)
			return getKey();
		return parent.getQualifiedKey() + "." + getKey(); //$NON-NLS-1$
	}

	/**
	 * @param tokenizer
	 * @throws IOException
	 */
	public abstract void importValue(StreamTokenizer tokenizer)
			throws IOException;

	public void importValue(String str) {
		StreamTokenizer tokenizer = getImportTokenizer(str);
		try {
			importValue(tokenizer);
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException(str, e);
		} catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
	}

	protected String escape(String x) {
		x = x.replaceAll("[\"\\\\]", "\\\\$0"); //$NON-NLS-1$//$NON-NLS-2$
		return "\"" + x + "\""; //$NON-NLS-1$//$NON-NLS-2$
	}

	/**
	 * @param str
	 * @return
	 */
	protected String unescape(String str) {
		StreamTokenizer tokenizer = getImportTokenizer(str);
		try {
			tokenizer.nextToken();
		} catch (IOException e) {
			return null;
		}
		String sval = tokenizer.sval;
		return sval;
	}
}