Skip to content

Commit 22ae007

Browse files
EXM-44317, #16: Added an option to limit the number of revised dates.
1 parent ed5166c commit 22ae007

File tree

10 files changed

+349
-20
lines changed

10 files changed

+349
-20
lines changed

i18n/translation.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
<val lang="nl_NL">Map</val>
4242
</key>
4343

44+
<key value="prolog.updater.limit.revised.dates.to">
45+
<comment>The text from check box which enable to limit the added revised dates</comment>
46+
<val lang="en_US">Limit the number of revised dates to</val>
47+
<val lang="de_DE">Limit the number of revised dates to</val>
48+
<val lang="fr_FR">Limit the number of revised dates to</val>
49+
<val lang="ja_JP">Limit the number of revised dates to</val>
50+
<val lang="nl_NL">Limit the number of revised dates to</val>
51+
</key>
4452
<key value="prolog.updater.enable.on.save">
4553
<comment>The text from check box which enable the prolog update</comment>
4654
<val lang="en_US">Enable automatic prolog update on save</val>

src/main/java/com/oxygenxml/prolog/updater/DitaPrologUpdater.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void updateProlog(final WSEditor wsEditor, final boolean isNewDocument) {
5151
WSEditorPage currentPage = wsEditor.getCurrentPage();
5252

5353
// create a PrologContentCreator
54-
PrologContentCreator prologContentCreater = new PrologContentCreator(getAuthorName(), getDateFormat());
54+
PrologContentCreator prologContentCreater = new PrologContentCreator(getAuthorName(), getDateFormat(), getMaxNoOfRevisedElements());
5555

5656
final DitaEditor[] ditaEditor = new DitaEditor[1];
5757
if (currentPage instanceof WSAuthorEditorPage) {
@@ -117,6 +117,24 @@ protected String getDateFormat() {
117117
}
118118

119119
/**
120+
* Get the maximum number of allowed revised elements.
121+
* @return
122+
*/
123+
protected int getMaxNoOfRevisedElements() {
124+
int max = -1;
125+
PluginWorkspace pluginWorkspace = PluginWorkspaceProvider.getPluginWorkspace();
126+
if(pluginWorkspace != null) {
127+
WSOptionsStorage optionsStorage = pluginWorkspace.getOptionsStorage();
128+
String shouldLimit = optionsStorage.getOption(OptionKeys.LIMIT_REVISED_ELEMENTS, String.valueOf(false));
129+
if(Boolean.valueOf(shouldLimit)) {
130+
String value = optionsStorage.getOption(OptionKeys.MAX_REVISED_ELEMENTS, String.valueOf(-1));
131+
max = Integer.valueOf(value);
132+
}
133+
}
134+
return max;
135+
}
136+
137+
/**
120138
* Show a warning message("The prolog wasn't updated") in results manager.
121139
* @param wsEditor The workspace editor access.
122140
*/

src/main/java/com/oxygenxml/prolog/updater/dita/editor/DitaTopicAuthorEditor.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import javax.swing.text.BadLocationException;
6+
import javax.swing.text.Position;
67

78
import org.apache.log4j.Logger;
89

@@ -101,6 +102,7 @@ public boolean updateProlog(boolean isNewDocument) {
101102
// Get the prolog element.
102103
AuthorElement prolog = getPrologElement(rootElement);
103104

105+
documentController.beginCompoundEdit();
104106
try {
105107
if (prolog != null) {
106108
// Prolog element exists; edit this element.
@@ -111,7 +113,9 @@ public boolean updateProlog(boolean isNewDocument) {
111113
}
112114
} catch (AuthorOperationException e) {
113115
toReturn = false;
114-
}
116+
} finally {
117+
documentController.endCompoundEdit();
118+
}
115119
}
116120
} else {
117121
toReturn = false;
@@ -334,9 +338,59 @@ private void addRevisedElement(AuthorElement critdatesElement) throws AuthorOper
334338
}
335339
// Now insert it.
336340
AuthorPageDocumentUtil.insertFragmentSchemaAware(page, documentController, fragment, offset);
341+
deleteExtraRevisedElements(critdatesElement);
337342
}
338343
}
339344

