1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /**
25  * Helper class to hold a Filename or a FileURL
26  * Something like File in Java,
27  * with some more extensions direct to ConvWatch and it's name conventions
28  *
29  *
30  */
31 package convwatch;
32 
33 import helper.URLHelper;
34 import convwatch.FileHelper;
35 import helper.StringHelper;
36 import util.utils;
37 
38 interface Filenamer
39 {
getSuffix()40     public String getSuffix();
getFileURL()41     public String getFileURL();
getAbsoluteSystemFilename()42     public String getAbsoluteSystemFilename();
getFilename()43     public String getFilename();
getSystemPath()44     public String getSystemPath();
45 }
46 
47 // -----------------------------------------------------------------------------
48 
49 abstract class FilenameHelper_impl implements Filenamer
50 {
51     String fs;                                   // file separator like '/'
52     String m_sPath;
53     String m_sFilename;
54     String m_sSuffix;
55     int    m_nNumber = 0;
56 
getNumber()57     public String getNumber()
58         {
59             return StringHelper.createValueString(m_nNumber, 4);
60         }
setNumber(int _n)61     public void setNumber(int _n)
62         {
63             m_nNumber = _n;
64         }
initMember()65     void initMember()
66         {
67             fs = System.getProperty("file.separator");
68         }
69 
70         /**
71          * initialise a FilenameHelper_impl with a complete filename.
72          * if the filename starts with 'file:///' it is interpret as a file URL
73          *
74          */
FilenameHelper_impl()75     public FilenameHelper_impl()
76         {
77             initMember();
78         }
79 
setCompleteFilename(String _sFilename)80     public void setCompleteFilename(String _sFilename)
81         {
82             if (_sFilename.startsWith("file:///"))
83             {
84                 _sFilename = FileHelper.getSystemPathFromFileURL(_sFilename);
85             }
86 			_sFilename = utils.replaceAll13(_sFilename, "\\\\", "/");
87 
88             String sPath = checkPath(FileHelper.getPath(_sFilename));
89             String sFilenameWithSuffix = checkFilename(FileHelper.getBasename(_sFilename));
90             String sSuffix = splitSuffix(sFilenameWithSuffix);
91 
92             m_sPath = sPath;
93             m_sFilename = FileHelper.getNameNoSuffix(sFilenameWithSuffix);
94             m_sSuffix = sSuffix;
95         }
96 
97         /**
98          * initialise a FilenameHelper_impl with a path a name and a suffix separately
99          */
FilenameHelper_impl(String _sPath, String _sName, String _sSuffix)100     public FilenameHelper_impl(String _sPath, String _sName, String _sSuffix)
101         {
102             initMember();
103 			_sPath = utils.replaceAll13(_sPath, "\\\\", "/");
104 
105             String sPath = checkPath(_sPath);
106             String sFilename = checkFilename(_sName);
107             String sSuffix = checkSuffix(_sSuffix);
108 
109             m_sPath = sPath;
110             m_sFilename = sFilename;
111             m_sSuffix = sSuffix;
112         }
113 
114     /**
115      * @return the current path as a OOo path URL
116      */
getFileURL()117     public String getFileURL()
118         {
119             String sSystemPath = createAbsoluteFilename();
120             String sFileURL = URLHelper.getFileURLFromSystemPath(sSystemPath);
121             return sFileURL;
122         }
123 
124 
125     /**
126      * @return the current path as a system path
127      */
getAbsoluteSystemFilename()128     public String getAbsoluteSystemFilename()
129         {
130             String sSystemFilename = createAbsoluteFilename();
131             sSystemFilename = utils.replaceAll13(sSystemFilename, "/", fs);
132             return sSystemFilename;
133         }
134 
135     /**
136      * @return the filename without it's suffix
137      */
getName()138     public String getName()
139     {
140         return m_sFilename;
141     }
142     /**
143      * set only the filename, maybe it's is only a directory.
144      */
setName(String _sName)145     public void setName(String _sName)
146         {
147             m_sFilename = _sName;
148         }
setPath(String _sName)149     public void setPath(String _sName)
150         {
151             m_sPath = _sName;
152         }
153 
154     /**
155      * @return a created name
156      */
buildName()157     abstract public String buildName();
158         // {
159         //    return getName();
160         // }
161 
162     /**
163      * @return the complete filename with it's suffix
164      */
getFilename()165     public String getFilename()
166         {
167             return buildName() + "." + getSuffix();
168         }
169 
170      /**
171       * @return the path as system path
172       */
getSystemPath()173     public String getSystemPath()
174     {
175         String sSystemPath = m_sPath;
176         sSystemPath = utils.replaceAll13(sSystemPath, "/", fs);
177         return sSystemPath;
178     }
179     /**
180      * @return true, if current SystemPath is a directory
181      */
isDirectory()182     public boolean isDirectory()
183         {
184             return FileHelper.isDir(getSystemPath());
185         }
186 
187     /**
188      * @return true, if the file really exist.
189      */
exists()190     public boolean exists()
191         {
192             return FileHelper.exists(createAbsoluteFilename());
193         }
194 
195     /**
196      * @return the current suffix
197      */
getSuffix()198     public String getSuffix()
199         {
200             return m_sSuffix;
201         }
202     /**
203      * @return the complete name. Without convert the path separator!
204      */
createAbsoluteFilename()205     String createAbsoluteFilename()
206         {
207             return m_sPath + fs + getFilename();
208         }
209 
210     /*
211      * remove follows 'file separators'
212      */
checkPath(String _sPath)213     String checkPath(String _sPath)
214         {
215             String sPath;
216             if (_sPath.endsWith("/") || _sPath.endsWith("\\"))
217             {
218                 sPath = _sPath.substring(0, _sPath.length() - 1);
219             }
220             else
221             {
222                sPath = _sPath;
223             }
224             return sPath;
225         }
226 
checkFilename(String _sFilename)227     String checkFilename(String _sFilename)
228         {
229             String sFilename;
230             if (_sFilename.startsWith("/") || _sFilename.startsWith("\\"))
231             {
232                 sFilename = _sFilename.substring(1);
233             }
234             else
235             {
236                sFilename = _sFilename;
237             }
238             return sFilename;
239         }
240 
checkSuffix(String _sSuffix)241     String checkSuffix(String _sSuffix)
242         {
243             String sSuffix;
244             if (_sSuffix.startsWith("."))
245             {
246                 sSuffix = _sSuffix.substring(1);
247             }
248             else
249             {
250                 sSuffix = _sSuffix;
251             }
252             return sSuffix;
253         }
254 
splitSuffix(String _sName)255     String splitSuffix(String _sName)
256         {
257             String sSuffix = FileHelper.getSuffix(_sName);
258             return checkSuffix(sSuffix);
259         }
260 
equals(FilenameHelper_impl _aOtherFN)261     public boolean equals(FilenameHelper_impl _aOtherFN)
262         {
263             String sPath = createAbsoluteFilename();
264             String sPathOther = _aOtherFN.createAbsoluteFilename();
265             if (sPath.equals(sPathOther))
266             {
267                 return true;
268             }
269             return false;
270         }
271 
272 }
273 
274 /**
275  * Original filename
276  */
277 class OriginalFilename  extends FilenameHelper_impl
278 {
buildName()279     public String buildName()
280         {
281             return getName();
282         }
283 
OriginalFilename()284     public OriginalFilename(){}
OriginalFilename(String _path, String _filename, String _ext)285     public OriginalFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
286 }
287 
288 /**
289  * Reference from original
290  */
291 class OriginalReferenceFilename extends FilenameHelper_impl
292 {
getSuffix()293     public String getSuffix()
294         {
295             return "prn";
296         }
buildName()297     public String buildName()
298         {
299             return getName();
300         }
OriginalReferenceFilename()301     public OriginalReferenceFilename(){}
OriginalReferenceFilename(String _path, String _filename, String _ext)302     public OriginalReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
303 }
304 
305 /**
306  * picture from reference from original
307  */
308 class OriginalReferencePictureFilename extends FilenameHelper_impl
309 {
getSuffix()310     public String getSuffix()
311         {
312             return "jpg";
313         }
buildName()314     public String buildName()
315         {
316             return getName() + "-" + getNumber() + "-ref";
317         }
getBuildString()318     public String getBuildString()
319         {
320             return getName() + "-" + "%04d" + "-ref";
321         }
322 
OriginalReferencePictureFilename()323     public OriginalReferencePictureFilename(){}
OriginalReferencePictureFilename(String _path, String _filename, String _ext)324     public OriginalReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
325 }
326 
327 /**
328  * Reference from OpenOffice.org
329  */
330 class CurrentReferenceFilename extends FilenameHelper_impl
331 {
getSuffix()332     public String getSuffix()
333         {
334             return "ps";
335         }
buildName()336     public String buildName()
337         {
338             return getName();
339         }
340 
CurrentReferenceFilename()341     public CurrentReferenceFilename(){}
CurrentReferenceFilename(String _path, String _filename, String _ext)342     public CurrentReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
343 }
344 
345 /**
346  * picture from reference from OpenOffice.org
347  */
348 class CurrentReferencePictureFilename extends FilenameHelper_impl
349 {
getSuffix()350     public String getSuffix()
351         {
352             return "jpg";
353         }
buildName()354     public String buildName()
355         {
356             return getName() + "-" + getNumber() + "-new-ref";
357         }
getBuildString()358     public String getBuildString()
359         {
360             return getName() + "-" + "%04d" + "-new-ref";
361         }
362 
CurrentReferencePictureFilename()363     public CurrentReferencePictureFilename(){}
CurrentReferencePictureFilename(String _path, String _filename, String _ext)364     public CurrentReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
365 }
366 
367 
368 public class FilenameHelper
369 {
370 
main(String[] args)371     public static void main(String[] args)
372         {
373             OriginalReferenceFilename d = new OriginalReferenceFilename();
374             d.setCompleteFilename("c:\\dir1\\dir2\\name.ext");
375             System.out.println("Suffix: " + d.getSuffix());
376             System.out.println("Path: " + d.getSystemPath());
377             System.out.println("Absolute system path filename: " + d.getAbsoluteSystemFilename());
378             System.out.println("URL: " + d.getFileURL());
379             System.out.println("Filename: " + d.getFilename());
380 
381             OriginalReferenceFilename a = new OriginalReferenceFilename("/dir1/dir2/", "name",".ext");
382             OriginalReferenceFilename a1 = new OriginalReferenceFilename("/dir1/dir2","name.ext","");
383             OriginalReferenceFilename a2 = new OriginalReferenceFilename("/dir1/dir2","/name.ext","");
384             OriginalReferenceFilename a3 = new OriginalReferenceFilename("/dir1/dir2","/name",".ext");
385             OriginalReferenceFilename a4 = new OriginalReferenceFilename("/dir1/dir2","name","ext");
386 
387 
388             // OriginalReferenceFilename b = new OriginalReferenceFilename("c:/dir1/dir2/name.ext");
389             // OriginalReferenceFilename c = new OriginalReferenceFilename("file:///dir1/dir2/name.ext");
390             // OriginalReferenceFilename e = new OriginalReferenceFilename("c:\\dir1\\dir2\\name");
391             // OriginalReferenceFilename f = new OriginalReferenceFilename("c:\\dir1\\dir2");
392             // OriginalReferenceFilename g = new OriginalReferenceFilename("c:\\dir1\\dir2\\");
393         }
394 }
395