1 /*
2 * Copyright 2012-2025 CodeLibs Project and the Others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package org.codelibs.fess.crawler.transformer;
17
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20 import org.codelibs.fess.crawler.entity.ResponseData;
21 import org.codelibs.fess.crawler.extractor.Extractor;
22 import org.codelibs.fess.crawler.extractor.ExtractorFactory;
23 import org.codelibs.fess.exception.FessSystemException;
24 import org.codelibs.fess.mylasta.direction.FessConfig;
25 import org.codelibs.fess.util.ComponentUtil;
26
27 import jakarta.annotation.PostConstruct;
28
29 /**
30 * File transformer implementation for the Fess search engine.
31 * This transformer handles file-based document transformation and content extraction
32 * using the Fess file transformation process with support for various file types.
33 *
34 * <p>It extends AbstractFessFileTransformer to provide specialized file processing
35 * capabilities using the appropriate extractor for each file type.</p>
36 */
37 public class FessFileTransformer extends AbstractFessFileTransformer {
38 /**
39 * Default constructor.
40 */
41 public FessFileTransformer() {
42 super();
43 }
44
45 /** Logger instance for this class */
46 private static final Logger logger = LogManager.getLogger(FessFileTransformer.class);
47
48 /**
49 * Initializes the transformer after dependency injection.
50 * Sets up the Fess configuration and data serializer components.
51 */
52 @PostConstruct
53 public void init() {
54 if (logger.isDebugEnabled()) {
55 logger.debug("Initializing {}", this.getClass().getSimpleName());
56 }
57 fessConfig = ComponentUtil.getFessConfig();
58 dataSerializer = ComponentUtil.getComponent("dataSerializer");
59 }
60
61 /**
62 * Gets the Fess configuration instance.
63 *
64 * @return the Fess configuration
65 */
66 @Override
67 public FessConfig getFessConfig() {
68 return fessConfig;
69 }
70
71 /**
72 * Gets the logger instance for this transformer.
73 *
74 * @return the logger instance
75 */
76 @Override
77 public Logger getLogger() {
78 return logger;
79 }
80
81 /**
82 * Gets the appropriate extractor for the given response data.
83 * Selects an extractor based on the MIME type of the file.
84 *
85 * @param responseData the response data containing the file to extract
86 * @return the extractor instance for processing the file
87 * @throws FessSystemException if no suitable extractor factory can be found
88 */
89 @Override
90 protected Extractor getExtractor(final ResponseData responseData) {
91 final ExtractorFactory extractorFactory = ComponentUtil.getExtractorFactory();
92 if (extractorFactory == null) {
93 throw new FessSystemException("Could not find extractorFactory.");
94 }
95 final Extractor extractor = extractorFactory.getExtractor(responseData.getMimeType());
96 if (logger.isDebugEnabled()) {
97 logger.debug("url={}, extractor={}", responseData.getUrl(), extractor);
98 }
99 return extractor;
100 }
101 }