001/* 002 * Copyright 2018 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.core.ui.minimize.css; 017 018import java.io.IOException; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.commons.io.IOUtils; 025import org.apache.excalibur.source.Source; 026import org.xml.sax.SAXException; 027 028import org.ametys.core.minimize.css.MinimizeCSSManager; 029import org.ametys.plugins.core.ui.minimize.AbstractMinimizeSourceMapReader; 030import org.ametys.plugins.core.ui.minimize.HashCache.UriData; 031 032/** 033 * Reader for Source map of minimized css files 034 */ 035public class MinimizeCSSSourceMapReader extends AbstractMinimizeSourceMapReader 036{ 037 private MinimizeCSSManager _cssMinimizeManager; 038 039 @Override 040 public void service(ServiceManager smanager) throws ServiceException 041 { 042 super.service(smanager); 043 044 _cssMinimizeManager = (MinimizeCSSManager) manager.lookup(MinimizeCSSManager.ROLE); 045 } 046 047 @Override 048 public void generate() throws IOException, SAXException, ProcessingException 049 { 050 Source sourceMap = _sourceMapCache.get(source + ".css.map"); 051 if (sourceMap == null) 052 { 053 List<UriData> filesForHash = _hashCache.getFilesForHash(source, true); 054 if (filesForHash != null) 055 { 056 // compiles the css files and store the source map in the source map cache 057 _cssMinimizeManager.minimizeAndAggregateURIs(filesForHash, source + ".css", true); 058 059 sourceMap = _sourceMapCache.get(source + ".css.map"); 060 } 061 } 062 063 if (sourceMap == null) 064 { 065 throw new IllegalStateException("There is no source map for minimized hash file '" + source + ".css'"); 066 } 067 068 IOUtils.copy(sourceMap.getInputStream(), out); 069 out.close(); 070 } 071}