View Javadoc
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   * Standard transformer implementation for the Fess search engine.
31   * This transformer handles document transformation and content extraction using
32   * the standard Fess file transformation process with support for various content types.
33   *
34   * <p>It extends AbstractFessFileTransformer to provide file-specific transformation
35   * capabilities while using the appropriate extractor for each document type.</p>
36   */
37  public class FessStandardTransformer extends AbstractFessFileTransformer {
38      /**
39       * Default constructor.
40       */
41      public FessStandardTransformer() {
42          super();
43      }
44  
45      /** Logger instance for this class */
46      private static final Logger logger = LogManager.getLogger(FessStandardTransformer.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 or falls back to the Tika extractor.
84       *
85       * @param responseData the response data containing the document to extract
86       * @return the extractor instance for processing the document
87       * @throws FessSystemException if no suitable extractor 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          Extractor extractor = extractorFactory.getExtractor(responseData.getMimeType());
96          if (extractor == null) {
97              extractor = ComponentUtil.getComponent("tikaExtractor");
98              if (extractor == null) {
99                  throw new FessSystemException("Could not find tikaExtractor.");
100             }
101         }
102 
103         if (logger.isDebugEnabled()) {
104             logger.debug("url={}, extractor={}", responseData.getUrl(), extractor);
105         }
106         return extractor;
107     }
108 }