BrightSide Workbench Full Report + Source Code
FileProperties.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.context.vc;
19 
20 import java.io.File;
21 import java.util.Date;
22 import org.turro.string.ObjectString;
23 
28 public class FileProperties {
29  public static final String
30  VALID_EXT_REGEXP = "(jsp|html|htm|txt)",
31  VALID_LOCALE_REGEXP = "(_[a-zA-Z]{2})?(_[a-zA-Z]{2})?(_[a-zA-Z]{2})?";
32  private File file;
33  private String fullName, name, extension, lang, author, version;
34  private Date creation, modification;
35  private boolean lastEntry = false;
36 
37 
39  public FileProperties(File file) {
40  this.file = file;
41  createFields();
42  }
43 
44  private void createFields() {
45  fullName = file.getName();
46  String parts[] = fullName.split("\\.");
47  int lParts = parts.length;
48  if(lParts > 1) {
49  extension = parts[lParts - 1];
50  if(parts[lParts - 2].indexOf('_') > -1) {
51  // extract langs
52  lang = "";
53  String rest = "";
54  String tmp[] = parts[lParts - 2].split("\\_");
55  for(int i = tmp.length - 1; i > 0; i--) {
56  if(tmp[i].length() == 2) {
57  lang = "_" + tmp[i] + getLang();
58  }
59  else {
60  rest = "_" + tmp[i] + rest;
61  }
62  }
63  parts[lParts - 2] = tmp[0] + rest;
64  }
65  // set name
66  name = parts[0];
67  if(name.matches("[0-9]*")) {
68  creation = (Date) ObjectString.parseString(name.substring(3),
69  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
70  }
71  if(lParts > 3) {
72  version = parts[1];
73  author = parts[2];
74  if(version.matches("[0-9]*")) {
75  modification = (Date) ObjectString.parseString(version,
76  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
77  }
78  }
79  }
80  }
81 
82  public String getFullName() {
83  return fullName;
84  }
85 
86  public String getName() {
87  return name;
88  }
89 
90  public String getExtension() {
91  return extension;
92  }
93 
94  public String getLang() {
95  return lang;
96  }
97 
98  public String getAuthor() {
99  return author;
100  }
101 
102  public String getVersion() {
103  return version;
104  }
105 
106  public Date getCreation() {
107  return creation;
108  }
109 
110  public Date getModification() {
111  return modification;
112  }
113 
114  public boolean isLastEntry() {
115  return lastEntry;
116  }
117 
118  public void setLastEntry(boolean lastEntry) {
119  this.lastEntry = lastEntry;
120  }
121 
122  public boolean isRenderedVersion() {
123  return extension.matches(VALID_EXT_REGEXP);
124  }
125 
126  public boolean isWiki() {
127  return "wiki".equals(extension);
128  }
129 
130 }