Saturday, July 27, 2013

SoyCssRenamingMap Implementation

If you want render soy templates on server side, with closure stylesheet compiler(GSS compiler) renaming map, you will need an implementation of SoyCssRenamingMap  interface.
The renaming map generated by GSS  compiler split keys with "-".  So, if  you just load the renaming maps generated to Map the soy will ignore the keys with "-"

The code below support keys with "-". 



1:  import java.util.Map;  
2:  import org.slf4j.Logger;  
3:  import org.slf4j.LoggerFactory;  
4:  import com.google.template.soy.shared.SoyCssRenamingMap;  
5:  public class SoyCssRenamingMapImpl implements SoyCssRenamingMap {  
6:       static boolean isMultipleCommandsSuppported = false;  
7:       final static Logger logger = LoggerFactory  
8:                 .getLogger(SoyCssRenamingMapImpl.class);  
9:       private Map<String, String> map;  
10:       public SoyCssRenamingMapImpl(Map<String, String> map) {  
11:            this.map = map;  
12:       }  
13:       private String parse(String string, String tokenRenaming, String regexKey) {  
14:            String ret = null;  
15:            String partialRenaming = null;  
16:            String[] keyArray = string.split(regexKey);  
17:            String renamingStr = partialRenaming = this.parseTopDown(keyArray[0]);  
18:            if (renamingStr != null) {  
19:                 int i = 1;  
20:                 while ((i < keyArray.length)  
21:                           && ((partialRenaming = this.parseTopDown(keyArray[i])) != null)) {  
22:                      renamingStr = renamingStr.concat(tokenRenaming).concat(  
23:                                partialRenaming);  
24:                      i++;  
25:                 }  
26:                 if (partialRenaming != null) {  
27:                      map.put(string, renamingStr);  
28:                      ret = renamingStr;  
29:                 }  
30:            }  
31:            return ret;  
32:       }  
33:       private String parseTopDown(String string) {  
34:            String ret = null;  
35:            if (string.contains(" ")) {  
36:                 if (isMultipleCommandsSuppported) {  
37:                      ret = this.parse(string, " ", "\\s+");  
38:                 } else {  
39:                      ret = null;  
40:                 }  
41:            } else if (string.contains("-")) {  
42:                 ret = this.parse(string, "-", "-");  
43:            } else {  
44:                 ret = map.get(string);  
45:                 if (ret==null){  
46:                      ret=string;  
47:                 }  
48:            }  
49:            return ret;  
50:       }  
51:       @Override  
52:       public String get(String key) {  
53:            // TODO Auto-generated method stub  
54:            String ret = map.get(key);  
55:            if (ret == null) {  
56:                 logger.debug("get miss to key:" + key);  
57:                 ret = parseTopDown(key);  
58:                 logger.debug("css renaming: key=" + key + " | value: " + ret);  
59:            }  
60:            return ret;  
61:       }  
62:  }  

The property "isMultipleCommandsSuppported" is experimental and allow multiples command in just one call.
Ex.: closure template css command: {css one-class other-class}



That's all folks!






No comments:

Post a Comment