乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java如何替換word中${}中的內(nèi)容?

       三十的狼 2018-02-20

      處理Word可以使用poi,
      如何替換使用下面三個類就好了。

      public class GenericTokenParser {
      
          private final String       openToken;
          private final String       closeToken;
          private final TokenHandler handler;
      
          public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
              this.openToken = openToken;
              this.closeToken = closeToken;
              this.handler = handler;
          }
      
          // 主要的邏輯就是取出expression,委托TokenHandler來替換expression。
          public String parse(String text) {
              if (text == null || text.isEmpty()) {
                  return "";
              }
              // search open token
              int start = text.indexOf(openToken, 0);
              if (start == -1) {
                  return text;
              }
              char[] src = text.toCharArray();
              int offset = 0;
              final StringBuilder builder = new StringBuilder();
              StringBuilder expression = null;
              while (start > -1) {
                  if (start > 0 && src[start - 1] == '\\') {
                      // this open token is escaped. remove the backslash and continue.
                      builder.append(src, offset, start - offset - 1).append(openToken);
                      offset = start + openToken.length();
                  } else {
                      // found open token. let's search close token.
                      if (expression == null) {
                          expression = new StringBuilder();
                      } else {
                          expression.setLength(0);
                      }
                      builder.append(src, offset, start - offset);
                      offset = start + openToken.length();
                      int end = text.indexOf(closeToken, offset);
                      while (end > -1) {
                          if (end > offset && src[end - 1] == '\\') {
                              // this close token is escaped. remove the backslash and continue.
                              expression.append(src, offset, end - offset - 1).append(closeToken);
                              offset = end + closeToken.length();
                              end = text.indexOf(closeToken, offset);
                          } else {
                              expression.append(src, offset, end - offset);
                              offset = end + closeToken.length();
                              break;
                          }
                      }
                      if (end == -1) {
                          // close token was not found.
                          builder.append(src, start, src.length - start);
                          offset = src.length;
                      } else {
                          builder.append(handler.handleToken(expression.toString()));
                          offset = end + closeToken.length();
                      }
                  }
                  start = text.indexOf(openToken, offset);
              }
              if (offset < src.length) {
                  builder.append(src, offset, src.length - offset);
              }
              return builder.toString();
          }
      }
      
      public class VariableTokenHandler implements TokenHandler {
              private final Properties variables;
              private final boolean    enableDefaultValue;
              private final String     defaultValueSeparator;
      
              public VariableTokenHandler(Properties variables) {
                  this.variables = variables;
                  this.enableDefaultValue = false;
                  this.defaultValueSeparator = ":";
              }
      
              private String getPropertyValue(String key, String defaultValue) {
                  return (variables == null) ? defaultValue : variables.getProperty(key, defaultValue);
              }
      
              @Override
              public String handleToken(String content) {
                  if (variables != null) {
                      String key = content;
                      if (enableDefaultValue) {
                          final int separatorIndex = content.indexOf(defaultValueSeparator);
                          String defaultValue = null;
                          if (separatorIndex >= 0) {
                              key = content.substring(0, separatorIndex);
                              defaultValue = content.substring(separatorIndex + defaultValueSeparator.length());
                          }
                          if (defaultValue != null) { 
                              return variables.getProperty(key, defaultValue);
                          }
                      }
                      if (variables.containsKey(key)) {
                          return variables.getProperty(key);
                      }
                  }
                  return "${" + content + "}"; 
              }
          }
      
      public interface TokenHandler {
          String handleToken(String content);
      }

      使用方法:

      public class Main {
          public static void main(String[] args) {
              Properties properties = new Properties();
              properties.put("name", "javaer");
              VariableTokenHandler tokenHandler = new VariableTokenHandler(properties);
              GenericTokenParser parser = new GenericTokenParser("${", "}", tokenHandler);
              String parse = parser.parse("my name is ${name}");
              System.out.println(parse);
          }
      }

      運行結(jié)果:
      Screen Shot 2017-12-26 at 11.43.29

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多