View Javadoc
1   /*
2    * Copyright (c) 2002-2016, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * FileUtils
56   */
57  public class FileUtils
58  {
59  
60      public static final Pattern PATTERN_GROUP_KEY = Pattern.compile( "\\[(.*?)\\]" );
61  
62      /**
63       * Load Terms from a file. Each term is separated by a comma or new line
64       *
65       * @param strRelativeFilePath
66       *            The file path
67       * @return The list of terms
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      * Load map of terms from a file. keys are within brackets and terms are following on new lines each
107      *
108      * @param strRelativeFilePath
109      *            The file path
110      * @return The map of terms
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; // Skip blank lines
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 }