345+
/**
346+
* Delete the first revised elements if the number of them is greater than allowed number.
347+
*
348+
* @param critdatesElement The parent of revised elements.
349+
*/
350+
private void deleteExtraRevisedElements(AuthorElement critdatesElement) {
351+
int noOfAllowedElements = prologCreator.getMaxNoOfRevisedElement();
352+
if(noOfAllowedElements != -1) {
353+
List<AuthorElement> allRevisedElements = AuthorPageDocumentUtil.findElementsByClass(
354+
critdatesElement, XmlElementsConstants.REVISED_DATE_ELEMENT_CLASS);
355+
int noOfElements = allRevisedElements.size();
356+
357+
// Get the caret position
358+
Position caretPosition = null;
359+
if (page instanceof WSAuthorEditorPage) {
360+
try {
361+
caretPosition = documentController.createPositionInContent(
362+
((WSAuthorEditorPage)page).getCaretOffset());
363+
} catch (BadLocationException e) {
364+
logger.error(e, e);
365+
}
366+
}
367+
368+
if(noOfElements > noOfAllowedElements) {
369+
int nuOfElementToDelete = noOfElements - noOfAllowedElements;
370+
for (int i = 0; i < nuOfElementToDelete; i++) {
371+
AuthorElement currentRevised = allRevisedElements.get(i);
372+
// Get the previous node
373+
try {
374+
AuthorNode previousSibling = documentController.getNodeAtOffset(currentRevised.getStartOffset() - 1);
375+
// We will delete the previous sibling if it's a comment
376+
if (previousSibling.getType() == AuthorNode.NODE_TYPE_COMMENT) {
377+
documentController.deleteNode(previousSibling);
378+
}
379+
} catch (BadLocationException e) {
380+
logger.debug(e.getMessage(), e);
381+
}
382+
// Delete the revised element.
383+
documentController.deleteNode(currentRevised);
384+
}
385+
386+
// Restore the caret position
387+
if(caretPosition != null) {
388+
((WSAuthorEditorPage)page).setCaretPosition(caretPosition.getOffset());
389+
}
390+
}
391+
}
392+
}
393+
340394
/**
341395
* Update the document adding the names of the authors.
342396
*

src/main/java/com/oxygenxml/prolog/updater/dita/editor/DitaTopicTextEditor.java

Lines changed: 94 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.oxygenxml.prolog.updater.dita.editor;
22

3+
import javax.swing.text.BadLocationException;
4+
35
import org.apache.log4j.Logger;
46

57
import com.oxygenxml.prolog.updater.prolog.content.PrologContentCreator;
@@ -74,6 +76,7 @@ public boolean updateProlog(boolean isNewDocument) {
7476
boolean toReturn = true;
7577
// get the prolog element
7678
WSXMLTextNodeRange[] prologs;
79+
wsTextEditorPage.beginCompoundUndoableEdit();
7780
try {
7881
prologs = wsTextEditorPage.findElementsByXPath(ElementXPathUtils.getPrologXpath(documentType));
7982
// The document doesn't have a prolog element
@@ -92,7 +95,9 @@ public boolean updateProlog(boolean isNewDocument) {
9295
toReturn = false;
9396
} catch (TextOperationException e) {
9497
toReturn = false;
95-
}
98+
} finally {
99+
wsTextEditorPage.endCompoundUndoableEdit();
100+
}
96101

97102
return toReturn;
98103
}
@@ -205,23 +210,97 @@ private void editCritdates(boolean isNewDocument) throws XPathException, TextOpe
205210
}
206211
} else {
207212
// it's not a new document
208-
// search for revised elements that have local date as modified and have
209-
// contributor as comment
210-
StringBuilder xPathBuilder = new StringBuilder();
211-
xPathBuilder.append(ElementXPathUtils.getCritdatesXpath(documentType));
212-
xPathBuilder.append("/revised[@modified = '" ).append(prologCreator.getLocalDate()).append("']/");
213-
xPathBuilder.append("preceding-sibling::node()[2][.='").append(prologCreator.getAuthor()).append("']");
214-
Object[] revisedElements = wsTextEditorPage.findElementsByXPath(xPathBuilder.toString());
215-
216-
// if the element wasn't found
217-
if (revisedElements.length == 0) {
218-
// add revised xml fragament
219-
TextPageDocumentUtil.insertXmlFragment(wsTextEditorPage, prologCreator.getRevisedDateFragment(documentType),
220-
ElementXPathUtils.getCritdatesXpath(documentType), RelativeInsertPosition.INSERT_LOCATION_AS_LAST_CHILD);
221-
}
213+
addRevisedElement();
222214
}
223215
}
224216

217+
/**
218+
* Add the revised element if it doesn't exits.
219+
*
220+
* @throws XPathException
221+
* @throws TextOperationException
222+
*/
223+
private void addRevisedElement() throws XPathException, TextOperationException{
224+
// Search for revised elements that have local date as modified and
225+
// contributor as comment
226+
StringBuilder xPathBuilder = new StringBuilder();
227+
xPathBuilder.append(ElementXPathUtils.getCritdatesXpath(documentType));
228+
xPathBuilder.append("/revised[@modified = '" ).append(prologCreator.getLocalDate()).append("']/");
229+
xPathBuilder.append("preceding-sibling::node()[2][.='").append(prologCreator.getAuthor()).append("']");
230+
Object[] revisedElements = wsTextEditorPage.findElementsByXPath(xPathBuilder.toString());
231+
232+
// if the element wasn't found
233+
if (revisedElements.length == 0) {
234+
// add revised xml fragament
235+
TextPageDocumentUtil.insertXmlFragment(wsTextEditorPage, prologCreator.getRevisedDateFragment(documentType),
236+
ElementXPathUtils.getCritdatesXpath(documentType), RelativeInsertPosition.INSERT_LOCATION_AS_LAST_CHILD);
237+
238+
deleteExtraRevisedElements();
239+
}
240+
}
241+
242+
/**
243+
* Delete the first revised elements if the number of them is greater than allowed number.
244+
*
245+
* @throws XPathException
246+
*/
247+
private void deleteExtraRevisedElements() throws XPathException {
248+
int noOfAllowedElements = prologCreator.getMaxNoOfRevisedElement();
249+
if(noOfAllowedElements != -1) {
250+
WSXMLTextNodeRange[] allRevisedElements = wsTextEditorPage.findElementsByXPath(
251+
ElementXPathUtils.getCritdatesXpath(documentType) + "/revised");
252+
253+
int noOfElements = allRevisedElements.length;
254+
if(noOfElements > noOfAllowedElements) {
255+
int nuOfElementToDelete = noOfElements - noOfAllowedElements;
256+
// We will delete some revised elements
257+
for (int i = 0; i < nuOfElementToDelete; i++) {
258+
WSXMLTextNodeRange currentElementRange = allRevisedElements[i];
259+
deleteRevisedElement(currentElementRange);
260+
}
261+
}
262+
}
263+
}
264+
265+
/**
266+
* Delete the revised element with the given range.
267+
*
268+
* @param revisedElementRange The range of revised element to delete.
269+
*/
270+
private void deleteRevisedElement(WSXMLTextNodeRange revisedElementRange) {
271+
try {
272+
int startLineOffset = wsTextEditorPage.getOffsetOfLineStart(revisedElementRange.getStartLine());
273+
274+
// We will calculate the offset where we start to delete and the length
275+
int startOffsetToDelete = startLineOffset + (revisedElementRange.getStartColumn() - 1);
276+
int lengthToDelete = revisedElementRange.getEndColumn() - revisedElementRange.getStartColumn();
277+
278+
// Check the text before revised element. This will be deleted if it's whitespace.
279+
String testFromStart = wsTextEditorPage.getDocument().getText(
280+
startLineOffset, revisedElementRange.getStartColumn() - 1);
281+
if(testFromStart.trim().isEmpty()) {
282+
startOffsetToDelete = startLineOffset - 1;
283+
lengthToDelete += testFromStart.length() + 1;
284+
285+
// In this case we can have a comment above with the author. We also want to delete it.
286+
int aboveLineStartOffset = wsTextEditorPage.getOffsetOfLineStart(revisedElementRange.getStartLine() - 1);
287+
int aboveLineEndOffset = wsTextEditorPage.getOffsetOfLineEnd(revisedElementRange.getStartLine() - 1);
288+
String aboveLineContet = wsTextEditorPage.getDocument().getText(
289+
aboveLineStartOffset, aboveLineEndOffset - aboveLineStartOffset);
290+
291+
if(aboveLineContet.trim().startsWith("<!--") && aboveLineContet.trim().endsWith("-->")) {
292+
startOffsetToDelete = aboveLineStartOffset;
293+
lengthToDelete += aboveLineContet.length();
294+
}
295+
}
296+
297+
wsTextEditorPage.getDocument().remove(startOffsetToDelete, lengthToDelete);
298+
} catch (BadLocationException e) {
299+
logger.debug(e, e);
300+
}
301+
}
302+
303+
225304
/**
226305
* Update the author elements of prolog.
227306
*

src/main/java/com/oxygenxml/prolog/updater/prolog/content/PrologContentCreator.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,32 @@ public class PrologContentCreator {
100100
*/
101101
private boolean updateMapRevisedDate = true;
102102

