1/*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.releng.generators;
12
13import java.io.IOException;
14import java.util.Enumeration;
15import java.util.Hashtable;
16import java.io.File;
17
18import java.util.Vector;
19import javax.xml.parsers.DocumentBuilderFactory;
20import javax.xml.parsers.DocumentBuilder;
21import javax.xml.parsers.ParserConfigurationException;
22
23import org.w3c.dom.Document;
24import org.w3c.dom.Element;
25import org.w3c.dom.Node;
26import org.w3c.dom.NodeList;
27import org.xml.sax.SAXException;
28
29/**
30 * @version 	1.0
31 * @author
32 */
33public class ErrorTracker {
34
35	// List of test logs expected at end of build
36	private Vector testLogs = new Vector();
37
38
39	// Platforms keyed on
40	private Hashtable platforms = new Hashtable();
41	private Hashtable logFiles = new Hashtable();
42	private Hashtable typesMap = new Hashtable();
43	private Vector typesList = new Vector();
44
45	public static void main(String[] args) {
46
47		// For testing only.  Should not be invoked
48
49		ErrorTracker anInstance = new ErrorTracker();
50		anInstance.loadFile("C:\\junk\\testManifest.xml");
51		String[] theTypes = anInstance.getTypes();
52		for (int i=0; i < theTypes.length; i++) {
53			// System.out.println("Type: " + theTypes[i]);
54			PlatformStatus[] thePlatforms = anInstance.getPlatforms(theTypes[i]);
55			for (int j=0; j < thePlatforms.length; j++) {
56				// System.out.println("Out ID: " + thePlatforms[j].getId());
57			}
58		}
59	}
60
61	public void loadFile(String fileName) {
62		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
63		DocumentBuilder parser=null;
64		try {
65			parser = docBuilderFactory.newDocumentBuilder();
66		} catch (ParserConfigurationException e1) {
67			e1.printStackTrace();
68		}
69		try {
70
71			Document document = parser.parse(fileName);
72			NodeList elements = document.getElementsByTagName("platform");
73			int elementCount = elements.getLength();
74			for (int i = 0; i < elementCount; i++) {
75				PlatformStatus aPlatform = new PlatformStatus((Element) elements.item(i));
76				//System.out.println("ID: " + aPlatform.getId());
77				platforms.put(aPlatform.getId(), aPlatform);
78
79				Node zipType = elements.item(i).getParentNode();
80				String zipTypeName = (String) zipType.getAttributes().getNamedItem("name").getNodeValue();
81
82				Vector aVector = (Vector) typesMap.get(zipTypeName);
83				if (aVector == null) {
84					typesList.add(zipTypeName);
85					aVector = new Vector();
86					typesMap.put(zipTypeName, aVector);
87				}
88				aVector.add(aPlatform.getId());
89
90			}
91
92			NodeList effectedFiles = document.getElementsByTagName("effectedFile");
93			int effectedFilesCount = effectedFiles.getLength();
94			for (int i = 0; i < effectedFilesCount; i++) {
95				Node anEffectedFile = effectedFiles.item(i);
96				Node logFile = anEffectedFile.getParentNode();
97				String logFileName = (String) logFile.getAttributes().getNamedItem("name").getNodeValue();
98				logFileName=convertPathDelimiters(logFileName);
99				String effectedFileID = (String) anEffectedFile.getAttributes().getNamedItem("id").getNodeValue();
100				//System.out.println(logFileName);
101				Vector aVector = (Vector) logFiles.get(logFileName);
102				if (aVector == null) {
103					aVector = new Vector();
104					logFiles.put(logFileName, aVector);
105
106				}
107				PlatformStatus ps=(PlatformStatus) platforms.get(effectedFileID);
108				if (ps!=null)
109					aVector.addElement(ps);
110			}
111
112			// store a list of the test logs expected after testing
113			NodeList testLogList = document.getElementsByTagName("logFile");
114				int testLogCount = testLogList.getLength();
115				for (int i = 0; i < testLogCount; i++) {
116
117					Node testLog = testLogList.item(i);
118					String testLogName = (String) testLog.getAttributes().getNamedItem("name").getNodeValue();
119					Node typeNode=testLog.getAttributes().getNamedItem("type");
120					String type="test";
121					if (typeNode!=null){
122						type = typeNode.getNodeValue();
123					}
124					if (testLogName.endsWith(".xml")&&type.equals("test")){
125						testLogs.add(testLogName);
126						//System.out.println(testLogName);
127					}
128
129			}
130
131
132//			// Test this mess.
133//			Object[] results = platforms.values().toArray();
134//			for (int i=0; i < results.length; i++) {
135//				PlatformStatus ap = (PlatformStatus) results[i];
136//				System.out.println("ID: " + ap.getId() + " passed: " + ap.getPassed());
137//			}
138//
139//			Enumeration anEnumeration = logFiles.keys();
140//			while (anEnumeration.hasMoreElements()) {
141//				String aKey = (String) anEnumeration.nextElement();
142//				System.out.println("Whack a key: " + aKey);
143//				((PlatformStatus) logFiles.get(aKey)).setPassed(false);
144//			}
145//
146//			results = platforms.values().toArray();
147//			for (int i=0; i < results.length; i++) {
148//				PlatformStatus ap = (PlatformStatus) results[i];
149//				System.out.println("ID: " + ap.getId() + " passed: " + ap.getPassed());
150//			}
151
152
153
154
155
156		} catch (IOException e) {
157			System.out.println("IOException: " + fileName);
158			// e.printStackTrace();
159
160		} catch (SAXException e) {
161			System.out.println("SAXException: " + fileName);
162			e.printStackTrace();
163
164		}
165	}
166
167	public void registerError(String fileName) {
168		// System.out.println("Found an error in: " + fileName);
169		if (logFiles.containsKey(fileName)) {
170			Vector aVector = (Vector) logFiles.get(fileName);
171			for (int i = 0; i < aVector.size(); i++) {
172				((PlatformStatus) aVector.elementAt(i)).registerError();
173			}
174		} else {
175
176			// If a log file is not specified explicitly it effects
177			// all "platforms" except JDT
178
179			Enumeration values = platforms.elements();
180			while (values.hasMoreElements()) {
181				PlatformStatus aValue = (PlatformStatus) values.nextElement();
182				if (!aValue.getId().equals("JA") &&
183					!aValue.getId().equals("EW") &&
184					!aValue.getId().equals("EA")) {
185						aValue.registerError();
186				}
187			}
188		}
189	}
190
191	public boolean hasErrors(String id) {
192		return ((PlatformStatus) platforms.get(id)).hasErrors();
193	}
194
195	// Answer a string array of the zip type names in the order they appear in
196	// the .xml file.
197	public String[] getTypes() {
198		return (String[]) typesList.toArray(new String[typesList.size()]);
199	}
200
201	// Answer an array of PlatformStatus objects for a given type.
202
203	public PlatformStatus[] getPlatforms(String type) {
204		Vector platformIDs = (Vector) typesMap.get(type);
205		PlatformStatus[] result = new PlatformStatus[platformIDs.size()];
206		for (int i = 0; i < platformIDs.size(); i++) {
207			result[i] = (PlatformStatus) platforms.get((String) platformIDs.elementAt(i));
208		}
209		return  result;
210	}
211
212	/**
213	 * Returns the testLogs.
214	 * @return Vector
215	 */
216	public Vector getTestLogs() {
217		return testLogs;
218	}
219
220	private String convertPathDelimiters(String path){
221		return new File(path).getPath();
222	}
223
224}
225