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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, 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 package hudson.util.io;
25
26 import hudson.ExtensionList;
27 import hudson.ExtensionPoint;
28 import hudson.remoting.Callable;
29 import hudson.remoting.Channel;
30 import jenkins.model.Jenkins;
31 import org.dom4j.io.SAXReader;
32
33 import java.io.IOException;
34 import java.io.Serializable;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38
39 /**
40 * Configures XML parsers to be used for various XML parsing activities inside Jenkins.
41 *
42 * <p>
43 * XML parsing is a complex enough activity that often certain degree of customization of the
44 * parsing behaviour is desired. This extension point enables that. To avoid creating
45 * new extension point for each different parsing scene, this extension point takes the type-less
46 * "context" argument, which should identify the context of the parse by type.
47 *
48 * <p>
49 * This extension point is added late in the game, so existing XML parsing behaviour should
50 * be retrofitted to use this as we find them. Similarly, additional overloaded versions are likely
51 * needed to support SAX, JAXP, and other means of parsing.
52 *
53 * @author Kohsuke Kawaguchi
54 * @since 1.416
55 */
56 public abstract class ParserConfigurator implements ExtensionPoint, Serializable {
57 private static final long serialVersionUID = -2523542286453177108L;
58
59 /**
60 * Configures the given {@link SAXReader}
61 *
62 * @param context
63 * Object that represents the context in which the parser is used.
64 * It is up to the caller to decide what to pass in here.
65 */
66 public void configure(SAXReader reader, Object context) {}
67
68 /**
69 * Returns all the registered {@link ParserConfigurator}s.
70 */
71 public static ExtensionList<ParserConfigurator> all() {
72 return Jenkins.getInstance().getExtensionList(ParserConfigurator.class);
73 }
74
75 public static void applyConfiguration(SAXReader reader, Object context) throws IOException, InterruptedException {
76 Collection<ParserConfigurator> all = Collections.emptyList();
77
78 if (Jenkins.getInstance()==null) {
79 Channel ch = Channel.current();
80 if (ch!=null)
81 all = ch.call(new Callable<Collection<ParserConfigurator>, IOException>() {
82
83 private static final long serialVersionUID = -2178106894481500733L;
84
85 public Collection<ParserConfigurator> call() throws IOException {
86 return new ArrayList<ParserConfigurator>(all());
87 }
88 });
89 } else
90 all = all();
91 for (ParserConfigurator pc : all)
92 pc.configure(reader,context);
93 }
94 }
95