103+
/**
104+
* The maximum number of revised elements
105+
*/
106+
private int maxNoOfRevised = -1;
107+
108+
/**
109+
* Constructor.
110+
*
111+
* @param author The name of the author.
112+
* @param dateFormat The format of the date.
113+
*/
114+
public PrologContentCreator(String author, String dateFormat) {
115+
this(author, dateFormat, -1);
116+
}
117+
103118
/**
104119
* Constructor.
105120
*
106121
* @param author The name of the author.
107122
* @param dateFormat The format of the date.
123+
* @param maxNoOfRevisedElements The maximum allowed number of revised elements.
108124
*/
109-
public PrologContentCreator(String author, String dateFormat) {
125+
public PrologContentCreator(String author, String dateFormat, int maxNoOfRevisedElements) {
110126
this.authorName = author;
111-
127+
this.maxNoOfRevised = maxNoOfRevisedElements;
128+
112129
if (authorName != null) {
113130
// Creator
114131
creatorFragment = XMLFragmentUtils.createAuthorFragment(authorName, XmlElementsConstants.CREATOR_TYPE);
@@ -299,6 +316,14 @@ public String getDateFragment(boolean isNewDocument, DocumentType documentType)
299316
return isNewDocument ? getCreatedDateFragment(documentType) : getRevisedDateFragment(documentType);
300317
}
301318

319+
/**
320+
* The maximum number of allowed revised elements.
321+
* @return
322+
*/
323+
public int getMaxNoOfRevisedElement() {
324+
return maxNoOfRevised;
325+
}
326+
302327
/**
303328
* Create the local date.
304329
*

src/main/java/com/oxygenxml/prolog/updater/tags/OptionKeys.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ private OptionKeys() {
2323
*/
2424
public static final String DATE_FORMAT = "prolog.updater.date.format";
2525

26+
/**
27+
* Option for limit the revised elements.
28+
*/
29+
public static final String LIMIT_REVISED_ELEMENTS = "limit.revised.elements";
30+
31+
/**
32+
* Option for the maximum number of revised elements.
33+
*/
34+
public static final String MAX_REVISED_ELEMENTS = "max.revised.elements";
35+
2636
/**
2737
* Option to enable the prolog updater for topics.
2838
*/

src/main/java/com/oxygenxml/prolog/updater/tags/Tags.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ private Tags() {
6565
*/
6666
public static final String ERROR_MESSAGE = "prolog.updater.error.message";
6767

68+
/**
69+
* en: Limit the number of revised dates to
70+
*/
71+
public static final String LIMIT_REVISED_DATES_TO = "prolog.updater.limit.revised.dates.to";
72+
6873
}

0 commit comments

Comments
 (0)