comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 1747:d2a17e990c70

Improved the Themes: we now support special themes for facets which need to match a given pattern string. flys-artifacts/trunk@3047 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 20 Oct 2011 13:45:45 +0000
parents 88a669785863
children 6ed439ff61bf
comparison
equal deleted inserted replaced
1746:94faf08d143c 1747:d2a17e990c70
1 package de.intevation.flys.themes; 1 package de.intevation.flys.themes;
2 2
3 import java.util.List;
3 import java.util.Map; 4 import java.util.Map;
4 5
5 import javax.xml.xpath.XPathConstants; 6 import javax.xml.xpath.XPathConstants;
6 7
7 import org.apache.log4j.Logger; 8 import org.apache.log4j.Logger;
59 * @param name The name of the mapping. 60 * @param name The name of the mapping.
60 * 61 *
61 * @return a theme. 62 * @return a theme.
62 */ 63 */
63 public static Theme getTheme(FLYSContext c, String name) { 64 public static Theme getTheme(FLYSContext c, String name) {
65 return getTheme(c, name, null);
66 }
67
68
69 public static Theme getTheme(FLYSContext c, String name, String pattern) {
70 logger.debug("Search theme for: " + name + " - " + pattern);
64 if (c == null || name == null) { 71 if (c == null || name == null) {
65 logger.warn("Cannot search for theme."); 72 logger.warn("Cannot search for theme.");
66 return null; 73 return null;
67 } 74 }
68 75
69 Map<String, String> map = (Map<String, String>) 76 Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>)
70 c.get(FLYSContext.THEME_MAPPING); 77 c.get(FLYSContext.THEME_MAPPING);
71 78
72 Map<String, Theme> t = (Map<String, Theme>) 79 Map<String, Theme> t = (Map<String, Theme>)
73 c.get(FLYSContext.THEMES); 80 c.get(FLYSContext.THEMES);
74 81
75 if (map == null || map.size() == 0 || t == null || t.size() == 0) { 82 if (map == null || map.size() == 0 || t == null || t.size() == 0) {
76 logger.warn("No mappings or themes found. Cannot retrieve theme!"); 83 logger.warn("No mappings or themes found. Cannot retrieve theme!");
77 return null; 84 return null;
78 } 85 }
79 86
80 String themeName = map.get(name); 87 List<ThemeMapping> mapping = map.get(name);
81 88
82 if (themeName == null) { 89 if (mapping == null) {
83 logger.warn("No theme found for mapping: " + name); 90 logger.warn("No theme fount for mapping: " + name);
84 } 91 return null;
85 92 }
86 return t.get(themeName); 93
94 int patternLen = pattern != null ? pattern.length() : 0;
95
96 Theme defaultTheme = null;
97
98 for (ThemeMapping tm: mapping) {
99 if (name.equals(tm.getFrom())) {
100 String tmPattern = tm.getPatternStr();
101
102 int tmLen = tmPattern != null ? tmPattern.length() : 0;
103
104 if (tmLen == 0 && patternLen == 0) {
105 // we want and get the default theme
106 return t.get(tm.getTo());
107 }
108 else if (tmLen == 0 && patternLen > 0) {
109 // found a defaultTheme, lets save it
110 defaultTheme = t.get(tm.getTo());
111 }
112 else if (tm.applyPattern(pattern)) {
113 // found the special theme we have searched for
114 return t.get(tm.getTo());
115 }
116 }
117 }
118
119 String msg =
120 "No theme found for '" + name +
121 "' with pattern '" + pattern + "'.";
122
123 if (defaultTheme != null) {
124 logger.info(msg + " Use the default instead.");
125 return defaultTheme;
126 }
127
128 logger.warn(msg);
129
130 return null;
87 } 131 }
88 132
89 133
90 protected static String getName(Node config) { 134 protected static String getName(Node config) {
91 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); 135 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING);

http://dive4elements.wald.intevation.org