1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 21 import org.apache.commons.jxpath.JXPathBeanInfo; 22 import org.apache.commons.jxpath.JXPathContext; 23 import org.apache.commons.jxpath.JXPathIntrospector; 24 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 35 public class JXPathHelper { 36 37 private JXPathHelper() { 38 } 40 41 51 public static JXPathHelperConfiguration setup(Configuration config) 52 throws ConfigurationException { 53 54 return new JXPathHelperConfiguration(config); 55 } 56 57 58 66 private static void setup(JXPathHelperConfiguration setup, JXPathContext context, Configuration conf) 67 throws ConfigurationException { 68 69 JXPathHelperConfiguration local = conf == null ? setup : new JXPathHelperConfiguration(setup, conf); 71 72 context.setLenient(setup.isLenient()); 74 context.setFunctions(local.getLibrary()); 75 if (local.getNamespaces() != null) { 76 for (Iterator i = local.getNamespaces().entrySet().iterator(); i.hasNext();) { 77 final Map.Entry entry = (Map.Entry ) i.next(); 78 context.registerNamespace((String ) entry.getKey(), (String ) entry.getValue()); 79 } 80 } 81 } 82 83 public static Object getAttribute(String name, 84 Configuration modeConf, 85 JXPathHelperConfiguration setup, 86 Object contextObj) 87 throws ConfigurationException { 88 89 if (contextObj == null) { 90 return null; 91 } 92 93 try { 94 JXPathContext jxContext = JXPathContext.newContext(contextObj); 95 setup(setup, jxContext, modeConf); 96 97 Object obj = jxContext.getValue(name); 98 return obj; 99 } catch (Exception e) { 100 throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e); 101 } 102 } 103 104 public static Object [] getAttributeValues(String name, 105 Configuration modeConf, 106 JXPathHelperConfiguration setup, 107 Object contextObj) 108 throws ConfigurationException { 109 110 if (contextObj == null) { 111 return null; 112 } 113 114 try { 115 JXPathContext jxContext = JXPathContext.newContext(contextObj); 116 setup(setup, jxContext, modeConf); 117 118 List values = null; 119 Iterator i = jxContext.iterate(name); 120 if (i.hasNext()) { 121 values = new LinkedList (); 122 } 123 while (i.hasNext()) { 124 values.add(i.next()); 125 } 126 Object [] obj = null; 127 if (values != null) { 128 obj = values.toArray(); 129 if (obj.length == 0) { 130 obj = null; 131 } 132 } 133 return obj; 134 } catch (Exception e) { 135 throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e); 136 } 137 } 138 139 140 public static Iterator getAttributeNames(JXPathHelperConfiguration setup, Object contextObj) 141 throws ConfigurationException { 142 143 if (contextObj == null) { 144 return null; 145 } 146 147 try { 148 JXPathBeanInfo info = JXPathIntrospector.getBeanInfo(contextObj.getClass()); 149 java.beans.PropertyDescriptor [] properties = info.getPropertyDescriptors(); 150 151 List names = new LinkedList (); 152 for (int i = 0; i < properties.length; i++) { 153 names.add(properties[i].getName()); 154 } 155 156 return names.listIterator(); 157 } catch (Exception e) { 158 throw new ConfigurationException("Error retrieving attribute names for class: " + contextObj.getClass(), e); 159 } 160 } 161 } 162 | Popular Tags |