summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/convwatch/FilenameHelper.java
blob: c26e8ee9992113bfdbf65c9e074ed92534ac56ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

/**
 * Helper class to hold a Filename or a FileURL
 * Something like File in Java,
 * with some more extensions direct to ConvWatch and its name conventions
 *
 *
 */
package convwatch;

import helper.URLHelper;
import convwatch.FileHelper;
import helper.StringHelper;

interface Filenamer
{
    public String getSuffix();
    public String getFileURL();
    public String getAbsoluteSystemFilename();
    public String getFilename();
    public String getSystemPath();
}



abstract class FilenameHelper_impl implements Filenamer
{
    private String fs;                                   // file separator like '/'
    private String m_sPath;
    private String m_sFilename;
    private String m_sSuffix;
    private int    m_nNumber = 0;

    public String getNumber()
        {
            return StringHelper.createValueString(m_nNumber, 4);
        }
    public void setNumber(int _n)
        {
            m_nNumber = _n;
        }
    void initMember()
        {
            fs = System.getProperty("file.separator");
        }

        /**
         * initialise a FilenameHelper_impl with a complete filename.
         * if the filename starts with 'file:///' it is interpret as a file URL
         *
         */
    public FilenameHelper_impl()
        {
            initMember();
        }

    public void setCompleteFilename(String _sFilename)
        {
            if (_sFilename.startsWith("file:///"))
            {
                _sFilename = FileHelper.getSystemPathFromFileURL(_sFilename);
            }
            _sFilename = _sFilename.replace("\\\\", "/");

            String sPath = checkPath(FileHelper.getPath(_sFilename));
            String sFilenameWithSuffix = checkFilename(FileHelper.getBasename(_sFilename));
            String sSuffix = splitSuffix(sFilenameWithSuffix);

            m_sPath = sPath;
            m_sFilename = FileHelper.getNameNoSuffix(sFilenameWithSuffix);
            m_sSuffix = sSuffix;
        }

        /**
         * initialise a FilenameHelper_impl with a path a name and a suffix separately
         */
    public FilenameHelper_impl(String _sPath, String _sName, String _sSuffix)
        {
            initMember();
            _sPath = _sPath.replace("\\\\", "/");

            String sPath = checkPath(_sPath);
            String sFilename = checkFilename(_sName);
            String sSuffix = checkSuffix(_sSuffix);

            m_sPath = sPath;
            m_sFilename = sFilename;
            m_sSuffix = sSuffix;
        }

    /**
     * @return the current path as a OOo path URL
     */
    public String getFileURL()
        {
            String sSystemPath = createAbsoluteFilename();
            String sFileURL = URLHelper.getFileURLFromSystemPath(sSystemPath);
            return sFileURL;
        }


    /**
     * @return the current path as a system path
     */
    public String getAbsoluteSystemFilename()
        {
            String sSystemFilename = createAbsoluteFilename();
            sSystemFilename = sSystemFilename.replace("/", fs);
            return sSystemFilename;
        }

    /**
     * @return the filename without it's suffix
     */
    public String getName()
    {
        return m_sFilename;
    }
    /**
     * set only the filename, maybe it's is only a directory.
     */
    public void setName(String _sName)
        {
            m_sFilename = _sName;
        }
    public void setPath(String _sName)
        {
            m_sPath = _sName;
        }

    /**
     * @return a created name
     */
    abstract public String buildName();

    /**
     * @return the complete filename with it's suffix
     */
    public String getFilename()
        {
            return buildName() + "." + getSuffix();
        }

     /**
      * @return the path as system path
      */
    public String getSystemPath()
    {
        String sSystemPath = m_sPath;
        sSystemPath = sSystemPath.replace("/", fs);
        return sSystemPath;
    }
    /**
     * @return true, if current SystemPath is a directory
     */
    public boolean isDirectory()
        {
            return FileHelper.isDir(getSystemPath());
        }

    /**
     * @return true, if the file really exist.
     */
    public boolean exists()
        {
            return FileHelper.exists(createAbsoluteFilename());
        }

    /**
     * @return the current suffix
     */
    public String getSuffix()
        {
            return m_sSuffix;
        }
    /**
     * @return the complete name. Without convert the path separator!
     */
    String createAbsoluteFilename()
        {
            return m_sPath + fs + getFilename();
        }

