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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
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
25 package hudson.model;
26
27 import hudson.search.Search;
28 import jenkins.model.Jenkins;
29 import org.kohsuke.stapler.HttpResponse;
30 import org.kohsuke.stapler.StaplerRequest;
31 import org.kohsuke.stapler.StaplerResponse;
32 import org.kohsuke.stapler.export.Flavor;
33
34 import javax.servlet.ServletException;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import javax.annotation.CheckForNull;
40
41 /**
42 * Data representation of the auto-completion candidates.
43 * <p>
44 * This object should be returned from your doAutoCompleteXYZ methods.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class AutoCompletionCandidates implements HttpResponse {
49 private final List<String> values = new ArrayList<String>();
50
51 public AutoCompletionCandidates add(String v) {
52 values.add(v);
53 return this;
54 }
55
56 public AutoCompletionCandidates add(String... v) {
57 values.addAll(Arrays.asList(v));
58 return this;
59 }
60
61 /**
62 * Exposes the raw value, in case you want to modify {@link List} directly.
63 * @since 1.402
64 */
65 public List<String> getValues() {
66 return values;
67 }
68
69 public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object o) throws IOException, ServletException {
70 Search.Result r = new Search.Result();
71 for (String value : values) {
72 r.suggestions.add(new hudson.search.Search.Item(value));
73 }
74 rsp.serveExposedBean(req,r, Flavor.JSON);
75 }
76
77 /**
78 * Auto-completes possible job names.
79 *
80 * @param type
81 * Limit the auto-completion to the subtype of this type.
82 * @param value
83 * The value the user has typed in. Matched as a prefix.
84 * @param self
85 * The contextual item for which the auto-completion is provided to.
86 * For example, if you are configuring a job, this is the job being configured.
87 * @param container
88 * The nearby contextual {@link ItemGroup} to resolve relative job names from.
89 * @since 1.489
90 */
91 public static <T extends Item> AutoCompletionCandidates ofJobNames(final Class<T> type, final String value, @CheckForNull Item self, ItemGroup container) {
92 if (self==container)
93 container = self.getParent();
94
95 final AutoCompletionCandidates candidates = new AutoCompletionCandidates();
96 class Visitor extends ItemVisitor {
97 String prefix;
98
99 Visitor(String prefix) {
100 this.prefix = prefix;
101 }
102
103 @Override
104 public void onItem(Item i) {
105 String n = contextualNameOf(i);
106 if ((n.startsWith(value) || value.startsWith(n))
107 // 'foobar' is a valid candidate if the current value is 'foo'.
108 // Also, we need to visit 'foo' if the current value is 'foo/bar'
109 && (value.length()>n.length() || !n.substring(value.length()).contains("/"))
110 // but 'foobar/zot' isn't if the current value is 'foo'
111 // we'll first show 'foobar' and then wait for the user to type '/' to show the rest
112 && i.hasPermission(Item.READ)
113 // and read permission required
114 ) {
115 if (type.isInstance(i) && n.startsWith(value))
116 candidates.add(n);
117
118 // recurse
119 String oldPrefix = prefix;
120 prefix = n;
121 super.onItem(i);
122 prefix = oldPrefix;
123 }
124 }
125
126 private String contextualNameOf(Item i) {
127 if (prefix.endsWith("/") || prefix.length()==0)
128 return prefix+i.getName();
129 else
130 return prefix+'/'+i.getName();
131 }
132 }
133
134 if (container==null || container==Jenkins.getInstance()) {
135 new Visitor("").onItemGroup(Jenkins.getInstance());
136 } else {
137 new Visitor("").onItemGroup(container);
138 if (value.startsWith("/"))
139 new Visitor("/").onItemGroup(Jenkins.getInstance());
140
141 for ( String p="../"; value.startsWith(p); p+="../") {
142 container = ((Item)container).getParent();
143 new Visitor(p).onItemGroup(container);
144 }
145 }
146
147 return candidates;
148 }
149 }
150