merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / convwatch / FilenameHelper.java
blob3d49703dc99584ada2e9cb24ba96149372d3ccaa
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 /**
29 * Helper class to hold a Filename or a FileURL
30 * Something like File in Java,
31 * with some more extensions direct to ConvWatch and it's name conventions
35 package convwatch;
37 import helper.URLHelper;
38 import convwatch.FileHelper;
39 import helper.StringHelper;
40 import util.utils;
42 interface Filenamer
44 public String getSuffix();
45 public String getFileURL();
46 public String getAbsoluteSystemFilename();
47 public String getFilename();
48 public String getSystemPath();
51 // -----------------------------------------------------------------------------
53 abstract class FilenameHelper_impl implements Filenamer
55 String fs; // file separator like '/'
56 String m_sPath;
57 String m_sFilename;
58 String m_sSuffix;
59 int m_nNumber = 0;
61 public String getNumber()
63 return StringHelper.createValueString(m_nNumber, 4);
65 public void setNumber(int _n)
67 m_nNumber = _n;
69 void initMember()
71 fs = System.getProperty("file.separator");
74 /**
75 * initialise a FilenameHelper_impl with a complete filename.
76 * if the filename starts with 'file:///' it is interpret as a file URL
79 public FilenameHelper_impl()
81 initMember();
84 public void setCompleteFilename(String _sFilename)
86 if (_sFilename.startsWith("file:///"))
88 _sFilename = FileHelper.getSystemPathFromFileURL(_sFilename);
90 _sFilename = utils.replaceAll13(_sFilename, "\\\\", "/");
92 String sPath = checkPath(FileHelper.getPath(_sFilename));
93 String sFilenameWithSuffix = checkFilename(FileHelper.getBasename(_sFilename));
94 String sSuffix = splitSuffix(sFilenameWithSuffix);
96 m_sPath = sPath;
97 m_sFilename = FileHelper.getNameNoSuffix(sFilenameWithSuffix);
98 m_sSuffix = sSuffix;
102 * initialise a FilenameHelper_impl with a path a name and a suffix separately
104 public FilenameHelper_impl(String _sPath, String _sName, String _sSuffix)
106 initMember();
107 _sPath = utils.replaceAll13(_sPath, "\\\\", "/");
109 String sPath = checkPath(_sPath);
110 String sFilename = checkFilename(_sName);
111 String sSuffix = checkSuffix(_sSuffix);
113 m_sPath = sPath;
114 m_sFilename = sFilename;
115 m_sSuffix = sSuffix;
119 * @return the current path as a OOo path URL
121 public String getFileURL()
123 String sSystemPath = createAbsoluteFilename();
124 String sFileURL = URLHelper.getFileURLFromSystemPath(sSystemPath);
125 return sFileURL;
130 * @return the current path as a system path
132 public String getAbsoluteSystemFilename()
134 String sSystemFilename = createAbsoluteFilename();
135 sSystemFilename = utils.replaceAll13(sSystemFilename, "/", fs);
136 return sSystemFilename;
140 * @return the filename without it's suffix
142 public String getName()
144 return m_sFilename;
147 * set only the filename, maybe it's is only a directory.
149 public void setName(String _sName)
151 m_sFilename = _sName;
153 public void setPath(String _sName)
155 m_sPath = _sName;
159 * @return a created name
161 abstract public String buildName();
162 // {
163 // return getName();
164 // }
167 * @return the complete filename with it's suffix
169 public String getFilename()
171 return buildName() + "." + getSuffix();
175 * @return the path as system path
177 public String getSystemPath()
179 String sSystemPath = m_sPath;
180 sSystemPath = utils.replaceAll13(sSystemPath, "/", fs);
181 return sSystemPath;
184 * @return true, if current SystemPath is a directory
186 public boolean isDirectory()
188 return FileHelper.isDir(getSystemPath());
192 * @return true, if the file really exist.
194 public boolean exists()
196 return FileHelper.exists(createAbsoluteFilename());
200 * @return the current suffix
202 public String getSuffix()
204 return m_sSuffix;
207 * @return the complete name. Without convert the path separator!
209 String createAbsoluteFilename()
211 return m_sPath + fs + getFilename();
215 * remove follows 'file separators'
217 String checkPath(String _sPath)
219 String sPath;
220 if (_sPath.endsWith("/") || _sPath.endsWith("\\"))
222 sPath = _sPath.substring(0, _sPath.length() - 1);
224 else
226 sPath = _sPath;
228 return sPath;
231 String checkFilename(String _sFilename)
233 String sFilename;
234 if (_sFilename.startsWith("/") || _sFilename.startsWith("\\"))
236 sFilename = _sFilename.substring(1);
238 else
240 sFilename = _sFilename;
242 return sFilename;
245 String checkSuffix(String _sSuffix)
247 String sSuffix;
248 if (_sSuffix.startsWith("."))
250 sSuffix = _sSuffix.substring(1);
252 else
254 sSuffix = _sSuffix;
256 return sSuffix;
259 String splitSuffix(String _sName)
261 String sSuffix = FileHelper.getSuffix(_sName);
262 return checkSuffix(sSuffix);
265 public boolean equals(FilenameHelper_impl _aOtherFN)
267 String sPath = createAbsoluteFilename();
268 String sPathOther = _aOtherFN.createAbsoluteFilename();
269 if (sPath.equals(sPathOther))
271 return true;
273 return false;
279 * Original filename
281 class OriginalFilename extends FilenameHelper_impl
283 public String buildName()
285 return getName();
288 public OriginalFilename(){}
289 public OriginalFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
293 * Reference from original
295 class OriginalReferenceFilename extends FilenameHelper_impl
297 public String getSuffix()
299 return "prn";
301 public String buildName()
303 return getName();
305 public OriginalReferenceFilename(){}
306 public OriginalReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
310 * picture from reference from original
312 class OriginalReferencePictureFilename extends FilenameHelper_impl
314 public String getSuffix()
316 return "jpg";
318 public String buildName()
320 return getName() + "-" + getNumber() + "-ref";
322 public String getBuildString()
324 return getName() + "-" + "%04d" + "-ref";
327 public OriginalReferencePictureFilename(){}
328 public OriginalReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
332 * Reference from OpenOffice.org
334 class CurrentReferenceFilename extends FilenameHelper_impl
336 public String getSuffix()
338 return "ps";
340 public String buildName()
342 return getName();
345 public CurrentReferenceFilename(){}
346 public CurrentReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
350 * picture from reference from OpenOffice.org
352 class CurrentReferencePictureFilename extends FilenameHelper_impl
354 public String getSuffix()
356 return "jpg";
358 public String buildName()
360 return getName() + "-" + getNumber() + "-new-ref";
362 public String getBuildString()
364 return getName() + "-" + "%04d" + "-new-ref";
367 public CurrentReferencePictureFilename(){}
368 public CurrentReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
372 public class FilenameHelper
375 public static void main(String[] args)
377 OriginalReferenceFilename d = new OriginalReferenceFilename();
378 d.setCompleteFilename("c:\\dir1\\dir2\\name.ext");
379 System.out.println("Suffix: " + d.getSuffix());
380 System.out.println("Path: " + d.getSystemPath());
381 System.out.println("Absolute system path filename: " + d.getAbsoluteSystemFilename());
382 System.out.println("URL: " + d.getFileURL());
383 System.out.println("Filename: " + d.getFilename());
385 OriginalReferenceFilename a = new OriginalReferenceFilename("/dir1/dir2/", "name",".ext");
386 OriginalReferenceFilename a1 = new OriginalReferenceFilename("/dir1/dir2","name.ext","");
387 OriginalReferenceFilename a2 = new OriginalReferenceFilename("/dir1/dir2","/name.ext","");
388 OriginalReferenceFilename a3 = new OriginalReferenceFilename("/dir1/dir2","/name",".ext");
389 OriginalReferenceFilename a4 = new OriginalReferenceFilename("/dir1/dir2","name","ext");
392 // OriginalReferenceFilename b = new OriginalReferenceFilename("c:/dir1/dir2/name.ext");
393 // OriginalReferenceFilename c = new OriginalReferenceFilename("file:///dir1/dir2/name.ext");
394 // OriginalReferenceFilename e = new OriginalReferenceFilename("c:\\dir1\\dir2\\name");
395 // OriginalReferenceFilename f = new OriginalReferenceFilename("c:\\dir1\\dir2");
396 // OriginalReferenceFilename g = new OriginalReferenceFilename("c:\\dir1\\dir2\\");