From version < 15.1 >
edited by Marius Dumitru Florea
on 2019/12/12 14:58
To version < 14.1 >
edited by Marius Dumitru Florea
on 2019/12/11 16:23
< >
Change comment: There is no comment for this version

Summary

Details

ExtensionCode.ExtensionClass[0]
Description
... ... @@ -135,17 +135,25 @@
135 135  To compute the changes you need to use the ##XMLDiff## component.
136 136  
137 137  {{code language="java"}}
138 [email protected]
139 -private XMLDiff xmlDiff;
140 -
141 [email protected]
142 -private XMLDiffConfiguration config;
143 -
144 -...
145 -
146 -Document left = parseXML("...");
147 -Document right = parseXML("...");
148 -Map<Node, Patch<?>> patches = this.xmlDiff.diff(left, right, this.config);
138 +public interface XMLDiff
139 +{
140 + /**
141 + * Computes the difference between two XML nodes and their descendants. When a value node type (e.g. text,
142 + * attribute, comment) is modified we compute the difference on the text value using the splitter indicated by the
143 + * configuration. Otherwise the difference is expressed at node level, as if two lists of nodes are compared.
144 + * <p>
145 + * The result is a mapping between nodes from the left side and the patches that need to be applied to these nodes
146 + * in order for the left tree to become the right tree. If the root nodes of the left and right trees don't match
147 + * then this change is mapped to the {@code null} key.
148 + *
149 + * @param left the left side of the comparison
150 + * @param right the right side of the comparison
151 + * @param config the configuration
152 + * @return the differences between the two XML nodes
153 + * @throws DiffException if the difference can't be computed
154 + */
155 + Map<Node, Patch<?>> diff(Node left, Node right, XMLDiffConfiguration config) throws DiffException;
156 +}
149 149  {{/code}}
150 150  
151 151  The default implementation uses the list diff API to compute the changes between child nodes, attributes and text content. The algorithm is quite simple:
... ... @@ -174,68 +174,33 @@
174 174  
175 175  The similarity threshold (0.6 by default) can be changed from the configuration.
176 176  
177 -As indicated in the algorithm described above, you can also change from the configuration the text splitter used for a specific node type. And, of course, you can implement your own splitter component.
178 -
179 -{{code language="java"}}
180 [email protected]
181 [email protected]("myCustomSplitter")
182 -private StringSplitter myCustomSplitter;
183 -
184 [email protected]
185 -private XMLDiffConfiguration config;
186 -
187 -...
188 -
189 -((DefaultXMLDiffConfiguration) this.config).setSplitterForNodeType(Node.TEXT_NODE, this.myCustomSplitter);
190 -{{/code}}
191 -
192 192  == Displaying the Changes ==
193 193  
194 -The component responsible for computing and displaying the changes is ##XMLDiffManager##. There's no generic (default) implementation provided at the moment. The provided implementation is dedicated to displaying changes between HTML documents.
187 +To compute and display the changes you can use the ##XMLDiffManager## component.
195 195  
196 196  {{code language="java"}}
197 -@Inject
198 -@Named("html/unified")
199 -private XMLDiffManager unifiedHTMLDiffManager;
200 -
201 -@Inject
202 [email protected]Named("html")
203 -private XMLDiffConfiguration config;
204 -
205 -...
206 -
207 -String previousHTML = "...";
208 -String nextHTML = "...";
209 -String diff = this.unifiedHTMLDiffManager.diff(previousHTML, nextHTML, this.config)
190 +public interface XMLDiffManager
191 +{
192 + /**
193 + * Computes and marks the differences between two XML documents.
194 + *
195 + * @param left the left side of the comparison
196 + * @param right the right side of the comparison
197 + * @param config the configuration
198 + * @return the differences between the two XML documents
199 + * @throws DiffException if the difference can't be computed
200 + */
201 + String diff(String left, String right, XMLDiffConfiguration config) throws DiffException;
202 +}
210 210  {{/code}}
211 211  
212 212  You can control from the configuration which ##XMLDiffFilter##s are applied on the XML documents before and after computing the changes. You can also implement your own filters, e.g. to remove irrelevant changes, or to ignore parts of the XML documents while computing the changes. The default configuration applies a filter to mark context (unmodified) nodes after the changes are computed.
213 213  
214 -{{code language="java"}}
215 [email protected]
216 [email protected]("myCustomFilter")
217 -private XMLDiffFilter myCustomFilter;
207 +=== HTML Visual Diff ===
218 218  
219 [email protected]
220 [email protected]("html")
221 -private XMLDiffConfiguration config;
209 +The ##XMLDiffManager## has an implementation dedicated to computing a visual diff on HTML. Best is to use the provided script service:
222 222  
223 -...
224 -
225 -this.config.getFilters().add(this.myCustomFilter);
226 -{{/code}}
227 -
228 -=== Script Service ===
229 -
230 -Here's how you can compute and display the changes from a Velocity script:
231 -
232 232  {{code language="none"}}
233 -{{velocity}}
234 -{{html clean="false"}}
235 -#set ($discard = $xwiki.ssfx.use('uicomponents/viewers/diff.css', true))
236 -#set ($discard = $xwiki.jsfx.use('uicomponents/viewers/diff.js'))
237 -#set ($previousHTML = '<p>one two three</p>')
238 -#set ($nextHTML = '<p>one 2 three</p>')
239 239  <div class="html-diff">
240 240   #set ($htmlDiff = $services.diff.html.unified($previousHTML, $nextHTML))
241 241   #if ($htmlDiff == '')
... ... @@ -246,8 +246,6 @@
246 246   $htmlDiff
247 247   #end
248 248  </div>
249 -{{/html}}
250 -{{/velocity}}
251 251  {{/code}}
252 252  
253 253  You can configure the diff by passing a third argument:
... ... @@ -254,10 +254,6 @@
254 254  
255 255  {{code language="none"}}
256 256  #set ($config = $services.diff.html.defaultConfiguration)
257 -#set ($discard = $config.setSimilarityThreshold(0.6))
258 -#set ($discard = $config.addFilter('hintOfMyCustomFilter'))
259 -## Change the splitter used for text nodes from 'character' to 'word'.
260 -#set ($discard = $config.setSplitterForNodeType(3, 'word'))
261 261  #set ($htmlDiff = $services.diff.html.unified($previousHTML, $nextHTML, $config))
262 262  {{/code}}
263 263  

Get Connected