You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollectionThemeSource.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.terminal.web;
  19. import java.io.InputStream;
  20. import java.util.Collection;
  21. import java.util.Iterator;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. /**
  25. * Theme source for consisting of collection of other theme sources. This class
  26. * is used to implement the retrieval of themes from multiple sources. Also this
  27. * class implements the inheritance of themes by first retrieving the relevant
  28. * parent theme information.
  29. *
  30. * @author IT Mill Ltd.
  31. * @version
  32. * @VERSION@
  33. * @since 3.0
  34. */
  35. public class CollectionThemeSource implements ThemeSource {
  36. private List sources = new LinkedList();
  37. /**
  38. * @see com.itmill.toolkit.terminal.web.ThemeSource#getName()
  39. */
  40. public String getName() {
  41. return "THEMES";
  42. }
  43. /**
  44. * @see com.itmill.toolkit.terminal.web.ThemeSource#getXSLStreams(Theme,
  45. * WebBrowser)
  46. */
  47. public Collection getXSLStreams(Theme theme, WebBrowser type)
  48. throws ThemeException {
  49. Collection xslFiles = new LinkedList();
  50. // Add parent theme XSL
  51. xslFiles.addAll(this.getParentXSLStreams(theme, type));
  52. // Add theme XSL, Handle subdirectories: return the first match
  53. for (Iterator i = this.sources.iterator(); i.hasNext();) {
  54. ThemeSource source = (ThemeSource) i.next();
  55. if (source.getThemes().contains(theme))
  56. xslFiles.addAll(source.getXSLStreams(theme, type));
  57. }
  58. return xslFiles;
  59. }
  60. private Collection getParentXSLStreams(Theme theme, WebBrowser type)
  61. throws ThemeException {
  62. Collection xslFiles = new LinkedList();
  63. String parentName = theme.getParent();
  64. if (parentName != null) {
  65. Theme parent = this.getThemeByName(parentName);
  66. if (parent != null) {
  67. xslFiles.addAll(this.getXSLStreams(parent, type));
  68. } else {
  69. throw new ThemeSource.ThemeException(
  70. "Parent theme not found for name: " + parentName);
  71. }
  72. }
  73. return xslFiles;
  74. }
  75. /**
  76. * @see com.itmill.toolkit.terminal.web.ThemeSource#getModificationTime()
  77. */
  78. public long getModificationTime() {
  79. long modTime = 0;
  80. for (Iterator i = this.sources.iterator(); i.hasNext();) {
  81. long t = ((ThemeSource) i.next()).getModificationTime();
  82. if (t > modTime)
  83. modTime = t;
  84. }
  85. return modTime;
  86. }
  87. /**
  88. * @see com.itmill.toolkit.terminal.web.ThemeSource#getResource(String)
  89. */
  90. public InputStream getResource(String resourceId) throws ThemeException {
  91. // Resolve theme name and resource name
  92. int delim = resourceId.indexOf("/");
  93. String subResourceId = "";
  94. String themeName = "";
  95. if (delim >= 0 && delim < resourceId.length() - 1) {
  96. subResourceId = resourceId.substring(delim + 1);
  97. themeName = resourceId.substring(0, delim);
  98. }
  99. // Get list of themes to look for the resource
  100. List themes = new LinkedList();
  101. while (themeName != null && themeName.length() > 0) {
  102. Theme t = this.getThemeByName(themeName);
  103. if (t != null)
  104. themes.add(themeName);
  105. themeName = t.getParent();
  106. }
  107. // Iterate all themes in list
  108. for (Iterator ti = themes.iterator(); ti.hasNext();) {
  109. String name = (String) ti.next();
  110. String resource = name + "/" + subResourceId;
  111. // Search all sources
  112. for (Iterator i = this.sources.iterator(); i.hasNext();) {
  113. try {
  114. InputStream in = ((ThemeSource) i.next())
  115. .getResource(resource);
  116. if (in != null)
  117. return in;
  118. } catch (ThemeException e) {
  119. // Ignore and continue to next source
  120. }
  121. }
  122. }
  123. throw new ThemeException("Theme resource not found:" + subResourceId
  124. + " in themes " + themes);
  125. }
  126. /**
  127. * @see com.itmill.toolkit.terminal.web.ThemeSource#getThemes()
  128. */
  129. public Collection getThemes() {
  130. Collection themes = new LinkedList();
  131. for (Iterator i = this.sources.iterator(); i.hasNext();) {
  132. Collection c = ((ThemeSource) i.next()).getThemes();
  133. themes.addAll(c);
  134. }
  135. return themes;
  136. }
  137. /**
  138. * @see com.itmill.toolkit.terminal.web.ThemeSource#getThemeByName(String)
  139. */
  140. public Theme getThemeByName(String name) {
  141. for (Iterator i = this.sources.iterator(); i.hasNext();) {
  142. Theme t = ((ThemeSource) i.next()).getThemeByName(name);
  143. if (t != null)
  144. return t;
  145. }
  146. return null;
  147. }
  148. /**
  149. * Add new theme source to this collection.
  150. *
  151. * @param source
  152. * Theme source to be added.
  153. */
  154. public void add(ThemeSource source) {
  155. this.sources.add(source);
  156. }
  157. }