1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  
19  package org.apache.commons.betwixt.schema;
20  
21  import java.io.PrintStream;
22  import java.util.Iterator;
23  
24  /**
25   * Helper class that prints differences between schema object models.
26   * Useful for debugging.
27   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
28   * @version $Revision: 561314 $
29   */
30  public class SchemaDiff {
31      
32      private PrintStream out;
33      
34      public SchemaDiff() {
35          this(System.err);
36      }
37      
38      public SchemaDiff(PrintStream out) {
39          this.out = out;
40      }
41      
42      public void printDifferences(Schema one, Schema two) {
43          for( Iterator it=one.getComplexTypes().iterator();it.hasNext(); ) {
44              GlobalComplexType complexType = (GlobalComplexType)it.next();
45              if (!two.getComplexTypes().contains(complexType)) {
46                  boolean matched = false;
47                  for (Iterator otherIter=two.getComplexTypes().iterator(); it.hasNext();) {
48                      GlobalComplexType otherType = (GlobalComplexType) otherIter.next();
49                      if (otherType.getName().equals(complexType.getName())) {
50                          printDifferences(complexType, otherType);
51                          matched = true;
52                          break;
53                      }
54                  }
55                  if (!matched) {
56                      out.println("Missing Complex type: " + complexType);
57                  }
58              }
59          }          
60          
61      }
62      
63      public void printDifferences(GlobalComplexType one, GlobalComplexType two) {
64          out.println("Type " + one + " is not equal to " + two);
65          for (Iterator it = one.getElements().iterator(); it.hasNext();) {
66              Element elementOne = (Element) it.next();
67              if (!two.getElements().contains(elementOne)) {
68                  boolean matched = false;
69                  for (Iterator otherIter=two.getElements().iterator(); it.hasNext();) {
70                      Element elementTwo = (Element) otherIter.next();
71                      if (elementOne.getName().equals(elementTwo.getName())) {
72                          printDifferences(elementOne, elementTwo);
73                          matched = true;
74                          break;
75                      }
76                  }
77                  if (!matched) {
78                      out.println("Missing Element: " + elementOne);
79                  }                
80              }
81          }
82          for (Iterator it = one.getAttributes().iterator(); it.hasNext();) {
83              Attribute attributeOne = (Attribute) it.next();
84              if (!two.getAttributes().contains(attributeOne)) {
85                  boolean matched = false;
86                  for (Iterator otherIter=two.getAttributes().iterator(); it.hasNext();) {
87                      Attribute attributeTwo = (Attribute) otherIter.next();
88                      if (attributeTwo.getName().equals(attributeTwo.getName())) {
89                          printDifferences(attributeOne, attributeTwo);
90                          matched = true;
91                          break;
92                      }
93                  }
94                  if (!matched) {
95                      out.println("Missing Attribute: " + attributeOne);
96                  }                
97              }
98          }
99      }
100     
101     private void printDifferences(Attribute one , Attribute two) {
102         out.println("Attribute " + one + " is not equals to " + two);
103     }
104     
105     private void printDifferences(Element one , Element two) {
106         out.println("Element " + one + " is not equals to " + two);
107     }
108 }