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.calendar.modules.text;
35
36 import java.io.BufferedReader;
37 import java.io.File;
38 import java.io.FileNotFoundException;
39 import java.io.FileReader;
40 import java.io.IOException;
41
42 import fr.paris.lutece.plugins.calendar.business.Agenda;
43 import fr.paris.lutece.plugins.calendar.business.SimpleAgenda;
44 import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
45 import fr.paris.lutece.plugins.calendar.service.AgendaLoader;
46 import fr.paris.lutece.plugins.calendar.service.Utils;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.service.util.AppPathService;
49
50
51
52
53
54
55
56
57 public class TextAgendaLoader implements AgendaLoader
58 {
59 private static final int MINIMUM_LINE_LENGTH = 10;
60
61
62 public TextAgendaLoader( )
63 {
64 }
65
66
67
68
69
70
71 public Agenda load( String strParameter )
72 {
73 SimpleAgenda agenda = new SimpleAgenda( );
74 String strFile = AppPathService.getWebAppPath( ) + strParameter;
75 File file = new File( strFile );
76 parseFileContent( file, agenda );
77
78 return agenda;
79 }
80
81
82
83
84
85
86 private void parseFileContent( File file, SimpleAgenda agenda )
87 {
88 BufferedReader input = null;
89
90 try
91 {
92
93
94
95 input = new BufferedReader( new FileReader( file ) );
96
97 String strLine = null;
98
99 while ( ( strLine = input.readLine( ) ) != null )
100 {
101 if ( strLine.length( ) > MINIMUM_LINE_LENGTH )
102 {
103 String strDate = getDate( strLine );
104
105 if ( Utils.isValid( strDate ) )
106 {
107 String strEvent = getEvent( strLine );
108 SimpleEvent event = new SimpleEvent( );
109 event.setDate( Utils.getDate( strDate ) );
110 event.setTitle( strEvent );
111 agenda.addEvent( event );
112 }
113 }
114 }
115 }
116 catch ( FileNotFoundException e )
117 {
118 AppLogService.error( e.getMessage( ), e );
119 }
120 catch ( IOException e )
121 {
122 AppLogService.error( e.getMessage( ), e );
123 }
124 finally
125 {
126 try
127 {
128 if ( input != null )
129 {
130
131 input.close( );
132 }
133 }
134 catch ( IOException e )
135 {
136 AppLogService.error( e.getMessage( ), e );
137 }
138 }
139 }
140
141
142
143
144
145
146 private String getDate( String strLine )
147 {
148 return strLine.substring( 0, 8 );
149 }
150
151
152
153
154
155
156 private String getEvent( String strLine )
157 {
158 return strLine.substring( 9 );
159 }
160 }