KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > templates > JahiaTemplatesPackageHandler


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaTemplatePackageHandler
15
//
16
// NK 13.01.2001
17
//
18
//
19

20 package org.jahia.data.templates;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.jahia.exceptions.JahiaException;
26 import org.jahia.utils.JahiaConsole;
27 import org.jahia.utils.zip.JahiaArchiveFileHandler;
28
29 /**
30  * This class is responsible for loading data from a Template Jar File
31  * The Format of the XML Template Descriptor File is :
32  *
33  * <pre>
34  * Format of XML document is as follows:
35  * <?xml version="1.0" encoding="ISO-8859-1"?>
36  *
37  * <tpml>
38  * <parameter name="package-name">DigitalCortexDHTML</parameter>
39  * <parameter name="root-folder">DigitalCortexDHTML</parameter>
40  * <parameter name="classes-file">digicorp.jar</parameter>
41  * <parameter name="provider">www.jahia.org</parameter>
42  * <parameter name="thumbnail">thumbnail.gif</parameter>
43  * <template id="1" visible="1" homepage="1">
44  * <parameter name="name">DigitalCortexDHTML</parameter>
45  * <parameter name="filename">digicorp.jsp</parameter>
46  * <parameter name="display-name">Digital Cortex DHTML</parameter>
47  * </template>
48  * </tpml>
49  *
50  * </pre>
51  *
52  * -----------------------------------------------------------------
53  * IMPORTANT !!!!! Must call the method closeArchiveFile()
54  * to be able to delete the file later !!!
55  * -----------------------------------------------------------------
56  *
57  *
58  * @author Khue ng
59  * @version 1.0
60  *
61  */

