View Javadoc

1   /*
2    * $Id: Template.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components.template;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /***
24   * A template.
25   * <p/>
26   * A template is used as a model for rendering output.
27   * This object contains basic common template information
28   */
29  public class Template implements Cloneable {
30      String dir;
31      String theme;
32      String name;
33  
34      /***
35       * Constructor.
36       *
37       * @param dir  base folder where the template is stored.
38       * @param theme  the theme of the template
39       * @param name   the name of the template.
40       */
41      public Template(String dir, String theme, String name) {
42          this.dir = dir;
43          this.theme = theme;
44          this.name = name;
45      }
46  
47      public String getDir() {
48          return dir;
49      }
50  
51      public String getTheme() {
52          return theme;
53      }
54  
55      public String getName() {
56          return name;
57      }
58  
59      public List getPossibleTemplates(TemplateEngine engine) {
60          List list = new ArrayList(3);
61          Template template = this;
62          String parentTheme;
63          list.add(template);
64          while ((parentTheme = (String) engine.getThemeProps(template).get("parent")) != null) {
65              try {
66                  template = (Template) template.clone();
67                  template.theme = parentTheme;
68                  list.add(template);
69              } catch (CloneNotSupportedException e) {
70                  // do nothing
71              }
72          }
73  
74          return list;
75      }
76  
77      /***
78       * Constructs a string in the format <code>/dir/theme/name</code>.
79       * @return a string in the format <code>/dir/theme/name</code>.
80       */
81      public String toString() {
82          return "/" + dir + "/" + theme + "/" + name;
83      }
84  
85      protected Object clone() throws CloneNotSupportedException {
86          return super.clone();
87      }
88  }