1 /**
2 * The Subversion Authentication Parse Module (SAPM for short).
3 *
4 * Copyright (c) 2010, 2011 by SoftwareEntwicklung Beratung Schulung (SoEBeS)
5 * Copyright (c) 2010, 2011 by Karl Heinz Marbaise
6 *
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package com.soebes.subversion.sapm;
23
24 /**
25 * This class will support you to extract the <code>extension</code> of
26 * a file, the <code>path</code> and the
27 * <code>filename incl. the extension</code>.
28 *
29 * We define a extension as one of the following:
30 * <ul>
31 * <li>.tar.gz</li>
32 * </ul>
33 *
34 * @author Karl Heinz Marbaise
35 *
36 */
37 public class FileName {
38
39 private String name;
40 private String ext;
41 private String baseName;
42 private String path;
43 private String nameWithoutExtension;
44
45 public FileName(String fileName, boolean isDir) {
46 init(fileName, isDir);
47 }
48
49 public FileName(String fileName) {
50 init(fileName, false);
51 }
52
53 private void init(String fileName, boolean isDir) {
54 setExt("");
55 setName("");
56 setBaseName("");
57 setNameWithoutExtension("");
58 setPath("");
59 if (!isDir) {
60 if (fileName.endsWith("/")) {
61 isDir = true;
62 }
63 }
64
65 if (isDir) {
66 if (!fileName.endsWith("/")) {
67 fileName += "/";
68 }
69 setPath(fileName);
70 } else {
71 int pos = fileName.lastIndexOf("/");
72 if (pos >= 0) {
73 setBaseName(fileName.substring(pos + 1));
74 setPath(fileName.substring(0, pos + 1));
75 } else {
76 setBaseName(fileName);
77 }
78 pos = getBaseName().indexOf('.');
79 if (pos >= 0) {
80 setNameWithoutExtension(getBaseName().substring(0, pos));
81 setName(getNameWithoutExtension());
82 setExt(getBaseName().substring(pos + 1));
83 } else {
84 setNameWithoutExtension(getBaseName());
85 setName(getBaseName());
86 }
87 }
88 }
89
90 /**
91 * @return The extension of a filename.
92 */
93 public String getExt() {
94 return ext;
95 }
96
97 public void setExt(String ext) {
98 this.ext = ext;
99 }
100
101 /**
102 * @return The name of a file.
103 */
104 public String getName() {
105 return name;
106 }
107
108 public void setName(String name) {
109 this.name = name;
110 }
111
112 public String getBaseName() {
113 return baseName;
114 }
115
116 public void setBaseName(String baseName) {
117 this.baseName = baseName;
118 }
119
120 public String getPath() {
121 return path;
122 }
123
124 public void setPath(String path) {
125 this.path = path;
126 }
127
128 public String getNameWithoutExtension() {
129 return nameWithoutExtension;
130 }
131
132 public void setNameWithoutExtension(String nameWithoutExtension) {
133 this.nameWithoutExtension = nameWithoutExtension;
134 }
135
136 }