227 init(null); |
227 init(null); |
228 else |
228 else |
229 init(inDescr.descriptorMap); |
229 init(inDescr.descriptorMap); |
230 } |
230 } |
231 |
231 |
232 |
232 /** |
233 /** |
233 * <p>Descriptor constructor taking an XML String or a |
234 * <p>Descriptor constructor taking an XML String.</p> |
234 * <i>fieldName=fieldValue</i> format String. The String parameter is |
|
235 * parsed as XML if it begins with a '<' character.</p> |
235 * |
236 * |
236 * <p>The format of the XML string is not defined, but an |
237 * <p>The format of the XML string is not defined, but an |
237 * implementation must ensure that the string returned by |
238 * implementation must ensure that the string returned by |
238 * {@link #toXMLString() toXMLString()} on an existing |
239 * {@link #toXMLString() toXMLString()} on an existing |
239 * descriptor can be used to instantiate an equivalent |
240 * descriptor can be used to instantiate an equivalent |
242 * <p>In this implementation, all field values will be created |
243 * <p>In this implementation, all field values will be created |
243 * as Strings. If the field values are not Strings, the |
244 * as Strings. If the field values are not Strings, the |
244 * programmer will have to reset or convert these fields |
245 * programmer will have to reset or convert these fields |
245 * correctly.</p> |
246 * correctly.</p> |
246 * |
247 * |
247 * @param inStr An XML-formatted string used to populate this |
248 * @param inStr An XML-format or a fieldName=fieldValue formatted string |
248 * Descriptor. The format is not defined, but any |
249 * used to populate this Descriptor. The XML format is not defined, but any |
249 * implementation must ensure that the string returned by |
250 * implementation must ensure that the string returned by |
250 * method {@link #toXMLString toXMLString} on an existing |
251 * method {@link #toXMLString toXMLString} on an existing |
251 * descriptor can be used to instantiate an equivalent |
252 * descriptor can be used to instantiate an equivalent |
252 * descriptor when instantiated using this constructor. |
253 * descriptor when instantiated using this constructor. |
253 * |
254 * |
254 * @exception RuntimeOperationsException If the String inStr |
255 * @exception RuntimeOperationsException If the String inStr passed in |
255 * passed in parameter is null |
256 * parameter is null or, when it is not an XML string, if the field name or |
|
257 * field value is illegal. If inStr is not an XML string then it must |
|
258 * contain an "=". "fieldValue", "fieldName", and "fieldValue" are illegal. |
|
259 * FieldName cannot be empty. "fieldName=" will cause the value to be empty. |
256 * @exception XMLParseException XML parsing problem while parsing |
260 * @exception XMLParseException XML parsing problem while parsing |
257 * the input String |
261 * the XML-format input String |
258 * @exception MBeanException Wraps a distributed communication Exception. |
262 * @exception MBeanException Wraps a distributed communication Exception. |
259 */ |
263 */ |
260 /* At some stage we should rewrite this code to be cleverer. Using |
264 /* At some stage we should rewrite this code to be cleverer. Using |
261 a StringTokenizer as we do means, first, that we accept a lot of |
265 a StringTokenizer as we do means, first, that we accept a lot of |
262 bogus strings without noticing they are bogus, and second, that we |
266 bogus strings without noticing they are bogus, and second, that we |
281 final String msg = "String in parameter is null"; |
285 final String msg = "String in parameter is null"; |
282 final RuntimeException iae = new IllegalArgumentException(msg); |
286 final RuntimeException iae = new IllegalArgumentException(msg); |
283 throw new RuntimeOperationsException(iae, msg); |
287 throw new RuntimeOperationsException(iae, msg); |
284 } |
288 } |
285 |
289 |
|
290 // parse parameter string into structures |
|
291 |
|
292 init(null); |
|
293 |
|
294 if(!inStr.startsWith("<")) { |
|
295 parseNamesValues(inStr); |
|
296 if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) { |
|
297 MODELMBEAN_LOGGER.logp(Level.FINEST, |
|
298 DescriptorSupport.class.getName(), |
|
299 "Descriptor(name=value)", "Exit"); |
|
300 } |
|
301 return; |
|
302 } |
|
303 |
286 final String lowerInStr = inStr.toLowerCase(); |
304 final String lowerInStr = inStr.toLowerCase(); |
287 if (!lowerInStr.startsWith("<descriptor>") |
305 if (!lowerInStr.startsWith("<descriptor>") |
288 || !lowerInStr.endsWith("</descriptor>")) { |
306 || !lowerInStr.endsWith("</descriptor>")) { |
289 throw new XMLParseException("No <descriptor>, </descriptor> pair"); |
307 throw new XMLParseException("No <descriptor>, </descriptor> pair"); |
290 } |
308 } |
291 |
309 |
292 // parse xmlstring into structures |
310 |
293 init(null); |
|
294 // create dummy descriptor: should have same size |
311 // create dummy descriptor: should have same size |
295 // as number of fields in xmlstring |
312 // as number of fields in xmlstring |
296 // loop through structures and put them in descriptor |
313 // loop through structures and put them in descriptor |
297 |
314 |
298 StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f"); |
315 StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f"); |
452 if (( fields == null ) || ( fields.length == 0)) |
469 if (( fields == null ) || ( fields.length == 0)) |
453 return; |
470 return; |
454 |
471 |
455 init(null); |
472 init(null); |
456 |
473 |
|
474 parseNamesValues(fields); |
|
475 |
|
476 if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) { |
|
477 MODELMBEAN_LOGGER.logp(Level.FINEST, |
|
478 DescriptorSupport.class.getName(), |
|
479 "Descriptor(String... fields)", "Exit"); |
|
480 } |
|
481 } |
|
482 |
|
483 private void parseNamesValues(String... fields) { |
457 for (int i=0; i < fields.length; i++) { |
484 for (int i=0; i < fields.length; i++) { |
458 if ((fields[i] == null) || (fields[i].equals(""))) { |
485 if ((fields[i] == null) || (fields[i].equals(""))) { |
459 continue; |
486 continue; |
460 } |
487 } |
461 int eq_separator = fields[i].indexOf("="); |
488 int eq_separator = fields[i].indexOf("="); |
492 final RuntimeException iae = new IllegalArgumentException(msg); |
519 final RuntimeException iae = new IllegalArgumentException(msg); |
493 throw new RuntimeOperationsException(iae, msg); |
520 throw new RuntimeOperationsException(iae, msg); |
494 } |
521 } |
495 |
522 |
496 setField(fieldName,fieldValue); |
523 setField(fieldName,fieldValue); |
497 } |
|
498 if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) { |
|
499 MODELMBEAN_LOGGER.logp(Level.FINEST, |
|
500 DescriptorSupport.class.getName(), |
|
501 "Descriptor(String... fields)", "Exit"); |
|
502 } |
524 } |
503 } |
525 } |
504 |
526 |
505 private void init(Map<String, ?> initMap) { |
527 private void init(Map<String, ?> initMap) { |
506 descriptorMap = |
528 descriptorMap = |