BrightSide Workbench Full Report + Source Code
CommentParser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.dossier.issue.parser;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.StringJoiner;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import java.util.stream.Collectors;
27 import org.turro.string.Strings;
28 import org.turro.elephant.context.Application;
29 
34 public class CommentParser {
35 
36  private final StringJoiner changes, comment, participants, attachments, relations,
37  endBlock, upload, description, priority, type, grouping, dossier,
38  version, publishable, control, delivery, startDate, status,
39  resolution, duplicated, hours, expenses, price, prevision;
40  private final String text, proppart, attachpart;
41 
42  public CommentParser(String text) {
43  this.text = text;
44  changes = toRegExp(Application.getAllLocales("Changes"));
45  comment = toRegExp(Application.getAllLocales("Comment"));
46  participants = toRegExp(Application.getAllLocales("Participants"));
47  attachments = toRegExp(Application.getAllLocales("Attachments"));
48  relations = toRegExp(Application.getAllLocales("Relations"));
49  endBlock = new StringJoiner("|");
50  endBlock.merge(changes)
51  .merge(comment)
52  .merge(participants)
53  .merge(attachments)
54  .merge(relations);
55  upload = toRegExp(Application.getAllLocales("Upload"));
56  description = toRegExp(Application.getAllLocales("Description"));
57  priority = toRegExp(Application.getAllLocales("Priority"));
58  type = toRegExp(Application.getAllLocales("Type"));
59  grouping = toRegExp(Application.getAllLocales("Grouping"));
60  dossier = toRegExp(Application.getAllLocales("Dossier"));
61  version = toRegExp(Application.getAllLocales("Version"));
62  publishable = toRegExp(Application.getAllLocales("Publishable"));
63  control = toRegExp(Application.getAllLocales("Control"));
64  delivery = toRegExp(Application.getAllLocales("Delivery"));
65  startDate = toRegExp(Application.getAllLocales("Start date"));
66  status = toRegExp(Application.getAllLocales("Status"));
67  resolution = toRegExp(Application.getAllLocales("Resolution"));
68  duplicated = toRegExp(Application.getAllLocales("Duplicated"));
69  hours = toRegExp(Application.getAllLocales("Hours"));
70  expenses = toRegExp(Application.getAllLocales("Expenses"));
71  price = toRegExp(Application.getAllLocales("Price"));
72  prevision = toRegExp(Application.getAllLocales("Prevision"));
73  proppart = getText(changes);
74  attachpart = getText(attachments);
75  }
76 
77  public String getComment() {
78  return getText(comment);
79  }
80 
81  public List<PairValue> getParticipants() {
82  String block = getText(participants);
83  if(!Strings.isBlank(block)) {
84  return Pattern.compile("\\R*(.+)\\s*:\\s*(.+)(\\R|$)")
85  .matcher(block)
86  .results()
87  .map((m) -> new PairValue(m.group(1), m.group(2)))
88  .collect(Collectors.toList());
89  }
90  return Collections.EMPTY_LIST;
91  }
92 
93  public List<String> getAttachments() {
94  return Strings.isBlank(attachpart) ? null : Pattern.compile("\\R(" + upload + ")\\s*\\[\\s*(.+)\\](\\R|$)")
95  .matcher(attachpart)
96  .results()
97  .map((m) -> m.group(2))
98  .collect(Collectors.toList());
99  }
100 
102  return getPairValue(description);
103  }
104 
106  return getPairValue(priority);
107  }
108 
109  public PairValue getType() {
110  return getPairValue(type);
111  }
112 
114  return getPairValue(grouping);
115  }
116 
118  return getPairValue(dossier);
119  }
120 
122  return getPairValue(version);
123  }
124 
126  return getPairValue(publishable);
127  }
128 
130  return getPairValue(control);
131  }
132 
134  return getPairValue(delivery);
135  }
136 
138  return getPairValue(startDate);
139  }
140 
141  public PairValue getStatus() {
142  return getPairValue(status);
143  }
144 
146  return getPairValue(resolution);
147  }
148 
150  return getPairValue(duplicated);
151  }
152 
153  public String getHours() {
154  return getSingleValue(hours);
155  }
156 
157  public String getExpenses() {
158  return getSingleValue(expenses);
159  }
160 
161  public String getPrice() {
162  return getSingleValue(price);
163  }
164 
166  return getPairValue(prevision, hours, "/");
167  }
168 
170  return getPairValue(prevision, expenses, "/");
171  }
172 
174  return getPairValue(prevision, price, "/");
175  }
176 
177  public String getText(StringJoiner block) {
178  int idxStart = 0, idxEnd;
179  Pattern pstart = Pattern.compile("(\\R|^)(" + block + ")\\R");
180  Matcher mstart = pstart.matcher(text);
181  if(mstart.find()) {
182  idxStart = mstart.end() - 1;
183  }
184  if(idxStart > 0) {
185  Pattern pend = Pattern.compile("\\R((" + endBlock + ")\\R|$)");
186  Matcher mend = pend.matcher(text);
187  if(mend.find(idxStart + 1)) {
188  idxEnd = mend.start();
189  } else {
190  idxEnd = text.length();
191  }
192  if(idxEnd > idxStart) {
193  return text.substring(idxStart, idxEnd);
194  }
195  }
196  return null;
197  }
198 
199  public String getSingleValue(StringJoiner options) {
200  if(!Strings.isBlank(proppart)) {
201  Pattern pattern = Pattern.compile("\\R(" + options + ")\\s*:\\s*(.+)(\\R|$)");
202  Matcher matcher = pattern.matcher(proppart);
203  if(matcher.find()) {
204  return matcher.group(2);
205  }
206  }
207  return null;
208  }
209 
210  public PairValue getPairValue(StringJoiner options) {
211  if(!Strings.isBlank(proppart)) {
212  Pattern pattern = Pattern.compile("\\R(" + options + ")\\s*\\[\\s*(.*)\\s*\\]:\\h*(.*)(\\R|$)");
213  Matcher matcher = pattern.matcher(proppart);
214  if(matcher.find()) {
215  return new PairValue(matcher.group(2), matcher.group(3));
216  }
217  }
218  return null;
219  }
220 
221  public PairValue getPairValue(StringJoiner prefix, StringJoiner options, String separator) {
222  if(!Strings.isBlank(proppart)) {
223  Pattern pattern = Pattern.compile("\\R((" + prefix + ")" + separator + "(" + options + "))\\s*\\[\\s*(.*)\\s*\\]:\\h*(.*)(\\R|$)");
224  Matcher matcher = pattern.matcher(proppart);
225  if(matcher.find()) {
226  return new PairValue(matcher.group(4), matcher.group(5));
227  }
228  }
229  return null;
230  }
231 
232  private StringJoiner toRegExp(List<String> opts) {
233  StringJoiner sj = new StringJoiner("|");
234  opts.forEach((opt) -> {
235  sj.add(opt);
236  });
237  return sj;
238  }
239 
240 }
PairValue getPairValue(StringJoiner options)
PairValue getPairValue(StringJoiner prefix, StringJoiner options, String separator)
String getSingleValue(StringJoiner options)
static List< String > getAllLocales(String key)