001/* 002 * Copyright 2019 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.core.ui.ribbonconfiguration; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.configuration.DefaultConfiguration; 026import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 027import org.apache.excalibur.source.Source; 028import org.apache.excalibur.source.SourceResolver; 029import org.xml.sax.SAXException; 030 031/** 032 * A ribbon configuration source and related data 033 */ 034public class RibbonConfigurationSource 035{ 036 Source _source; 037 String _uri; 038 Long _lastModified; 039 040 /** 041 * Constructor 042 * @param uri The source uri 043 * @param source The source 044 * @param returnedParameters The resolve parameters for additional data 045 */ 046 public RibbonConfigurationSource(String uri, Source source, Map<String, Object> returnedParameters) 047 { 048 _uri = uri; 049 _source = source; 050 _lastModified = returnedParameters.containsKey("lastModified") ? (Long) returnedParameters.get("lastModified") : source.getLastModified(); 051 } 052 053 /** 054 * Create a new ribbon configuration source 055 * @param uri The uri 056 * @param resolver The source resolver 057 * @return the ribbon configuration source 058 * @throws IOException If an error occurred when resolving the URI 059 */ 060 public static RibbonConfigurationSource createFromUri(String uri, SourceResolver resolver) throws IOException 061 { 062 Map<String, Object> returnedParameters = new HashMap<>(); 063 Source resolvedSource = resolver.resolveURI(uri, null, returnedParameters); 064 return new RibbonConfigurationSource(uri, resolvedSource, returnedParameters); 065 } 066 067 /** 068 * Get the source 069 * @return The source 070 */ 071 public Source getSource() 072 { 073 return _source; 074 } 075 076 /** 077 * Get the ribbon configuration URI 078 * @return The uri 079 */ 080 public String getUri() 081 { 082 return _uri; 083 } 084 085 /** 086 * Get the last modified date 087 * @return The last modified date 088 */ 089 public Long getLastModified() 090 { 091 return _lastModified; 092 } 093 094 /** 095 * build the configuration from the source 096 * @return The configuration 097 * @throws ConfigurationException If an error occurred 098 * @throws SAXException If an error occurred 099 * @throws IOException If an error occurred 100 */ 101 public Configuration getConfiguration() throws ConfigurationException, SAXException, IOException 102 { 103 if (_source.exists()) 104 { 105 try (InputStream is = _source.getInputStream()) 106 { 107 return new DefaultConfigurationBuilder().build(is); 108 } 109 } 110 111 // default empty configuration 112 return new DefaultConfiguration("ribbon"); 113 } 114 115}