View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU 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   * MyCoRe 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 General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.xeditor.tracker;
20  
21  import java.util.Iterator;
22  
23  import org.jdom2.Document;
24  import org.jdom2.Element;
25  import org.jdom2.ProcessingInstruction;
26  import org.jdom2.filter.Filters;
27  import org.mycore.common.MCRException;
28  import org.mycore.common.config.MCRConfiguration;
29  
30  public class MCRChangeTracker implements Cloneable {
31  
32      private static final String CONFIG_PREFIX = "MCR.XEditor.ChangeTracker.";
33  
34      public static final String PREFIX = "xed-tracker-";
35  
36      private int counter = 0;
37  
38      public void track(MCRChangeData data) {
39          ProcessingInstruction pi = data.getProcessingInstruction();
40          pi.setTarget(PREFIX + (++counter) + "-" + pi.getTarget());
41          data.getContext().addContent(data.getPosition(), pi);
42      }
43  
44      public int getChangeCounter() {
45          return counter;
46      }
47  
48      public void undoChanges(Document doc) {
49          undoChanges(doc, 0);
50      }
51  
52      public void undoChanges(Document doc, int stepNumber) {
53          while (counter > stepNumber)
54              undoLastChange(doc);
55      }
56  
57      public String undoLastBreakpoint(Document doc) {
58          while (counter > 0) {
59              MCRChangeData change = undoLastChange(doc);
60              if ("breakpoint".equals(change.getType()))
61                  return change.getText();
62          }
63          return null;
64      }
65  
66      public MCRChangeData undoLastChange(Document doc) {
67          MCRChangeData data = findLastChange(doc);
68          data.getProcessingInstruction().detach();
69          counter--;
70  
71          String property = CONFIG_PREFIX + data.getType() + ".Class";
72          MCRChange change = MCRConfiguration.instance().getSingleInstanceOf(property);
73          change.undo(data);
74          return data;
75      }
76  
77      public MCRChangeData findLastChange(Document doc) {
78          String typePrefix = PREFIX + counter + "-";
79          for (ProcessingInstruction instruction : doc.getDescendants(Filters.processinginstruction())) {
80              String target = instruction.getTarget();
81  
82              if (target.startsWith(typePrefix))
83                  return new MCRChangeData(instruction, typePrefix);
84          }
85          throw new MCRException("Lost processing instruction for undo, not found: " + typePrefix);
86      }
87  
88      public static Document removeChangeTracking(Document doc) {
89          Document clone = doc.clone();
90          removeChangeTracking(clone.getRootElement());
91          return clone;
92      }
93  
94      public static void removeChangeTracking(Element element) {
95          for (Iterator<ProcessingInstruction> iter = element.getDescendants(Filters.processinginstruction())
96              .iterator(); iter.hasNext();) {
97              if (iter.next().getTarget().startsWith(PREFIX))
98                  iter.remove();
99          }
100     }
101 
102     @Override
103     public MCRChangeTracker clone() {
104         MCRChangeTrackerMCRChangeTracker.html#MCRChangeTracker">MCRChangeTracker tracker = new MCRChangeTracker();
105         tracker.counter = this.counter;
106         return tracker;
107     }
108 }