001/*
002 *  Copyright 2014 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.plugins.flipbook;
017
018import java.awt.image.BufferedImage;
019import java.io.File;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026import javax.imageio.ImageIO;
027
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.ProcessingException;
031import org.apache.cocoon.environment.ObjectModelHelper;
032import org.apache.cocoon.environment.Request;
033import org.apache.cocoon.generation.ServiceableGenerator;
034import org.apache.cocoon.xml.AttributesImpl;
035import org.apache.cocoon.xml.XMLUtils;
036import org.apache.commons.io.FileUtils;
037import org.apache.commons.lang.StringUtils;
038import org.xml.sax.SAXException;
039
040import org.ametys.core.util.URLEncoder;
041import org.ametys.plugins.repository.AmetysObjectResolver;
042
043/**
044 * Generates information on the generated images.
045 */
046public class ImagesGenerator extends ServiceableGenerator
047{
048    /** Request attribute from path to images' directory */
049    public static final String IMAGES_DIRECTORY_PATH_REQUEST_ATTR = "images-base-directory";
050    
051    /** The ametys object resolver. */
052    protected AmetysObjectResolver _resolver;
053    
054    @Override
055    public void service(ServiceManager serviceManager) throws ServiceException
056    {
057        super.service(serviceManager);
058        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
059    }
060    
061    @Override
062    public void generate() throws IOException, SAXException, ProcessingException
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        String basePath = (String) request.getAttribute(IMAGES_DIRECTORY_PATH_REQUEST_ATTR);
066        
067        // Search the document file.
068        File documentFolder = new File(basePath, "/document");
069        Iterator<File> documentFiles = FileUtils.listFiles(documentFolder, new String[] {"document"}, false).iterator();
070        File documentFile = documentFiles.hasNext() ? documentFiles.next() : null;
071        
072        // List the image files.
073        File imageFolder = new File(basePath + "/pages");
074        
075        List<File> imageFiles = new ArrayList<>(FileUtils.listFiles(imageFolder, new String[] {"png"}, false));
076        Collections.sort(imageFiles);
077        
078        // Compute the width and height of the images.
079        int width = -1;
080        int height = -1;
081        if (!imageFiles.isEmpty())
082        {
083            File file = imageFiles.get(0);
084            BufferedImage image = ImageIO.read(file);
085            width = image.getWidth();
086            height = image.getHeight();
087        }
088        
089        contentHandler.startDocument();
090        
091        String relPath = URLEncoder.encodePath(URLEncoder.encodePath(StringUtils.substringAfter(basePath, "flipbook")));
092        AttributesImpl attrs = new AttributesImpl();
093        attrs.addCDATAAttribute("basePath", relPath);
094        attrs.addCDATAAttribute("pagePath", relPath + "/pages");
095        attrs.addCDATAAttribute("width", Integer.toString(width));
096        attrs.addCDATAAttribute("height", Integer.toString(height));
097        if (documentFile != null)
098        {
099            attrs.addCDATAAttribute("documentPath", relPath + "/document");
100            attrs.addCDATAAttribute("documentName", documentFile.getName());
101            attrs.addCDATAAttribute("documentSize", Long.toString(documentFile.length()));
102        }
103        
104        attrs.addCDATAAttribute("nb-page", String.valueOf(imageFiles.size()));
105        XMLUtils.startElement(contentHandler, "images", attrs);
106        for (File file : imageFiles)
107        {
108            AttributesImpl pageAttrs = new AttributesImpl();
109            pageAttrs.addCDATAAttribute("fileName", file.getName());
110            
111            XMLUtils.createElement(contentHandler, "image", pageAttrs);
112        }
113        
114        File previewFile = new File(imageFolder, "preview.jpg");
115        if (previewFile.exists())
116        {
117            BufferedImage bi = ImageIO.read(previewFile);
118            
119            AttributesImpl previewAttrs = new AttributesImpl();
120            previewAttrs.addCDATAAttribute("name", previewFile.getName());
121            previewAttrs.addCDATAAttribute("width", String.valueOf(bi.getWidth()));
122            
123            int rows = (int) Math.ceil(1 + imageFiles.size() / 2);
124            previewAttrs.addCDATAAttribute("height", String.valueOf(bi.getHeight() / rows));
125            XMLUtils.createElement(contentHandler, "preview", previewAttrs);
126        }
127        
128        XMLUtils.endElement(contentHandler, "images");
129        
130        contentHandler.endDocument();
131    }
132}