    /*
     * remove follows 'file separators'
     */
    String checkPath(String _sPath)
        {
            String sPath;
            if (_sPath.endsWith("/") || _sPath.endsWith("\\"))
            {
                sPath = _sPath.substring(0, _sPath.length() - 1);
            }
            else
            {
               sPath = _sPath;
            }
            return sPath;
        }

    String checkFilename(String _sFilename)
        {
            String sFilename;
            if (_sFilename.startsWith("/") || _sFilename.startsWith("\\"))
            {
                sFilename = _sFilename.substring(1);
            }
            else
            {
               sFilename = _sFilename;
            }
            return sFilename;
        }

    String checkSuffix(String _sSuffix)
        {
            String sSuffix;
            if (_sSuffix.startsWith("."))
            {
                sSuffix = _sSuffix.substring(1);
            }
            else
            {
                sSuffix = _sSuffix;
            }
            return sSuffix;
        }

    String splitSuffix(String _sName)
        {
            String sSuffix = FileHelper.getSuffix(_sName);
            return checkSuffix(sSuffix);
        }

    public boolean equals(FilenameHelper_impl _aOtherFN)
        {
            String sPath = createAbsoluteFilename();
            String sPathOther = _aOtherFN.createAbsoluteFilename();
            if (sPath.equals(sPathOther))
            {
                return true;
            }
            return false;
        }

}

/**
 * Original filename
 */
class OriginalFilename  extends FilenameHelper_impl
{
    @Override
    public String buildName()
        {
            return getName();
        }

    public OriginalFilename(){}
    public OriginalFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
}

/**
 * Reference from original
 */
class OriginalReferenceFilename extends FilenameHelper_impl
{
    @Override
    public String getSuffix()
        {
            return "prn";
        }
    @Override
    public String buildName()
        {
            return getName();
        }
    public OriginalReferenceFilename(){}
    public OriginalReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
}

/**
 * picture from reference from original
 */
class OriginalReferencePictureFilename extends FilenameHelper_impl
{
    @Override
    public String getSuffix()
        {
            return "jpg";
        }
    @Override
    public String buildName()
        {
            return getName() + "-" + getNumber() + "-ref";
        }
    public String getBuildString()
        {
            return getName() + "-" + "%04d" + "-ref";
        }

    public OriginalReferencePictureFilename(){}
    public OriginalReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
}

/**
 * Reference from OpenOffice.org
 */
class CurrentReferenceFilename extends FilenameHelper_impl
{
    @Override
    public String getSuffix()
        {
            return "ps";
        }
    @Override
    public String buildName()
        {
            return getName();
        }

    public CurrentReferenceFilename(){}
    public CurrentReferenceFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
}

/**
 * picture from reference from OpenOffice.org
 */
class CurrentReferencePictureFilename extends FilenameHelper_impl
{
    @Override
    public String getSuffix()
        {
            return "jpg";
        }
    @Override
    public String buildName()
        {
            return getName() + "-" + getNumber() + "-new-ref";
        }
    public String getBuildString()
        {
            return getName() + "-" + "%04d" + "-new-ref";
        }

    public CurrentReferencePictureFilename(){}
    public CurrentReferencePictureFilename(String _path, String _filename, String _ext) { super(_path, _filename, _ext);}
}


public class FilenameHelper
{

    public static void main(String[] args)
        {
            OriginalReferenceFilename d = new OriginalReferenceFilename();
            d.setCompleteFilename("c:\\dir1\\dir2\\name.ext");
            System.out.println("Suffix: " + d.getSuffix());
            System.out.println("Path: " + d.getSystemPath());
            System.out.println("Absolute system path filename: " + d.getAbsoluteSystemFilename());
            System.out.println("URL: " + d.getFileURL());
            System.out.println("Filename: " + d.getFilename());

            new OriginalReferenceFilename("/dir1/dir2/", "name",".ext");
            new OriginalReferenceFilename("/dir1/dir2","name.ext","");
            new OriginalReferenceFilename("/dir1/dir2","/name.ext","");
            new OriginalReferenceFilename("/dir1/dir2","/name",".ext");
            new OriginalReferenceFilename("/dir1/dir2","name","ext");

        }
}