1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.doc.rcs

File XWikiPatch.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
34
13
1
233
113
20
0.59
2.62
13
1.54

Classes

Class Line # Actions
XWikiPatch 41 34 0% 20 13
0.771929877.2%
 

Contributing tests

This file is covered by 6 tests. .

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 com.xpn.xwiki.doc.rcs;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28    import org.suigeneris.jrcs.util.ToString;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32    import com.xpn.xwiki.doc.XWikiDocument;
33   
34    /**
35    * Contains differences between revisions. One field (diff for xml or full xml) for now. Created for easy migration to
36    * future XWikiPatch system.
37    *
38    * @version $Id: 20be04bcd1aec6b754c7611f4caa9daf1e79882e $
39    * @since 1.2M1
40    */
 
41    public class XWikiPatch
42    {
43    /** Logger. */
44    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiPatch.class);
45   
46    /** string serialization for patch. */
47    private String content;
48   
49    /** is content a difference, or full version. */
50    private boolean isDiff;
51   
52    /** Default constructor, need for hibernate. */
 
53  13298 toggle public XWikiPatch()
54    {
55    }
56   
57    /**
58    * @param content - patch content
59    * @param isDiff - is patch a difference or full version
60    */
 
61  150 toggle public XWikiPatch(String content, boolean isDiff)
62    {
63  150 setContent(content);
64  150 setDiff(isDiff);
65    }
66   
67    /**
68    * @return string serialization for patch
69    */
 
70  20393 toggle public String getContent()
71    {
72  20393 return this.content;
73    }
74   
75    /**
76    * @param content - string serialization for patch
77    */
 
78  13830 toggle public void setContent(String content)
79    {
80  13830 this.content = content;
81    }
82   
83    /**
84    * @return is content a difference. using content field to determine.
85    */
 
86  32365 toggle private boolean isContentDiff()
87    {
88  32365 return !this.content.startsWith("<");
89    }
90   
91    /**
92    * @return is content a difference, or full version
93    */
 
94  25953 toggle public boolean isDiff()
95    {
96  25953 if (this.content != null) {
97  25953 if (this.isDiff != isContentDiff()) {
98  0 LOGGER.warn("isDiff: Patch is inconsistent. Content and diff field are contradicting");
99  0 return isContentDiff();
100    }
101    }
102  25953 return this.isDiff;
103    }
104   
105    /**
106    * @param isDiff - is content a difference, or full version
107    */
 
108  13958 toggle public void setDiff(boolean isDiff)
109    {
110  13958 if (this.content != null) {
111  6412 if (isDiff != isContentDiff()) {
112  0 LOGGER.warn("setDiff: Patch is inconsistent. Content and diff field are contradicting");
113  0 this.isDiff = isContentDiff();
114  0 return;
115    }
116    }
117  13958 this.isDiff = isDiff;
118    }
119   
120    /**
121    * Store the XML export of the document as the history patch; this will be a history milestone.
122    *
123    * @param version Document version to store in the history patch.
124    * @param context Needed for serializing documents to xml.
125    * @return Self, with the patch content set to the XML export of the document version.
126    * @throws XWikiException if any error
127    */
 
128  5752 toggle public XWikiPatch setFullVersion(XWikiDocument version, XWikiContext context)
129    throws XWikiException
130    {
131  5752 return setFullVersion(version.toXML(context));
132    }
133   
134    /**
135    * Store the XML export of the document as the history patch; this will be a history milestone.
136    *
137    * @param versionXml Document version to store in the history patch, in the XML export format.
138    * @return Self, with the patch content set to the XML export of the document version.
139    * @throws XWikiException if any error occurs
140    */
 
141  5754 toggle public XWikiPatch setFullVersion(String versionXml) throws XWikiException
142    {
143  5754 setContent(versionXml);
144  5754 setDiff(false);
145  5754 return this;
146    }
147   
148    /**
149    * Create history patch between originalVersion and newVersion as difference on the XML export of the two versions.
150    * The patch is created between newVersion and originalVersion.
151    *
152    * @param originalVersion Original version of the document document.
153    * @param newVersion Current version of the document.
154    * @param context Needed for serializing documents to xml.
155    * @return Self, with the patch content set to the generated diff between the two version.
156    * @throws XWikiException if any error occurs
157    */
 
158  0 toggle public XWikiPatch setDiffVersion(XWikiDocument originalVersion, XWikiDocument newVersion,
159    XWikiContext context) throws XWikiException
160    {
161  0 return setDiffVersion(originalVersion.toXML(context), newVersion.toXML(context),
162    newVersion.getFullName());
163    }
164   
165    /**
166    * Create history patch between originalVersion and newVersion as difference on the XML export of the two versions.
167    * The patch is created between newVersion and originalVersion.
168    *
169    * @param originalVersionXml Original version of the document document, in the XML export format.
170    * @param newVersion Current version of the document.
171    * @param context Needed for serializing documents to xml.
172    * @return Self, with the patch content set to the generated diff between the two version.
173    * @throws XWikiException if any error occurs
174    */
 
175  379 toggle public XWikiPatch setDiffVersion(String originalVersionXml, XWikiDocument newVersion,
176    XWikiContext context) throws XWikiException
177    {
178  379 return setDiffVersion(originalVersionXml, newVersion.toXML(context), newVersion
179    .getFullName());
180    }
181   
182    /**
183    * Create history patch between originalVersion and newVersion as difference on the XML export of the two versions.
184    * The patch is created between newVersion and originalVersion.
185    *
186    * @param originalVersionXml Original version of the document document, in the XML export format.
187    * @param newVersionXml Current version of the document, in the XML export format.
188    * @param docName Needed for the exception report.
189    * @return Self, with the patch content set to the generated diff between the two version.
190    * @throws XWikiException if any error occurs
191    */
 
192  380 toggle public XWikiPatch setDiffVersion(String originalVersionXml, String newVersionXml,
193    String docName) throws XWikiException
194    {
195  380 try {
196    // The history keeps reversed patches, from the most recent to the previous version.
197  380 setContent(XWikiPatchUtils.getDiff(newVersionXml, originalVersionXml));
198  380 setDiff(true);
199    } catch (Exception e) {
200  0 Object[] args = { docName };
201  0 throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF,
202    XWikiException.ERROR_XWIKI_DIFF_XML_ERROR,
203    "Failed to create diff for doc {0}",
204    e,
205    args);
206    }
207  380 return this;
208    }
209   
210    /**
211    * Patch text.
212    *
213    * @param origText - text to patch
214    * @throws XWikiException if exception while patching
215    */
 
216  74 toggle public void patch(List<String> origText) throws XWikiException
217    {
218  74 if (!isDiff()) {
219  37 origText.clear();
220  37 origText.addAll(
221    new ArrayList<String>(Arrays.asList(ToString.stringToArray(getContent()))));
222    } else {
223  37 try {
224  37 XWikiPatchUtils.patch(origText, getContent());
225    } catch (Exception e) {
226  0 throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF,
227    XWikiException.ERROR_XWIKI_DIFF_XML_ERROR,
228    "Exception while patching",
229    e);
230    }
231    }
232    }
233    }