Package: SimpleFileHelpAccessor

SimpleFileHelpAccessor

nameinstructionbranchcomplexitylinemethod
SimpleFileHelpAccessor()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
contentLine(String)
M: 2 C: 14
88%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getDetailedHelp(Class)
M: 13 C: 16
55%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
getSummaryHelp(Class)
M: 13 C: 16
55%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
helpResourceReader(Class)
M: 16 C: 39
71%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 3 C: 11
79%
M: 0 C: 1
100%
helpResourceUrl(Class, String)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
readAllButFirstHelpLines(BufferedReader)
M: 0 C: 29
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
readFirstHelpLine(BufferedReader)
M: 2 C: 14
88%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
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: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.shell.internal.help;
13:
14: import static java.nio.charset.StandardCharsets.UTF_8;
15:
16: import java.io.BufferedReader;
17: import java.io.IOException;
18: import java.io.InputStream;
19: import java.io.InputStreamReader;
20: import java.net.URL;
21: import java.util.ArrayList;
22: import java.util.Collections;
23: import java.util.List;
24:
25: import org.eclipse.virgo.util.io.IOUtils;
26: import org.osgi.framework.Bundle;
27: import org.osgi.framework.FrameworkUtil;
28: import org.slf4j.Logger;
29: import org.slf4j.LoggerFactory;
30:
31: /**
32: * Implementation of {@link HelpAccessor} which searches for a simple text file resource in the bundle of the class.
33: * <p/>
34: * The name of the file resource is the fully-qualified class name followed by "<code>.help</code>". <br/>
35: * Lines beginning with a hash (pound) sign '#', as the first character, are comment lines; all other lines are
36: * 'content' lines. <br/>
37: * Comment lines are ignored. <br/>
38: * All strings returned are terminated by a newline character.
39: * <p />
40: *
41: * <strong>Concurrent Semantics</strong><br />
42: * Thread-safe.
43: *
44: */
45: public class SimpleFileHelpAccessor implements HelpAccessor {
46:
47: private static final char HELP_FILE_COMMENT_CHARACTER = '#';
48:
49: private static final String HELP_ACCESSOR_RESOURCE_EXTENSION = ".help";
50:
51: private static final Logger logger = LoggerFactory.getLogger(SimpleFileHelpAccessor.class);
52:
53: /**
54: * {@inheritDoc}
55: */
56: public List<String> getDetailedHelp(Class<?> clazz) {
57: try (BufferedReader readFileIn = this.helpResourceReader(clazz)) {
58:• if (readFileIn != null) {
59: return readAllButFirstHelpLines(readFileIn);
60: }
61: } catch (IOException ioe) {
62: logger.error(String.format("Exception reading help resource for class '%s'.", clazz.getCanonicalName()), ioe);
63: }
64: return Collections.emptyList();
65: }
66:
67: /**
68: * {@inheritDoc}
69: */
70: public String getSummaryHelp(Class<?> clazz) {
71: try (BufferedReader readFileIn = this.helpResourceReader(clazz)) {
72:• if (readFileIn != null) {
73: return readFirstHelpLine(readFileIn);
74: }
75: } catch (IOException ioe) {
76: logger.error(String.format("Exception reading help resource for class '%s'.", clazz.getCanonicalName()), ioe);
77: }
78: return null;
79: }
80:
81: private final String readFirstHelpLine(BufferedReader reader) throws IOException {
82: String line = reader.readLine();
83:• while (line != null) {
84:• if (contentLine(line)) {
85: return line;
86: }
87: line = reader.readLine();
88: }
89: return line;
90: }
91:
92: private final List<String> readAllButFirstHelpLines(BufferedReader reader) throws IOException {
93: List<String> lines = new ArrayList<String>();
94:
95: String line = reader.readLine();
96: boolean skipLine = true;
97:• while (line != null) {
98:• if (contentLine(line)) {
99:• if (!skipLine) {
100: lines.add(line);
101: } else {
102: skipLine = false;
103: }
104: }
105: line = reader.readLine();
106: }
107:
108: return lines;
109: }
110:
111: private static final boolean contentLine(String line) {
112:• if (line == null)
113: return false;
114:• return (line.length() == 0 || line.charAt(0) != HELP_FILE_COMMENT_CHARACTER);
115: }
116:
117: private final BufferedReader helpResourceReader(Class<?> clazz) {
118: BufferedReader readFileIn = null;
119: String className = clazz.getCanonicalName();
120:• if (className != null) {
121: String fileResourceName = new StringBuffer(className).append(HELP_ACCESSOR_RESOURCE_EXTENSION).toString();
122: URL resourceUrl = this.helpResourceUrl(clazz, fileResourceName);
123:• if (resourceUrl != null) {
124: InputStream resourceIn = null;
125: try {
126: resourceIn = resourceUrl.openStream();
127: readFileIn = new BufferedReader(new InputStreamReader(resourceIn, UTF_8));
128: } catch (IOException ioe) {
129: logger.error(String.format("Exception reading help resource '%s'.", resourceUrl), ioe);
130: IOUtils.closeQuietly(resourceIn);
131: return null;
132: }
133: }
134: }
135: return readFileIn;
136: }
137:
138: protected URL helpResourceUrl(Class<?> clazz, String fileResourceName) {
139: Bundle bundle = FrameworkUtil.getBundle(clazz);
140: try {
141:• if (bundle != null) {
142: return bundle.getResource(fileResourceName);
143: }
144: } catch (IllegalStateException ise) {
145: logger.error(String.format("Exception obtaining help resource file '%s'.", fileResourceName), ise);
146: }
147: return null;
148: }
149: }