View Javadoc

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