View Javadoc

1   /*
2    * Project Bimbo.
3    * Copyright 2008 Frank Cornelis.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Bimbo Style Advice Manager component.
35   * 
36   * @author fcorneli
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  	 * Sets the welcome page class. This welcome page determines the global
49  	 * style advice.
50  	 * 
51  	 * @param welcomePageClass
52  	 */
53  	public static void setWelcomePageClass(Class<?> welcomePageClass) {
54  		globalStyleAdvice = loadStyleAdvice(welcomePageClass);
55  	}
56  
57  	/**
58  	 * Main constructor.
59  	 * 
60  	 * @param pageClass
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 }