A Java class for processing strings such as abc ${p1 }$ {p2} def

Source: Internet
Author: User

A Map is used to set the parameter values for Java classes that process strings such as abc $ {p1} $ {p2} def. [Java] package com. softparty. fw. common; import java. util. hashMap; import java. util. map;/*** analyze the parameter class from the string */public class ParametersUtils {// execution status ID/NORMAL, NORMAL status // BEGIN, after the $ character // START, after the $ {character, private static final int NORMAL = 0, BEGIN = 1, START = 2; // Save the result www.2cto.com private String result = "";/*** constructor * @ param value * @ param request */public ParametersUtils (String value, ValueCallbackHander hander) {this. parse (value, hander );} /*** resolution String * @ param value a String with a $ {name} placeholder * @ param hander gets the handler object of the placeholder parameter */private void parse (String value, ValueCallbackHander hander) {// string buffer object StringBuilder result = new StringBuilder (value. length (); // Save the string buffer object StringBuilder param = new StringBuilder () for each parameter name; // Save the status value int mark = NORMAL; // traverse the string for (int I = 0; I <value. length (); I ++) {// obtain the specified character char c = value of the string. charAt (I); // switch (c) {case '$' Based on the character value: // If the status is NORMAL, convert to BEGIN status if (mark = NORMAL) {mark = BEGIN;} else {// if two $ characters are consecutively present, ignore the previous $ character, maintain the BEGIN status if (mark = BEGIN) {result. append ('$');} else {// if it is another State (START) result. append (c) ;}} break; case '{': // For the in status, convert the status to the START status if (mark = BEGIN) {mark = START ;} else {// for other statuses, convert to NORMAL mark = NORMAL; // Add the result string result. append (c);} break; case '}': if (mark = START) {Object p = hander. getValue (param. toString (); if (p = null) {p = "" ;}// add result string result. append (p. toString (); param. setLength (0);} else {result. append (c) ;}// for the START state, convert to the NORMAL state, and obtain the parameter value mark = NORMAL; break; default: // for other characters if (mark = START) {// param whose processing status is START. append (c) ;}else {mark = NORMAL;} if (mark = NORMAL) {// result whose processing status is NORMAL. append (c) ;}} this. result = result. toString ();}/*** get processing result */public String getResult () {return result ;} /*** test */public static void main (String [] args) {final Map <String, Object> map = new HashMap <> (); map. put ("name", "OK"); map. put ("test", 1234); map. put ("OK", true); ParametersUtils utils = new ParametersUtils ("hello $ {name }$ {test}, {OK} {$ {OK }}", new ValueCallbackHander () {@ Override public Object getValue (String key) {return map. get (key) ;}}); System. out. println (utils. getResult () ;}} the ValueCallbackHandler interface used is as follows: [java] package com. softparty. fw. common; /*** Handler interface used to obtain Value based on the Key */public interface ValueCallbackHander {/*** get Value * @ param key the specified Key * @ return Value */Object getValue (String key );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.