001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.skin.generators; 017 018import java.io.File; 019import java.io.IOException; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.generation.ServiceableGenerator; 025import org.apache.cocoon.serialization.ZipArchiveSerializer; 026import org.apache.cocoon.xml.AttributesImpl; 027import org.apache.cocoon.xml.XMLUtils; 028import org.xml.sax.SAXException; 029 030import org.ametys.web.skin.Skin; 031import org.ametys.web.skin.SkinsManager; 032 033/** 034 * Generates a ZIP archive for the given skin 035 * 036 */ 037public class ZipArchiveGenerator extends ServiceableGenerator 038{ 039 private SkinsManager _skinsManager; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 _skinsManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE); 045 } 046 047 @Override 048 public void generate() throws IOException, SAXException, ProcessingException 049 { 050 String id = parameters.getParameter("id", null); 051 052 Skin skin = _skinsManager.getSkin(id); 053 054 File file = skin.getFile(); 055 056 contentHandler.startDocument(); 057 contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE); 058 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 059 060 _saxEntries (file, _skinsManager.getSkinsLocation()); 061 062 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 063 contentHandler.endPrefixMapping("zip"); 064 contentHandler.endDocument(); 065 } 066 067 /** 068 * SAX zip entries 069 * @param file the file 070 * @param prefixPath the path's prefix 071 * @throws SAXException if an error occured while saxing ZIP entries 072 */ 073 protected void _saxEntries (File file, String prefixPath) throws SAXException 074 { 075 for (File child : file.listFiles()) 076 { 077 if (child.isFile()) 078 { 079 AttributesImpl attr = new AttributesImpl(); 080 attr.addCDATAAttribute("name", child.getAbsolutePath().substring(prefixPath.length() + 1)); 081 attr.addCDATAAttribute("src", child.toURI().toString()); 082 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", attr); 083 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry"); 084 } 085 else 086 { 087 _saxEntries(child, prefixPath); 088 } 089 } 090 } 091 092}