Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 au.id.hazelwood.xmltvguidebuilder.config; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.lang.time.StopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import java.io.File; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import static au.id.hazelwood.xmltvguidebuilder.utils.ClassPathResourceUtils.toInputStream; import static au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.formatDurationWords; /** * @author Ricky Hazelwood * @version 1.0 */ public class ConfigFactory { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigFactory.class); private final Schema schema; public ConfigFactory() throws Exception { StopWatch stopWatch = new StopWatch(); stopWatch.start(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = schemaFactory.newSchema(new StreamSource(toInputStream("/config.xsd"))); stopWatch.stop(); LOGGER.debug("ConfigFactory created in {}", formatDurationWords(stopWatch.getTime())); } public Config create(File file) throws InvalidConfigException { try { StopWatch stopWatch = new StopWatch(); stopWatch.start(); LOGGER.debug("Validating config file {}", file); schema.newValidator().validate(new StreamSource(file)); LOGGER.debug("Loading config file {}", file); HierarchicalConfiguration configuration = new XMLConfiguration(file); int offset = configuration.getInt("listing.offset"); int days = configuration.getInt("listing.days"); int synopsis = configuration.getInt("listing.synopsis"); String regionId = configuration.getString("listing.regionId"); Map<Integer, ChannelConfig> channelConfigById = loadChannelConfigs(configuration); Config config = new Config(offset, days, synopsis, regionId, new ArrayList<>(channelConfigById.values())); LOGGER.debug("Loaded {} from {} in {}", config, file, formatDurationWords(stopWatch.getTime())); return config; } catch (Throwable e) { throw new InvalidConfigException(e.getMessage(), e); } } private Map<Integer, ChannelConfig> loadChannelConfigs(HierarchicalConfiguration config) { Map<Integer, ChannelConfig> channelConfigById = new LinkedHashMap<>(); for (Configuration channelConfig : getConfigurationList(config.configurationsAt("channels.channel"))) { int id = channelConfig.getInt("[@id]"); String name = channelConfig.getString(""); channelConfigById.put(id, new ChannelConfig(id, name)); } return channelConfigById; } private List<Configuration> getConfigurationList(List list) { return getTypeList(list, Configuration.class); } private <T> List<T> getTypeList(List entries, Class<T> clazz) { List<T> result = new ArrayList<T>(); for (Object entry : entries) { result.add(clazz.cast(entry)); } return result; } }