net.roboconf.core.utils.Utils.expandTemplate()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(120)

本文整理了Java中net.roboconf.core.utils.Utils.expandTemplate()方法的一些代码示例,展示了Utils.expandTemplate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.expandTemplate()方法的具体详情如下:
包路径:net.roboconf.core.utils.Utils
类名称:Utils
方法名:expandTemplate

Utils.expandTemplate介绍

[英]Expands a template, replacing each {{ param }} by the corresponding value.

Eg. "My name is {{ name }}" will result in "My name is Bond", provided that "params" contains "name=Bond".
[中]展开模板,用相应的值替换每个{param}}。
例如,“我的名字是{name}}”将导致“我的名字是邦德”,前提是“params”包含“name=Bond”。

代码示例

代码示例来源:origin: net.roboconf/roboconf-dm

/**
 * Expands the properties.
 * <p>
 * Property values that contain {{ ip }} will be updated.
 * {{ ip }} will be replaced by the IP address of the root instance
 * (which may - or not) be the same than the scoped instance).
 * </p>
 *
 * @param scopedInstance the scoped instance (not null)
 * @param targetProperties the target properties (not null)
 * @return a new map equivalent to the input but with expanded properties)
 */
public static Map<String,String> expandProperties( Instance scopedInstance, Map<String,String> targetProperties ) {
  Logger logger = Logger.getLogger( TargetHelpers.class.getName());
  String ipAddress = scopedInstance.data.get( Instance.IP_ADDRESS );
  if( ipAddress == null )
    ipAddress = InstanceHelpers.findRootInstance( scopedInstance ).data.get( Instance.IP_ADDRESS );
  if( ipAddress == null )
    ipAddress = "";
  Properties params = new Properties();
  params.setProperty( Constants.SPECIFIC_VARIABLE_IP, ipAddress );
  Map<String,String> newTargetProperties = new HashMap<>( targetProperties.size());
  for( Map.Entry<String,String> entry : targetProperties.entrySet()) {
    String p = Utils.expandTemplate( entry.getValue(), params );
    newTargetProperties.put( entry.getKey(), p );
    if( ! p.equals( entry.getValue()))
      logger.fine( "Target property '" + entry.getKey() + "' was expanded to " + p );
  }
  return newTargetProperties;
}

代码示例来源:origin: roboconf/roboconf-platform

@Test
public void testExpandString() {
  // Null properties
  Assert.assertEquals( "toto", Utils.expandTemplate( "toto", null ));
  // No property
  Properties params = new Properties();
  Assert.assertEquals( "toto", Utils.expandTemplate( "toto", params ));
  // With properties
  params.setProperty("firstname", "James");
  params.setProperty("lastname", "Bond");
  String tmpl = "My name is {{lastname}}, {{ firstname }} {{ lastname }}!";
  Assert.assertEquals(
      "My name is Bond, James Bond!",
      Utils.expandTemplate(tmpl, params));
  tmpl = "This is an {{ unknown }} parameter";
  Assert.assertEquals( tmpl, Utils.expandTemplate(tmpl, params));
}

相关文章