aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/terminal/web/CollectionThemeSource.java
blob: 1b94e1e836562c375e5d4f13597503d0f6274f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* *************************************************************************
 
 IT Mill Toolkit 

 Development of Browser User Interfaces Made Easy

 Copyright (C) 2000-2006 IT Mill Ltd
 
 *************************************************************************

 This product is distributed under commercial license that can be found
 from the product package on license.pdf. Use of this product might 
 require purchasing a commercial license from IT Mill Ltd. For guidelines 
 on usage, see licensing-guidelines.html

 *************************************************************************
 
 For more information, contact:
 
 IT Mill Ltd                           phone: +358 2 4802 7180
 Ruukinkatu 2-4                        fax:   +358 2 4802 7181
 20540, Turku                          email:  info@itmill.com
 Finland                               company www: www.itmill.com
 
 Primary source for information and releases: www.itmill.com

 ********************************************************************** */

package com.itmill.toolkit.terminal.web;

import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Theme source for consisting of collection of other theme sources. This class
 * is used to implement the retrieval of themes from multiple sources. Also this
 * class implements the inheritance of themes by first retrieving the relevant
 * parent theme information.
 * 
 * @author IT Mill Ltd.
 * @version
 * @VERSION@
 * @since 3.0
 */
public class CollectionThemeSource implements ThemeSource {

	private List sources = new LinkedList();

	/**
	 * Gets the name of the ThemeSource.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getName()
	 */
	public String getName() {
		return "THEMES";
	}

	/**
	 * Gets the XSL stream for the specified theme and web-browser type.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getXSLStreams(Theme,
	 *      WebBrowser)
	 */
	public Collection getXSLStreams(Theme theme, WebBrowser type)
			throws ThemeException {
		Collection xslFiles = new LinkedList();

		// Adds parent theme XSL
		xslFiles.addAll(this.getParentXSLStreams(theme, type));

		// Adds theme XSL, Handle subdirectories: return the first match
		for (Iterator i = this.sources.iterator(); i.hasNext();) {
			ThemeSource source = (ThemeSource) i.next();
			if (source.getThemes().contains(theme))
				xslFiles.addAll(source.getXSLStreams(theme, type));
		}

		return xslFiles;
	}
	
/**
 * 
 * @param theme
 * @param type
 * @return
 * @throws ThemeException If the resource is not found or there was
 * 			 some problem finding the resource.
 */
	private Collection getParentXSLStreams(Theme theme, WebBrowser type)
			throws ThemeException {
		Collection xslFiles = new LinkedList();
		String parentName = theme.getParent();
		if (parentName != null) {
			Theme parent = this.getThemeByName(parentName);
			if (parent != null) {
				xslFiles.addAll(this.getXSLStreams(parent, type));
			} else {
				throw new ThemeSource.ThemeException(
						"Parent theme not found for name: " + parentName);
			}
		}
		return xslFiles;
	}

	/**
	 * Gets the last modification time, used to reload theme on changes.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getModificationTime()
	 */
	public long getModificationTime() {
		long modTime = 0;
		for (Iterator i = this.sources.iterator(); i.hasNext();) {
			long t = ((ThemeSource) i.next()).getModificationTime();
			if (t > modTime)
				modTime = t;
		}
		return modTime;
	}

	/**
	 * Gets the input stream for the resource with the specified resource id.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getResource(String)
	 */
	public InputStream getResource(String resourceId) throws ThemeException {

		// Resolves theme name and resource name
		int delim = resourceId.indexOf("/");
		String subResourceId = "";
		String themeName = "";
		if (delim >= 0 && delim < resourceId.length() - 1) {
			subResourceId = resourceId.substring(delim + 1);
			themeName = resourceId.substring(0, delim);
		}

		// Gets the list of themes to look for the resource
		List themes = new LinkedList();
		while (themeName != null && themeName.length() > 0) {
			Theme t = this.getThemeByName(themeName);
			if (t != null) 
				themes.add(themeName);
			themeName = t.getParent();
		}

		// Iterate all themes in list
		for (Iterator ti = themes.iterator(); ti.hasNext();) {
			String name = (String) ti.next();
			String resource = name + "/" + subResourceId;
			// Search all sources
			for (Iterator i = this.sources.iterator(); i.hasNext();) {
				try {
					InputStream in = ((ThemeSource) i.next())
							.getResource(resource);
					if (in != null)
						return in;
				} catch (ThemeException e) {
					// Ignore and continue to next source
				}
			}
		}

		throw new ThemeException("Theme resource not found:" + subResourceId
				+ " in themes " + themes);
	}

	/**
	 * Gets the list of themes in the theme source.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getThemes()
	 */
	public Collection getThemes() {
		Collection themes = new LinkedList();
		for (Iterator i = this.sources.iterator(); i.hasNext();) {
			Collection c = ((ThemeSource) i.next()).getThemes();
			themes.addAll(c);
		}
		return themes;
	}

	/**
	 * Gets the theme instance by name.
	 * @param name the theme name.
	 * @see com.itmill.toolkit.terminal.web.ThemeSource#getThemeByName(String)
	 */
	public Theme getThemeByName(String name) {
		for (Iterator i = this.sources.iterator(); i.hasNext();) {
			Theme t = ((ThemeSource) i.next()).getThemeByName(name);
			if (t != null)
				return t;
		}
		return null;
	}

	/**
	 * Adds new theme source to this collection.
	 * 
	 * @param source
	 *            the Theme source to be added.
	 */
	public void add(ThemeSource source) {
		this.sources.add(source);
	}

}