Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 20bdb251b2cd59cb473f7cd1fab2fda871530328 (plain) (blame)
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
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.bundles.tests.apireport;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Queue;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.papyrus.bundles.tests.Activator;

/**
 * Encapsulation of the report resources in the workspace metadata area.
 */
public class ReportFixture {
	private static final IPath XML_REPORTS = new Path("apireports/xml"); //$NON-NLS-1$
	private static final IPath HTML_REPORTS = new Path("apireports/html"); //$NON-NLS-1$

	private static final IPath XML_REPORT_FILE = XML_REPORTS.append("api.xml"); //$NON-NLS-1$
	private static final IPath HTML_REPORT_FILE = HTML_REPORTS.append("api.html"); //$NON-NLS-1$

	private final File xmlReportFile;
	private final File htmlReportFile;

	/**
	 * Initializes the XML and HTML outputs of the API report. For example, certain
	 * stylesheets and images are emitted if necessary for the HTML report.
	 *
	 * @param baseOutputDir
	 *            the base directory in which to generate the resulting reports
	 * 
	 * @throws IOException
	 *             on any problem in initializing the contents of the output directory
	 */
	public ReportFixture(IPath baseOutputDir) throws IOException {
		super();

		xmlReportFile = baseOutputDir.append(XML_REPORT_FILE).toFile();
		htmlReportFile = baseOutputDir.append(HTML_REPORT_FILE).toFile();

		ensureContents(xmlReportFile.getParentFile(), XML_REPORTS);
		ensureContents(htmlReportFile.getParentFile(), HTML_REPORTS);
	}

	public File getXMLReportFile() {
		return xmlReportFile;
	}

	public File getHTMLReportFile() {
		return htmlReportFile;
	}

	private void ensureContents(File directory, IPath resourcePath) throws IOException {
		if (!directory.exists()) {
			directory.mkdirs();
		}

		IPath base = new Path(directory.getAbsolutePath());

		// Initial queue of resources to fetch
		Queue<String> queue = new LinkedList<>();
		enqueueResources(new Path("resources").append(resourcePath).toString(), queue); //$NON-NLS-1$

		for (String next = queue.poll(); next != null; next = queue.poll()) {
			// Enqueue further resources
			enqueueResources(next, queue);

			IPath path = new Path(next);
			if (!path.hasTrailingSeparator()) {
				// It's a file to be copied
				URL url = Activator.getDefault().getBundle().getEntry(next);
				if (url != null) {
					// Strip the "resources" segment also (the +1)
					copyResource(url, base.append(path.removeFirstSegments(resourcePath.segmentCount() + 1)));
				}
			}
		}
	}

	private void enqueueResources(String basePath, Queue<? super String> queue) {
		Enumeration<String> entries = Activator.getDefault().getBundle().getEntryPaths(basePath);
		if (entries != null) {
			while (entries.hasMoreElements()) {
				queue.add(entries.nextElement());
			}
		}
	}

	private void copyResource(URL source, IPath destination) throws IOException {
		File localFile = destination.toFile();

		if (!localFile.exists()) {
			if (!localFile.getParentFile().exists()) {
				localFile.getParentFile().mkdirs();
			}

			try (InputStream input = source.openStream()) {
				Files.copy(input, Paths.get(localFile.getAbsolutePath()));
			}
		}
	}
}

Back to the top