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.jdom.Document;
\r
26 import org.jdom.Element;
\r
27 import org.jdom.JDOMException;
\r
28 import org.jdom.input.SAXBuilder;
\r
29 import org.jaxen.XPath;
\r
30 import org.jaxen.jdom.JDOMXPath;
\r
33 import java.io.IOException;
\r
34 import java.io.StringReader;
\r
35 import java.util.ArrayList;
\r
36 import java.util.HashMap;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
40 import org.collectionspace.services.IntegrationTests.xmlreplay.TreeWalkResults.TreeWalkEntry;
\r
41 import org.jdom.output.XMLOutputter;
\r
45 * $LastChangedRevision: $
\r
46 * $LastChangedDate: $
\r
48 public class XmlCompareJdom {
\r
50 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";
\r
52 public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {
\r
53 org.jdom.Document doc;
\r
55 builder = new SAXBuilder();
\r
56 builder.setValidation(false); //has no effect, I think.
\r
57 doc = builder.build(new StringReader(source));
\r
61 public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){
\r
62 TreeWalkResults list = new TreeWalkResults();
\r
65 list.leftID = leftID;
\r
66 list.rightID = rightID;
\r
67 TreeWalkResults.TreeWalkEntry infoentry = new TreeWalkResults.TreeWalkEntry();
\r
68 infoentry.status = TreeWalkResults.TreeWalkEntry.STATUS.INFO;
\r
69 infoentry.message = "\r\n LEFT file: "+leftID+"\r\n RIGHT file: "+rightID;
\r
70 list.add(infoentry);
\r
71 if (Tools.isEmpty(expectedContent)){
\r
72 TreeWalkEntry entry = new TreeWalkEntry();
\r
73 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
74 entry.errmessage = "L dom was empty.";
\r
76 } else if (Tools.isEmpty(actualPartContent)){
\r
77 TreeWalkEntry entry = new TreeWalkEntry();
\r
78 entry.errmessage = "R dom was empty.";
\r
79 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
82 Document expected = getDocumentFromContent(expectedContent);
\r
83 Document actual = getDocumentFromContent(actualPartContent);
\r
84 treeWalk(expected, actual, list);
\r
86 } catch (Throwable t){
\r
87 String msg = "ERROR in XmlReplay.compareParts(): "+t;
\r
88 System.out.println(msg);
\r
89 TreeWalkEntry entry = new TreeWalkEntry();
\r
90 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
91 entry.errmessage = msg;
\r
97 public static List select(Element element, String xpathExpression) throws Exception {
\r
98 XPath xpath = new JDOMXPath(xpathExpression);
\r
99 return xpath.selectNodes(element);
\r
102 public static Object selectSingleNode(Element element, String xpathExpression) throws Exception {
\r
103 XPath xpath = new JDOMXPath(xpathExpression);
\r
104 return xpath.selectSingleNode(element);
\r
110 public static boolean treeWalk(Document left, Document right, TreeWalkResults list) throws Exception {
\r
111 boolean res = treeWalk(left.getRootElement(), right.getRootElement(), "/", list);
\r
115 public static boolean treeWalk(Element left, Element right, String parentPath, TreeWalkResults msgList) throws Exception {
\r
116 String SPACE = " ";
\r
117 if (left == null && right == null){
\r
123 if (right == null){
\r
126 List l = left.getChildren();
\r
127 Map foundRightMap = new HashMap();
\r
128 List<String> foundRepeatingList = new ArrayList<String>();
\r
129 boolean result = true;
\r
130 for (Object o : l) {
\r
131 if (!(o instanceof Element)){
\r
134 Element leftChild = (Element)o;
\r
135 String leftChildName = leftChild.getName();
\r
136 if (Tools.isEmpty(leftChildName)){
\r
139 String leftChildPath = Tools.glue(parentPath, "/", leftChildName);
\r
141 if (foundRepeatingList.indexOf(leftChildPath)>=0){
\r
144 List leftlist = select(left, leftChildName);
\r
145 if (leftlist != null && leftlist.size() > 1){
\r
146 //System.out.println("-----------------doRepeating------"+leftChildPath);
\r
147 foundRepeatingList.add(leftChildPath);
\r
148 boolean repeatingIdentical =
\r
149 doRepeatingFieldComparison(leftlist, leftChildPath, leftChildName, left, right, msgList) ; //todo: deal with foundRightMap in this repeating field block.
\r
150 if ( ! repeatingIdentical ){
\r
151 //System.out.println("\r\n\r\n\r\n*****************************\r\nOne repeating field failed: "+msgList);
\r
154 foundRightMap.put(leftChildName, "OK");
\r
156 Element rightChild = (Element)selectSingleNode(right,leftChildName);
\r
157 if (rightChild == null){
\r
158 TreeWalkEntry entry = new TreeWalkEntry();
\r
159 entry.lpath = leftChildPath;
\r
160 entry.status = TreeWalkEntry.STATUS.R_MISSING;
\r
161 msgList.add(entry);
\r
164 foundRightMap.put(leftChildName, "OK");
\r
165 String leftChildTextTrim = leftChild.getText().trim();
\r
166 String rightChildTextTrim = rightChild.getText().trim();
\r
167 TreeWalkEntry entry = new TreeWalkEntry();
\r
168 entry.ltextTrimmed = leftChildTextTrim;
\r
169 entry.rtextTrimmed = rightChildTextTrim;
\r
170 entry.lpath = leftChildPath;
\r
171 entry.rpath = leftChildPath; //same
\r
173 if (leftChildTextTrim.equals(rightChildTextTrim)){
\r
174 entry.status = TreeWalkEntry.STATUS.MATCHED;
\r
175 msgList.add(entry);
\r
177 entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;
\r
178 msgList.add(entry);
\r
180 //============ DIVE !! =====================================================
\r
181 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);
\r
184 for (Object r : right.getChildren()){
\r
185 if (!(r instanceof Element)){
\r
188 Element rightChild = (Element)r;
\r
189 String rname = rightChild.getName();
\r
190 if (null==foundRightMap.get(rname)){
\r
191 String rightChildPath = Tools.glue(parentPath, "/", rname);
\r
193 TreeWalkEntry entry = new TreeWalkEntry();
\r
194 entry.rpath = rightChildPath;
\r
195 entry.status = TreeWalkEntry.STATUS.R_ADDED;
\r
196 msgList.add(entry);
\r
202 private static void dumpXML_OUT(Element el) throws Exception {
\r
203 XMLOutputter outputter = new XMLOutputter();
\r
204 outputter.output(el, System.out);
\r
206 private static String dumpXML(Element el) throws Exception {
\r
207 XMLOutputter outputter = new XMLOutputter();
\r
208 return outputter.outputString(el);
\r
211 public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)
\r
213 //todo: deal with foundRightMap in this repeating field block.
\r
214 List rightList = select(right, leftChildName);
\r
215 if (rightList == null || rightList.size() == 0 || rightList.size() < leftList.size()){
\r
216 TreeWalkEntry twe = new TreeWalkEntry();
\r
217 twe.lpath = leftChildPath;
\r
218 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
219 String rmsg = (rightList == null)
\r
221 : " Right: "+rightList.size();
\r
222 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;
\r
226 if (rightList.size() > leftList.size()){
\r
227 TreeWalkEntry twe = new TreeWalkEntry();
\r
228 twe.lpath = leftChildPath;
\r
229 twe.status = TreeWalkEntry.STATUS.R_ADDED;
\r
230 twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+" Right: "+rightList.size();
\r
235 for (Object le : leftList){
\r
236 boolean found = false;
\r
237 Element leftEl = (Element)le;
\r
238 //pl("left", leftEl);
\r
239 for(Object re : rightList){
\r
240 Element rightEl = (Element)re;
\r
241 //pl("right", rightEl);
\r
242 TreeWalkResults msgListInner = new TreeWalkResults();
\r
243 treeWalk(leftEl, rightEl, leftChildPath, msgListInner);
\r
244 if (msgListInner.isStrictMatch()){
\r
246 TreeWalkEntry twe = new TreeWalkEntry();
\r
247 twe.lpath = leftChildPath;
\r
248 twe.status = TreeWalkEntry.STATUS.MATCHED;
\r
250 //System.out.println("===========================\r\nfound match for "+leftEl+"\r\n===========================\r\n");
\r
251 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
256 TreeWalkEntry twe = new TreeWalkEntry();
\r
257 twe.lpath = leftChildPath;
\r
258 twe.status = TreeWalkEntry.STATUS.R_MISSING;
\r
259 twe.message = "Repeating field not matched. Source: {"+dumpXML(leftEl)+"}";
\r
267 private static void pl(String name, Element el) throws Exception {
\r
268 Object lobid = selectSingleNode(el, "@ID");
\r
271 lid = lobid.toString();
\r
274 System.out.println(name+": "+lid);
\r
276 System.out.println();
\r