ReportOutputFolder.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
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 * Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.report;
13
14import java.io.IOException;
15import java.io.OutputStream;
16import java.util.HashMap;
17import java.util.Map;
18
19/**
20 * Logical representation of a folder in the output structure. This utility
21 * ensures valid and unique file names and helps to create relative links.
22 *
23 * @author Marc R. Hoffmann
24 * @version 0.4.1.20101007204400
25 */
26public class ReportOutputFolder {
27
28 private final IMultiReportOutput output;
29
30 private final ReportOutputFolder parent;
31
32 private final String path;
33
34 /** Cached sub-folder instances to guarantee stable normalization */
35 private final Map<String, ReportOutputFolder> subFolders = new HashMap<String, ReportOutputFolder>();
36
37 private final NormalizedFileNames fileNames;
38
39 /**
40 * Creates a new root folder for the given output.
41 *
42 * @param output
43 * output for generated files
44 */
45 public ReportOutputFolder(final IMultiReportOutput output) {
46 this(output, null, "");
47 }
48
49 /**
50 * Creates a new root folder for the given output.
51 *
52 * @param output
53 * output for generated files
54 */
55 private ReportOutputFolder(final IMultiReportOutput output,
56 final ReportOutputFolder parent, final String path) {
57 this.output = output;
58 this.parent = parent;
59 this.path = path;
60 fileNames = new NormalizedFileNames();
61 }
62
63 /**
64 * Creates a sub-folder with the given name.
65 *
66 * @param name
67 * name of the sub-folder
68 * @return handle for output into the sub-folder
69 */
70 public ReportOutputFolder subFolder(final String name) {
71 final String normalizedName = normalize(name);
72 ReportOutputFolder folder = subFolders.get(normalizedName);
73 if (folder != null) {
74 return folder;
75 }
76 folder = new ReportOutputFolder(output, this, path + normalizedName
77 + "/");
78 subFolders.put(normalizedName, folder);
79 return folder;
80 }
81
82 /**
83 * Creates a new file in this folder with the given local name.
84 *
85 * @param name
86 * name of the sub-folder
87 * @return handle for output into the sub-folder
88 * @throws IOException
89 * if the file creation fails
90 */
91 public OutputStream createFile(final String name) throws IOException {
92 return output.createFile(path + normalize(name));
93 }
94
95 /**
96 * Returns a link relative to a given base to a resource within this folder.
97 *
98 * @param base
99 * base to create the relative link from
100 * @param name
101 * name of the file or folder in this folder
102 * @return relative link
103 * @throws IllegalArgumentException
104 * if this folder and the base do not have the same root
105 */
106 public String getLink(final ReportOutputFolder base, final String name) {
107 if (base.isAncestorOf(this)) {
108 return this.path.substring(base.path.length()) + normalize(name);
109 }
110 if (base.parent == null) {
111 throw new IllegalArgumentException("Folders with different roots.");
112 }
113 return "../" + this.getLink(base.parent, name);
114 }
115
116 private boolean isAncestorOf(final ReportOutputFolder folder) {
117 if (this == folder) {
118 return true;
119 }
120 return folder.parent == null ? false : isAncestorOf(folder.parent);
121 }
122
123 private String normalize(final String name) {
124 return fileNames.getFileName(name);
125 }
126
127}