View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pdf;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.Writer;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import org.apache.maven.doxia.document.DocumentTOC;
29  import org.apache.maven.doxia.document.DocumentTOCItem;
30  import org.codehaus.plexus.util.ReaderFactory;
31  import org.codehaus.plexus.util.WriterFactory;
32  import org.kopitubruk.util.json.IndentPadding;
33  import org.kopitubruk.util.json.JSONConfig;
34  import org.kopitubruk.util.json.JSONParser;
35  import org.kopitubruk.util.json.JSONUtil;
36  
37  /**
38   * Helper to save then reload TOC content (to a json file), to be able to aggregate TOCs.
39   *
40   * @author anthony-beurive
41   * @since 1.5
42   */
43  class TocFileHelper {
44      private static final String FILENAME = "toc.json";
45  
46      static void saveTOC(File workingDirectory, DocumentTOC toc, Locale locale) throws IOException {
47          // FIXME: manage locales.
48          JSONConfig jsonConfig = new JSONConfig();
49          jsonConfig.setIndentPadding(new IndentPadding("  ", "\n"));
50          jsonConfig.addReflectClass(DocumentTOC.class);
51          jsonConfig.addReflectClass(DocumentTOCItem.class);
52  
53          try (Writer writer = WriterFactory.newWriter(getTocFile(workingDirectory), "UTF-8")) {
54              JSONUtil.toJSON(toc, jsonConfig, writer);
55          }
56      }
57  
58      static Map<String, Object> loadToc(File workingDirectory) throws IOException {
59          try (Reader reader = ReaderFactory.newReader(getTocFile(workingDirectory), "UTF-8")) {
60              return (Map) JSONParser.parseJSON(reader);
61          }
62      }
63  
64      private static File getTocFile(File workingDirectory) {
65          return new File(workingDirectory, FILENAME);
66      }
67  }