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

File DiffScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

0
14
4
1
139
62
6
0.43
3.5
4
1.5

Classes

Class Line # Actions
DiffScriptService 50 14 0% 6 16
0.1111111111.1%
 

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.DiffConfiguration;
31    import org.xwiki.diff.DiffException;
32    import org.xwiki.diff.DiffManager;
33    import org.xwiki.diff.DiffResult;
34    import org.xwiki.diff.MergeConfiguration;
35    import org.xwiki.diff.MergeException;
36    import org.xwiki.diff.MergeResult;
37    import org.xwiki.diff.internal.DefaultDiffResult;
38    import org.xwiki.diff.internal.DefaultMergeResult;
39    import org.xwiki.script.service.ScriptService;
40   
41    /**
42    * Provide script oriented APIs to do diff and merges.
43    *
44    * @version $Id: caeaf8756568b9c4bfbf6bf1676afdc586ca957a $
45    * @since 4.1RC1
46    */
47    @Component
48    @Named("diff")
49    @Singleton
 
50    public class DiffScriptService implements ScriptService
51    {
52    /**
53    * The key under which the last encountered error is stored in the current execution context.
54    */
55    static final String DIFF_ERROR_KEY = "scriptservice.diff.error";
56   
57    /**
58    * The component used to access the execution context.
59    */
60    @Inject
61    private Execution execution;
62   
63    /**
64    * The component used to create the diff.
65    */
66    @Inject
67    private DiffManager diffManager;
68   
69    /**
70    * The displayer oriented sub API.
71    */
72    @Inject
73    @Named("diff.display")
74    private ScriptService diffDisplayScriptService;
75   
76    /**
77    * @return the display oriented API
78    */
 
79  7 toggle public ScriptService getDisplay()
80    {
81  7 return this.diffDisplayScriptService;
82    }
83   
84    /**
85    * Produce a diff between the two provided versions.
86    *
87    * @param <E> the type of compared elements
88    * @param previous the previous version of the content to compare
89    * @param next the next version of the content to compare
90    * @param configuration the configuration of the diff behavior
91    * @return the result of the diff
92    */
 
93  0 toggle public <E> DiffResult<E> diff(List<E> previous, List<E> next, DiffConfiguration<E> configuration)
94    {
95  0 DiffResult<E> result;
96  0 try {
97  0 result = this.diffManager.diff(previous, next, configuration);
98    } catch (DiffException e) {
99  0 result = new DefaultDiffResult<E>(previous, next);
100  0 result.getLog().error("Failed to execute diff", e);
101    }
102   
103  0 return result;
104    }
105   
106    /**
107    * Execute a 3-way merge on provided versions.
108    *
109    * @param <E> the type of compared elements
110    * @param commonAncestor the common ancestor of the two versions of the content to compare
111    * @param next the next version of the content to compare
112    * @param current the current version of the content to compare
113    * @param configuration the configuration of the merge behavior
114    * @return the result of the merge
115    */
 
116  0 toggle public <E> MergeResult<E> merge(List<E> commonAncestor, List<E> next, List<E> current,
117    MergeConfiguration<E> configuration)
118    {
119  0 MergeResult<E> result;
120  0 try {
121  0 result = this.diffManager.merge(commonAncestor, next, current, configuration);
122    } catch (MergeException e) {
123  0 result = new DefaultMergeResult<E>(commonAncestor, next, current);
124  0 result.getLog().error("Failed to execute merge", e);
125    }
126   
127  0 return result;
128    }
129   
130    /**
131    * Get the error generated while performing the previously called action.
132    *
133    * @return an eventual exception or {@code null} if no exception was thrown
134    */
 
135  0 toggle public Exception getLastError()
136    {
137  0 return (Exception) this.execution.getContext().getProperty(DIFF_ERROR_KEY);
138    }
139    }