1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.easyrulesbot.util;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.service.util.AppPathService;
38
39 import java.io.BufferedReader;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.FileReader;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46
47 import java.util.ArrayList;
48 import java.util.HashMap;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.regex.Matcher;
52 import java.util.regex.Pattern;
53
54
55
56
57 public class FileUtils
58 {
59
60 public static final Pattern PATTERN_GROUP_KEY = Pattern.compile( "\\[(.*?)\\]" );
61
62
63
64
65
66
67
68
69 public static List<String> loadTermsFromFile( String strRelativeFilePath )
70 {
71 List<String> list = new ArrayList<String>( );
72
73 try
74 {
75 BufferedReader br = null;
76 try
77 {
78 String strFilePath = AppPathService.getAbsolutePathFromRelativePath( strRelativeFilePath );
79 br = new BufferedReader( new FileReader( strFilePath ) );
80
81 String strLine = br.readLine( );
82
83 while ( strLine != null )
84 {
85 list.add( strLine.trim( ) );
86 strLine = br.readLine( );
87 }
88 }
89 finally
90 {
91 if ( br != null )
92 {
93 br.close( );
94 }
95 }
96 }
97 catch( IOException ex )
98 {
99 AppLogService.error( "Error loading list of terms file : " + ex.getMessage( ), ex );
100 }
101
102 return list;
103 }
104
105
106
107
108
109
110
111
112 public static Map<String, List<String>> loadMapFromFile( String strRelativeFilePath )
113 {
114 Map<String, List<String>> map = null;
115 String strFilePath = AppPathService.getAbsolutePathFromRelativePath( strRelativeFilePath );
116 try
117 {
118 map = FileUtils.loadMapFromFile( new FileInputStream( strFilePath ) );
119 }
120 catch( FileNotFoundException ex )
121 {
122 AppLogService.error( "Error loading map of terms file : " + ex.getMessage( ), ex );
123 }
124 return map;
125
126 }
127
128 public static Map<String, List<String>> loadMapFromFile( InputStream is )
129 {
130 Map<String, List<String>> map = new HashMap<>( );
131
132 try
133 {
134 BufferedReader br = null;
135 try
136 {
137 br = new BufferedReader( new InputStreamReader( is, "UTF-8" ) );
138
139 String strLine = br.readLine( );
140
141 String strCurrentKey = null;
142 while ( strLine != null )
143 {
144 if ( strLine.trim( ).length( ) == 0 )
145 {
146 strLine = br.readLine( );
147 continue;
148 }
149
150 Matcher m = PATTERN_GROUP_KEY.matcher( strLine );
151
152 if ( m.find( ) )
153 {
154 strCurrentKey = m.group( 1 );
155 map.put( strCurrentKey, new ArrayList<String>( ) );
156 }
157 else
158 if ( strCurrentKey != null )
159 {
160 List<String> list = map.get( strCurrentKey );
161 list.add( strLine.trim( ) );
162 }
163 strLine = br.readLine( );
164 }
165 }
166 finally
167 {
168 if ( br != null )
169 {
170 br.close( );
171 }
172 }
173 }
174 catch( IOException ex )
175 {
176 AppLogService.error( "Error loading list of terms file : " + ex.getMessage( ), ex );
177 }
178 return map;
179 }
180
181 }