001/* 002 * Copyright 2020 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.resources.css.sass; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.commons.lang3.StringUtils; 023import org.apache.excalibur.source.Source; 024import org.apache.excalibur.source.SourceResolver; 025 026import org.ametys.core.resources.AbstractResourceHandlerProvider; 027import org.ametys.core.resources.ResourceHandler; 028import org.ametys.core.resources.ResourceHandlerProvider; 029 030/** 031 * {@link ResourceHandlerProvider} for Sass source map files. 032 */ 033public class SassSourceMapResourceHandlerProvider extends AbstractResourceHandlerProvider 034{ 035 private static final String[] __SASS_EXTENSION = new String[] {".scss", ".sass"}; 036 037 private SourceResolver _resolver; 038 039 @Override 040 public void service(ServiceManager manager) throws ServiceException 041 { 042 super.service(manager); 043 _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 044 } 045 046 public int getPriority() 047 { 048 return MIN_PRIORITY + 1000; 049 } 050 051 public ResourceHandler getResourceHandler(String source) throws Exception 052 { 053 String lcSource = StringUtils.lowerCase(source); 054 055 for (String ext : __SASS_EXTENSION) 056 { 057 if (StringUtils.endsWith(lcSource, ext + ".map")) 058 { 059 return setup(new SassSourceMapResourceHandler()); 060 } 061 } 062 063 if (lcSource.endsWith(".css.map")) 064 { 065 String sourceWithoutExt = StringUtils.substringBeforeLast(source, ".css.map"); 066 067 for (String ext : __SASS_EXTENSION) 068 { 069 Source src = null; 070 try 071 { 072 src = _resolver.resolveURI(sourceWithoutExt + ext); 073 if (src.exists()) 074 { 075 // .scss or .sass file found 076 return setup(new SassSourceMapResourceHandler(src)); 077 } 078 else 079 { 080 _resolver.release(src); 081 } 082 } 083 catch (IOException e) 084 { 085 // Nothing 086 } 087 } 088 } 089 090 return null; 091 } 092}