Java tutorial
/** * Copyright 2012 the project-owners * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package inti.ws.spring.resource; import inti.ws.spring.resource.config.ConfigParser; import inti.ws.spring.resource.config.ConfigParserSettings; import inti.ws.spring.resource.minify.Minifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.inject.Inject; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @Component @Scope("prototype") public class CommonMinifyableConfig implements ConfigParser<WebResource> { @Inject protected ApplicationContext context; @Inject protected ServletContext ctx; protected ConfigParserSettings settings; protected Minifier minifier; @SuppressWarnings("unchecked") @Override public Map<String, WebResource> instanceWebResources(ObjectMapper mapper, JsonNode node) throws Exception { return parseJs(mapper.convertValue(node, Map.class)); } @Override public void configureWebResources(ObjectMapper mapper, JsonNode node, Map<String, WebResource> resources, Map<String, Map<String, WebResource>> resourceStack) throws Exception { } protected Map<String, WebResource> parseJs(Map<String, Map<String, Boolean>> node) throws Exception { Map<String, WebResource> resources; List<WebResource> files; resources = new HashMap<String, WebResource>(); if (node != null) { for (Map.Entry<String, Map<String, Boolean>> entry : node.entrySet()) { if (entry.getValue() != null) { files = new ArrayList<WebResource>(); for (Map.Entry<String, Boolean> file : entry.getValue().entrySet()) { files.add(new WebResource(ctx, file.getKey(), null, minifier, file.getValue())); } resources.put(entry.getKey(), new WebResourceSet(entry.getKey(), files)); } } } return resources; } @Override public ConfigParserSettings getConfigParserSettings() { return settings; } @Override public void setConfigParserSettings(ConfigParserSettings settings) { this.settings = settings; minifier = context.getBean(settings.getMinifier()); } }