Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/obinary/cms/beans/TemplateInfo.java


1   /**
2    *
3    * Magnolia and its source-code is licensed under the LGPL.
4    * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5    * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6    * you are required to provide proper attribution to obinary.
7    * If you reproduce or distribute the document without making any substantive modifications to its content,
8    * please use the following attribution line:
9    *
10   * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11   *
12   * */
13  
14  
15  
16  
17  
18  package com.obinary.cms.beans;
19  
20  
21  import com.obinary.cms.core.*;
22  
23  import javax.jcr.ElementNotFoundException;
24  import javax.jcr.RepositoryException;
25  import java.util.Iterator;
26  import java.util.Hashtable;
27  import java.util.ArrayList;
28  
29  
30  /**
31   * User: sameercharles
32   * Date: Apr 28, 2003
33   * Time: 11:20:59 AM
34   * @author Sameer Charles
35   * @version 1.0
36   */
37  
38  
39  public class TemplateInfo {
40  
41      private static final String START_PAGE = "/templateInfo";
42  
43      public static final int CUSTOM_TEMPLATES = 1;
44      public static final int ADMIN_TEMPLATES = 2;
45  
46      private static Iterator templates;
47      private static Iterator adminTemplates;
48      private static ArrayList visibleTemplates;
49      private static ArrayList visibleAdminTemplates;
50      private static Hashtable cachedContent;
51  
52      /**
53       * Mandataory
54       */
55      private String name;
56      /**
57       * Mandataory
58       */
59      private String path;
60  
61      private Hashtable alternativePaths;
62  
63  
64      /**
65       * Mandataory
66       */
67      private String type;
68      /**
69       * Mandataory
70       */
71      private boolean visible;
72  
73  
74      /**
75       * Optional fields
76       */
77      private String description;
78      private String image;
79      private String title;
80  
81  
82  
83  
84      /**
85       * constructor
86       *
87       */
88      public TemplateInfo() {
89      }
90  
91  
92  
93      /**
94       * constructor
95       *
96       * @throws ElementNotFoundException
97       * @throws RepositoryException
98       */
99      public TemplateInfo(boolean initialize) throws ElementNotFoundException, RepositoryException {
100         this.image = "";
101         this.title = "";
102         this.description = "";
103         TemplateInfo.init();
104     }
105 
106 
107 
108     /**
109      * <p>load all temple definitions available as a collection of Content objects</p>
110      *
111      * @throws ElementNotFoundException
112      * @throws RepositoryException
113      */
114     public static void init() throws ElementNotFoundException, RepositoryException {
115         HierarchyManager hm = new HierarchyManager();
116         hm.setStartPage(ConfigLoader.configRoot);
117         Content startPage = hm.getPage(START_PAGE);
118         TemplateInfo.templates = startPage.getContainerList("Templates").getChildren().iterator();
119         TemplateInfo.adminTemplates = startPage.getContainerList("AdminTemplates").getChildren().iterator();
120         TemplateInfo.cacheContent();
121     }
122 
123 
124 
125     /**
126      * <p>get templates collection</p>
127      *
128      * @param type , type could be TemplateInfo.CUSTOM_TEMPLATES or TemplateInfo.ADMIN_TEMPLATES
129      * @return Collection list containing templates as Content objects
130      */
131     public static Iterator getAvailableTemplates(int type) {
132         if (type == TemplateInfo.CUSTOM_TEMPLATES)
133             return TemplateInfo.visibleTemplates.iterator();
134         else
135             return TemplateInfo.visibleAdminTemplates.iterator();
136     }
137 
138 
139 
140     /**
141      * <p>load content of this template info page in a hash table
142      * caching at the system load, this will save lot of time on every request
143      * while matching template info
144      * </p>
145      *
146      */
147     private static void cacheContent() {
148         TemplateInfo.cachedContent = new Hashtable();
149         TemplateInfo.visibleTemplates = new ArrayList();
150         addTemplatesToCache(TemplateInfo.templates,TemplateInfo.visibleTemplates);
151         TemplateInfo.templates = null;
152         TemplateInfo.visibleAdminTemplates = new ArrayList();
153         addTemplatesToCache(TemplateInfo.adminTemplates,TemplateInfo.visibleAdminTemplates);
154         TemplateInfo.adminTemplates = null;
155     }
156 
157 
158 
159     /**
160      * <p>adds templates definition to TemplatesInfo cache</p>
161      *
162      * @param templates iterator as read from the repository
163      * @param visibleTemplates ArrayList in with all visible templates will be added
164      */
165     private static void addTemplatesToCache(Iterator templates, ArrayList visibleTemplates) {
166         while (templates.hasNext()) {
167             Container c = (Container) templates.next();
168             try {
169                 TemplateInfo ti = new TemplateInfo();
170                 ti.name = c.getAtom("name").getValue().getString();
171                 ti.path = c.getAtom("path").getValue().getString();
172                 TemplateInfo.addAlternativePaths(c,ti);
173                 ti.type = c.getAtom("type").getValue().getString();
174                 ti.visible = c.getAtom("visible").getValue().getBoolean();
175                 try {
176                     ti.title = c.getAtom("title").getString();
177                 } catch (Exception e) {e.printStackTrace();}
178                 try {
179                     ti.description = c.getAtom("description").getString();
180                 } catch (Exception e) {e.printStackTrace();}
181                 try {
182                     ti.image = c.getAtom("image").getString();
183                 } catch (Exception e) {e.printStackTrace();}
184                 TemplateInfo.cachedContent.put(ti.name,ti);
185                 if (ti.visible)
186                     visibleTemplates.add(ti);
187             } catch (RepositoryException re) { re.printStackTrace(); }
188         }
189     }
190 
191 
192 
193     /**
194      * <p>add alternative extention paths to templates cache</p>
195      *
196      * @param container
197      * @param ti TemplateInfo
198      */
199     private static void addAlternativePaths(Container container, TemplateInfo ti) {
200         try {
201             ContainerList cl = container.getContainerList("SubTemplates");
202             Iterator it = cl.getChildren().iterator();
203             ti.alternativePaths = new Hashtable();
204             while (it.hasNext()) {
205                 Container c = (Container)it.next();
206                 ti.alternativePaths.put(c.getAtom("extension").getString(),c.getAtom("path").getString());
207             }
208         } catch (RepositoryException re) {}
209     }
210 
211 
212 
213     /**
214      * <p>returns the cached content of the requested template<br>
215      * TemplateInfo properties :<br>
216      * 1. title - title describing template<br>
217      * 2. type - jsp / servlet<br>
218      * 3. path - jsp / servlet path<br>
219      * 4. description - description of a template
220      * </p>
221      *
222      * @return TemplateInfo
223      */
224     public static TemplateInfo getInfo(String key) throws Exception {
225         return (TemplateInfo) TemplateInfo.cachedContent.get(key);
226     }
227 
228 
229 
230     /**
231      *
232      */
233     public String getName() {
234         return this.name;
235     }
236 
237 
238 
239     /**
240      *
241      */
242     public String getTitle() {
243         return this.title;
244     }
245 
246 
247 
248     /**
249      *
250      */
251     public String getDescription() {
252         return this.description;
253     }
254 
255 
256 
257     /**
258      * @return default template path
259      */
260     public String getPath() {
261         return this.path;
262     }
263 
264 
265 
266     /**
267      * @param extension
268      * @return template path for the specified extension
269      */
270     public String getPath(String extension) {
271         try {
272             String path = (String)this.alternativePaths.get(extension);
273             if (path == null)
274                 return this.getPath();
275             return path;
276         } catch (Exception e) {
277             return this.getPath();
278         }
279     }
280 
281 
282 
283     /**
284      *
285      */
286     public String getType() {
287         return this.type;
288     }
289 
290 
291 
292     /**
293      *
294      */
295     public String getImage() {
296         return this.image;
297     }
298 
299 
300 
301     /**
302      *
303      */
304     public boolean isVisible() {
305         return this.visible;
306     }
307 
308 
309 
310 }