summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-stable-20060304/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java
blob: a3772816222fa19051579deaf3a67d12944892fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.sdo.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestUtil
{
  private static void getAllNodes(NodeList nodeList, List<Node> nodes)
  {
    int length = nodeList.getLength();
    if (length == 0)
    {
      return;
    }
    
    for (int i=0; i<length; i++)
    {
      Node node = nodeList.item(i);
      nodes.add(node);
      getAllNodes(node.getChildNodes(), nodes);
    } // for
  }
  
  private static boolean equalNodes(NodeList sourceNodeList, NodeList targetNodeList)
  {
    ArrayList<Node> sourceNodes = new ArrayList<Node>();
    ArrayList<Node> targetNodes = new ArrayList<Node>();
    
    getAllNodes(sourceNodeList, sourceNodes);
    getAllNodes(targetNodeList, targetNodes);
    
    int sourceLength = sourceNodes.size();
    int targetLength = targetNodes.size();
    
    if (sourceLength != targetLength)
    {
      return false;
    }
    
    for (int i=0; i<sourceLength; i++)
    {
      Node sourceNode = sourceNodes.get(i);
      Node targetNode = targetNodes.get(i);
      
      if (!sourceNode.isEqualNode(targetNode))
      {
        return false;
      }
    } // for
    
    return true;
  }
  
  public static boolean equalXmlFiles(URL source, URL target)
  { 
    try {
      return equalXmlFiles(source.openStream(), target.openStream());
    }
    catch (IOException e)
    {
      return false;
    }
  }
  
  public static boolean equalXmlFiles(InputStream sourceStream, URL target)
  {
    try {
      return equalXmlFiles(sourceStream, target.openStream());
    }
    catch (IOException e)
    {
      return false;
    }
  }
  
  public static boolean equalXmlFiles(URL source, InputStream targetStream)
  {
    try {
      return equalXmlFiles(source.openStream(), targetStream);
    }
    catch (IOException e)
    {
      return false;
    }
  }
  
  public static boolean equalXmlFiles(InputStream sourceStream, InputStream targetStream)
  {
    DocumentBuilder builder;
    Document sourceDocument;
    Document targetDocument;
    
    try {
      builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      sourceDocument = builder.parse(sourceStream);
      targetDocument = builder.parse(targetStream);
    }
    catch (FactoryConfigurationError fce) {
      return false;
    }
    catch (ParserConfigurationException ce) {
      return false;
    }
    catch (SAXException se)
    {
      return false;
    }
    catch (IOException ie)
    {
      return false;
    }

    sourceDocument.normalizeDocument();
    targetDocument.normalizeDocument();
    
    if (!sourceDocument.getXmlVersion().equals(targetDocument.getXmlVersion()))
    {
      return false;
    }
    
    String sourceXmlEncoding = sourceDocument.getXmlEncoding();
    String targetXmlEncoding = targetDocument.getXmlEncoding();
    
    if (sourceXmlEncoding != null && targetXmlEncoding != null &&
        sourceXmlEncoding.equalsIgnoreCase(targetXmlEncoding))
    {
      // continue
    }
    else
    {
      return false;
    }

    NodeList sourceNodes = sourceDocument.getChildNodes();
    NodeList targetNodes = targetDocument.getChildNodes();
    
    return equalNodes(sourceNodes, targetNodes);
  }
}