1 19 20 28 29 package org.netbeans.test.j2ee.lib; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.IOException ; 35 import java.util.Iterator ; 36 import java.util.LinkedList ; 37 import java.util.List ; 38 import java.util.jar.Attributes ; 39 import java.util.jar.Manifest ; 40 import javax.xml.parsers.DocumentBuilder ; 41 import javax.xml.parsers.DocumentBuilderFactory ; 42 import javax.xml.parsers.ParserConfigurationException ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.NamedNodeMap ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.xml.sax.SAXException ; 48 49 53 public final class ContentComparator { 54 55 56 private ContentComparator() { 57 } 58 59 67 public static boolean equalsXML(File f1, File f2) { 68 try { 69 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 70 DocumentBuilder db = dbf.newDocumentBuilder(); 71 Document d1 = db.parse(f1); 72 Document d2 = db.parse(f2); 73 return compare(d1.getDocumentElement(), d2.getDocumentElement()); 74 } catch (ParserConfigurationException e) { 75 System.err.println("Exception from test - comparing XML files"); 76 e.printStackTrace(System.err); 77 } catch (SAXException e) { 78 System.err.println("Exception from test - comparing XML files"); 79 e.printStackTrace(System.err); 80 } catch (IOException e) { 81 System.err.println("Exception from test - comparing XML files"); 82 e.printStackTrace(System.err); 83 } 84 return false; 85 } 86 87 95 public static boolean equalsManifest(File mf1, File mf2, String [] ignoredEntries) { 96 if (ignoredEntries == null) { 97 ignoredEntries = new String [] {}; 98 } 99 try { 100 Manifest m1 = new Manifest (new FileInputStream (mf1)); 101 Manifest m2 = new Manifest (new FileInputStream (mf2)); 102 Attributes a1 = m1.getMainAttributes(); 103 Attributes a2 = m2.getMainAttributes(); 104 if (a1.size() != a2.size()) { 105 return false; 106 } 107 for (Iterator i = a1.keySet().iterator(); i.hasNext();) { 108 Attributes.Name a = (Attributes.Name ) i.next(); 109 boolean b = true; 110 for (int j = 0; j < ignoredEntries.length; j++) { 111 if (a.toString().equals(ignoredEntries[j])) { 112 a2.remove(a); 113 b = false; 114 break; 115 } 116 } 117 if (b && (a1.get(a).equals(a2.get(a)))) { 118 a2.remove(a); 119 } 120 } 121 return a2.isEmpty(); 122 } catch (FileNotFoundException fnfe) { 123 System.err.println("Exception from test - comparing manifests"); 124 fnfe.printStackTrace(System.err); 125 } catch (IOException ioe) { 126 System.err.println("Exception from test - comparing manifests"); 127 ioe.printStackTrace(System.err); 128 } 129 return false; 130 } 131 132 private static boolean compare(Node n1, Node n2) { 135 List l1 = new LinkedList (); 136 List l2 = new LinkedList (); 137 l1.add(n1); 138 l2.add(n2); 139 while (!l1.isEmpty() && !l2.isEmpty()) { 140 Node m1 = (Node ) l1.remove(0); 141 Node m2 = (Node ) l2.remove(0); 142 if (sameNode(m1, m2)) { 144 NodeList nl = m1.getChildNodes(); 146 for (int i = 0; i < nl.getLength(); i++) { 147 Node e = nl.item(i); 148 if (e.getNodeType() == Node.TEXT_NODE) { 149 if (e.getNodeValue().trim().equals("")) { 151 continue; 152 } 153 } 154 l1.add(nl.item(i)); 155 } 156 nl = m2.getChildNodes(); 157 for (int i = 0; i < nl.getLength(); i++) { 158 Node e = nl.item(i); 159 if (e.getNodeType() == Node.TEXT_NODE) { 160 if (e.getNodeValue().trim().equals("")) { 162 continue; 163 } 164 } 165 l2.add(nl.item(i)); 166 } 167 } else { 168 System.err.println("================================================"); 170 System.err.println("m1: " + m1.getNodeName() + "; \'" + m1.getNodeValue() + "\'"); 171 System.err.println("m2: " + m2.getNodeName() + "; \'" + m2.getNodeValue() + "\'"); 172 System.err.println("================================================"); 173 return false; 174 } 175 } 176 return true; 177 } 178 179 private static boolean sameNode(Node n1, Node n2) { 181 if (!n1.getNodeName().equals(n2.getNodeName())) { 183 System.err.println("================================================"); 184 System.err.println("Expected node: " + n1.getNodeName() + ", got: " + n2.getNodeName()); 185 System.err.println("================================================"); 186 return false; 187 } 188 if (!((n1.getNodeValue() != null) 190 ? n1.getNodeValue().equals(n2.getNodeValue()) 191 : (n2.getNodeValue() == null))) { 192 System.err.println("================================================"); 193 System.err.println("Expected node value: " + n1.getNodeValue() + ", got: " + n2.getNodeValue()); 194 System.err.println("================================================"); 195 return false; 196 } 197 NamedNodeMap nnm1 = n1.getAttributes(); 199 NamedNodeMap nnm2 = n2.getAttributes(); 200 if ((nnm1 == null && nnm2 != null) 201 || (nnm1 != null && nnm2 == null)) { 202 return false; 203 } 204 if (nnm1 == null && nnm2 == null) { 205 return true; 206 } 207 for (int i = 0; i < nnm1.getLength(); i++) { 208 Node x = nnm1.item(i); 209 Node y = nnm2.item(i); 210 if (!(x.getNodeName().equals(y.getNodeName()) 211 && x.getNodeValue().equals(y.getNodeValue()))) { 212 System.err.println("================================================"); 214 System.err.println("Expected attribute: " + x.getNodeName() + "=\'" + x.getNodeValue() + "\'," 215 + " got: " + y.getNodeName() + "=\'" + y.getNodeValue() + "\'"); 216 System.err.println("================================================"); 217 return false; 218 } 219 } 220 return true; 221 } 222 223 } 224 | Popular Tags |