1. /*
  2. * @(#)PropertyParser.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.spi.orb ;
  8. import java.util.List ;
  9. import java.util.LinkedList ;
  10. import java.util.Map ;
  11. import java.util.HashMap ;
  12. import java.util.Iterator ;
  13. import java.util.Properties ;
  14. import com.sun.corba.se.impl.orb.ParserAction ;
  15. import com.sun.corba.se.impl.orb.ParserActionFactory ;
  16. public class PropertyParser {
  17. private List actions ;
  18. public PropertyParser( )
  19. {
  20. actions = new LinkedList() ;
  21. }
  22. public PropertyParser add( String propName,
  23. Operation action, String fieldName )
  24. {
  25. actions.add( ParserActionFactory.makeNormalAction( propName,
  26. action, fieldName ) ) ;
  27. return this ;
  28. }
  29. public PropertyParser addPrefix( String propName,
  30. Operation action, String fieldName, Class componentType )
  31. {
  32. actions.add( ParserActionFactory.makePrefixAction( propName,
  33. action, fieldName, componentType ) ) ;
  34. return this ;
  35. }
  36. /** Return a map from field name to value.
  37. */
  38. public Map parse( Properties props )
  39. {
  40. Map map = new HashMap() ;
  41. Iterator iter = actions.iterator() ;
  42. while (iter.hasNext()) {
  43. ParserAction act = (ParserAction)(iter.next()) ;
  44. Object result = act.apply( props ) ;
  45. // A null result means that the property was not set for
  46. // this action, so do not override the default value in this case.
  47. if (result != null)
  48. map.put( act.getFieldName(), result ) ;
  49. }
  50. return map ;
  51. }
  52. public Iterator iterator()
  53. {
  54. return actions.iterator() ;
  55. }
  56. }