1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
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
32  *
33  *
34  */
35 package convwatch;
36 
37 import helper.URLHelper;
38 import convwatch.FileHelper;
39 import helper.StringHelper;
40 import util.utils;
41 
42 interface Filenamer
43 {
44     public String getSuffix();
45     public String getFileURL();
46     public String getAbsoluteSystemFilename();
47     public String getFilename();
48     public String getSystemPath();
49 }
50 
51 // -----------------------------------------------------------------------------
52 
53 abstract class FilenameHelper_impl implements Filenamer
54 {
55     String fs;                                   // file separator like '/'
56     String m_sPath;
57     String m_sFilename;
58     String m_sSuffix;
59     int    m_nNumber = 0;
60 
61     public String getNumber()
62         {
63             return StringHelper.createValueString(m_nNumber, 4);
64         }
65     public void setNumber(int _n)
66         {
67             m_nNumber = _n;
68         }
69     void initMember()
70         {
71             fs = System.getProperty("file.separator");
72         }
73 
74         /**
75          * initialise a FilenameHelper_impl with a complete filename.
76          * if the filename starts with 'file:///' it is interpret as a file URL
77          *
78          */
79     public FilenameHelper_impl()
80         {
81             initMember();
82         }
83 
84     public void setCompleteFilename(String _sFilename)
85         {
86             if (_sFilename.startsWith("file:///"))
87             {
88                 _sFilename = FileHelper.getSystemPathFromFileURL(_sFilename);
89             }
90 			_sFilename = utils.replaceAll13(_sFilename, "\\\\", "/");
91 
92             String sPath = checkPath(FileHelper.getPath(_sFilename));
93             String sFilenameWithSuffix = checkFilename(FileHelper.getBasename(_sFilename));
94             String sSuffix = splitSuffix(sFilenameWithSuffix);
95 
96             m_sPath = sPath;
97             m_sFilename = FileHelper.getNameNoSuffix(sFilenameWithSuffix);
98             m_sSuffix = sSuffix;
99         }
100 
101         /**
102          * initialise a FilenameHelper_impl with a path a name and a suffix separately
103          */
104     public FilenameHelper_impl(String _sPath, String _sName, String _sSuffix)
105         {
106             initMember();
107 			_sPath = utils.replaceAll13(_sPath, "\\\\", "/");
108 
109             String sPath = checkPath(_sPath);
110             String sFilename = checkFilename(_sName);
111             String sSuffix = checkSuffix(_sSuffix);
112 
113             m_sPath = sPath;
114             m_sFilename = sFilename;
115             m_sSuffix = sSuffix;
116         }
117 
118     /**
119      * @return the current path as a OOo path URL
120      */
121     public String getFileURL()
122         {
123             String sSystemPath = createAbsoluteFilename();
124             String sFileURL = URLHelper.getFileURLFromSystemPath(sSystemPath);
125             return sFileURL;
126         }
127 
128 
129     /**
130      * @return the current path as a system path
131      */
132     public String getAbsoluteSystemFilename()
133         {
134             String sSystemFilename = createAbsoluteFilename();
135             sSystemFilename = utils.replaceAll13(sSystemFilename, "/", fs);
136             return sSystemFilename;
137         }
138 
139     /**
140      * @return the filename without it's suffix
141      */
142     public String getName()
143     {
144         return m_sFilename;
145     }
146     /**
147      * set only the filename, maybe it's is only a directory.
148      */
149     public void setName(String _sName)
150         {
151             m_sFilename = _sName;
152         }
153     public void setPath(String _sName)
154         {
155             m_sPath = _sName;
156         }
157 
158     /**
159      * @return a created name
160      */
161     abstract public String buildName();
162         // {
163         //    return getName();
164         // }
165 
166     /**
167      * @return the complete filename with it's suffix
168      */
169     public String getFilename()
170         {
171             return buildName() + "." + getSuffix();
172         }
173 
174      /**
175       * @return the path as system path
176       */
177     public String getSystemPath()
178     {
179         String sSystemPath = m_sPath;
180         sSystemPath = utils.replaceAll13(sSystemPath, "/", fs);
181         return sSystemPath;
182     }
183     /**
184      * @return true, if current SystemPath is a directory
185      */
186     public boolean isDirectory()
187         {
188             return FileHelper.isDir(getSystemPath());
189         }
190 
191     /**
192      * @return true, if the file really exist.
193      */
194     public boolean exists()
195         {
196             return FileHelper.exists(createAbsoluteFilename());
197         }
198 
199     /**
200      * @return the current suffix
201      */
202     public String getSuffix()
203         {
204             return m_sSuffix;
205         }
206     /**
207      * @return the complete name. Without convert the path separator!
208      */
209     String createAbsoluteFilename()
210         {
211             return m_sPath + fs + getFilename();
212         }
213 
214     /*
215      * remove follows 'file separators'
216      */
217     String checkPath(String _sPath)
218         {
219             String sPath;
220             if (_sPath.endsWith("/") || _sPath.endsWith("\\"))
221             {
222                 sPath = _sPath.substring(0, _sPath.length() - 1);
223             }
224             else
225             {
226                sPath = _sPath;
227             }
228             return sPath;
229         }
230 
231     String checkFilename(String _sFilename)
232         {
233             String sFilename;
234             if (_sFilename.startsWith("/") || _sFilename.startsWith("\\"))
235             {
236                 sFilename = _sFilename.substring(1);
237             }
238             else
239             {
240                sFilename = _sFilename;
241             }
242             return sFilename;
243         }
244 
245     String checkSuffix(String _sSuffix)
246         {
247             String sSuffix;
248             if (_sSuffix.startsWith("."))
249             {
250                 sSuffix = _sSuffix.substring(1);
251             }
252             else
253             {
254                 sSuffix = _sSuffix;
255             }
256             return sSuffix;
257         }
258 
259     String splitSuffix(String _sName)
260         {
261             String sSuffix = FileHelper.getSuffix(_sName);
262             return checkSuffix(sSuffix);
263         }
264 
265     public boolean equals(FilenameHelper_impl _aOtherFN)
266         {
267             String sPath = createAbsoluteFilename();
268             String sPathOther = _aOtherFN.createAbsoluteFilename();
269             if (sPath.equals(sPathOther))
270             {
271                 return true;
272             }
273             return false;
274         }
275 
276 }
277 
278 /**
279  * Original filename
280  */
281 class OriginalFilename  extends FilenameHelper_impl
282 {
283     public String buildName()
284         {
285             return getName();
286         }
287 
288     public OriginalFilename(){}
289     public OriginalFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
290 }
291 
292 /**
293  * Reference from original
294  */
295 class OriginalReferenceFilename extends FilenameHelper_impl
296 {
297     public String getSuffix()
298         {
299             return "prn";
300         }
301     public String buildName()
302         {
303             return getName();
304         }
305     public OriginalReferenceFilename(){}
306     public OriginalReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
307 }
308 
309 /**
310  * picture from reference from original
311  */
312 class OriginalReferencePictureFilename extends FilenameHelper_impl
313 {
314     public String getSuffix()
315         {
316             return "jpg";
317         }
318     public String buildName()
319         {
320             return getName() + "-" + getNumber() + "-ref";
321         }
322     public String getBuildString()
323         {
324             return getName() + "-" + "%04d" + "-ref";
325         }
326 
327     public OriginalReferencePictureFilename(){}
328     public OriginalReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
329 }
330 
331 /**
332  * Reference from OpenOffice.org
333  */
334 class CurrentReferenceFilename extends FilenameHelper_impl
335 {
336     public String getSuffix()
337         {
338             return "ps";
339         }
340     public String buildName()
341         {
342             return getName();
343         }
344 
345     public CurrentReferenceFilename(){}
346     public CurrentReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
347 }
348 
349 /**
350  * picture from reference from OpenOffice.org
351  */
352 class CurrentReferencePictureFilename extends FilenameHelper_impl
353 {
354     public String getSuffix()
355         {
356             return "jpg";
357         }
358     public String buildName()
359         {
360             return getName() + "-" + getNumber() + "-new-ref";
361         }
362     public String getBuildString()
363         {
364             return getName() + "-" + "%04d" + "-new-ref";
365         }
366 
367     public CurrentReferencePictureFilename(){}
368     public CurrentReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
369 }
370 
371 
372 public class FilenameHelper
373 {
374 
375     public static void main(String[] args)
376         {
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());
384 
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");
390 
391 
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\\");
397         }
398 }
399