]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
18d510657083a2093144809e4398a7a99460ea27
[tmp/jakarta-migration.git] /
1 /**\r
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
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c) 2009 Regents of the University of California\r
10  *\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
13  *\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
16  *\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
22  */\r
23 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
24 \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
31 \r
32 \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
39 \r
40 import org.collectionspace.services.IntegrationTests.xmlreplay.TreeWalkResults.TreeWalkEntry;\r
41 \r
42 /**\r
43  * User: laramie\r
44  * $LastChangedRevision:  $\r
45  * $LastChangedDate:  $\r
46  */\r
47 public class XmlCompareJdom {\r
48 \r
49 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";\r
50 \r
51     public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {\r
52         org.jdom.Document doc;\r
53         SAXBuilder builder;\r
54         builder = new SAXBuilder();\r
55         builder.setValidation(false); //has no effect, I think.\r
56         doc = builder.build(new StringReader(source));\r
57         return doc;\r
58     }\r
59 \r
60     public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){\r
61         TreeWalkResults list = new TreeWalkResults();\r
62         try {\r
63 \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
74                 list.add(entry);\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
79                 list.add(entry);\r
80             } else {\r
81                 Document expected = getDocumentFromContent(expectedContent);\r
82                 Document actual = getDocumentFromContent(actualPartContent);\r
83                 treeWalk(expected, actual, list);\r
84             }\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
91                 list.add(entry);\r
92         }\r
93         return list;\r
94     }\r
95 \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
99     }\r
100 \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
104     }\r
105 \r
106 \r
107 \r
108 \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
111         return res;\r
112     }\r
113 \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
117             return true;\r
118         }\r
119         if (left == null){\r
120             return false;\r
121         }\r
122         if (right == null){\r
123             return false;\r
124         }\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
130                 continue;\r
131             }\r
132             Element leftChild = (Element)o;\r
133             String leftChildName = leftChild.getName();\r
134             if (Tools.isEmpty(leftChildName)){\r
135                 continue;\r
136             }\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
144                 continue;\r
145             }\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
154 \r
155             if (leftChildTextTrim.equals(rightChildTextTrim)){\r
156                 entry.status = TreeWalkEntry.STATUS.MATCHED;\r
157                 msgList.add(entry);\r
158             } else {\r
159                 entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;\r
160                 msgList.add(entry);\r
161             }\r
162 \r
163             //============ DIVE !! =====================================================\r
164             result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);\r
165         }\r
166         for (Object r : right.getChildren()){\r
167             if (!(r instanceof Element)){\r
168                 continue;\r
169             }\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
174 \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
179             }\r
180         }\r
181         return true;\r
182     }\r
183     \r
184 }\r