File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\search\SuggestedItem.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.search;
25
26 /**
27 * One item of a search result.
28 *
29 * @author Kohsuke Kawaguchi
30 */
31 public class SuggestedItem {
32 private final SuggestedItem parent;
33 public final SearchItem item;
34 private String path;
35
36 public SuggestedItem(SearchItem top) {
37 this(null,top);
38 }
39
40 public SuggestedItem(SuggestedItem parent, SearchItem item) {
41 this.parent = parent;
42 this.item = item;
43 }
44
45 public String getPath() {
46 if(path!=null) return path;
47 if(parent==null)
48 return path=item.getSearchName();
49 else {
50 StringBuilder buf = new StringBuilder();
51 getPath(buf);
52 return path=buf.toString();
53 }
54 }
55
56 private void getPath(StringBuilder buf) {
57 if(parent==null)
58 buf.append(item.getSearchName());
59 else {
60 parent.getPath(buf);
61 buf.append(' ').append(item.getSearchName());
62 }
63 }
64
65 /**
66 * Gets the URL to this item.
67 * @return
68 * URL that starts with '/' but doesn't end with '/'.
69 * The path is the combined path from the {@link SearchIndex} where the search started
70 * to the final item found. Thus to convert to the actual URL, the caller would need
71 * to prepend the URL of the object where the search started.
72 */
73 public String getUrl() {
74 StringBuilder buf = new StringBuilder();
75 getUrl(buf);
76 return buf.toString();
77 }
78
79 private void getUrl(StringBuilder buf) {
80 if(parent!=null) {
81 parent.getUrl(buf);
82 }
83 String f = item.getSearchUrl();
84 if(f.startsWith("/")) {
85 buf.setLength(0);
86 buf.append(f);
87 } else {
88 if(buf.length()==0 || buf.charAt(buf.length()-1)!='/')
89 buf.append('/');
90 buf.append(f);
91 }
92 }
93 }
94