001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2006-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.registry.xml;
019:
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.text.DateFormat;
023: import java.text.NumberFormat;
024: import java.text.ParseException;
025: import java.text.SimpleDateFormat;
026: import java.util.Locale;
027: import java.util.StringTokenizer;
028: import org.java.plugin.registry.Extension;
029: import org.java.plugin.registry.ExtensionPoint;
030: import org.java.plugin.registry.ParameterType;
031: import org.java.plugin.registry.PluginRegistry;
032: import org.java.plugin.registry.ExtensionPoint.ParameterDefinition;
033:
034: /**
035: * @version $Id$
036: */
037: class ParameterValueParser {
038: private static ExtensionPoint getExtensionPoint(
039: final PluginRegistry registry, final String uniqueId) {
040: String pluginId = registry.extractPluginId(uniqueId);
041: if (!registry.isPluginDescriptorAvailable(pluginId)) {
042: return null;
043: }
044: String pointId = registry.extractId(uniqueId);
045: for (ExtensionPoint point : registry.getPluginDescriptor(
046: pluginId).getExtensionPoints()) {
047: if (point.getId().equals(pointId)) {
048: return point;
049: }
050: }
051: return null;
052: }
053:
054: private Object value;
055: private final boolean isParsingSucceeds;
056: private String parsingMessage;
057:
058: ParameterValueParser(final PluginRegistry registry,
059: final ParameterDefinition definition, final String rawValue) {
060: if (definition == null) {
061: parsingMessage = "parameter definition is NULL"; //$NON-NLS-1$
062: isParsingSucceeds = false;
063: return;
064: }
065: if (rawValue == null) {
066: isParsingSucceeds = true;
067: return;
068: }
069: if ((ParameterType.ANY == definition.getType())
070: || (ParameterType.NULL == definition.getType())) {
071: isParsingSucceeds = true;
072: return;
073: } else if (ParameterType.STRING == definition.getType()) {
074: value = rawValue;
075: isParsingSucceeds = true;
076: return;
077: }
078: String val = rawValue.trim();
079: if (val.length() == 0) {
080: isParsingSucceeds = true;
081: return;
082: }
083: switch (definition.getType()) {
084: case BOOLEAN:
085: if ("true".equals(val)) { //$NON-NLS-1$
086: value = Boolean.TRUE;
087: } else if ("false".equals(val)) { //$NON-NLS-1$
088: value = Boolean.FALSE;
089: } else {
090: isParsingSucceeds = false;
091: return;
092: }
093: break;
094: case NUMBER:
095: try {
096: value = NumberFormat.getInstance(Locale.ENGLISH).parse(
097: val);
098: } catch (ParseException nfe) {
099: isParsingSucceeds = false;
100: return;
101: }
102: break;
103: case DATE: {
104: DateFormat fmt = new SimpleDateFormat(
105: "yyyy-MM-dd", Locale.ENGLISH); //$NON-NLS-1$
106: try {
107: value = fmt.parse(val);
108: } catch (ParseException pe) {
109: isParsingSucceeds = false;
110: return;
111: }
112: break;
113: }
114: case TIME: {
115: DateFormat fmt = new SimpleDateFormat(
116: "HH:mm:ss", Locale.ENGLISH); //$NON-NLS-1$
117: try {
118: value = fmt.parse(val);
119: } catch (ParseException pe) {
120: isParsingSucceeds = false;
121: return;
122: }
123: break;
124: }
125: case DATE_TIME: {
126: DateFormat fmt = new SimpleDateFormat(
127: "yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); //$NON-NLS-1$
128: try {
129: value = fmt.parse(val);
130: } catch (ParseException pe) {
131: isParsingSucceeds = false;
132: return;
133: }
134: break;
135: }
136: case PLUGIN_ID:
137: try {
138: value = registry.getPluginDescriptor(val);
139: } catch (IllegalArgumentException iae) {
140: parsingMessage = "unknown plug-in ID " + val; //$NON-NLS-1$
141: isParsingSucceeds = false;
142: return;
143: }
144: break;
145: case EXTENSION_POINT_ID:
146: value = getExtensionPoint(registry, val);
147: if (value == null) {
148: parsingMessage = "unknown extension point UID " + val; //$NON-NLS-1$
149: isParsingSucceeds = false;
150: return;
151: }
152: if (definition.getCustomData() != null) {
153: ExtensionPoint customExtPoint = getExtensionPoint(
154: registry, definition.getCustomData());
155: if (customExtPoint == null) {
156: parsingMessage = "unknown extension point UID " //$NON-NLS-1$
157: + definition.getCustomData()
158: + " provided as custom data"; //$NON-NLS-1$
159: isParsingSucceeds = false;
160: return;
161: }
162: if (!((ExtensionPoint) value)
163: .isSuccessorOf(customExtPoint)) {
164: parsingMessage = "extension point with UID " + val //$NON-NLS-1$
165: + " doesn't \"inherit\" point that is defined" //$NON-NLS-1$
166: + " according to custom data in parameter" //$NON-NLS-1$
167: + " definition - " //$NON-NLS-1$
168: + definition.getCustomData();
169: isParsingSucceeds = false;
170: return;
171: }
172: }
173: break;
174: case EXTENSION_ID:
175: String extId = registry.extractId(val);
176: for (Extension ext : registry.getPluginDescriptor(
177: registry.extractPluginId(val)).getExtensions()) {
178: if (ext.getId().equals(extId)) {
179: value = ext;
180: break;
181: }
182: }
183: if (value == null) {
184: parsingMessage = "unknown extension UID " + val; //$NON-NLS-1$
185: isParsingSucceeds = false;
186: return;
187: }
188: if (definition.getCustomData() != null) {
189: ExtensionPoint customExtPoint = getExtensionPoint(
190: registry, definition.getCustomData());
191: if (customExtPoint == null) {
192: parsingMessage = "unknown extension point UID " //$NON-NLS-1$
193: + definition.getCustomData()
194: + " provided as custom data in parameter definition " //$NON-NLS-1$
195: + definition;
196: isParsingSucceeds = false;
197: return;
198: }
199: String extPointUid = registry.makeUniqueId(
200: ((Extension) value).getExtendedPluginId(),
201: ((Extension) value).getExtendedPointId());
202: ExtensionPoint extPoint = getExtensionPoint(registry,
203: extPointUid);
204: if (extPoint == null) {
205: parsingMessage = "extension point " + extPointUid //$NON-NLS-1$
206: + " is unknown for extension " //$NON-NLS-1$
207: + ((Extension) value).getUniqueId();
208: isParsingSucceeds = false;
209: return;
210: }
211: if (!extPoint.equals(customExtPoint)
212: && !extPoint.isSuccessorOf(customExtPoint)) {
213: parsingMessage = "extension with UID " + val //$NON-NLS-1$
214: + " extends point that not allowed according" //$NON-NLS-1$
215: + " to custom data defined in parameter" //$NON-NLS-1$
216: + " definition - " //$NON-NLS-1$
217: + definition.getCustomData();
218: isParsingSucceeds = false;
219: return;
220: }
221: }
222: break;
223: case FIXED:
224: for (StringTokenizer st = new StringTokenizer(definition
225: .getCustomData(), "|", false); //$NON-NLS-1$
226: st.hasMoreTokens();) {
227: if (val.equals(st.nextToken().trim())) {
228: value = val;
229: isParsingSucceeds = true;
230: return;
231: }
232: }
233: parsingMessage = "not allowed value " + val; //$NON-NLS-1$
234: isParsingSucceeds = false;
235: return;
236: case RESOURCE:
237: try {
238: value = new URL(val);
239: } catch (MalformedURLException mue) {
240: parsingMessage = "can't parse value " + val //$NON-NLS-1$
241: + " as an absolute URL, will treat it as relative URL"; //$NON-NLS-1$
242: //return Boolean.FALSE;
243: value = null;
244: }
245: isParsingSucceeds = true;
246: return;
247: case ANY:
248: // no-op
249: break;
250: case NULL:
251: // no-op
252: break;
253: case STRING:
254: // no-op
255: break;
256: }
257: isParsingSucceeds = true;
258: }
259:
260: Object getValue() {
261: return value;
262: }
263:
264: String getParsingMessage() {
265: return parsingMessage;
266: }
267:
268: boolean isParsingSucceeds() {
269: return isParsingSucceeds;
270: }
271: }
|