ReVo Blog

Template class

« Older   Newer »
  Share  
.ReVo.
view post Posted on 27/8/2014, 14:19 by: .ReVo.




Passato a format la stringa da formttare e in variables le variabili con cui bisogna sostituire le corrispondenze.

CODICE
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* La classe ti permette di formattare una stringa X
* e di sostituire dei blocchi con valori di variabili.
*
* @author Marco Acierno
*/
public class Template {
   private final Pattern pattern;

   public Template(String start, String end) {
       pattern = Pattern.compile(Pattern.quote(start) + "([^}]+)" + Pattern.quote(end));
   }

   public Template() {
       this("{", "}");
   }

   public String format(String text, Map<String, String> variables) {
       Matcher matcher = pattern.matcher(text);
       /* appendReplacement e appendTail accettano solo StringBuffer */
       StringBuffer buffer = new StringBuffer();

       while (matcher.find()) {
           String key = matcher.group(1);
           String value = variables.get(key);

           if (value == null) {
               matcher.appendReplacement(buffer, "{" + key + "}");
               continue;
           }

           matcher.appendReplacement(buffer, value);
       }

       matcher.appendTail(buffer);
       return buffer.toString();
   }
}


Alcuni test-cases per capire come funziona:

CODICE
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class TemplateTest {

   @Test
   public void testFormat() throws Exception {
       Template template = new Template();

       String text = "Hello {who}, {saluto}";
       String result = "Hello ReVo_, ok?";

       Map<String, String> vars = new HashMap<>();
       vars.put("who", "ReVo_");
       vars.put("saluto", "ok?");

       assertEquals(result, template.format(text, vars));
   }

   @Test
   public void testWithoutAnyArgument() throws Exception {
       Template template = new Template();

       String text = "Hello world!";
       String result = "Hello world!";

       Map<String, String> vars = new HashMap<>();
       vars.put("who", "ReVo_");
       vars.put("saluto", "ok?");

       assertEquals(result, template.format(text, vars));
   }

   @Test
   public void testWithSomeArgumentsWithoutBind() throws Exception {
       Template template = new Template();

       String text = "Hello {world}!";
       String result = "Hello {world}!";

       Map<String, String> vars = new HashMap<>();
       vars.put("who", "ReVo_");
       vars.put("saluto", "ok?");

       assertEquals(result, template.format(text, vars));
   }

   @Test
   public void testWithADifferentStartEnd() throws Exception {
       Template template = new Template("|", "|");

       String text = "Hello |world|!";
       String result = "Hello ReVo_!";

       Map<String, String> vars = new HashMap<>();
       vars.put("world", "ReVo_");

       assertEquals(result, template.format(text, vars));
   }

   @Test
   public void testWithADifferentStartEnd2() throws Exception {
       Template template = new Template("|", "-");

       String text = "Hello |world-!";
       String result = "Hello ReVo_!";

       Map<String, String> vars = new HashMap<>();
       vars.put("world", "ReVo_");

       assertEquals(result, template.format(text, vars));
   }
}


Ho creato questa classe per il progetto JBucket-API per specificare l'url dove eseguire la richiesta e le parti variabili:

CODICE
@Loader(url = "https://bitbucket.org/api/2.0/{type}/{username}/followers?page={page}", type = LoaderType.Followers)
 
Top
0 replies since 27/8/2014, 14:19   4 views
  Share