]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
837b605f35f07ce1c49a6bd349ecda6eaf75e700
[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.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
32 \r
33 \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
40 \r
41 import org.collectionspace.services.IntegrationTests.xmlreplay.TreeWalkResults.TreeWalkEntry;\r
42 import org.jdom.output.XMLOutputter;\r
43 \r
44 /**\r
45  * User: laramie\r
46  * $LastChangedRevision:  $\r
47  * $LastChangedDate:  $\r
48  */\r
49 public class XmlCompareJdom {\r
50 \r
51 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";\r
52 \r
53     public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {\r
54         org.jdom.Document doc;\r
55         SAXBuilder builder;\r
56         builder = new SAXBuilder();\r
57         builder.setValidation(false); //has no effect, I think.\r
58         doc = builder.build(new StringReader(source));\r
59         return doc;\r
60     }\r
61 \r
62     public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){\r
63         TreeWalkResults list = new TreeWalkResults();\r
64         try {\r
65 \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
76                 list.add(entry);\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
81                 list.add(entry);\r
82             } else {\r
83                 Document expected = getDocumentFromContent(expectedContent);\r
84                 Document actual = getDocumentFromContent(actualPartContent);\r
85                 treeWalk(expected, actual, list);\r
86             }\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
93                 list.add(entry);\r
94         }\r
95         return list;\r
96     }\r
97 \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
101     }\r
102 \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
106     }\r
107 \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
113     }\r
114 \r
115 \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
118         return res;\r
119     }\r
120 \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
124             return true;\r
125         }\r
126         if (left == null){\r
127             return false;\r
128         }\r
129         if (right == null){\r
130             return false;\r
131         }\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
138                 continue;\r
139             }\r
140             Element leftChild = (Element)o;\r
141             String leftChildName = leftChild.getName();\r
142             if (Tools.isEmpty(leftChildName)){\r
143                 continue;\r
144             }\r
145             String leftChildPath = Tools.glue(parentPath, "/", leftChildName);\r
146 \r
147             if (foundRepeatingList.indexOf(leftChildPath)>=0){\r
148                 continue;\r
149             }\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
158                     return false;\r
159                 }\r
160                 foundRightMap.put(leftChildName, "OK");\r
161             } else {\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
168                     continue;\r
169                 }\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
178 \r
179                 if (leftChildTextTrim.equals(rightChildTextTrim)){\r
180                     entry.status = TreeWalkEntry.STATUS.MATCHED;\r
181                     msgList.add(entry);\r
182                 } else {\r
183                     entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;\r
184                     msgList.add(entry);\r
185                 }\r
186                 //============ DIVE !! =====================================================\r
187                 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);\r
188             }\r
189         }\r
190         for (Object r : right.getChildren()){\r
191             if (!(r instanceof Element)){\r
192                 continue;\r
193             }\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
198 \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
203             }\r
204         }\r
205         return true;\r
206     }\r
207 \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
211     }\r
212     private static String dumpXML(Element el) throws Exception {\r
213         XMLOutputter outputter = new XMLOutputter();\r
214         return outputter.outputString(el);\r
215     }\r
216 \r
217     public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)\r
218     throws Exception {\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
226                     ? " Right: 0"\r
227                     : " Right: "+rightList.size();\r
228             twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;\r
229             msgList.add(twe);\r
230             return false;\r
231         }\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
237             msgList.add(twe);\r
238             return false;\r
239         }\r
240 \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
251                     found = true;\r
252                     TreeWalkEntry twe = new TreeWalkEntry();\r
253                     twe.lpath = leftChildPath;\r
254                     twe.status = TreeWalkEntry.STATUS.MATCHED;\r
255                     msgList.add(twe);\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
258                     break;\r
259                 }\r
260             }\r
261             if ( ! found){\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
266                 msgList.add(twe);\r
267                 return false;\r
268             }\r
269         }\r
270         return true;\r
271     }\r
272 \r
273     private static void pl(String name, Element el) throws Exception {\r
274         Object lobid = selectSingleNode(el, "@ID");\r
275         String lid = "";\r
276         if (lobid!=null){\r
277             lid = lobid.toString();\r
278         }\r
279 \r
280         System.out.println(name+": "+lid);\r
281         dumpXML_OUT(el);\r
282         System.out.println();\r
283 \r
284     }\r
285 }\r