1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FilenameHelper.java,v $
10 * $Revision: 1.5.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 * Helper class to hold a Filename or a FileURL
33 * Something like File in Java,
34 * with some more extensions direct to ConvWatch and it's name conventions
40 import helper
.URLHelper
;
41 import convwatch
.FileHelper
;
42 import helper
.StringHelper
;
47 public String
getSuffix();
48 public String
getFileURL();
49 public String
getAbsoluteSystemFilename();
50 public String
getFilename();
51 public String
getSystemPath();
54 // -----------------------------------------------------------------------------
56 abstract class FilenameHelper_impl
implements Filenamer
58 String fs
; // file separator like '/'
64 public String
getNumber()
66 return StringHelper
.createValueString(m_nNumber
, 4);
68 public void setNumber(int _n
)
74 fs
= System
.getProperty("file.separator");
78 * initialise a FilenameHelper_impl with a complete filename.
79 * if the filename starts with 'file:///' it is interpret as a file URL
82 public FilenameHelper_impl()
87 public void setCompleteFilename(String _sFilename
)
89 if (_sFilename
.startsWith("file:///"))
91 _sFilename
= FileHelper
.getSystemPathFromFileURL(_sFilename
);
93 _sFilename
= utils
.replaceAll13(_sFilename
, "\\\\", "/");
95 String sPath
= checkPath(FileHelper
.getPath(_sFilename
));
96 String sFilenameWithSuffix
= checkFilename(FileHelper
.getBasename(_sFilename
));
97 String sSuffix
= splitSuffix(sFilenameWithSuffix
);
100 m_sFilename
= FileHelper
.getNameNoSuffix(sFilenameWithSuffix
);
105 * initialise a FilenameHelper_impl with a path a name and a suffix separately
107 public FilenameHelper_impl(String _sPath
, String _sName
, String _sSuffix
)
110 _sPath
= utils
.replaceAll13(_sPath
, "\\\\", "/");
112 String sPath
= checkPath(_sPath
);
113 String sFilename
= checkFilename(_sName
);
114 String sSuffix
= checkSuffix(_sSuffix
);
117 m_sFilename
= sFilename
;
122 * @return the current path as a OOo path URL
124 public String
getFileURL()
126 String sSystemPath
= createAbsoluteFilename();
127 String sFileURL
= URLHelper
.getFileURLFromSystemPath(sSystemPath
);
133 * @return the current path as a system path
135 public String
getAbsoluteSystemFilename()
137 String sSystemFilename
= createAbsoluteFilename();
138 sSystemFilename
= utils
.replaceAll13(sSystemFilename
, "/", fs
);
139 return sSystemFilename
;
143 * @return the filename without it's suffix
145 public String
getName()
150 * set only the filename, maybe it's is only a directory.
152 public void setName(String _sName
)
154 m_sFilename
= _sName
;
156 public void setPath(String _sName
)
162 * @return a created name
164 abstract public String
buildName();
170 * @return the complete filename with it's suffix
172 public String
getFilename()
174 return buildName() + "." + getSuffix();
178 * @return the path as system path
180 public String
getSystemPath()
182 String sSystemPath
= m_sPath
;
183 sSystemPath
= utils
.replaceAll13(sSystemPath
, "/", fs
);
187 * @return true, if current SystemPath is a directory
189 public boolean isDirectory()
191 return FileHelper
.isDir(getSystemPath());
195 * @return true, if the file really exist.
197 public boolean exists()
199 return FileHelper
.exists(createAbsoluteFilename());
203 * @return the current suffix
205 public String
getSuffix()
210 * @return the complete name. Without convert the path separator!
212 String
createAbsoluteFilename()
214 return m_sPath
+ fs
+ getFilename();
218 * remove follows 'file separators'
220 String
checkPath(String _sPath
)
223 if (_sPath
.endsWith("/") || _sPath
.endsWith("\\"))
225 sPath
= _sPath
.substring(0, _sPath
.length() - 1);
234 String
checkFilename(String _sFilename
)
237 if (_sFilename
.startsWith("/") || _sFilename
.startsWith("\\"))
239 sFilename
= _sFilename
.substring(1);
243 sFilename
= _sFilename
;
248 String
checkSuffix(String _sSuffix
)
251 if (_sSuffix
.startsWith("."))
253 sSuffix
= _sSuffix
.substring(1);
262 String
splitSuffix(String _sName
)
264 String sSuffix
= FileHelper
.getSuffix(_sName
);
265 return checkSuffix(sSuffix
);
268 public boolean equals(FilenameHelper_impl _aOtherFN
)
270 String sPath
= createAbsoluteFilename();
271 String sPathOther
= _aOtherFN
.createAbsoluteFilename();
272 if (sPath
.equals(sPathOther
))
284 class OriginalFilename
extends FilenameHelper_impl
286 public String
buildName()
291 public OriginalFilename(){}
292 public OriginalFilename(String _path
, String _filename
, String _ext
) { super(_path
, _filename
, _ext
);}
296 * Reference from original
298 class OriginalReferenceFilename
extends FilenameHelper_impl
300 public String
getSuffix()
304 public String
buildName()
308 public OriginalReferenceFilename(){}
309 public OriginalReferenceFilename(String _path
, String _filename
, String _ext
) { super(_path
, _filename
, _ext
);}
313 * picture from reference from original
315 class OriginalReferencePictureFilename
extends FilenameHelper_impl
317 public String
getSuffix()
321 public String
buildName()
323 return getName() + "-" + getNumber() + "-ref";
325 public String
getBuildString()
327 return getName() + "-" + "%04d" + "-ref";
330 public OriginalReferencePictureFilename(){}
331 public OriginalReferencePictureFilename(String _path
, String _filename
, String _ext
) { super(_path
, _filename
, _ext
);}
335 * Reference from OpenOffice.org
337 class CurrentReferenceFilename
extends FilenameHelper_impl
339 public String
getSuffix()
343 public String
buildName()
348 public CurrentReferenceFilename(){}
349 public CurrentReferenceFilename(String _path
, String _filename
, String _ext
) { super(_path
, _filename
, _ext
);}
353 * picture from reference from OpenOffice.org
355 class CurrentReferencePictureFilename
extends FilenameHelper_impl
357 public String
getSuffix()
361 public String
buildName()
363 return getName() + "-" + getNumber() + "-new-ref";
365 public String
getBuildString()
367 return getName() + "-" + "%04d" + "-new-ref";
370 public CurrentReferencePictureFilename(){}
371 public CurrentReferencePictureFilename(String _path
, String _filename
, String _ext
) { super(_path
, _filename
, _ext
);}
375 public class FilenameHelper
378 public static void main(String
[] args
)
380 OriginalReferenceFilename d
= new OriginalReferenceFilename();
381 d
.setCompleteFilename("c:\\dir1\\dir2\\name.ext");
382 System
.out
.println("Suffix: " + d
.getSuffix());
383 System
.out
.println("Path: " + d
.getSystemPath());
384 System
.out
.println("Absolute system path filename: " + d
.getAbsoluteSystemFilename());
385 System
.out
.println("URL: " + d
.getFileURL());
386 System
.out
.println("Filename: " + d
.getFilename());
388 OriginalReferenceFilename a
= new OriginalReferenceFilename("/dir1/dir2/", "name",".ext");
389 OriginalReferenceFilename a1
= new OriginalReferenceFilename("/dir1/dir2","name.ext","");
390 OriginalReferenceFilename a2
= new OriginalReferenceFilename("/dir1/dir2","/name.ext","");
391 OriginalReferenceFilename a3
= new OriginalReferenceFilename("/dir1/dir2","/name",".ext");
392 OriginalReferenceFilename a4
= new OriginalReferenceFilename("/dir1/dir2","name","ext");
395 // OriginalReferenceFilename b = new OriginalReferenceFilename("c:/dir1/dir2/name.ext");
396 // OriginalReferenceFilename c = new OriginalReferenceFilename("file:///dir1/dir2/name.ext");
397 // OriginalReferenceFilename e = new OriginalReferenceFilename("c:\\dir1\\dir2\\name");
398 // OriginalReferenceFilename f = new OriginalReferenceFilename("c:\\dir1\\dir2");
399 // OriginalReferenceFilename g = new OriginalReferenceFilename("c:\\dir1\\dir2\\");