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
44 * $LastChangedRevision: $
\r
45 * $LastChangedDate: $
\r
47 public class XmlCompareJdom {
\r
49 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";
\r
51 public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {
\r
52 org.jdom.Document doc;
\r
54 builder = new SAXBuilder();
\r
55 builder.setValidation(false); //has no effect, I think.
\r
56 doc = builder.build(new StringReader(source));
\r
60 public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){
\r
61 TreeWalkResults list = new TreeWalkResults();
\r
64 list.leftID = leftID;
\r
65 list.rightID = rightID;
\r
66 TreeWalkResults.TreeWalkEntry infoentry = new TreeWalkResults.TreeWalkEntry();
\r
67 infoentry.status = TreeWalkResults.TreeWalkEntry.STATUS.INFO;
\r
68 infoentry.message = "\r\n LEFT file: "+leftID+"\r\n RIGHT file: "+rightID;
\r
69 list.add(infoentry);
\r
70 if (Tools.isEmpty(expectedContent)){
\r
71 TreeWalkEntry entry = new TreeWalkEntry();
\r
72 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
73 entry.errmessage = "L dom was empty.";
\r
75 } else if (Tools.isEmpty(actualPartContent)){
\r
76 TreeWalkEntry entry = new TreeWalkEntry();
\r
77 entry.errmessage = "R dom was empty.";
\r
78 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
81 Document expected = getDocumentFromContent(expectedContent);
\r
82 Document actual = getDocumentFromContent(actualPartContent);
\r
83 treeWalk(expected, actual, list);
\r
85 } catch (Throwable t){
\r
86 String msg = "ERROR in XmlReplay.compareParts(): "+t;
\r
87 System.out.println(msg);
\r
88 TreeWalkEntry entry = new TreeWalkEntry();
\r
89 entry.status = TreeWalkEntry.STATUS.DOC_ERROR;
\r
90 entry.errmessage = msg;
\r
96 public static List select(Element element, String xpathExpression) throws Exception {
\r
97 XPath xpath = new JDOMXPath(xpathExpression);
\r
98 return xpath.selectNodes(element);
\r
101 public static Object selectSingleNode(Element element, String xpathExpression) throws Exception {
\r
102 XPath xpath = new JDOMXPath(xpathExpression);
\r
103 return xpath.selectSingleNode(element);
\r
109 public static boolean treeWalk(Document left, Document right, TreeWalkResults list) throws Exception {
\r
110 boolean res = treeWalk(left.getRootElement(), right.getRootElement(), "/", list);
\r
114 public static boolean treeWalk(Element left, Element right, String parentPath, TreeWalkResults msgList) throws Exception {
\r
115 String SPACE = " ";
\r
116 if (left == null && right == null){
\r
122 if (right == null){
\r
125 List l = left.getChildren();
\r
126 Map foundRightMap = new HashMap();
\r
127 boolean result = true;
\r
128 for (Object o : l) {
\r
129 if (!(o instanceof Element)){
\r
132 Element leftChild = (Element)o;
\r
133 String leftChildName = leftChild.getName();
\r
134 if (Tools.isEmpty(leftChildName)){
\r
137 String leftChildPath = Tools.glue(parentPath, "/", leftChildName);
\r
138 Element rightChild = (Element)selectSingleNode(right,leftChildName);
\r
139 if (rightChild == null){
\r
140 TreeWalkEntry entry = new TreeWalkEntry();
\r
141 entry.lpath = leftChildPath;
\r
142 entry.status = TreeWalkEntry.STATUS.R_MISSING;
\r
143 msgList.add(entry);
\r
146 foundRightMap.put(leftChildName, "OK");
\r
147 String leftChildTextTrim = leftChild.getText().trim();
\r
148 String rightChildTextTrim = rightChild.getText().trim();
\r
149 TreeWalkEntry entry = new TreeWalkEntry();
\r
150 entry.ltextTrimmed = leftChildTextTrim;
\r
151 entry.rtextTrimmed = rightChildTextTrim;
\r
152 entry.lpath = leftChildPath;
\r
153 entry.rpath = leftChildPath; //same
\r
155 if (leftChildTextTrim.equals(rightChildTextTrim)){
\r
156 entry.status = TreeWalkEntry.STATUS.MATCHED;
\r
157 msgList.add(entry);
\r
159 entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;
\r
160 msgList.add(entry);
\r
163 //============ DIVE !! =====================================================
\r
164 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);
\r
166 for (Object r : right.getChildren()){
\r
167 if (!(r instanceof Element)){
\r
170 Element rightChild = (Element)r;
\r
171 String rname = rightChild.getName();
\r
172 if (null==foundRightMap.get(rname)){
\r
173 String rightChildPath = Tools.glue(parentPath, "/", rname);
\r
175 TreeWalkEntry entry = new TreeWalkEntry();
\r
176 entry.rpath = rightChildPath;
\r
177 entry.status = TreeWalkEntry.STATUS.R_ADDED;
\r
178 msgList.add(entry);
\r