]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7d2672bca6514b2891c12cb56e41f71dd4baced6
[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.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 \r
109 \r
110 \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
113         return res;\r
114     }\r
115 \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
119             return true;\r
120         }\r
121         if (left == null){\r
122             return false;\r
123         }\r
124         if (right == null){\r
125             return false;\r
126         }\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
133                 continue;\r
134             }\r
135             Element leftChild = (Element)o;\r
136             String leftChildName = leftChild.getName();\r
137             if (Tools.isEmpty(leftChildName)){\r
138                 continue;\r
139             }\r
140             String leftChildPath = Tools.glue(parentPath, "/", leftChildName);\r
141 \r
142             if (foundRepeatingList.indexOf(leftChildPath)>=0){\r
143                 continue;\r
144             }\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
153                     return false;\r
154                 }\r
155                 foundRightMap.put(leftChildName, "OK");\r
156             } else {\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
163                     continue;\r
164                 }\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
173 \r
174                 if (leftChildTextTrim.equals(rightChildTextTrim)){\r
175                     entry.status = TreeWalkEntry.STATUS.MATCHED;\r
176                     msgList.add(entry);\r
177                 } else {\r
178                     entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;\r
179                     msgList.add(entry);\r
180                 }\r
181                 //============ DIVE !! =====================================================\r
182                 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);\r
183             }\r
184         }\r
185         for (Object r : right.getChildren()){\r
186             if (!(r instanceof Element)){\r
187                 continue;\r
188             }\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
193 \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
198             }\r
199         }\r
200         return true;\r
201     }\r
202 \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
206     }\r
207     private static String dumpXML(Element el) throws Exception {\r
208         XMLOutputter outputter = new XMLOutputter();\r
209         return outputter.outputString(el);\r
210     }\r
211 \r
212     public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)\r
213     throws Exception {\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
221                     ? " Right: 0"\r
222                     : " Right: "+rightList.size();\r
223             twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;\r
224             msgList.add(twe);\r
225             return false;\r
226         }\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
232             msgList.add(twe);\r
233             return false;\r
234         }\r
235 \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
246                     found = true;\r
247                     TreeWalkEntry twe = new TreeWalkEntry();\r
248                     twe.lpath = leftChildPath;\r
249                     twe.status = TreeWalkEntry.STATUS.MATCHED;\r
250                     msgList.add(twe);\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
253                     break;\r
254                 }\r
255             }\r
256             if ( ! found){\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
261                 msgList.add(twe);\r
262                 return false;\r
263             }\r
264         }\r
265         return true;\r
266     }\r
267 \r
268     private static void pl(String name, Element el) throws Exception {\r
269         Object lobid = selectSingleNode(el, "@ID");\r
270         String lid = "";\r
271         if (lobid!=null){\r
272             lid = lobid.toString();\r
273         }\r
274 \r
275         System.out.println(name+": "+lid);\r
276         dumpXML_OUT(el);\r
277         System.out.println();\r
278 \r
279     }\r
280 }\r