1 20 package org.jahia.data.templates; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import org.jahia.exceptions.JahiaException; 26 import org.jahia.utils.JahiaConsole; 27 import org.jahia.utils.zip.JahiaArchiveFileHandler; 28 29 62 public class JahiaTemplatesPackageHandler { 63 64 private static final String CLASS_NAME = JahiaTemplatesPackageHandler.class.getName(); 65 66 67 private String m_TemplateRootFolder; 68 69 private String m_FilePath; 70 71 private JahiaArchiveFileHandler m_ArchFile; 72 73 private JahiaTemplatesPackage m_Package; 74 75 private Templates_Xml m_XMLDocument; 76 77 private File m_ClassesFile; 78 79 private File m_ThumbnailFile; 80 81 82 private JahiaArchiveFileHandler m_JarClassesFile; 83 84 85 private static String TEMPLATES_XML_FILE = "templates.xml"; 86 87 88 92 public JahiaTemplatesPackageHandler (String filePath) 93 throws JahiaException { 94 95 m_FilePath = filePath; 96 97 File tmpFile = new File (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 e ) { 107 108 String 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 142 protected void buildTemplatesPackage(boolean isFile) throws JahiaException { 143 144 try { 146 147 if ( isFile ) { 148 File 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 ioe){ 159 160 String 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 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 189 public File getClassesFile(){ 190 191 return m_ClassesFile; 192 } 193 194 195 200 public JahiaTemplatesPackage getPackage(){ 201 202 return m_Package; 203 } 204 205 206 207 211 public void unzip() throws JahiaException { 212 213 m_ArchFile.unzip(); 215 216 } 217 218 219 224 public void unzip(String path) throws JahiaException { 225 226 m_ArchFile.unzip(path); 228 } 229 230 231 236 public void unzipClassesFile(String path) throws JahiaException { 237 238 if ( m_JarClassesFile != null ) { 241 m_JarClassesFile.unzip(path); 242 } 243 } 244 245 246 252 public void extractEntry( String entryName, 253 String path) throws JahiaException { 254 255 m_ArchFile.extractEntry(entryName, path); 257 } 258 259 260 264 public void closeArchiveFile(){ 265 266 m_ArchFile.closeArchiveFile(); 267 if ( m_ArchFile != null ){ 268 m_ArchFile.closeArchiveFile(); 269 } 270 } 271 272 273 277 public void deleteClassesFile(){ 278 279 if ( m_JarClassesFile != null ){ 280 m_JarClassesFile.closeArchiveFile(); 281 m_ClassesFile.delete(); 282 } 283 } 284 285 286 } | Popular Tags |