001package arcommon.net; 002 003import java.util.Arrays; 004import java.util.LinkedHashMap; 005import java.util.Map; 006 007import arcommon.ArrayUtil; 008import arcommon.xml.XmlUtil; 009 010public class IdAndStyle { 011 public static final String[] VALID_COLORS = new String[] {"blue", "green"}; 012 public static final String[] VALID_TEXT_ALIGNS = new String[] {"center"}; 013 public static final String[] VALID_DISPLAYS = new String[] {"block"}; 014 public static final String[] VALID_MARGINS = new String[] {"auto"}; 015 public static final String[] VALID_VERTICAL_ALIGN = new String[] {"top"}; 016 017 private String id; 018 private String cssClass; 019 private LinkedHashMap<String, String> styleMap = new LinkedHashMap<String, String>(); 020 021 public IdAndStyle(String pid, String pcssClass, String... style) { 022 id = pid; 023 cssClass = pcssClass; 024 if (cssClass != null && cssClass.contains(".")) { 025 throw new RuntimeException("cssClass.contains(\".\") \"" + cssClass + "\""); 026 } 027 if (style.length % 2 == 1) { 028 throw new RuntimeException("Style must written in pars (style.length % 2 == 1) " + style.length); 029 } 030 for (int i = 0; i < style.length; i += 2) { 031 String key = style[i]; 032 String value = style[i + 1]; 033 addStyle(key, value); 034 } 035 HtmlWriter.validateId(id); 036 } 037 038 public void addClass(String pcssClass) { 039 if (pcssClass.contains(".")) { 040 throw new RuntimeException("cssClass.contains(\".\") \"" + pcssClass + "\""); 041 } 042 if (pcssClass.contains(" ")) { 043 throw new RuntimeException("cssClass.contains(\" \") \"" + pcssClass + "\""); 044 } 045 if (cssClass == null) { 046 cssClass = ""; 047 } else { 048 if (cssClass.length() > 0) { 049 cssClass += " "; 050 } 051 } 052 cssClass += pcssClass; 053 } 054 055 public void addStyle(String key, String value) { 056 if ("color".equals(key)) { 057 if (ArrayUtil.indexOf(VALID_COLORS, value) == -1) { 058 throw new RuntimeException("The color \"" + value + "\" is not valid. Must be one of " + Arrays.toString(VALID_COLORS) + "."); 059 } 060 } else if ("text-align".equals(key)) { 061 if (ArrayUtil.indexOf(VALID_TEXT_ALIGNS, value) == -1) { 062 throw new RuntimeException("The text align \"" + value + "\" is not valid. Must be one of " + Arrays.toString(VALID_TEXT_ALIGNS) + "."); 063 } 064 } else if ("display".equals(key)) { 065 if (ArrayUtil.indexOf(VALID_DISPLAYS, value) == -1) { 066 throw new RuntimeException("The display \"" + value + "\" is not valid. Must be one of " + Arrays.toString(VALID_DISPLAYS) + "."); 067 } 068 } else if ("margin".equals(key)) { 069 if (ArrayUtil.indexOf(VALID_MARGINS, value) == -1) { 070 throw new RuntimeException("The margin \"" + value + "\" is not valid. Must be one of " + Arrays.toString(VALID_MARGINS) + "."); 071 } 072 } else if ("vertical-align".equals(key)) { 073 if (ArrayUtil.indexOf(VALID_VERTICAL_ALIGN, value) == -1) { 074 throw new RuntimeException("The vertical align \"" + value + "\" is not valid. Must be one of " + Arrays.toString(VALID_VERTICAL_ALIGN) + "."); 075 } 076 } else { 077 throw new RuntimeException("Unknown key \"" + key + "\". Value: \"" + value + "\""); 078 } 079 if (value.contains(":")) { 080 throw new RuntimeException("value.contains(\":\". Value: \"" + value + "\""); 081 } 082 if (styleMap.containsKey(key)) { 083 throw new RuntimeException("Key \"" + key + "\" is already set in " + styleMap); 084 } 085 styleMap.put(key, value); 086 } 087 088 @Override 089 public String toString() { 090 String result = HtmlWriter.idToStringNoValidation(id); 091 if (cssClass != null && cssClass.length() > 0) { 092 result += " class=\"" + XmlUtil.encodeXmlString(cssClass) + "\""; 093 } 094 095 boolean found = false; 096 for (Map.Entry<String, String> entry : styleMap.entrySet()) { 097 String key = entry.getKey(); 098 String value = entry.getValue(); 099 if (!found) { 100 found = true; 101 result += " style=\""; 102 } else { 103 result += ";"; 104 } 105 result += key + ":" + XmlUtil.encodeXmlString(value); 106 } 107 if (found) { 108 result += "\""; 109 } 110 111 return result; 112 } 113 114 public String getId() { 115 return id; 116 } 117}