1 | /** |
---|
2 | * Objet métier représentant un nom de fichier dans le système Ether |
---|
3 | * |
---|
4 | * @version $Id: EtherFileName.java,v 1.1 2001/01/15 15:38:47 pn Exp $ |
---|
5 | * @author ppa |
---|
6 | */ |
---|
7 | |
---|
8 | package fr.alcatel.ether.app.data; |
---|
9 | |
---|
10 | // JDK - io |
---|
11 | import java.io.Serializable; |
---|
12 | |
---|
13 | //import fr.alcatel.ether.persistency.*; |
---|
14 | import fr.alcatel.ether.tools.*; |
---|
15 | //import fr.alcatel.ether.app.order.*; |
---|
16 | //import fr.alcatel.ether.app.experiment.*; |
---|
17 | import fr.alcatel.ether.app.common.*; |
---|
18 | import java.util.*; |
---|
19 | import java.io.File; // for File.separator |
---|
20 | import java.math.BigDecimal; |
---|
21 | |
---|
22 | public class EtherFileName implements Serializable |
---|
23 | { |
---|
24 | boolean DEBUG = true; |
---|
25 | /** fichier archive où réside le fichier */ |
---|
26 | String tarfile; |
---|
27 | |
---|
28 | /** chemin du fichier - s'il réside dans un fichier archive, c'est le chemin à l'intérieur du fichier archive */ |
---|
29 | String path; |
---|
30 | |
---|
31 | /** nom canonique identifiant le jeu de données dans Ether selon Note technique élements de normalisation ETH-NT-0-TECH-583-CN */ |
---|
32 | String canonical; |
---|
33 | |
---|
34 | /** |
---|
35 | * Constructeur |
---|
36 | * |
---|
37 | */ |
---|
38 | public EtherFileName( ) |
---|
39 | { |
---|
40 | super( ); |
---|
41 | init( null, null, null ); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Constructeur |
---|
46 | * |
---|
47 | * @param path: chemin d'accès à ce fichier |
---|
48 | */ |
---|
49 | public EtherFileName( String path ) |
---|
50 | { |
---|
51 | super( ); |
---|
52 | init( path, null, null ); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Constructeur |
---|
57 | * |
---|
58 | * @param tarfile: archive où réside ce fichier (éventuellement null) |
---|
59 | * @param path: chemin d'accès à ce fichier |
---|
60 | */ |
---|
61 | public EtherFileName( String path, String tarfile ) |
---|
62 | { |
---|
63 | super( ); |
---|
64 | init( path, tarfile, null ); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | * Constructeur |
---|
70 | * |
---|
71 | * @param tarfile: archive où réside ce fichier (éventuellement null) |
---|
72 | * @param path: chemin d'accès à ce fichier |
---|
73 | * @param canonical: chemin canonique de ce fichier (identifie le jeu de données |
---|
74 | */ |
---|
75 | public EtherFileName( String path, String tarfile, String canonical ) |
---|
76 | { |
---|
77 | super( ); |
---|
78 | init( path, tarfile, canonical ); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Initialisation |
---|
83 | * |
---|
84 | * @param tarfile: archive où réside ce fichier (éventuellement null) |
---|
85 | * @param path: chemin d'accès à ce fichier |
---|
86 | */ |
---|
87 | protected void init( String path, String tarfile, String canonical ) |
---|
88 | { |
---|
89 | // test , si le champ tar ne se finit pas par .tar |
---|
90 | // le fichier n'est pas contenu dans une archive |
---|
91 | if ( (tarfile != null) && ( ! tarfile.endsWith(".tar") )) |
---|
92 | { |
---|
93 | // fichier non contenu dans fichier tar |
---|
94 | this.tarfile = null; |
---|
95 | this.path = tarfile; |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | this.path = path; |
---|
100 | this.tarfile = tarfile; |
---|
101 | } |
---|
102 | this.canonical = canonical; |
---|
103 | Config.getConfig( ); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Obtenir le chemin de ce fichier |
---|
108 | */ |
---|
109 | public String getPath( ){ return path; } |
---|
110 | |
---|
111 | /** |
---|
112 | * Mettre à jour le chemin de ce fichier |
---|
113 | */ |
---|
114 | public void setPath( String path ) { |
---|
115 | //this.path = path; |
---|
116 | if ( (tarfile != null) && ( ! tarfile.endsWith(".tar") )) |
---|
117 | { |
---|
118 | // fichier non contenu dans fichier tar |
---|
119 | this.path = this.tarfile; |
---|
120 | this.tarfile = null; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | this.path = path; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Obtenir le chemin de l'archive |
---|
130 | */ |
---|
131 | public String getTarfile( ){ return tarfile; } |
---|
132 | |
---|
133 | /** |
---|
134 | * Mettre à jour le chemin de l'archive contenant ce fichier |
---|
135 | */ |
---|
136 | public void setTarfile( String tarfile ) { |
---|
137 | //this.tarfile = tarfile; |
---|
138 | if ( (tarfile != null) && ( ! tarfile.endsWith(".tar") )) |
---|
139 | { |
---|
140 | // fichier non contenu dans fichier tar |
---|
141 | this.path = tarfile; |
---|
142 | this.tarfile = null; |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | this.tarfile = tarfile; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Obtenir le chemin canonique de l'archive |
---|
152 | */ |
---|
153 | public String getCanonical( ){ return canonical; } |
---|
154 | |
---|
155 | /** |
---|
156 | * Mettre à jour le chemin de l'archive contenant ce fichier |
---|
157 | */ |
---|
158 | public void setCanonical( String canonical ) { |
---|
159 | this.canonical = canonical; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Obtenir le chemin relatif de ce fichier, étant donné un préfixe à retrancher. |
---|
164 | * Cette méthode permet de fabriquer un nom de fichier à un endroit différent |
---|
165 | * pour effectuer des copies de fichier. |
---|
166 | * |
---|
167 | * @param prefix: le préfixe à retrancher du nom de fichier. |
---|
168 | * |
---|
169 | * @return le chemin du fichier ôté du préfixe. |
---|
170 | */ |
---|
171 | public String getRelativePath( String prefix ) { |
---|
172 | String path = getPath( ); |
---|
173 | if ( prefix != null && path != null ) { |
---|
174 | if ( path.regionMatches( 0, prefix, 0, prefix.length() ) ) { |
---|
175 | StringBuffer absolute = new StringBuffer( ); |
---|
176 | absolute.append( path ); |
---|
177 | absolute.delete( 0, prefix.length() ); |
---|
178 | return absolute.toString( ); |
---|
179 | } |
---|
180 | } |
---|
181 | else { |
---|
182 | if ( DEBUG ) { Trace.debug( this, "Null prefix given to getRelativePath() or offline file !" ); } |
---|
183 | } |
---|
184 | return path; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** |
---|
189 | * Obtenir le chemin relatif de ce fichier, en retranchant le nom du fichier. |
---|
190 | * Si le path ne contient que le nom du fichier , "/" est retourné. |
---|
191 | * |
---|
192 | * @return le chemin du fichier ôté du nom du fichier. |
---|
193 | */ |
---|
194 | public String getPathWithoutName() { |
---|
195 | String path = getPath( ); |
---|
196 | if ( path != null ) { |
---|
197 | StringBuffer result = new StringBuffer( ); |
---|
198 | int index = path.lastIndexOf(File.separator); |
---|
199 | if ( index != -1 ) |
---|
200 | { |
---|
201 | result.append(path.substring(0,index)); |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | result.append(File.separator); |
---|
206 | } |
---|
207 | return result.toString(); |
---|
208 | } |
---|
209 | |
---|
210 | return path; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | * Obtenir le nom du fichier. |
---|
216 | * Si c'est une archive, le path est retourné |
---|
217 | * sinon, le nom du fichier est extrait du path ( car tarfile est null ). |
---|
218 | * |
---|
219 | * @return le nom du fichier |
---|
220 | */ |
---|
221 | public String getFileName() |
---|
222 | { |
---|
223 | String path = getPath(); |
---|
224 | if ( (path != null) && ( ! path.equals("") )) |
---|
225 | { |
---|
226 | // suppression du chemin s'il existe |
---|
227 | int sep = path.lastIndexOf( File.separator ); |
---|
228 | // path contains a path |
---|
229 | if ( sep != -1 ) { |
---|
230 | StringBuffer copyPath = new StringBuffer( path); |
---|
231 | path = copyPath.substring( sep + 1 , path.length() ); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | return path; |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * <p>Obtenir le chemin du fichier archive.</p> |
---|
240 | * |
---|
241 | * @return chemin contenant le fichier archive |
---|
242 | */ |
---|
243 | public String getTarfilePath( ) { |
---|
244 | // ----------------------------------------------------- |
---|
245 | // If it is not an archive, return empty string |
---|
246 | // ----------------------------------------------------- |
---|
247 | if ( !isArchive() ) { |
---|
248 | return ""; |
---|
249 | } |
---|
250 | // ----------------------------------------------------- |
---|
251 | // If it's an archive, remove name of archive |
---|
252 | // and keep path to archive |
---|
253 | // ----------------------------------------------------- |
---|
254 | String archiveFile = getTarfile( ); |
---|
255 | String returnPath; |
---|
256 | int sep = archiveFile.lastIndexOf( File.separator ); |
---|
257 | // tar file contains a path |
---|
258 | if ( sep != -1 ) { |
---|
259 | StringBuffer tarpath = new StringBuffer( archiveFile ); |
---|
260 | returnPath = tarpath.substring( 0, sep ); |
---|
261 | } |
---|
262 | // tar file does not contain a path |
---|
263 | else { |
---|
264 | returnPath = ""; |
---|
265 | } |
---|
266 | // ----------------------------------------------------- |
---|
267 | // return path where tar file is stored |
---|
268 | // ----------------------------------------------------- |
---|
269 | return returnPath; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * Déterminer si ce fichier appartient à une archive. |
---|
274 | */ |
---|
275 | public boolean isArchive( ){ return tarfile != null; } |
---|
276 | |
---|
277 | public static void main( String[] args ) |
---|
278 | {} |
---|
279 | } |
---|