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.URIUtils; 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 contentHandler.startDocument(); 079 080 String relPath = URIUtils.encodePath(StringUtils.substringAfter(basePath, "flipbook")); 081 AttributesImpl attrs = new AttributesImpl(); 082 attrs.addCDATAAttribute("basePath", relPath); 083 attrs.addCDATAAttribute("pagePath", relPath + "/pages"); 084 if (documentFile != null) 085 { 086 attrs.addCDATAAttribute("documentPath", relPath + "/document"); 087 attrs.addCDATAAttribute("documentName", documentFile.getName()); 088 attrs.addCDATAAttribute("documentSize", Long.toString(documentFile.length())); 089 } 090 091 attrs.addCDATAAttribute("nb-page", String.valueOf(imageFiles.size())); 092 093 XMLUtils.startElement(contentHandler, "images", attrs); 094 for (File file : imageFiles) 095 { 096 BufferedImage image = ImageIO.read(file); 097 int width = image.getWidth(); 098 int height = image.getHeight(); 099 100 AttributesImpl pageAttrs = new AttributesImpl(); 101 pageAttrs.addCDATAAttribute("fileName", file.getName()); 102 pageAttrs.addCDATAAttribute("width", Integer.toString(width)); 103 pageAttrs.addCDATAAttribute("height", Integer.toString(height)); 104 105 XMLUtils.createElement(contentHandler, "image", pageAttrs); 106 } 107 108 File previewFile = new File(imageFolder, "preview.jpg"); 109 if (previewFile.exists()) 110 { 111 BufferedImage bi = ImageIO.read(previewFile); 112 113 AttributesImpl previewAttrs = new AttributesImpl(); 114 previewAttrs.addCDATAAttribute("name", previewFile.getName()); 115 previewAttrs.addCDATAAttribute("width", String.valueOf(bi.getWidth())); 116 117 int rows = (int) Math.ceil(1 + imageFiles.size() / 2); 118 previewAttrs.addCDATAAttribute("height", String.valueOf(bi.getHeight() / rows)); 119 XMLUtils.createElement(contentHandler, "preview", previewAttrs); 120 } 121 122 XMLUtils.endElement(contentHandler, "images"); 123 124 contentHandler.endDocument(); 125 } 126}