KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portal > skin > impl > SkinConfigServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.portal.skin.impl;
6
7 import java.util.*;
8 import java.io.InputStream JavaDoc;
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 /**
19  * Created by The eXo Platform SARL .
20  * Author : Tuan Nguyen
21  * tuan08@users.sourceforge.net
22  * Date: Jun 14, 2003
23  * Time: 1:12:22 PM
24  */

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 JavaDoc is) throws Exception JavaDoc {
43     String JavaDoc 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 JavaDoc fileUrl) throws Exception JavaDoc {
74
75   }
76
77   public Collection getPortalDecorators() {
78     return portalDecoratorMap_.values();
79   }
80
81   public Decorator getPortalDecorator(String JavaDoc rendererType) {
82     return getDecorator(portalDecoratorMap_, rendererType);
83   }
84
85   public Style getPortalDecoratorStyle(String JavaDoc rendererType, String JavaDoc 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 JavaDoc rendererType) {
95     return getDecorator(pageDecoratorMap_, rendererType);
96   }
97
98   public Style getPageDecoratorStyle(String JavaDoc rendererType, String JavaDoc 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 JavaDoc rendererType) {
108     return getDecorator(containerDecoratorMap_, rendererType);
109   }
110
111   public Style getContainerDecoratorStyle(String JavaDoc rendererType, String JavaDoc 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 JavaDoc rendererType) {
121     return getDecorator(portletDecoratorMap_, rendererType);
122   }
123
124   public Style getPortletDecoratorStyle(String JavaDoc rendererType, String JavaDoc styleName) {
125     Decorator decorator = (Decorator) portletDecoratorMap_.get(rendererType);
126     return searchStyle(decorator, styleName);
127   }
128
129   public List getPortletStyles(String JavaDoc 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 JavaDoc portletName, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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