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