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.Text; 22 import org.jdom.xpath.XPath; 23 24 /** 25 * Helper to create Hudson configuration entry elements. 26 */ 27 public final class HudsonConfigEntry extends AbstractHudsonConfigHelper 28 { 29 // ********************************* Fields ********************************* 30 31 // --- constants ------------------------------------------------------------ 32 33 // --- members -------------------------------------------------------------- 34 35 // ****************************** Initializer ******************************* 36 37 // ****************************** Constructors ****************************** 38 39 /** 40 * Default constructor. 41 * 42 * @param hudsonConfig the Hudson configuration XML file to add to. 43 */ 44 public HudsonConfigEntry(final Document hudsonConfig) 45 { 46 super(hudsonConfig); 47 } 48 49 // ****************************** Inner Classes ***************************** 50 51 // ********************************* Methods ******************************** 52 53 // --- init ----------------------------------------------------------------- 54 55 // --- get&set -------------------------------------------------------------- 56 57 // --- business ------------------------------------------------------------- 58 59 /** 60 * Adds the given entry to the document. 61 * 62 * @param entry the entry to add. 63 * @throws JDOMException on any problem accessing the Hudson configuration XML 64 * document. 65 */ 66 public void addEntry(final Element entry) throws JDOMException 67 { 68 final String xPathString = "/maven2-moduleset/project-properties"; 69 final XPath xPath = XPath.newInstance(xPathString); 70 71 Element entries = (Element) xPath.selectSingleNode(hudsonConfig); 72 if (entries == null) 73 { 74 entries = new Element("project-properties"); 75 entries.setAttribute("class", "java.util.concurrent.ConcurrentHashMap"); 76 final Element rootElement = hudsonConfig.getRootElement(); 77 rootElement.addContent(entries); 78 } 79 entries.addContent((Element) entry.clone()); 80 } 81 82 /** 83 * Adds information for the given SCM provider as an entry to the document. 84 * 85 * @param scmType the type of the SCM provider to add. 86 * @param scmUrl the connection URL to the SCM server. 87 * @throws JDOMException on any problem accessing the Hudson configuration XML 88 * document. 89 */ 90 public void addScmConfig(final ScmType scmType, final String scmUrl) 91 throws JDOMException 92 { 93 final Element entry = new Element("entry"); 94 final Element string = new Element("string"); 95 final Text text = new Text("scm"); 96 final Element scmProperty = new Element("scm-property"); 97 final Element originalValue = new Element("originalValue"); 98 originalValue.setAttribute("class", scmType.getId()); 99 100 final Element locations = scmType.create(scmUrl); 101 102 entry.addContent(string); 103 string.addContent(text); 104 entry.addContent(scmProperty); 105 scmProperty.addContent(originalValue); 106 originalValue.addContent(locations); 107 108 addEntry(entry); 109 } 110 111 // --- object basics -------------------------------------------------------- 112 113 }