1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.sf.bimbo.impl;
19
20 import java.io.InputStream;
21
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.JAXBElement;
24 import javax.xml.bind.JAXBException;
25 import javax.xml.bind.Unmarshaller;
26
27 import net.sf.bimbo.style._1.ObjectFactory;
28 import net.sf.bimbo.style._1.StyleAdviceType;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34
35
36
37
38
39 public class StyleAdviceManager {
40
41 private static final Log LOG = LogFactory.getLog(StyleAdviceManager.class);
42
43 private final StyleAdviceType styleAdvice;
44
45 private static StyleAdviceType globalStyleAdvice;
46
47
48
49
50
51
52
53 public static void setWelcomePageClass(Class<?> welcomePageClass) {
54 globalStyleAdvice = loadStyleAdvice(welcomePageClass);
55 }
56
57
58
59
60
61
62 public StyleAdviceManager(Class<?> pageClass) {
63 this.styleAdvice = loadStyleAdvice(pageClass);
64 }
65
66 public String getApplicationTitleStyle() {
67 if (null != this.styleAdvice) {
68 String advice = this.styleAdvice.getApplicationTitleStyle();
69 if (null != advice) {
70 return advice.trim();
71 }
72 }
73 if (null != globalStyleAdvice) {
74 String advice = globalStyleAdvice.getApplicationTitleStyle();
75 if (null != advice) {
76 return advice.trim();
77 }
78 }
79 String advice = "text-align: center;";
80 return advice;
81 }
82
83 public String getPageTitleStyle() {
84 if (null != this.styleAdvice) {
85 String advice = this.styleAdvice.getPageTitleStyle();
86 if (null != advice) {
87 return advice.trim();
88 }
89 }
90 if (null != globalStyleAdvice) {
91 String advice = globalStyleAdvice.getPageTitleStyle();
92 if (null != advice) {
93 return advice.trim();
94 }
95 }
96 String advice = "text-align: left;";
97 return advice;
98 }
99
100 @SuppressWarnings("unchecked")
101 private static StyleAdviceType loadStyleAdvice(Class<?> pageClass) {
102 String styleResourceName = "/"
103 + pageClass.getName().replaceAll("\\.", "/") + ".xml";
104 Thread currentThread = Thread.currentThread();
105 ClassLoader classLoader = currentThread.getContextClassLoader();
106 InputStream styleInputStream = classLoader
107 .getResourceAsStream(styleResourceName);
108 if (null == styleInputStream) {
109 return null;
110 }
111 try {
112 JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
113 Unmarshaller unmarshaller = context.createUnmarshaller();
114 return ((JAXBElement<StyleAdviceType>) unmarshaller
115 .unmarshal(styleInputStream)).getValue();
116 } catch (JAXBException e) {
117 LOG.debug("Couldn't load style advice for class: "
118 + pageClass.getName());
119 LOG.debug("JAXB error: " + e.getMessage(), e);
120 return null;
121 }
122 }
123 }