001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.schema;
019:
020: import java.io.PrintStream;
021: import java.util.Iterator;
022:
023: /**
024: * Helper class that prints differences between schema object models.
025: * Useful for debugging.
026: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
027: * @version $Revision: 438373 $
028: */
029: public class SchemaDiff {
030:
031: private PrintStream out;
032:
033: public SchemaDiff() {
034: this (System.err);
035: }
036:
037: public SchemaDiff(PrintStream out) {
038: this .out = out;
039: }
040:
041: public void printDifferences(Schema one, Schema two) {
042: for (Iterator it = one.getComplexTypes().iterator(); it
043: .hasNext();) {
044: GlobalComplexType complexType = (GlobalComplexType) it
045: .next();
046: if (!two.getComplexTypes().contains(complexType)) {
047: boolean matched = false;
048: for (Iterator otherIter = two.getComplexTypes()
049: .iterator(); it.hasNext();) {
050: GlobalComplexType otherType = (GlobalComplexType) otherIter
051: .next();
052: if (otherType.getName().equals(
053: complexType.getName())) {
054: printDifferences(complexType, otherType);
055: matched = true;
056: break;
057: }
058: }
059: if (!matched) {
060: out.println("Missing Complex type: " + complexType);
061: }
062: }
063: }
064:
065: }
066:
067: public void printDifferences(GlobalComplexType one,
068: GlobalComplexType two) {
069: out.println("Type " + one + " is not equal to " + two);
070: for (Iterator it = one.getElements().iterator(); it.hasNext();) {
071: Element elementOne = (Element) it.next();
072: if (!two.getElements().contains(elementOne)) {
073: boolean matched = false;
074: for (Iterator otherIter = two.getElements().iterator(); it
075: .hasNext();) {
076: Element elementTwo = (Element) otherIter.next();
077: if (elementOne.getName().equals(
078: elementTwo.getName())) {
079: printDifferences(elementOne, elementTwo);
080: matched = true;
081: break;
082: }
083: }
084: if (!matched) {
085: out.println("Missing Element: " + elementOne);
086: }
087: }
088: }
089: for (Iterator it = one.getAttributes().iterator(); it.hasNext();) {
090: Attribute attributeOne = (Attribute) it.next();
091: if (!two.getAttributes().contains(attributeOne)) {
092: boolean matched = false;
093: for (Iterator otherIter = two.getAttributes()
094: .iterator(); it.hasNext();) {
095: Attribute attributeTwo = (Attribute) otherIter
096: .next();
097: if (attributeTwo.getName().equals(
098: attributeTwo.getName())) {
099: printDifferences(attributeOne, attributeTwo);
100: matched = true;
101: break;
102: }
103: }
104: if (!matched) {
105: out.println("Missing Attribute: " + attributeOne);
106: }
107: }
108: }
109: }
110:
111: private void printDifferences(Attribute one, Attribute two) {
112: out.println("Attribute " + one + " is not equals to " + two);
113: }
114:
115: private void printDifferences(Element one, Element two) {
116: out.println("Element " + one + " is not equals to " + two);
117: }
118: }
|