Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HudsonConfigScmElement |
|
|
2.0;2 |
1 | /* |
|
2 | * Copyright 2012 smartics, Kronseder & Reiner GmbH |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package de.smartics.ci.config.hudson; |
|
17 | ||
18 | import org.jdom.Document; |
|
19 | import org.jdom.Element; |
|
20 | import org.jdom.JDOMException; |
|
21 | import org.jdom.xpath.XPath; |
|
22 | ||
23 | import de.smartics.ci.config.utils.JDomUtils; |
|
24 | ||
25 | /** |
|
26 | * Creates a Hudson configuration element for the scm information. |
|
27 | * |
|
28 | * @see HudsonConfigEntry#addScmConfig(ScmType, String) |
|
29 | */ |
|
30 | public final class HudsonConfigScmElement extends AbstractHudsonConfigHelper |
|
31 | { |
|
32 | // ********************************* Fields ********************************* |
|
33 | ||
34 | // --- constants ------------------------------------------------------------ |
|
35 | ||
36 | // --- members -------------------------------------------------------------- |
|
37 | ||
38 | // ****************************** Initializer ******************************* |
|
39 | ||
40 | // ****************************** Constructors ****************************** |
|
41 | ||
42 | /** |
|
43 | * Constructor. |
|
44 | * |
|
45 | * @param hudsonConfig the Hudson configuration XML file to add to. |
|
46 | * @see de.smartics.ci.config.hudson.AbstractHudsonConfigHelper#AbstractHudsonConfigHelper(org.jdom.Document) |
|
47 | */ |
|
48 | public HudsonConfigScmElement(final Document hudsonConfig) |
|
49 | { |
|
50 | 0 | super(hudsonConfig); |
51 | 0 | } |
52 | ||
53 | // ****************************** Inner Classes ***************************** |
|
54 | ||
55 | // ********************************* Methods ******************************** |
|
56 | ||
57 | // --- init ----------------------------------------------------------------- |
|
58 | ||
59 | // --- get&set -------------------------------------------------------------- |
|
60 | ||
61 | // --- business ------------------------------------------------------------- |
|
62 | ||
63 | /** |
|
64 | * Adds information for the given SCM provider as an element to the document. |
|
65 | * |
|
66 | * @param scmType the type of the SCM provider to add. |
|
67 | * @param scmUrl the connection URL to the SCM server. |
|
68 | * @throws JDOMException on any problem accessing the Hudson configuration XML |
|
69 | * document. |
|
70 | */ |
|
71 | public void addScmConfig(final ScmType scmType, final String scmUrl) |
|
72 | throws JDOMException |
|
73 | { |
|
74 | 0 | final String xpathString = "/maven2-moduleset/scm"; |
75 | ||
76 | 0 | final XPath xPathScm = XPath.newInstance(xpathString); |
77 | 0 | final Element scmElement = |
78 | (Element) xPathScm.selectSingleNode(hudsonConfig); |
|
79 | ||
80 | 0 | if (scmElement == null) |
81 | { |
|
82 | 0 | final Element scm = new Element("scm"); |
83 | 0 | scm.setAttribute("class", scmType.getId()); |
84 | 0 | final Element locations = scmType.create(scmUrl); |
85 | 0 | scm.addContent(locations); |
86 | ||
87 | 0 | hudsonConfig.getRootElement().addContent(scm); |
88 | 0 | } |
89 | else |
|
90 | { |
|
91 | 0 | final XPath xpath = scmType.createScmUrlSelectionXPath(xpathString); |
92 | ||
93 | 0 | final Element scmUrlElement = |
94 | (Element) xpath.selectSingleNode(hudsonConfig); |
|
95 | 0 | if (scmUrlElement == null) |
96 | { |
|
97 | 0 | final Element scm = new Element("scm"); |
98 | 0 | scm.setAttribute("class", scmType.getId()); |
99 | 0 | final Element locations = scmType.create(scmUrl); |
100 | 0 | scm.addContent(locations); |
101 | ||
102 | 0 | JDomUtils.merge(scmElement, scm); |
103 | 0 | } |
104 | else |
|
105 | { |
|
106 | 0 | overrideScmUrlValue(scmUrl, scmUrlElement); |
107 | } |
|
108 | } |
|
109 | 0 | } |
110 | ||
111 | private void overrideScmUrlValue(final String scmUrl, |
|
112 | final Element scmUrlElement) |
|
113 | { |
|
114 | 0 | final String scmUrlcontent = scmUrlElement.getTextTrim(); |
115 | 0 | if (!scmUrl.equals(scmUrlcontent)) |
116 | { |
|
117 | 0 | scmUrlElement.setText(scmUrl); |
118 | } |
|
119 | 0 | } |
120 | ||
121 | // --- object basics -------------------------------------------------------- |
|
122 | ||
123 | } |