extensions/net.sf.basedb.relax/trunk/src/net/sf/basedb/relax/XmlConfig.java

Code
Comments
Other
Rev Date Author Line
4613 03 Oct 17 nicklas 1 package net.sf.basedb.relax;
4613 03 Oct 17 nicklas 2
4613 03 Oct 17 nicklas 3 import java.util.ArrayList;
4613 03 Oct 17 nicklas 4 import java.util.Arrays;
4613 03 Oct 17 nicklas 5 import java.util.Collections;
4613 03 Oct 17 nicklas 6 import java.util.List;
4613 03 Oct 17 nicklas 7
4613 03 Oct 17 nicklas 8 import net.sf.basedb.core.ConfigurationException;
4613 03 Oct 17 nicklas 9 import net.sf.basedb.util.Values;
4613 03 Oct 17 nicklas 10
4613 03 Oct 17 nicklas 11 import org.jdom2.Attribute;
4613 03 Oct 17 nicklas 12 import org.jdom2.Element;
4613 03 Oct 17 nicklas 13 import org.jdom2.xpath.XPathFactory;
4613 03 Oct 17 nicklas 14
4613 03 Oct 17 nicklas 15 /**
4613 03 Oct 17 nicklas 16   Utility class for wokring with Reggie XML configuration file.
4613 03 Oct 17 nicklas 17
4613 03 Oct 17 nicklas 18   @author nicklas
4613 03 Oct 17 nicklas 19   @since 2.18
4613 03 Oct 17 nicklas 20 */
4613 03 Oct 17 nicklas 21 public class XmlConfig 
4613 03 Oct 17 nicklas 22 {
4613 03 Oct 17 nicklas 23
4613 03 Oct 17 nicklas 24   private final Element root;
4613 03 Oct 17 nicklas 25   private final String id;
4613 03 Oct 17 nicklas 26   
4613 03 Oct 17 nicklas 27   /**
4613 03 Oct 17 nicklas 28     Create a new configuration item using the given XML element as the
4613 03 Oct 17 nicklas 29     root element.
4613 03 Oct 17 nicklas 30     @param root The root XML element
4613 03 Oct 17 nicklas 31     @param id The id of the root element (used in error message)
4613 03 Oct 17 nicklas 32   */
4613 03 Oct 17 nicklas 33   public XmlConfig(Element root, String id)
4613 03 Oct 17 nicklas 34   {
4613 03 Oct 17 nicklas 35     this.root = root;
4613 03 Oct 17 nicklas 36     this.id = id;
4613 03 Oct 17 nicklas 37   }
4613 03 Oct 17 nicklas 38   
4613 03 Oct 17 nicklas 39   /**
4613 03 Oct 17 nicklas 40     Get the XML root element.
4613 03 Oct 17 nicklas 41   */
4613 03 Oct 17 nicklas 42   public Element getRoot()
4613 03 Oct 17 nicklas 43   {
4613 03 Oct 17 nicklas 44     return root;
4613 03 Oct 17 nicklas 45   }
4613 03 Oct 17 nicklas 46
4613 03 Oct 17 nicklas 47   /**
4613 03 Oct 17 nicklas 48     Get the value of a required configuration option. If no
4613 03 Oct 17 nicklas 49     value is found an exception is thrown.
4613 03 Oct 17 nicklas 50     @param xpath An XPath expression identifying a (single) configuration element
4613 03 Oct 17 nicklas 51     @param parameterSet A named parameter set that should be used before the unnamed parameters
4613 03 Oct 17 nicklas 52     @since 3.1
4613 03 Oct 17 nicklas 53   */
4613 03 Oct 17 nicklas 54   public String getRequiredConfig(String xpath, String parameterSet)
4613 03 Oct 17 nicklas 55   {
4613 03 Oct 17 nicklas 56     String value = getConfig(xpath, parameterSet, null);
4613 03 Oct 17 nicklas 57     if (value == null)
4613 03 Oct 17 nicklas 58     {
4613 03 Oct 17 nicklas 59       throw new ConfigurationException("Can't find '" + xpath + "' setting for " + id + "[parameter-set: " + parameterSet + "]");
4613 03 Oct 17 nicklas 60     }
4613 03 Oct 17 nicklas 61     return value;
4613 03 Oct 17 nicklas 62   }
4613 03 Oct 17 nicklas 63   
4613 03 Oct 17 nicklas 64   /**
4613 03 Oct 17 nicklas 65     Shortcut for {@link #getConfig(String, String, String)} with null
4613 03 Oct 17 nicklas 66     values for parameterSet and defaultValue.
4613 03 Oct 17 nicklas 67     @since 4.1
4613 03 Oct 17 nicklas 68   */
4613 03 Oct 17 nicklas 69   public String getConfig(String xpath)
4613 03 Oct 17 nicklas 70   {
4613 03 Oct 17 nicklas 71     return getConfig(xpath, null, null);
4613 03 Oct 17 nicklas 72   }
4613 03 Oct 17 nicklas 73   
4613 03 Oct 17 nicklas 74   /**
4613 03 Oct 17 nicklas 75     Searches for an ordered list of configuration values all with
4613 03 Oct 17 nicklas 76     the given root prefix. For example if prefix is 'run-archive' 
4613 03 Oct 17 nicklas 77     this will look for 'run-archive-N', 'run-archive-(N+1)', and so on
4613 03 Oct 17 nicklas 78     until an empty element or no element is found (N=starting index).
4613 03 Oct 17 nicklas 79     @since 4.6
4613 03 Oct 17 nicklas 80   */
4613 03 Oct 17 nicklas 81   public List<String> getConfigList(String prefix, int startAt)
4613 03 Oct 17 nicklas 82   {
4613 03 Oct 17 nicklas 83     int index = startAt;
4613 03 Oct 17 nicklas 84     List<String> list = new ArrayList<>();
4613 03 Oct 17 nicklas 85     do
4613 03 Oct 17 nicklas 86     {
4613 03 Oct 17 nicklas 87       String value = getConfig(prefix + "-" + index);
4613 03 Oct 17 nicklas 88       if (value == null) break;
4613 03 Oct 17 nicklas 89       list.add(value);
4613 03 Oct 17 nicklas 90       index++;
4613 03 Oct 17 nicklas 91     } while (true);
4613 03 Oct 17 nicklas 92     
4613 03 Oct 17 nicklas 93     return list;
4613 03 Oct 17 nicklas 94   }
4613 03 Oct 17 nicklas 95
4613 03 Oct 17 nicklas 96   
4613 03 Oct 17 nicklas 97   /**
4613 03 Oct 17 nicklas 98     Get the value of a configuration option. If no value is found the default value is used.
4613 03 Oct 17 nicklas 99     
4613 03 Oct 17 nicklas 100     @param xpath An XPath expression identifying a (single) configuration element or attribute
4613 03 Oct 17 nicklas 101     @param parameterSet A named parameter set that should be used before the unnamed parameters
4613 03 Oct 17 nicklas 102     @since 3.1
4613 03 Oct 17 nicklas 103   */
4613 03 Oct 17 nicklas 104   public String getConfig(String xpath, String parameterSet, String defaultValue)
4613 03 Oct 17 nicklas 105   {
4613 03 Oct 17 nicklas 106     String value = null;
4613 03 Oct 17 nicklas 107     if (xpath == null) return defaultValue;
4613 03 Oct 17 nicklas 108     
4613 03 Oct 17 nicklas 109     List<Object> list = XPathFactory.instance().compile(xpath).evaluate(root);
4613 03 Oct 17 nicklas 110     for (Object n : list)
4613 03 Oct 17 nicklas 111     {
4613 03 Oct 17 nicklas 112       Element e = null;
4613 03 Oct 17 nicklas 113       String tmp = null;
4613 03 Oct 17 nicklas 114       if (n instanceof Element)
4613 03 Oct 17 nicklas 115       {
4613 03 Oct 17 nicklas 116         e = (Element)n;
4613 03 Oct 17 nicklas 117         tmp = Values.getStringOrNull(e.getTextTrim());
4613 03 Oct 17 nicklas 118       }
4613 03 Oct 17 nicklas 119       else if (n instanceof Attribute)
4613 03 Oct 17 nicklas 120       {
4613 03 Oct 17 nicklas 121         Attribute a = (Attribute)n;
4613 03 Oct 17 nicklas 122         e = a.getParent();
4613 03 Oct 17 nicklas 123         tmp = Values.getStringOrNull(a.getValue());
4613 03 Oct 17 nicklas 124       }
4613 03 Oct 17 nicklas 125       if (e != null)
4613 03 Oct 17 nicklas 126       {
4613 03 Oct 17 nicklas 127         String ps = e.getAttributeValue("parameter-set");
4613 03 Oct 17 nicklas 128         if (ps == null)
4613 03 Oct 17 nicklas 129         {
4613 03 Oct 17 nicklas 130           // Default parameter set; use this unless we find the specific parameter set
4613 03 Oct 17 nicklas 131           value = tmp;
4613 03 Oct 17 nicklas 132           if (parameterSet == null) break;
4613 03 Oct 17 nicklas 133         }
4613 03 Oct 17 nicklas 134         else if (parameterSet != null && Arrays.asList(ps.split(",")).indexOf(parameterSet) != -1)
4613 03 Oct 17 nicklas 135         {
4613 03 Oct 17 nicklas 136           // We have found the parameter set we are lookup for, break and use this value
4613 03 Oct 17 nicklas 137           value = tmp;
4613 03 Oct 17 nicklas 138           break;
4613 03 Oct 17 nicklas 139         }
4613 03 Oct 17 nicklas 140       }
4613 03 Oct 17 nicklas 141     }
4613 03 Oct 17 nicklas 142     return value == null ? defaultValue : value;
4613 03 Oct 17 nicklas 143   }
4613 03 Oct 17 nicklas 144   
4613 03 Oct 17 nicklas 145   /**
4613 03 Oct 17 nicklas 146     Get all parameters in the given parameter set.
4613 03 Oct 17 nicklas 147     @since 3.1
4613 03 Oct 17 nicklas 148   */
4613 03 Oct 17 nicklas 149   public List<Element> getAllInParameterSet(String parameterSet)
4613 03 Oct 17 nicklas 150   {
4613 03 Oct 17 nicklas 151     if (parameterSet == null) return Collections.emptyList();
4613 03 Oct 17 nicklas 152     
4613 03 Oct 17 nicklas 153     List<Object> list = XPathFactory.instance().compile("//*[@parameter-set]").evaluate(root);
4613 03 Oct 17 nicklas 154     List<Element> parameters = new ArrayList<Element>();
4613 03 Oct 17 nicklas 155     
4613 03 Oct 17 nicklas 156     for (Object o : list)
4613 03 Oct 17 nicklas 157     {
4613 03 Oct 17 nicklas 158       Element e = (Element)o;
4613 03 Oct 17 nicklas 159       String ps = e.getAttributeValue("parameter-set");
4613 03 Oct 17 nicklas 160       if (Arrays.asList(ps.split(",")).indexOf(parameterSet) != -1)
4613 03 Oct 17 nicklas 161       {
4613 03 Oct 17 nicklas 162         parameters.add(e);
4613 03 Oct 17 nicklas 163       }
4613 03 Oct 17 nicklas 164     }
4613 03 Oct 17 nicklas 165     return parameters;
4613 03 Oct 17 nicklas 166   }
4613 03 Oct 17 nicklas 167   
4613 03 Oct 17 nicklas 168 }