1 | /* |
2 | * jDTAUS Banking RI Bankleitzahlenverzeichnis |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * <cs@schulte.it> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | package org.jdtaus.banking.ri.blzdirectory; |
22 | |
23 | import java.io.IOException; |
24 | import java.text.NumberFormat; |
25 | import java.text.ParseException; |
26 | import java.text.SimpleDateFormat; |
27 | import java.util.Date; |
28 | import java.util.Properties; |
29 | |
30 | /** |
31 | * {@code BankfileProvider} implementation backed by a properties file. |
32 | * |
33 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
34 | * @version $JDTAUS: AbstractPropertiesBankfileProvider.java 8661 2012-09-27 11:29:58Z schulte $ |
35 | * |
36 | * @see BankfileBankleitzahlenVerzeichnis |
37 | */ |
38 | public abstract class AbstractPropertiesBankfileProvider implements BankfileProvider |
39 | { |
40 | |
41 | /** Key of the bankfileCount property. */ |
42 | private static final String BANKFILE_COUNT_PROPERTY = "BankleitzahlenVerzeichnis.bankfileCount"; |
43 | |
44 | /** Prefix of bank file properties. */ |
45 | private static final String BANKFILE_PREFIX = "BankleitzahlenDatei."; |
46 | |
47 | /** Properties backing the instance. */ |
48 | private Properties properties; |
49 | |
50 | /** Creates a new {@code AbstractPropertiesBankfileProvider} instance. */ |
51 | public AbstractPropertiesBankfileProvider() |
52 | { |
53 | super(); |
54 | } |
55 | |
56 | public long getLastModifiedMillis() throws IOException |
57 | { |
58 | return 0L; |
59 | } |
60 | |
61 | public final int getBankfileCount() throws IOException |
62 | { |
63 | try |
64 | { |
65 | final String value = this.getProperties().getProperty( BANKFILE_COUNT_PROPERTY, Integer.toString( 0 ) ); |
66 | return NumberFormat.getIntegerInstance().parse( value ).intValue(); |
67 | } |
68 | catch ( final ParseException e ) |
69 | { |
70 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Gets a bankfile resource location. |
76 | * |
77 | * @param index The index of the bankfile resource location to get. |
78 | * |
79 | * @return The bankfile resource location at {@code index}. |
80 | * |
81 | * @throws IndexOutOfBoundsException if {@code index} is negative or greater or equal to the value returned by |
82 | * method {@code getBankfileCount()}. |
83 | * @throws IOException if getting the bankfile resource location fails. |
84 | */ |
85 | public String getBankfileLocation( final int index ) throws IOException |
86 | { |
87 | if ( index < 0 || index >= this.getBankfileCount() ) |
88 | { |
89 | throw new IndexOutOfBoundsException( Integer.toString( index ) ); |
90 | } |
91 | |
92 | final String propertyKey = BANKFILE_PREFIX + index + ".location"; |
93 | final String location = this.getProperties().getProperty( propertyKey ); |
94 | assert location != null : "Property '" + propertyKey + "' not found."; |
95 | return location; |
96 | } |
97 | |
98 | public final Date getDateOfValidity( final int index ) throws IOException |
99 | { |
100 | if ( index < 0 || index >= this.getBankfileCount() ) |
101 | { |
102 | throw new IndexOutOfBoundsException( Integer.toString( index ) ); |
103 | } |
104 | |
105 | try |
106 | { |
107 | final String propertyKey = BANKFILE_PREFIX + index + ".dateOfValidity"; |
108 | final String value = this.getProperties().getProperty( propertyKey ); |
109 | assert value != null : "Property '" + propertyKey + "' not found."; |
110 | return new SimpleDateFormat( "yyyyMMdd" ).parse( value ); |
111 | } |
112 | catch ( final ParseException e ) |
113 | { |
114 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
115 | } |
116 | } |
117 | |
118 | public final Date getDateOfExpiration( final int index ) throws IOException |
119 | { |
120 | if ( index < 0 || index >= this.getBankfileCount() ) |
121 | { |
122 | throw new IndexOutOfBoundsException( Integer.toString( index ) ); |
123 | } |
124 | |
125 | try |
126 | { |
127 | final String propertyKey = BANKFILE_PREFIX + index + ".dateOfExpiration"; |
128 | final String value = this.getProperties().getProperty( propertyKey ); |
129 | assert value != null : "Property '" + propertyKey + "' not found."; |
130 | return new SimpleDateFormat( "yyyyMMdd" ).parse( value ); |
131 | } |
132 | catch ( final ParseException e ) |
133 | { |
134 | throw (IOException) new IOException( e.getMessage() ).initCause( e ); |
135 | } |
136 | } |
137 | |
138 | /** |
139 | * Gets the properties of the instance. |
140 | * |
141 | * @return The properties of the instance. |
142 | * |
143 | * @throws IOException if getting the properties fails. |
144 | */ |
145 | public Properties getProperties() throws IOException |
146 | { |
147 | if ( this.properties == null ) |
148 | { |
149 | this.properties = new Properties(); |
150 | } |
151 | |
152 | return this.properties; |
153 | } |
154 | |
155 | /** |
156 | * Sets the properties of the instance. |
157 | * |
158 | * @param value The new properties of the instance or {@code null}. |
159 | */ |
160 | public void setProperties( final Properties value ) |
161 | { |
162 | this.properties = value; |
163 | } |
164 | |
165 | } |