1 package org.apache.maven.plugins.pdf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
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
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 }