1 package org.java.plugin.registry.xml; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.text.DateFormat ; 6 import java.text.NumberFormat ; 7 import java.text.ParseException ; 8 import java.text.SimpleDateFormat ; 9 import java.util.Iterator ; 10 import java.util.Locale ; 11 import java.util.StringTokenizer ; 12 13 import org.java.plugin.registry.Extension; 14 import org.java.plugin.registry.ExtensionPoint; 15 import org.java.plugin.registry.PluginRegistry; 16 import org.java.plugin.registry.ExtensionPoint.ParameterDefinition; 17 18 21 class ParameterValueParser { 22 private static ExtensionPoint getExtensionPoint( 23 final PluginRegistry registry, final String uniqueId) { 24 String pluginId = registry.extractPluginId(uniqueId); 25 if (!registry.isPluginDescriptorAvailable(pluginId)) { 26 return null; 27 } 28 String pointId = registry.extractId(uniqueId); 29 for (Iterator it = registry.getPluginDescriptor(pluginId) 30 .getExtensionPoints().iterator(); it.hasNext();) { 31 ExtensionPoint point = (ExtensionPoint) it.next(); 32 if (point.getId().equals(pointId)) { 33 return point; 34 } 35 } 36 return null; 37 } 38 39 private Object value; 40 private final boolean isParsingSucceeds; 41 private String parsingMessage; 42 43 ParameterValueParser(final PluginRegistry registry, 44 final ParameterDefinition definition, final String rawValue) { 45 if (definition == null) { 46 parsingMessage = "parameter definition is NULL"; isParsingSucceeds = false; 48 return; 49 } 50 if (rawValue == null) { 51 isParsingSucceeds = true; 52 return; 53 } 54 if (ParameterDefinition.TYPE_ANY.equals(definition.getType()) 55 || ParameterDefinition.TYPE_NULL.equals( 56 definition.getType())) { 57 isParsingSucceeds = true; 58 return; 59 } else if (ParameterDefinition.TYPE_STRING.equals( 60 definition.getType())) { 61 value = rawValue; 62 isParsingSucceeds = true; 63 return; 64 } 65 String val = rawValue.trim(); 66 if (val.length() == 0) { 67 isParsingSucceeds = true; 68 return; 69 } 70 if (ParameterDefinition.TYPE_BOOLEAN.equals(definition.getType())) { 71 if ("true".equals(val)) { value = Boolean.TRUE; 73 } else if ("false".equals(val)) { value = Boolean.FALSE; 75 } else { 76 isParsingSucceeds = false; 77 return; 78 } 79 } else if (ParameterDefinition.TYPE_NUMBER.equals( 80 definition.getType())) { 81 try { 82 value = 83 NumberFormat.getInstance(Locale.ENGLISH).parse(val); 84 } catch (ParseException nfe) { 85 isParsingSucceeds = false; 86 return; 87 } 88 } else if (ParameterDefinition.TYPE_DATE.equals( 89 definition.getType())) { 90 DateFormat fmt = 91 new SimpleDateFormat ("yyyy-MM-dd", Locale.ENGLISH); try { 93 value = fmt.parse(val); 94 } catch (ParseException pe) { 95 isParsingSucceeds = false; 96 return; 97 } 98 } else if (ParameterDefinition.TYPE_TIME.equals( 99 definition.getType())) { 100 DateFormat fmt = 101 new SimpleDateFormat ("HH:mm:ss", Locale.ENGLISH); try { 103 value = fmt.parse(val); 104 } catch (ParseException pe) { 105 isParsingSucceeds = false; 106 return; 107 } 108 } else if (ParameterDefinition.TYPE_DATETIME.equals( 109 definition.getType())) { 110 DateFormat fmt = 111 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); try { 113 value = fmt.parse(val); 114 } catch (ParseException pe) { 115 isParsingSucceeds = false; 116 return; 117 } 118 } else if (ParameterDefinition.TYPE_PLUGIN_ID.equals( 119 definition.getType())) { 120 try { 121 value = registry.getPluginDescriptor(val); 122 } catch (IllegalArgumentException iae) { 123 parsingMessage = "unknown plug-in ID " + val; isParsingSucceeds = false; 125 return; 126 } 127 } else if (ParameterDefinition.TYPE_EXTENSION_POINT_ID.equals( 128 definition.getType())) { 129 value = getExtensionPoint(registry, val); 130 if (value == null) { 131 parsingMessage = "unknown extension point UID " + val; isParsingSucceeds = false; 133 return; 134 } 135 if (definition.getCustomData() != null) { 136 ExtensionPoint customExtPoint = 137 getExtensionPoint(registry, definition.getCustomData()); 138 if (customExtPoint == null) { 139 parsingMessage = "unknown extension point UID " + definition.getCustomData() 141 + " provided as custom data"; isParsingSucceeds = false; 143 return; 144 } 145 if (!((ExtensionPoint) value).isSuccessorOf( 146 customExtPoint)) { 147 parsingMessage = "extension point with UID " + val + " doesn't \"inherit\" point that is defined" + " according to custom data in parameter" + " definition - " + definition.getCustomData(); 152 isParsingSucceeds = false; 153 return; 154 } 155 } 156 } else if (ParameterDefinition.TYPE_EXTENSION_ID.equals( 157 definition.getType())) { 158 String extId = registry.extractId(val); 159 for (Iterator it = registry.getPluginDescriptor( 160 registry.extractPluginId(val)).getExtensions().iterator(); 161 it.hasNext();) { 162 Extension ext = (Extension) it.next(); 163 if (ext.getId().equals(extId)) { 164 value = ext; 165 break; 166 } 167 } 168 if (value == null) { 169 parsingMessage = "unknown extension UID " + val; isParsingSucceeds = false; 171 return; 172 } 173 if (definition.getCustomData() != null) { 174 ExtensionPoint customExtPoint = 175 getExtensionPoint(registry, definition.getCustomData()); 176 if (customExtPoint == null) { 177 parsingMessage = "unknown extension point UID " + definition.getCustomData() 179 + " provided as custom data in parameter definition " + definition; 181 isParsingSucceeds = false; 182 return; 183 } 184 String extPointUid = registry.makeUniqueId( 185 ((Extension) value).getExtendedPluginId(), 186 ((Extension) value).getExtendedPointId()); 187 ExtensionPoint extPoint = 188 getExtensionPoint(registry, extPointUid); 189 if (extPoint == null) { 190 parsingMessage = "extension point " + extPointUid + " is unknown for extension " + ((Extension) value).getUniqueId(); 193 isParsingSucceeds = false; 194 return; 195 } 196 if (!extPoint.equals(customExtPoint) 197 && !extPoint.isSuccessorOf(customExtPoint)) { 198 parsingMessage = "extension with UID " + val + " extends point that not allowed according" + " to custom data defined in parameter" + " definition - " + definition.getCustomData(); 203 isParsingSucceeds = false; 204 return; 205 } 206 } 207 } else if (ParameterDefinition.TYPE_FIXED.equals( 208 definition.getType())) { 209 for (StringTokenizer st = new StringTokenizer ( 210 definition.getCustomData(), "|", false); st.hasMoreTokens();) { 212 if (val.equals(st.nextToken().trim())) { 213 value = val; 214 isParsingSucceeds = true; 215 return; 216 } 217 } 218 parsingMessage = "not allowed value " + val; isParsingSucceeds = false; 220 return; 221 } else if (ParameterDefinition.TYPE_RESOURCE.equals( 222 definition.getType())) { 223 try { 224 value = new URL (val); 225 } catch (MalformedURLException mue) { 226 parsingMessage = "can't parse value " + val + " as an absolute URL, will treat it as relative URL"; value = null; 230 } 231 isParsingSucceeds = true; 232 return; 233 } 234 isParsingSucceeds = true; 235 } 236 237 Object getValue() { 238 return value; 239 } 240 241 String getParsingMessage() { 242 return parsingMessage; 243 } 244 245 boolean isParsingSucceeds() { 246 return isParsingSucceeds; 247 } 248 } 249 | Popular Tags |