1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.diff.script

File DiffDisplayerScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

0
26
7
1
209
94
11
0.42
3.71
7
1.57

Classes

Class Line # Actions
DiffDisplayerScriptService 50 26 0% 11 24
0.2727272827.3%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software 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 GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.diff.script;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.diff.DiffException;
31    import org.xwiki.diff.DiffManager;
32    import org.xwiki.diff.DiffResult;
33    import org.xwiki.diff.display.InlineDiffChunk;
34    import org.xwiki.diff.display.InlineDiffDisplayer;
35    import org.xwiki.diff.display.Splitter;
36    import org.xwiki.diff.display.UnifiedDiffBlock;
37    import org.xwiki.diff.display.UnifiedDiffConfiguration;
38    import org.xwiki.diff.display.UnifiedDiffDisplayer;
39    import org.xwiki.script.service.ScriptService;
40   
41    /**
42    * Provide script oriented APIs to display diff.
43    *
44    * @version $Id: 05e0cb1049fd56fb68076cbff958f079d34ee768 $
45    * @since 4.1RC1
46    */
47    @Component
48    @Named("diff.display")
49    @Singleton
 
50    public class DiffDisplayerScriptService implements ScriptService
51    {
52    /**
53    * The component used to access the execution context.
54    */
55    @Inject
56    private Execution execution;
57   
58    /**
59    * The component used to split a text into lines.
60    */
61    @Inject
62    @Named("line")
63    private Splitter<String, String> lineSplitter;
64   
65    /**
66    * The component used to split a text into its characters.
67    */
68    @Inject
69    private Splitter<String, Character> charSplitter;
70   
71    /**
72    * The component used to create the diff.
73    */
74    @Inject
75    private DiffManager diffManager;
76   
77    /**
78    * The component used to display in-line diffs.
79    */
80    @Inject
81    private InlineDiffDisplayer inlineDiffDisplayer;
82   
83    /**
84    * The component used to display unified diffs.
85    */
86    @Inject
87    private UnifiedDiffDisplayer unifiedDiffDisplayer;
88   
89    /**
90    * Builds an in-line diff between two versions of a list of elements.
91    *
92    * @param previous the previous version
93    * @param next the next version
94    * @param <E> the type of elements that are compared to produce the diff
95    * @return the list of in-line diff chunks
96    */
 
97  0 toggle public <E> List<InlineDiffChunk<E>> inline(List<E> previous, List<E> next)
98    {
99  0 setError(null);
100   
101  0 try {
102  0 return this.inlineDiffDisplayer.display(this.diffManager.diff(previous, next, null));
103    } catch (DiffException e) {
104  0 setError(e);
105  0 return null;
106    }
107    }
108   
109    /**
110    * Builds an in-line diff between two versions of a text.
111    *
112    * @param previous the previous version
113    * @param next the next version
114    * @return the list of in-line diff chunks
115    */
 
116  0 toggle public List<InlineDiffChunk<Character>> inline(String previous, String next)
117    {
118  0 setError(null);
119   
120  0 try {
121  0 return this.inlineDiffDisplayer
122    .display(this.diffManager.diff(this.charSplitter.split(previous), this.charSplitter.split(next), null));
123    } catch (DiffException e) {
124  0 setError(e);
125  0 return null;
126    }
127    }
128   
129    /**
130    * Builds an unified diff between two versions of a text. The unified diff provides information about both
131    * line-level and character-level changes (the later only when a line is modified).
132    *
133    * @param previous the previous version
134    * @param next the next version
135    * @return the list of extended diff blocks
136    */
 
137  7 toggle public List<UnifiedDiffBlock<String, Character>> unified(String previous, String next)
138    {
139  7 setError(null);
140   
141  7 try {
142  7 DiffResult<String> diffResult =
143    this.diffManager.diff(this.lineSplitter.split(previous), this.lineSplitter.split(next), null);
144  7 UnifiedDiffConfiguration<String, Character> config = this.unifiedDiffDisplayer.getDefaultConfiguration();
145  7 config.setSplitter(this.charSplitter);
146  7 return this.unifiedDiffDisplayer.display(diffResult, config);
147    } catch (DiffException e) {
148  0 setError(e);
149  0 return null;
150    }
151    }
152   
153    /**
154    * Builds an unified diff between two versions of a list of elements. If a splitter is provided through the given
155    * configuration object then the unified diff will display changes at two levels of granularity: elements and their
156    * sub-elements.
157    *
158    * @param previous the previous version
159    * @param next the next version
160    * @param config the configuration object
161    * @param <E> the type of composite elements that are compared to produce the first level diff
162    * @param <F> the type of sub-elements that are compared to produce the second level diff when a composite element
163    * @return the list of extended diff blocks
164    */
 
165  0 toggle public <E, F> List<UnifiedDiffBlock<E, F>> unified(List<E> previous, List<E> next,
166    UnifiedDiffConfiguration<E, F> config)
167    {
168  0 setError(null);
169   
170  0 try {
171  0 return this.unifiedDiffDisplayer.display(this.diffManager.diff(previous, next, null), config);
172    } catch (DiffException e) {
173  0 setError(e);
174  0 return null;
175    }
176    }
177   
178    /**
179    * @param <E> the type of composite elements that are compared to produce the first level diff
180    * @param <F> the type of sub-elements that are compared to produce the second level diff when a composite element
181    * is modified
182    * @return the unified diff configuration
183    */
 
184  0 toggle public <E, F> UnifiedDiffConfiguration<E, F> getUnifiedDiffConfiguration()
185    {
186  0 return this.unifiedDiffDisplayer.getDefaultConfiguration();
187    }
188   
189    /**
190    * Get the error generated while performing the previously called action.
191    *
192    * @return an eventual exception or {@code null} if no exception was thrown
193    */
 
194  0 toggle public Exception getLastError()
195    {
196  0 return (Exception) this.execution.getContext().getProperty(DiffScriptService.DIFF_ERROR_KEY);
197    }
198   
199    /**
200    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
201    *
202    * @param e the exception to store, can be {@code null} to clear the previously stored exception
203    * @see #getLastError()
204    */
 
205  7 toggle private void setError(Exception e)
206    {
207  7 this.execution.getContext().setProperty(DiffScriptService.DIFF_ERROR_KEY, e);
208    }
209    }