1 61 62 package org.jaxen.javabean; 63 64 import org.jaxen.Context; 65 import org.jaxen.BaseXPath; 66 import org.jaxen.JaxenException; 67 68 import java.util.List ; 69 import java.util.ArrayList ; 70 import java.util.Collection ; 71 import java.util.Iterator ; 72 73 92 public class JavaBeanXPath extends BaseXPath 93 { 94 101 public JavaBeanXPath(String xpathExpr) throws JaxenException 102 { 103 super( xpathExpr, DocumentNavigator.getInstance() ); 104 } 105 106 protected Context getContext(Object node) 107 { 108 if ( node instanceof Context ) 109 { 110 return (Context) node; 111 } 112 113 if ( node instanceof Element ) 114 { 115 return super.getContext( node ); 116 } 117 118 if ( node instanceof List ) 119 { 120 List newList = new ArrayList (); 121 122 for ( Iterator listIter = ((List )node).iterator(); 123 listIter.hasNext(); ) 124 { 125 newList.add( new Element( null, "root", listIter.next() ) ); 126 } 127 128 return super.getContext( newList ); 129 } 130 131 return super.getContext( new Element( null, "root", node ) ); 132 } 133 134 public Object evaluate(Object node) 135 throws JaxenException 136 { 137 Object result = super.evaluate( node ); 138 139 if ( result instanceof Element ) 140 { 141 return ((Element)result).getObject(); 142 } 143 else if ( result instanceof Collection ) 144 { 145 List newList = new ArrayList (); 146 147 for ( Iterator listIter = ((Collection )result).iterator(); 148 listIter.hasNext(); ) 149 { 150 Object member = listIter.next(); 151 152 if ( member instanceof Element ) 153 { 154 newList.add( ((Element)member).getObject() ); 155 } 156 else 157 { 158 newList.add( member ); 159 } 160 } 161 162 return newList; 163 } 164 165 return result; 166 } 167 } 168 | Popular Tags |