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.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
111 public static boolean treeWalk(Document left, Document right, TreeWalkResults list) throws Exception {
\r
112 boolean res = treeWalk(left.getRootElement(), right.getRootElement(), "/", list);
\r
116 public static boolean treeWalk(Element left, Element right, String parentPath, TreeWalkResults msgList) throws Exception {
\r
117 String SPACE = " ";
\r
118 if (left == null && right == null){
\r
124 if (right == null){
\r
127 List l = left.getChildren();
\r
128 Map foundRightMap = new HashMap();
\r
129 List<String> foundRepeatingList = new ArrayList<String>();
\r
130 boolean result = true;
\r
131 for (Object o : l) {
\r
132 if (!(o instanceof Element)){
\r
135 Element leftChild = (Element)o;
\r
136 String leftChildName = leftChild.getName();
\r
137 if (Tools.isEmpty(leftChildName)){
\r
140 String leftChildPath = Tools.glue(parentPath, "/", leftChildName);
\r
142 if (foundRepeatingList.indexOf(leftChildPath)>=0){
\r
145 List leftlist = select(left, leftChildName);
\r
146 if (leftlist != null && leftlist.size() > 1){
\r
147 //System.out.println("-----------------doRepeating------"+leftChildPath);
\r
148 foundRepeatingList.add(leftChildPath);
\r
149 boolean repeatingIdentical =
\r
150 doRepeatingFieldComparison(leftlist, leftChildPath, leftChildName, left, right, msgList) ; //todo: deal with foundRightMap in this repeating field block.
\r
151 if ( ! repeatingIdentical ){
\r
152 //System.out.println("\r\n\r\n\r\n*****************************\r\nOne repeating field failed: "+msgList);
\r
155 foundRightMap.put(leftChildName, "OK");
\r
157 Element rightChild = (Element)selectSingleNode(right,leftChildName);
\r
158 if (rightChild == null){
\r
159 TreeWalkEntry entry = new TreeWalkEntry();
\r
160 entry.lpath = leftChildPath;
\r
161 entry.status = TreeWalkEntry.STATUS.R_MISSING;
\r
162 msgList.add(entry);
\r
165 foundRightMap.put(leftChildName, "OK");
\r
166 String leftChildTextTrim = leftChild.getText().trim();
\r
167 String rightChildTextTrim = rightChild.getText().trim();
\r
168 TreeWalkEntry entry = new TreeWalkEntry();
\r
169 entry.ltextTrimmed = leftChildTextTrim;
\r
170 entry.rtextTrimmed = rightChildTextTrim;
\r
171 entry.lpath = leftChildPath;
\r
172 entry.rpath = leftChildPath; //same
\r
174 if (leftChildTextTrim.equals(rightChildTextTrim)){
\r
175 entry.status = TreeWalkEntry.STATUS.MATCHED;
\r
176 msgList.add(entry);
\r
178 entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;
\r
179 msgList.add(entry);
\r
181 //============ DIVE !! =====================================================
\r
182 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);
\r
185 for (Object r : right.getChildren()){
\r
186 if (!(r instanceof Element)){
\r
189 Element rightChild = (Element)r;
\r
190 String rname = rightChild.getName();
\r
191 if (null==foundRightMap.get(rname)){
\r
192 String rightChildPath = Tools.glue(parentPath, "/", rname);
\r
194 TreeWalkEntry entry = new TreeWalkEntry();
\r
195 entry.rpath = rightChildPath;
\r
196 entry.status = TreeWalkEntry.STATUS.R_ADDED;
\r
197 msgList.add(entry);
\r
203 private static void dumpXML_OUT(Element el) throws Exception {
\r
204 XMLOutputter outputter = new XMLOutputter();
\r
205 outputter.output(el, System.out);
\r
207 private static String dumpXML(Element el) throws Exception {
\r
208 XMLOutputter outputter = new XMLOutputter();
\r
209 return outputter.outputString(el);
\r
212 public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)
\r
214 //todo: deal with foundRightMap in this repeating field block.
\r
215 List rightList = select(right, leftChildName);
\r
216 if (rightList == null || rightList.size() == 0 || rightList.size() < leftList.size()){
\r
217 TreeWalkEntry twe = new TreeWalkEntry();
\r
218 twe.lpath = leftChildPath;
\r
219 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
220 String rmsg = (rightList == null)
\r
222 : " Right: "+rightList.size();
\r
223 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;
\r
227 if (rightList.size() > leftList.size()){
\r
228 TreeWalkEntry twe = new TreeWalkEntry();
\r
229 twe.lpath = leftChildPath;
\r
230 twe.status = TreeWalkEntry.STATUS.R_ADDED;
\r
231 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+" Right: "+rightList.size();
\r
236 for (Object le : leftList){
\r
237 boolean found = false;
\r
238 Element leftEl = (Element)le;
\r
239 //pl("left", leftEl);
\r
240 for(Object re : rightList){
\r
241 Element rightEl = (Element)re;
\r
242 //pl("right", rightEl);
\r
243 TreeWalkResults msgListInner = new TreeWalkResults();
\r
244 treeWalk(leftEl, rightEl, leftChildPath, msgListInner);
\r
245 if (msgListInner.isStrictMatch()){
\r
247 TreeWalkEntry twe = new TreeWalkEntry();
\r
248 twe.lpath = leftChildPath;
\r
249 twe.status = TreeWalkEntry.STATUS.MATCHED;
\r
251 //System.out.println("===========================\r\nfound match for "+leftEl+"\r\n===========================\r\n");
\r
252 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
257 TreeWalkEntry twe = new TreeWalkEntry();
\r
258 twe.lpath = leftChildPath;
\r
259 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
260 twe.message = "Repeating field not matched. Source: {"+dumpXML(leftEl)+"}";
\r
268 private static void pl(String name, Element el) throws Exception {
\r
269 Object lobid = selectSingleNode(el, "@ID");
\r
272 lid = lobid.toString();
\r
275 System.out.println(name+": "+lid);
\r
277 System.out.println();
\r