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 super(hudsonConfig); 51 } 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 final String xpathString = "/maven2-moduleset/scm"; 75 76 final XPath xPathScm = XPath.newInstance(xpathString); 77 final Element scmElement = 78 (Element) xPathScm.selectSingleNode(hudsonConfig); 79 80 if (scmElement == null) 81 { 82 final Element scm = new Element("scm"); 83 scm.setAttribute("class", scmType.getId()); 84 final Element locations = scmType.create(scmUrl); 85 scm.addContent(locations); 86 87 hudsonConfig.getRootElement().addContent(scm); 88 } 89 else 90 { 91 final XPath xpath = scmType.createScmUrlSelectionXPath(xpathString); 92 93 final Element scmUrlElement = 94 (Element) xpath.selectSingleNode(hudsonConfig); 95 if (scmUrlElement == null) 96 { 97 final Element scm = new Element("scm"); 98 scm.setAttribute("class", scmType.getId()); 99 final Element locations = scmType.create(scmUrl); 100 scm.addContent(locations); 101 102 JDomUtils.merge(scmElement, scm); 103 } 104 else 105 { 106 overrideScmUrlValue(scmUrl, scmUrlElement); 107 } 108 } 109 } 110 111 private void overrideScmUrlValue(final String scmUrl, 112 final Element scmUrlElement) 113 { 114 final String scmUrlcontent = scmUrlElement.getTextTrim(); 115 if (!scmUrl.equals(scmUrlcontent)) 116 { 117 scmUrlElement.setText(scmUrl); 118 } 119 } 120 121 // --- object basics -------------------------------------------------------- 122 123 }