jdk/src/share/classes/javax/management/modelmbean/DescriptorSupport.java
changeset 4156 acaa49a2768a
parent 1698 2f0b565a475e
child 5506 202f599c92aa
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
   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      * <p>Descriptor constructor taking an XML String or a
   233     /**
   234      * <i>fieldName=fieldValue</i> format String. The String parameter is
   234      * <p>Descriptor constructor taking an XML String.</p>
   235      * parsed as XML if it begins with a '<' character.</p>
       
   236      *
   235      *
   237      * <p>The format of the XML string is not defined, but an
   236      * <p>The format of the XML string is not defined, but an
   238      * implementation must ensure that the string returned by
   237      * implementation must ensure that the string returned by
   239      * {@link #toXMLString() toXMLString()} on an existing
   238      * {@link #toXMLString() toXMLString()} on an existing
   240      * descriptor can be used to instantiate an equivalent
   239      * descriptor can be used to instantiate an equivalent
   243      * <p>In this implementation, all field values will be created
   242      * <p>In this implementation, all field values will be created
   244      * as Strings.  If the field values are not Strings, the
   243      * as Strings.  If the field values are not Strings, the
   245      * programmer will have to reset or convert these fields
   244      * programmer will have to reset or convert these fields
   246      * correctly.</p>
   245      * correctly.</p>
   247      *
   246      *
   248      * @param inStr An XML-format or a fieldName=fieldValue formatted string
   247      * @param inStr An XML-formatted string used to populate this
   249      * used to populate this Descriptor.  The XML format is not defined, but any
   248      * Descriptor.  The format is not defined, but any
   250      * implementation must ensure that the string returned by
   249      * implementation must ensure that the string returned by
   251      * method {@link #toXMLString toXMLString} on an existing
   250      * method {@link #toXMLString toXMLString} on an existing
   252      * descriptor can be used to instantiate an equivalent
   251      * descriptor can be used to instantiate an equivalent
   253      * descriptor when instantiated using this constructor.
   252      * descriptor when instantiated using this constructor.
   254      *
   253      *
   255      * @exception RuntimeOperationsException If the String inStr passed in
   254      * @exception RuntimeOperationsException If the String inStr
   256      * parameter is null or, when it is not an XML string, if the field name or
   255      * passed in parameter is null
   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.
       
   260      * @exception XMLParseException XML parsing problem while parsing
   256      * @exception XMLParseException XML parsing problem while parsing
   261      * the XML-format input String
   257      * the input String
   262      * @exception MBeanException Wraps a distributed communication Exception.
   258      * @exception MBeanException Wraps a distributed communication Exception.
   263      */
   259      */
   264     /* At some stage we should rewrite this code to be cleverer.  Using
   260     /* At some stage we should rewrite this code to be cleverer.  Using
   265        a StringTokenizer as we do means, first, that we accept a lot of
   261        a StringTokenizer as we do means, first, that we accept a lot of
   266        bogus strings without noticing they are bogus, and second, that we
   262        bogus strings without noticing they are bogus, and second, that we
   285             final String msg = "String in parameter is null";
   281             final String msg = "String in parameter is null";
   286             final RuntimeException iae = new IllegalArgumentException(msg);
   282             final RuntimeException iae = new IllegalArgumentException(msg);
   287             throw new RuntimeOperationsException(iae, msg);
   283             throw new RuntimeOperationsException(iae, msg);
   288         }
   284         }
   289 
   285 
   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 
       
   304         final String lowerInStr = inStr.toLowerCase();
   286         final String lowerInStr = inStr.toLowerCase();
   305         if (!lowerInStr.startsWith("<descriptor>")
   287         if (!lowerInStr.startsWith("<descriptor>")
   306             || !lowerInStr.endsWith("</descriptor>")) {
   288             || !lowerInStr.endsWith("</descriptor>")) {
   307             throw new XMLParseException("No <descriptor>, </descriptor> pair");
   289             throw new XMLParseException("No <descriptor>, </descriptor> pair");
   308         }
   290         }
   309 
   291 
   310 
   292         // parse xmlstring into structures
       
   293         init(null);
   311         // create dummy descriptor: should have same size
   294         // create dummy descriptor: should have same size
   312         // as number of fields in xmlstring
   295         // as number of fields in xmlstring
   313         // loop through structures and put them in descriptor
   296         // loop through structures and put them in descriptor
   314 
   297 
   315         StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f");
   298         StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f");
   469         if (( fields == null ) || ( fields.length == 0))
   452         if (( fields == null ) || ( fields.length == 0))
   470             return;
   453             return;
   471 
   454 
   472         init(null);
   455         init(null);
   473 
   456 
   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) {
       
   484         for (int i=0; i < fields.length; i++) {
   457         for (int i=0; i < fields.length; i++) {
   485             if ((fields[i] == null) || (fields[i].equals(""))) {
   458             if ((fields[i] == null) || (fields[i].equals(""))) {
   486                 continue;
   459                 continue;
   487             }
   460             }
   488             int eq_separator = fields[i].indexOf("=");
   461             int eq_separator = fields[i].indexOf("=");
   519                 final RuntimeException iae = new IllegalArgumentException(msg);
   492                 final RuntimeException iae = new IllegalArgumentException(msg);
   520                 throw new RuntimeOperationsException(iae, msg);
   493                 throw new RuntimeOperationsException(iae, msg);
   521             }
   494             }
   522 
   495 
   523             setField(fieldName,fieldValue);
   496             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");
   524         }
   502         }
   525     }
   503     }
   526 
   504 
   527     private void init(Map<String, ?> initMap) {
   505     private void init(Map<String, ?> initMap) {
   528         descriptorMap =
   506         descriptorMap =