2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c) 2009 Regents of the University of California
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
23 package org.collectionspace.services.IntegrationTests.xmlreplay;
\r
25 import org.collectionspace.services.common.api.Tools;
\r
26 import org.jdom.Document;
\r
27 import org.jdom.Element;
\r
28 import org.jdom.JDOMException;
\r
29 import org.jdom.input.SAXBuilder;
\r
30 import org.jaxen.XPath;
\r
31 import org.jaxen.jdom.JDOMXPath;
\r
34 import java.io.IOException;
\r
35 import java.io.StringReader;
\r
36 import java.util.ArrayList;
\r
37 import java.util.HashMap;
\r
38 import java.util.List;
\r
39 import java.util.Map;
\r
41 import org.collectionspace.services.IntegrationTests.xmlreplay.TreeWalkResults.TreeWalkEntry;
\r
42 import org.jdom.output.XMLOutputter;
\r
46 * $LastChangedRevision: $
\r
47 * $LastChangedDate: $
\r
49 public class XmlCompareJdom {
\r
51 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";
\r
53 public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {
\r
54 org.jdom.Document doc;
\r
56 builder = new SAXBuilder();
\r
57 builder.setValidation(false); //has no effect, I think.
\r
58 doc = builder.build(new StringReader(source));
\r
62 public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){
\r
63 TreeWalkResults list = new TreeWalkResults();
\r
66 list.leftID = leftID;
\r
67 list.rightID = rightID;
\r
68 TreeWalkResults.TreeWalkEntry infoentry = new TreeWalkResults.TreeWalkEntry();
\r
69 infoentry.status = TreeWalkResults.TreeWalkEntry.STATUS.INFO;
\r
70 infoentry.message = "\r\n LEFT file: "+leftID+"\r\n RIGHT file: "+rightID;
\r
71 list.add(infoentry);
\r
72 if (Tools.isEmpty(expectedContent)){
\r
73 TreeWalkEntry entry = new TreeWalkEntry();
\r
74 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
75 entry.errmessage = "L dom was empty.";
\r
77 } else if (Tools.isEmpty(actualPartContent)){
\r
78 TreeWalkEntry entry = new TreeWalkEntry();
\r
79 entry.errmessage = "R dom was empty.";
\r
80 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
83 Document expected = getDocumentFromContent(expectedContent);
\r
84 Document actual = getDocumentFromContent(actualPartContent);
\r
85 treeWalk(expected, actual, list);
\r
87 } catch (Throwable t){
\r
88 String msg = "ERROR in XmlReplay.compareParts(): "+t;
\r
89 System.out.println(msg);
\r
90 TreeWalkEntry entry = new TreeWalkEntry();
\r
91 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
92 entry.errmessage = msg;
\r
98 public static List select(Element element, String xpathExpression) throws Exception {
\r
99 XPath xpath = new JDOMXPath(xpathExpression);
\r
100 return xpath.selectNodes(element);
\r
103 public static Object selectSingleNode(Element element, String xpathExpression) throws Exception {
\r
104 XPath xpath = new JDOMXPath(xpathExpression);
\r
105 return xpath.selectSingleNode(element);
\r
108 public static Object selectSingleNode(String docSource, String xpathExpression) throws Exception {
\r
109 Document doc = getDocumentFromContent(docSource);
\r
110 Element element = doc.getRootElement();
\r
111 XPath xpath = new JDOMXPath(xpathExpression);
\r
112 return xpath.selectSingleNode(element);
\r
116 public static boolean treeWalk(Document left, Document right, TreeWalkResults list) throws Exception {
\r
117 boolean res = treeWalk(left.getRootElement(), right.getRootElement(), "/", list);
\r
121 public static boolean treeWalk(Element left, Element right, String parentPath, TreeWalkResults msgList) throws Exception {
\r
122 String SPACE = " ";
\r
123 if (left == null && right == null){
\r
129 if (right == null){
\r
132 List l = left.getChildren();
\r
133 Map foundRightMap = new HashMap();
\r
134 List<String> foundRepeatingList = new ArrayList<String>();
\r
135 boolean result = true;
\r
136 for (Object o : l) {
\r
137 if (!(o instanceof Element)){
\r
140 Element leftChild = (Element)o;
\r
141 String leftChildName = leftChild.getName();
\r
142 if (Tools.isEmpty(leftChildName)){
\r
145 String leftChildPath = Tools.glue(parentPath, "/", leftChildName);
\r
147 if (foundRepeatingList.indexOf(leftChildPath)>=0){
\r
150 List leftlist = select(left, leftChildName);
\r
151 if (leftlist != null && leftlist.size() > 1){
\r
152 //System.out.println("-----------------doRepeating------"+leftChildPath);
\r
153 foundRepeatingList.add(leftChildPath);
\r
154 boolean repeatingIdentical =
\r
155 doRepeatingFieldComparison(leftlist, leftChildPath, leftChildName, left, right, msgList) ; //todo: deal with foundRightMap in this repeating field block.
\r
156 if ( ! repeatingIdentical ){
\r
157 //System.out.println("\r\n\r\n\r\n*****************************\r\nOne repeating field failed: "+msgList);
\r
160 foundRightMap.put(leftChildName, "OK");
\r
162 Element rightChild = (Element)selectSingleNode(right,leftChildName);
\r
163 if (rightChild == null){
\r
164 TreeWalkEntry entry = new TreeWalkEntry();
\r
165 entry.lpath = leftChildPath;
\r
166 entry.status = TreeWalkEntry.STATUS.R_MISSING;
\r
167 msgList.add(entry);
\r
170 foundRightMap.put(leftChildName, "OK");
\r
171 String leftChildTextTrim = leftChild.getText().trim();
\r
172 String rightChildTextTrim = rightChild.getText().trim();
\r
173 TreeWalkEntry entry = new TreeWalkEntry();
\r
174 entry.ltextTrimmed = leftChildTextTrim;
\r
175 entry.rtextTrimmed = rightChildTextTrim;
\r
176 entry.lpath = leftChildPath;
\r
177 entry.rpath = leftChildPath; //same
\r
179 if (leftChildTextTrim.equals(rightChildTextTrim)){
\r
180 entry.status = TreeWalkEntry.STATUS.MATCHED;
\r
181 msgList.add(entry);
\r
183 entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;
\r
184 msgList.add(entry);
\r
186 //============ DIVE !! =====================================================
\r
187 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);
\r
190 for (Object r : right.getChildren()){
\r
191 if (!(r instanceof Element)){
\r
194 Element rightChild = (Element)r;
\r
195 String rname = rightChild.getName();
\r
196 if (null==foundRightMap.get(rname)){
\r
197 String rightChildPath = Tools.glue(parentPath, "/", rname);
\r
199 TreeWalkEntry entry = new TreeWalkEntry();
\r
200 entry.rpath = rightChildPath;
\r
201 entry.status = TreeWalkEntry.STATUS.R_ADDED;
\r
202 msgList.add(entry);
\r
208 private static void dumpXML_OUT(Element el) throws Exception {
\r
209 XMLOutputter outputter = new XMLOutputter();
\r
210 outputter.output(el, System.out);
\r
212 private static String dumpXML(Element el) throws Exception {
\r
213 XMLOutputter outputter = new XMLOutputter();
\r
214 return outputter.outputString(el);
\r
217 public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)
\r
219 //todo: deal with foundRightMap in this repeating field block.
\r
220 List rightList = select(right, leftChildName);
\r
221 if (rightList == null || rightList.size() == 0 || rightList.size() < leftList.size()){
\r
222 TreeWalkEntry twe = new TreeWalkEntry();
\r
223 twe.lpath = leftChildPath;
\r
224 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
225 String rmsg = (rightList == null)
\r
227 : " Right: "+rightList.size();
\r
228 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;
\r
232 if (rightList.size() > leftList.size()){
\r
233 TreeWalkEntry twe = new TreeWalkEntry();
\r
234 twe.lpath = leftChildPath;
\r
235 twe.status = TreeWalkEntry.STATUS.R_ADDED;
\r
236 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+" Right: "+rightList.size();
\r
241 for (Object le : leftList){
\r
242 boolean found = false;
\r
243 Element leftEl = (Element)le;
\r
244 //pl("left", leftEl);
\r
245 for(Object re : rightList){
\r
246 Element rightEl = (Element)re;
\r
247 //pl("right", rightEl);
\r
248 TreeWalkResults msgListInner = new TreeWalkResults();
\r
249 treeWalk(leftEl, rightEl, leftChildPath, msgListInner);
\r
250 if (msgListInner.isStrictMatch()){
\r
252 TreeWalkEntry twe = new TreeWalkEntry();
\r
253 twe.lpath = leftChildPath;
\r
254 twe.status = TreeWalkEntry.STATUS.MATCHED;
\r
256 //System.out.println("===========================\r\nfound match for "+leftEl+"\r\n===========================\r\n");
\r
257 rightList.remove(re); //found it, don't need to inspect this element again. Since we are breaking from loop, removing element won't mess up iterator--we get a new one on the next loop.
\r
262 TreeWalkEntry twe = new TreeWalkEntry();
\r
263 twe.lpath = leftChildPath;
\r
264 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
265 twe.message = "Repeating field not matched. Source: {"+dumpXML(leftEl)+"}";
\r
273 private static void pl(String name, Element el) throws Exception {
\r
274 Object lobid = selectSingleNode(el, "@ID");
\r
277 lid = lobid.toString();
\r
280 System.out.println(name+": "+lid);
\r
282 System.out.println();
\r