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