1 5 package org.exoplatform.services.portal.skin.impl; 6 7 import java.util.*; 8 import java.io.InputStream ; 9 import org.exoplatform.commons.utils.IOUtil; 10 import org.exoplatform.services.portal.skin.SkinConfigService; 11 import org.exoplatform.services.portal.skin.converter.*; 12 import org.exoplatform.services.portal.skin.model.*; 13 14 import com.thoughtworks.xstream.XStream; 15 import com.thoughtworks.xstream.io.xml.XppDriver; 16 17 18 25 public class SkinConfigServiceImpl implements SkinConfigService { 26 static private XStream xstream_; 27 28 private Map portalDecoratorMap_; 29 private Map pageDecoratorMap_; 30 private Map containerDecoratorMap_; 31 private Map portletDecoratorMap_; 32 private Map portletStyleConfigMap_; 33 34 public SkinConfigServiceImpl() { 35 portalDecoratorMap_ = new HashMap(); 36 pageDecoratorMap_ = new HashMap(); 37 containerDecoratorMap_ = new HashMap(); 38 portletDecoratorMap_ = new HashMap(); 39 portletStyleConfigMap_ = new HashMap(); 40 } 41 42 public void addConfiguration(InputStream is) throws Exception { 43 String xml = IOUtil.getStreamContentAsString(is); 44 XStream xstream = getXStreamInstance(); 45 SkinConfig config = (SkinConfig) xstream.fromXML(xml); 46 47 addDecorator(portalDecoratorMap_, config.getPortalDecorators()); 48 addDecorator(pageDecoratorMap_, config.getPageDecorators()); 49 addDecorator(containerDecoratorMap_, config.getContainerDecorators()); 50 addDecorator(portletDecoratorMap_, config.getPortletDecorators()); 51 52 List portletStyleConfigs = config.getPortletStyleConfig(); 53 if (portletStyleConfigs == null) return; 54 for (int i = 0; i < portletStyleConfigs.size(); i++) { 55 PortletStyleConfig psconfig = (PortletStyleConfig) portletStyleConfigs.get(i); 56 addPortletStyle(portletStyleConfigMap_, psconfig); 57 } 58 } 59 60 private void addPortletStyle(Map portletStyleConfigMap_, PortletStyleConfig psconfig) { 61 if (portletStyleConfigMap_.containsKey(psconfig.getPortletName())) { 62 PortletStyleConfig config = (PortletStyleConfig) portletStyleConfigMap_.get(psconfig.getPortletName()); 63 List styles2add = psconfig.getStyles(); 64 for (Iterator iterator = styles2add.iterator(); iterator.hasNext();) { 65 Style style = (Style) iterator.next(); 66 config.addStyle(style); 67 } 68 } else { 69 portletStyleConfigMap_.put(psconfig.getPortletName(), psconfig); 70 } 71 } 72 73 public void addConfiguration(String fileUrl) throws Exception { 74 75 } 76 77 public Collection getPortalDecorators() { 78 return portalDecoratorMap_.values(); 79 } 80 81 public Decorator getPortalDecorator(String rendererType) { 82 return getDecorator(portalDecoratorMap_, rendererType); 83 } 84 85 public Style getPortalDecoratorStyle(String rendererType, String styleName) { 86 Decorator decorator = getPortalDecorator(rendererType); 87 return searchStyle(decorator, styleName); 88 } 89 90 public Collection getPageDecorators() { 91 return pageDecoratorMap_.values(); 92 } 93 94 public Decorator getPageDecorator(String rendererType) { 95 return getDecorator(pageDecoratorMap_, rendererType); 96 } 97 98 public Style getPageDecoratorStyle(String rendererType, String styleName) { 99 Decorator decorator = getPageDecorator(rendererType); 100 return searchStyle(decorator, styleName); 101 } 102 103 public Collection getContainerDecorators() { 104 return containerDecoratorMap_.values(); 105 } 106 107 public Decorator getContainerDecorator(String rendererType) { 108 return getDecorator(containerDecoratorMap_, rendererType); 109 } 110 111 public Style getContainerDecoratorStyle(String rendererType, String styleName) { 112 Decorator decorator = getContainerDecorator(rendererType); 113 return searchStyle(decorator, styleName); 114 } 115 116 public Collection getPortletDecorators() { 117 return portletDecoratorMap_.values(); 118 } 119 120 public Decorator getPortletDecorator(String rendererType) { 121 return getDecorator(portletDecoratorMap_, rendererType); 122 } 123 124 public Style getPortletDecoratorStyle(String rendererType, String styleName) { 125 Decorator decorator = (Decorator) portletDecoratorMap_.get(rendererType); 126 return searchStyle(decorator, styleName); 127 } 128 129 public List getPortletStyles(String portletName) { 130 PortletStyleConfig config = (PortletStyleConfig) portletStyleConfigMap_.get(portletName); 131 if (config == null) return null; 132 return config.getStyles(); 133 } 134 135 public Style getPortletStyle(String portletName, String styleName) { 136 PortletStyleConfig config = (PortletStyleConfig) portletStyleConfigMap_.get(portletName); 137 if (config == null) return null; 138 List list = config.getStyles(); 139 for (int i = 0; i < list.size(); i++) { 140 Style style = (Style) list.get(i); 141 if (styleName.equals(style.getName())) return style; 142 } 143 return null; 144 } 145 146 private Decorator getDecorator(Map map, String rendererType) { 147 if (rendererType == null || rendererType.length() == 0 || "default".equals(rendererType)) { 148 return (Decorator) map.values().iterator().next(); 149 } 150 return (Decorator) map.get(rendererType); 151 } 152 153 private Style searchStyle(Decorator decorator, String styleName) { 154 if (decorator == null) return null; 155 List list = decorator.getStyles(); 156 for (int i = 0; i < list.size(); i++) { 157 Style style = (Style) list.get(i); 158 if (styleName.equals(style.getName())) return style; 159 } 160 return null; 161 } 162 163 private void addDecorator(Map map, List decorators) { 164 if (decorators == null) return; 165 for (int i = 0; i < decorators.size(); i++) { 166 Decorator decorator = (Decorator) decorators.get(i); 167 String rendererType = decorator.getRendererType(); 168 if (map.containsKey(rendererType)) { 169 Decorator deco2update = (Decorator) map.get(rendererType); 170 List styles2add = decorator.getStyles(); 171 for (Iterator iterator = styles2add.iterator(); iterator.hasNext();) { 172 Style style = (Style) iterator.next(); 173 deco2update.addStyle(style); 174 } 175 } else { 176 map.put(rendererType, decorator); 177 } 178 } 179 } 180 181 public static XStream getXStreamInstance() { 182 if (xstream_ == null) { 183 xstream_ = new XStream(new XppDriver()); 184 xstream_.alias("skin-config", SkinConfig.class); 185 xstream_.alias("decorator", Decorator.class); 186 xstream_.alias("style", Decorator.class); 187 xstream_.alias("portlet-style-config", PortletStyleConfig.class); 188 xstream_.registerConverter(new SkinConfigConverter()); 189 xstream_.registerConverter(new DecoratorConverter()); 190 xstream_.registerConverter(new StyleConverter()); 191 xstream_.registerConverter(new PortletStyleConfigConverter()); 192 } 193 return xstream_; 194 } 195 } | Popular Tags |