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