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.job;
17  
18  import java.util.Collection;
19  import java.util.Map;
20  import java.util.Set;
21  
22  /**
23   * Formatter that outputs index documents as HTML files.
24   */
25  public class HtmlIndexExportFormatter implements IndexExportFormatter {
26  
27      /**
28       * Creates a new HtmlIndexExportFormatter instance.
29       */
30      public HtmlIndexExportFormatter() {
31          // default constructor
32      }
33  
34      @Override
35      public String getFileExtension() {
36          return ".html";
37      }
38  
39      @Override
40      public String getIndexFileName() {
41          return "index.html";
42      }
43  
44      @Override
45      public String format(final Map<String, Object> source, final Set<String> excludeFields) {
46          final String title = escapeHtml(getStringValue(source, "title"));
47          final String content = escapeHtml(getStringValue(source, "content"));
48          final String lang = escapeHtml(getStringValue(source, "lang"));
49  
50          final StringBuilder html = new StringBuilder();
51          html.append("<!DOCTYPE html>\n");
52          html.append("<html lang=\"").append(lang).append("\">\n");
53          html.append("<head>\n");
54          html.append("<meta charset=\"UTF-8\">\n");
55          html.append("<title>").append(title).append("</title>\n");
56  
57          for (final Map.Entry<String, Object> entry : source.entrySet()) {
58              final String field = entry.getKey();
59              if ("title".equals(field) || "content".equals(field) || "lang".equals(field)) {
60                  continue;
61              }
62              if (excludeFields.contains(field)) {
63                  continue;
64              }
65  
66              final Object value = entry.getValue();
67              if (value instanceof Collection) {
68                  for (final Object item : (Collection<?>) value) {
69                      html.append("<meta name=\"fess:")
70                              .append(escapeHtml(field))
71                              .append("\" content=\"")
72                              .append(escapeHtml(String.valueOf(item)))
73                              .append("\">\n");
74                  }
75              } else if (value != null) {
76                  html.append("<meta name=\"fess:")
77                          .append(escapeHtml(field))
78                          .append("\" content=\"")
79                          .append(escapeHtml(String.valueOf(value)))
80                          .append("\">\n");
81              }
82          }
83  
84          html.append("</head>\n");
85          html.append("<body>\n");
86          html.append(content).append("\n");
87          html.append("</body>\n");
88          html.append("</html>\n");
89  
90          return html.toString();
91      }
92  
93      private String getStringValue(final Map<String, Object> source, final String key) {
94          final Object value = source.get(key);
95          return value != null ? value.toString() : "";
96      }
97  
98      private String escapeHtml(final String text) {
99          if (text == null || text.isEmpty()) {
100             return "";
101         }
102         return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("'", "&#39;");
103     }
104 }