View Javadoc

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 static de.smartics.ci.config.utils.JDomUtils.calcXPath;
19  
20  import org.jdom.Element;
21  import org.jdom.JDOMException;
22  import org.jdom.Text;
23  import org.jdom.xpath.XPath;
24  
25  /**
26   * Definition of XML elements of a Hudson configuration.
27   */
28  public enum HudsonConfigElement
29  {
30    // ****************************** Enumeration *******************************
31  
32    /**
33     * Identifies the <code>entry</code> XML element.
34     */
35    ENTRY("entry")
36    {
37      @Override
38      public Element findElementWithin(final Element rootElement,
39          final Element element, final Element targetRootElement)
40        throws JDOMException
41      {
42        final XPath xpath = XPath.newInstance("string/text()");
43        final Text entryIdText = (Text) xpath.selectSingleNode(element);
44        if (entryIdText == null)
45        {
46          return null;
47        }
48  
49        final String id = entryIdText.getTextTrim();
50        final String xPathString =
51            "/maven2-moduleset/project-properties/entry/string[text()='" + id
52                + "']";
53        final XPath xPath = XPath.newInstance(xPathString);
54        final Object object = xPath.selectSingleNode(targetRootElement);
55        if (object != null)
56        {
57          final Element parent = ((Element) object).getParentElement();
58          return parent;
59        }
60        return null;
61      }
62    };
63  
64    // ********************************* Fields *********************************
65  
66    // --- constants ------------------------------------------------------------
67  
68    // --- members --------------------------------------------------------------
69  
70    /**
71     * The name of the XML element this enum element identifies.
72     */
73    private final String name;
74  
75    // ****************************** Constructors ******************************
76  
77    /**
78     * Default constructor.
79     *
80     * @param name the name of the XML element this enum element identifies.
81     */
82    private HudsonConfigElement(final String name)
83    {
84      this.name = name;
85    }
86  
87    // ********************************* Methods ********************************
88  
89    // --- init -----------------------------------------------------------------
90  
91    // --- get&set --------------------------------------------------------------
92  
93    /**
94     * Returns the name of the XML element this enum element identifies.
95     *
96     * @return the name of the XML element this enum element identifies.
97     */
98    public String getName()
99    {
100     return name;
101   }
102 
103   // --- business -------------------------------------------------------------
104 
105   /**
106    * Returns the element for the given name.
107    *
108    * @param elementName the name of the request element.
109    * @return the requested element.
110    * @throws IllegalArgumentException if the {@code elementName} does not match
111    *           a known element name.
112    */
113   public static HudsonConfigElement valueFor(final String elementName)
114     throws IllegalArgumentException
115   {
116     for (final HudsonConfigElement element : values())
117     {
118       if (element.name.equals(elementName))
119       {
120         return element;
121       }
122     }
123 
124     throw new IllegalArgumentException("Cannot find element for name '"
125                                        + elementName + "'.");
126   }
127 
128   /**
129    * Finds the element within the target root element.
130    * <p>
131    * {@code rootElement} is analog to {@code targetRootElement}. This method
132    * finds the analog element of {@code element} within the target document.
133    * </p>
134    *
135    * @param rootElement the root element to the element.
136    * @param element the element relative to the {@code rootElement}.
137    * @param targetRootElement the root element within which the element
138    *          specified by {@code element} is requested. This root element is
139    *          the root element of the target document.
140    * @return the analog element of {@code element} within
141    *         {@code targetRootElement}.
142    * @throws JDOMException on any problem accessing any element information.
143    */
144   public static Element findElement(final Element rootElement,
145       final Element element, final Element targetRootElement)
146     throws JDOMException
147   {
148     final String elementName = element.getName();
149     try
150     {
151       final HudsonConfigElement config = valueFor(elementName);
152       return config.findElementWithin(rootElement, element, targetRootElement);
153     }
154     catch (final IllegalArgumentException e)
155     {
156       return defaultFindElement(rootElement, element, targetRootElement);
157     }
158   }
159 
160   private static Element defaultFindElement(final Element rootElement,
161       final Element element, final Element targetRootElement)
162     throws JDOMException
163   {
164     final String path = calcXPath(rootElement, element);
165     final XPath xPath = XPath.newInstance(path);
166     final Object object = xPath.selectSingleNode(targetRootElement);
167     return (Element) object;
168   }
169 
170   abstract Element findElementWithin(final Element rootElement,
171       final Element element, final Element targetRootElement)
172     throws JDOMException;
173 
174   // --- object basics --------------------------------------------------------
175 
176 }