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.extraction.execution.pipeline.impl; 017 018import java.io.OutputStream; 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.configuration.Configurable; 025import org.apache.avalon.framework.configuration.Configuration; 026import org.apache.avalon.framework.configuration.ConfigurationException; 027import org.apache.excalibur.source.SourceResolver; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import org.ametys.core.util.SizeUtils.ExcludeFromSizeCalculation; 032import org.ametys.plugins.extraction.execution.pipeline.ExtractionMatcher; 033import org.ametys.plugins.extraction.execution.pipeline.Pipeline; 034import org.ametys.plugins.extraction.execution.pipeline.PipelineDescriptor; 035import org.ametys.plugins.extraction.execution.pipeline.PipelineSerializerModel; 036import org.ametys.plugins.extraction.execution.pipeline.PipelineSerializerModelExtensionPoint; 037import org.ametys.runtime.i18n.I18nizableText; 038 039/** 040 * Implementation of {@link PipelineDescriptor} which is {@link Configurable}. 041 */ 042public class ConfigurablePipelineDescriptor implements PipelineDescriptor, Configurable 043{ 044 @ExcludeFromSizeCalculation 045 private static final Logger __LOGGER = LoggerFactory.getLogger(ConfigurablePipelineDescriptor.class); 046 private static final String __DEFAULT_SERIALIZER = "xml"; 047 048 private String _id; 049 private I18nizableText _label; 050 private List<String> _extractions; 051 private List<String> _xslts; 052 private Map<String, String> _outputParams; 053 @ExcludeFromSizeCalculation 054 private PipelineSerializerModel _serializer; 055 056 @ExcludeFromSizeCalculation 057 private SourceResolver _resolver; 058 @ExcludeFromSizeCalculation 059 private PipelineSerializerModelExtensionPoint _pipelineSerializers; 060 061 /** 062 * Default constructor 063 * @param id The id of the pipeline descriptor 064 * @param resolver The source resolver 065 * @param pipelineSerializers The extension point for pipeline serializers 066 */ 067 public ConfigurablePipelineDescriptor(String id, SourceResolver resolver, PipelineSerializerModelExtensionPoint pipelineSerializers) 068 { 069 _id = id; 070 _resolver = resolver; 071 _pipelineSerializers = pipelineSerializers; 072 } 073 074 @Override 075 public void configure(Configuration configuration) throws ConfigurationException 076 { 077 _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "application"); 078 079 _extractions = new ArrayList<>(); 080 for (Configuration extractionConf : configuration.getChild("extractions").getChildren("extraction")) 081 { 082 String filePath = extractionConf.getValue(); 083 _extractions.add(filePath); 084 } 085 086 _xslts = new ArrayList<>(); 087 for (Configuration xsltConf : configuration.getChild("stylesheets").getChildren("xslt")) 088 { 089 String name = xsltConf.getAttribute("name"); 090 _xslts.add(name); 091 } 092 093 setOutputParameters(configuration.getChild("out", false)); 094 String serializerType = _outputParams.getOrDefault("type", __DEFAULT_SERIALIZER); 095 _serializer = _pipelineSerializers.getExtension(serializerType); 096 if (_serializer == null) 097 { 098 __LOGGER.warn("The serializer type '{}' in <out> tag of the pipeline descriptor '{}' is invalid.", serializerType, _id); 099 _serializer = _pipelineSerializers.getExtension(__DEFAULT_SERIALIZER); 100 } 101 } 102 103 @Override 104 public I18nizableText getLabel() 105 { 106 return _label; 107 } 108 109 @Override 110 public ExtractionMatcher getExtractionMatcher() 111 { 112 return !_extractions.isEmpty() ? new ListExtractionMatcher(_extractions) : new AllExtractionMatcher(); 113 } 114 115 @Override 116 public List<String> getStylesheets() 117 { 118 return _xslts; 119 } 120 121 @Override 122 public PipelineSerializerModel getSerializerModel() 123 { 124 return _serializer; 125 } 126 127 @Override 128 public String getResultSubfolder() 129 { 130 return _outputParams.getOrDefault("path", "" /* root folder */); 131 } 132 133 @Override 134 public String getDefaultExtension() 135 { 136 return _outputParams.getOrDefault("extension", _serializer.getDefaultFileExtension()); 137 } 138 139 @Override 140 public Pipeline newPipeline(OutputStream out) 141 { 142 return new PipelineImpl(this, out, _resolver); 143 } 144 145 @Override 146 public Map<String, String> getOutputParameters() 147 { 148 return _outputParams; 149 } 150 151 /** 152 * Set the output parameters to get the extension, path, encoding, etc. 153 * @param outputConf The output configuration 154 * @throws ConfigurationException throws an exception if an error occurs 155 */ 156 protected void setOutputParameters(Configuration outputConf) throws ConfigurationException 157 { 158 _outputParams = new HashMap<>(); 159 if (outputConf != null) 160 { 161 for (String name : outputConf.getAttributeNames()) 162 { 163 _outputParams.put(name, outputConf.getAttribute(name)); 164 } 165 } 166 } 167}