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