1 61 62 package org.jaxen.function.ext; 63 64 import java.util.ArrayList ; 65 import java.util.Enumeration ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 69 import org.jaxen.Context; 70 import org.jaxen.Function; 71 import org.jaxen.Navigator; 72 import org.jaxen.function.StringFunction; 73 74 78 79 public class MatrixConcatFunction implements Function { 80 81 public Object call(Context context, List args) { 82 if ( args.size() >= 2 ) { 83 return evaluate(args, context.getNavigator()); 84 } 85 return null; 86 } 87 88 public static Object evaluate(List list, Navigator nav) { 89 ArrayList matrix = new ArrayList (); 90 91 Iterator argIter = list.iterator(); 92 93 while (argIter.hasNext()) { 94 ArrayList v = new ArrayList (); 95 Object obj = argIter.next(); 96 if (obj instanceof List ) { 97 List args = (List ) obj; 98 for ( int i = 0, size = args.size(); i < size; i++ ) { 99 v.add( StringFunction.evaluate( args.get(i), nav ) ); 100 } 101 } 102 else { 103 v.add( StringFunction.evaluate( obj, nav ) ); 104 } 105 matrix.add(v); 106 } 107 108 ArrayList result = new ArrayList (); 109 Enumeration elemList = new MatrixEnum( matrix ); 110 while (elemList.hasMoreElements()) { 111 Object obj = elemList.nextElement(); 112 if (obj instanceof List ) { 113 StringBuffer text = new StringBuffer (127); 114 List args = (List ) obj; 115 for (Iterator it = args.iterator(); it.hasNext(); ) { 116 text.append(it.next()); 117 } 118 result.add( text.toString() ); 119 } 120 else { 121 result.add( obj ); 122 } 123 } 124 return result; 125 } 126 127 public static class MatrixEnum implements Enumeration { 128 private ArrayList m_source; 129 private int m_maxSize = 0; 130 private int m_currIdx = -1; 131 132 public MatrixEnum (ArrayList _source) { 133 m_source = _source; 134 135 for ( Iterator iter = m_source.iterator(); iter.hasNext(); ) { 136 ArrayList element = (ArrayList ) iter.next(); 137 int size = element.size(); 138 if (size > m_maxSize) { 139 m_maxSize = size; 140 } 141 } 142 } 143 144 public MatrixEnum (ArrayList _source, int _maxSize) { 145 m_source = _source; 146 m_maxSize = _maxSize; 147 } 148 149 public boolean hasMoreElements() { 150 if ((m_maxSize != 0) && (++m_currIdx < m_maxSize)) { 151 return true; 152 } 153 else { 154 return false; 155 } 156 } 157 158 public Object nextElement() { 159 ArrayList result = new ArrayList (); 160 for ( Iterator iter = m_source.iterator(); iter.hasNext(); ) { 161 ArrayList element = (ArrayList ) iter.next(); 162 int size = element.size(); 163 if ( m_currIdx < size ) { 164 result.add( element.get( m_currIdx ) ); 165 } 166 else { 167 if ( size > 0 ) { 168 result.add( element.get( size - 1 ) ); 169 } 170 else { 171 result.add( "" ); 173 } 174 } 175 } 176 return result; 177 } 178 } 179 } 180 | Popular Tags |