BrightSide Workbench Full Report + Source Code
WebResourceParts.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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 
19 package org.turro.elephant.web.context;
20 
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.util.Date;
24 import java.util.List;
25 import org.turro.string.ObjectString;
26 import org.turro.string.Strings;
27 import org.turro.action.Contacts;
28 import org.turro.plugin.contacts.IContact;
29 
34 public class WebResourceParts {
35 
36  private static final String
37  RESOURCE_EXT = "\\.(jsp|html|htm|txt|zul|zhtml)$",
38  EDITABLE_EXT = "\\.(wiki)$",
39  LOCALE = "((?:_[a-zA-Z]{2})?(?:_[a-zA-Z]{2})?(?:_[a-zA-Z]{2})?)",
40  BASE = "^([^\\.\\_]+)",
41  MODIFIED = "\\.([0-9]+)",
42  AUTHOR = "\\.([^\\.\\_]+)",
43  RESOURCE = BASE + LOCALE + RESOURCE_EXT,
44  RESOURCE_GLOBAL = BASE + RESOURCE_EXT,
45  EDITABLE = BASE + MODIFIED + AUTHOR + LOCALE + EDITABLE_EXT;
46 
47  private String base, locale, extension, author, modified;
48  private Date modifiedDate;
49 
50  public WebResourceParts(File file) {
51  matchFile(file);
52  }
53 
54  public String getBase() {
55  return base;
56  }
57 
58  public String getLocale() {
59  return locale;
60  }
61 
62  public String getExtension() {
63  return extension;
64  }
65 
66  public String getAuthor() {
67  return author;
68  }
69 
70  public String getModified() {
71  return modified;
72  }
73 
74  public Date getModifiedDate() {
75  return modifiedDate;
76  }
77 
78  /* Utils */
79 
80  public boolean isEmpty() {
81  return Strings.isBlank(base);
82  }
83 
84  public boolean isResource() {
85  return !isEmpty() && Strings.isBlank(modified);
86  }
87 
88  public boolean isEditable() {
89  return !isEmpty() && !Strings.isBlank(modified);
90  }
91 
92  public String nameAsResource() {
93  return base + locale + ".html";
94  }
95 
96  public String nameAsEditable(IContact contact) {
97  return base + "." + modified + "." + contact.getId() + locale + ".wiki";
98  }
99 
100  public File getEditableFile(Path folder, IContact contact) {
101  return Path.of(folder.toString(), nameAsEditable(contact)).toFile();
102  }
103 
104  public File getResourceFile(Path folder) {
105  return Path.of(folder.toString(), nameAsResource()).toFile();
106  }
107 
108  public IContact getUser() {
109  return Contacts.getContactById(author);
110  }
111 
112  public static WebResourceParts editable(IContact contact, String base, String locale) {
113  WebResourceParts wrp = new WebResourceParts(null);
114  if(Strings.isBlank(base)) {
115  wrp.base = "000" + ObjectString.formatObject(new Date(),
116  ObjectString.COMPRESSED_DATE_PATTERN, false);
117  } else {
118  wrp.base = base;
119  }
120  wrp.modified = ObjectString.formatObject(new Date(),
121  ObjectString.COMPRESSED_DATE_PATTERN, false);
122  wrp.author = contact.getId();
123  wrp.locale = locale;
124  wrp.extension = "wiki";
125  return wrp;
126  }
127 
128  private void matchFile(File file) {
129  if(file == null) return;
130  String fileName = file.getName();
131  if(fileName.matches(RESOURCE)) {
132  List<String> parts = Strings.extractAllGroups(fileName, RESOURCE);
133  if(parts.size() == 3) {
134  base = parts.get(0);
135  locale = parts.get(1);
136  extension = parts.get(2);
137  }
138  } else if(fileName.matches(RESOURCE_GLOBAL)) {
139  List<String> parts = Strings.extractAllGroups(fileName, RESOURCE_GLOBAL);
140  if(parts.size() == 2) {
141  base = parts.get(0);
142  locale = "";
143  extension = parts.get(1);
144  }
145  } else if(fileName.matches(EDITABLE)) {
146  List<String> parts = Strings.extractAllGroups(fileName, EDITABLE);
147  if(parts.size() == 5) {
148  base = parts.get(0);
149  modified = parts.get(1);
150  author = parts.get(2);
151  locale = parts.get(3);
152  extension = parts.get(4);
153  modifiedDate = (Date) ObjectString.parseString(modified,
154  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
155  }
156  }
157  }
158 
159 }
static IContact getContactById(String id)
Definition: Contacts.java:72
File getEditableFile(Path folder, IContact contact)
static WebResourceParts editable(IContact contact, String base, String locale)