62 public class JahiaTemplatesPackageHandler {
63
64     private static final String JavaDoc CLASS_NAME = JahiaTemplatesPackageHandler.class.getName();
65
66    /** The root folder **/
67    private String JavaDoc m_TemplateRootFolder;
68    /** The full path to the Template Jar File **/
69    private String JavaDoc m_FilePath;
70    /** The Jar File Handler of the Template Jar File **/
71    private JahiaArchiveFileHandler m_ArchFile;
72    /** The JahiaTemplatesPackage Object created with data from the templates.xml file **/
73    private JahiaTemplatesPackage m_Package;
74    /** the XML Document **/
75    private Templates_Xml m_XMLDocument;
76    /** The classes_file.jar provided within the templates package **/
77    private File JavaDoc m_ClassesFile;
78    /** The thumbnail image file **/
79    private File JavaDoc m_ThumbnailFile;
80
81    /** The Jar File Handler of the classes_file.jar provided within the templates package **/
82    private JahiaArchiveFileHandler m_JarClassesFile;
83
84     /** the entry to the templates.xml file **/
85     private static String JavaDoc TEMPLATES_XML_FILE = "templates.xml";
86
87
88    /**
89     * Constructor is initialized with the template File
90     *
91     */

92    public JahiaTemplatesPackageHandler (String JavaDoc filePath)
93    throws JahiaException {
94
95         m_FilePath = filePath;
96
97         File JavaDoc tmpFile = new File JavaDoc(filePath);
98         boolean isFile = true;
99
100         if ( tmpFile.isFile() && tmpFile.canWrite()){
101
102             try {
103
104                 m_ArchFile = new JahiaArchiveFileHandler( filePath );
105
106             } catch (IOException JavaDoc e ) {
107
108                 String JavaDoc errMsg = "Failed creating an Archive File Handler " ;
109                 JahiaConsole.println("JahiaTemplatesPackageHandler::Constructor", errMsg + "\n" + e.toString());
110                 throw new JahiaException ("JahiaTemplatesPackageHandler", errMsg ,
111                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, e);
112
113             }
114
115         } else {
116             isFile = false;
117         }
118
119
120         try {
121             buildTemplatesPackage(isFile);
122         } catch ( JahiaException je ) {
123
124             if ( m_ArchFile != null ){
125                 m_ArchFile.closeArchiveFile();
126             }
127
128             JahiaConsole.println("JahiaTemplatesPackageHandler::Constructor", "error building the TemplatesPackageHandler" + je.toString());
129             throw new JahiaException ("JahiaTemplatesPackageHandler", "error building the TemplatesPackageHandler" ,
130                                    JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, je);
131         }
132
133     }
134
135
136
137     /**
138      * Extract data from the templates.xml file and build the JahiaTemplatesPackage object
139      *
140      * @param isFile , are we handling a file or instead a directory ?
141      */

142     protected void buildTemplatesPackage(boolean isFile) throws JahiaException {
143
144         // extract data from the templates.xml file
145
try {
146
147             if ( isFile ) {
148                 File JavaDoc tmpFile = m_ArchFile.extractFile(TEMPLATES_XML_FILE);
149                 m_XMLDocument = new Templates_Xml(tmpFile.getAbsolutePath());
150                 m_XMLDocument.extractDocumentData();
151                 tmpFile.deleteOnExit();
152                 tmpFile.delete();
153             } else {
154                 m_XMLDocument = new Templates_Xml(m_FilePath + File.separator + TEMPLATES_XML_FILE);
155                 m_XMLDocument.extractDocumentData();
156             }
157
158         } catch (IOException JavaDoc ioe){
159
160             String JavaDoc errMsg = "Failed extracting templates.xml file data " ;
161             JahiaConsole.println("JahiaTemplatesPackageHandler::Constructor", errMsg + "\n" + ioe.toString());
162             throw new JahiaException ("JahiaTemplatesPackageHandler", errMsg ,
163                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, ioe);
164
165         }
166
167
168         // built the JahiaTemplatesPackage Object
169
m_Package = new JahiaTemplatesPackage(
170                                                 m_XMLDocument.getPackageName(),
171                                                 m_XMLDocument.getRootFolder(),
172                                                 m_XMLDocument.getClassesFile(),
173                                                 m_XMLDocument.getProvider(),
174                                                 m_XMLDocument.getThumbnail()
175                                             );
176
177         m_Package.setFilePath(m_FilePath);
178         m_Package.setTemplates(m_XMLDocument.getTemplates());
179
180    }
181
182
183
184     /**
185      * Returns the Classes File
186      *
187      * @return (File) the Classes File
188      */

189     public File JavaDoc getClassesFile(){
190
191         return m_ClassesFile;
192     }
193
194
195     /**
196      * Returns the Generated JahiaTemplatesPackage Object
197      *
198      * @return (JahiaTemplatesPackage) the package object
199      */

200     public JahiaTemplatesPackage getPackage(){
201
202         return m_Package;
203     }
204
205
206
207     /**
208      * Unzip the contents of the jar file in it's current folder
209      *
210      */

211     public void unzip() throws JahiaException {
212
213         // Unzip the file
214
m_ArchFile.unzip();
215
216     }
217
218
219     /**
220      * Unzip the contents of the jar file in a gived folder
221      *
222      * @param (String) path , the path where to extract file
223      */

224     public void unzip(String JavaDoc path) throws JahiaException {
225
226         // Unzip the file
227
m_ArchFile.unzip(path);
228     }
229
230
231     /**
232      * Unzip the classes file in a gived folder
233      *
234      * @param (String) path , the path where to extract file
235      */

236     public void unzipClassesFile(String JavaDoc path) throws JahiaException {
237
238         // Unzip the file
239
//m_JarClassesFile.extractEntry(CLASSES_FILE_ENTRY, path);
240
if ( m_JarClassesFile != null ) {
241             m_JarClassesFile.unzip(path);
242         }
243     }
244
245
246     /**
247      * Unzip an entry in a gived folder
248      *
249      * @param (String) entryName , the name of the entry
250      * @param (String) path , the path where to extract file
251      */

252     public void extractEntry( String JavaDoc entryName,
253                              String JavaDoc path) throws JahiaException {
254
255         // Unzip the entry
256
m_ArchFile.extractEntry(entryName, path);
257     }
258
259
260     /**
261      * Close the Jar file
262      *
263      */

264     public void closeArchiveFile(){
265
266         m_ArchFile.closeArchiveFile();
267         if ( m_ArchFile != null ){
268             m_ArchFile.closeArchiveFile();
269         }
270     }
271
272
273     /**
274      * Delete the classes file
275      *
276      */

277     public void deleteClassesFile(){
278
279         if ( m_JarClassesFile != null ){
280             m_JarClassesFile.closeArchiveFile();
281             m_ClassesFile.delete();
282         }
283     }
284
285
286 } // End Class JahiaTemplatePackage
287
Popular Tags