]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bf7d3ee8bad05c739782f9144642391d3adf0b47
[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 import org.jdom.output.XMLOutputter;\r
42 \r
43 /**\r
44  * User: laramie\r
45  * $LastChangedRevision:  $\r
46  * $LastChangedDate:  $\r
47  */\r
48 public class XmlCompareJdom {\r
49 \r
50 private static final String DEFAULT_SAX_DRIVER_CLASS = "org.apache.xerces.parsers.SAXParser";\r
51 \r
52     public static org.jdom.Document getDocumentFromContent(String source) throws IOException, JDOMException {\r
53         org.jdom.Document doc;\r
54         SAXBuilder builder;\r
55         builder = new SAXBuilder();\r
56         builder.setValidation(false); //has no effect, I think.\r
57         doc = builder.build(new StringReader(source));\r
58         return doc;\r
59     }\r
60 \r
61     public static TreeWalkResults compareParts(String expectedContent, String leftID, String actualPartContent, String rightID){\r
62         TreeWalkResults list = new TreeWalkResults();\r
63         try {\r
64 \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
75                 list.add(entry);\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
80                 list.add(entry);\r
81             } else {\r
82                 Document expected = getDocumentFromContent(expectedContent);\r
83                 Document actual = getDocumentFromContent(actualPartContent);\r
84                 treeWalk(expected, actual, list);\r
85             }\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
92                 list.add(entry);\r
93         }\r
94         return list;\r
95     }\r
96 \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
100     }\r
101 \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
105     }\r
106 \r
107 \r
108 \r
109 \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
112         return res;\r
113     }\r
114 \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
118             return true;\r
119         }\r
120         if (left == null){\r
121             return false;\r
122         }\r
123         if (right == null){\r
124             return false;\r
125         }\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
132                 continue;\r
133             }\r
134             Element leftChild = (Element)o;\r
135             String leftChildName = leftChild.getName();\r
136             if (Tools.isEmpty(leftChildName)){\r
137                 continue;\r
138             }\r
139             String leftChildPath = Tools.glue(parentPath, "/", leftChildName);\r
140 \r
141             if (foundRepeatingList.indexOf(leftChildPath)>=0){\r
142                 continue;\r
143             }\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
152                     return false;\r
153                 }\r
154                 foundRightMap.put(leftChildName, "OK");\r
155             } else {\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
162                     continue;\r
163                 }\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
172 \r
173                 if (leftChildTextTrim.equals(rightChildTextTrim)){\r
174                     entry.status = TreeWalkEntry.STATUS.MATCHED;\r
175                     msgList.add(entry);\r
176                 } else {\r
177                     entry.status = TreeWalkEntry.STATUS.TEXT_DIFFERENT;\r
178                     msgList.add(entry);\r
179                 }\r
180                 //============ DIVE !! =====================================================\r
181                 result = result && treeWalk( leftChild, rightChild, leftChildPath, msgList);\r
182             }\r
183         }\r
184         for (Object r : right.getChildren()){\r
185             if (!(r instanceof Element)){\r
186                 continue;\r
187             }\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
192 \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
197             }\r
198         }\r
199         return true;\r
200     }\r
201 \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
205     }\r
206     private static String dumpXML(Element el) throws Exception {\r
207         XMLOutputter outputter = new XMLOutputter();\r
208         return outputter.outputString(el);\r
209     }\r
210 \r
211     public static boolean doRepeatingFieldComparison(List leftList, String leftChildPath, String leftChildName, Element left, Element right, TreeWalkResults msgList)\r
212     throws Exception {\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
220                     ? " Right: 0"\r
221                     : " Right: "+rightList.size();\r
222             twe.message = "Repeating field count not matched. Field: "+leftChildPath+" Left: "+leftList.size()+rmsg;\r
223             msgList.add(twe);\r
224             return false;\r
225         }\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
231             msgList.add(twe);\r
232             return false;\r
233         }\r
234 \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
245                     found = true;\r
246                     TreeWalkEntry twe = new TreeWalkEntry();\r
247                     twe.lpath = leftChildPath;\r
248                     twe.status = TreeWalkEntry.STATUS.MATCHED;\r
249                     msgList.add(twe);\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
252                     break;\r
253                 }\r
254             }\r
255             if ( ! found){\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
260                 msgList.add(twe);\r
261                 return false;\r
262             }\r
263         }\r
264         return true;\r
265     }\r
266 \r
267     private static void pl(String name, Element el) throws Exception {\r
268         Object lobid = selectSingleNode(el, "@ID");\r
269         String lid = "";\r
270         if (lobid!=null){\r
271             lid = lobid.toString();\r
272         }\r
273 \r
274         System.out.println(name+": "+lid);\r
275         dumpXML_OUT(el);\r
276         System.out.println();\r
277 \r
278     }\r
279 }\r