1 50 51 package org.openlaszlo.iv.flash.context; 52 53 import org.openlaszlo.iv.flash.api.*; 54 import org.openlaszlo.iv.flash.util.*; 55 import org.openlaszlo.iv.flash.url.*; 56 import org.openlaszlo.iv.flash.xml.*; 57 58 import org.w3c.dom.Node ; 59 import java.io.*; 60 import java.util.*; 61 62 67 68 public abstract class ContextFactory 69 { 70 72 81 public static Context createContext( String [][] data, int row ) throws IVException 82 { 83 return createContext( null, data, row ); 84 } 85 86 105 public static Context createContext( Context parent, String [][] data, int row ) throws IVException 106 { 107 StandardContext context = new StandardContext( parent ); 108 109 setStandardContextData( context, data, row ); 110 111 return context; 112 } 113 114 122 public static Context createContext( String [][] data ) throws IVException 123 { 124 return createContext( null, data ); 125 } 126 127 145 public static Context createContext( Context parent, String [][] data ) throws IVException 146 { 147 StandardContext context = new StandardContext( parent ); 148 149 setStandardContextData( context, data ); 150 151 return context; 152 } 153 154 156 public static Context createContext( Node n ) 157 { 158 return createContext( null, n ); 159 } 160 161 public static Context createContext( Context parent, Node n ) { 162 return XMLContext.newXMLContext(parent, n); 163 } 164 165 167 public static Context createContext( Map o ) 168 { 169 return createContext( null, o ); 170 } 171 172 public static Context createContext( Context parent, Map o ) 173 { 174 return new BeanContext( parent, o ); 175 } 176 177 189 public static Object readContext( String surl, 190 FlashFile flashFile ) throws IVException, java.io.IOException 191 { 192 return DataSourceHelper.readContextData(surl, flashFile); 193 } 194 195 207 public static String [][] readStandardContext( String surl, 208 FlashFile flashFile ) throws IVException, java.io.IOException 209 { 210 Object dsrc = readContext( surl, flashFile ); 211 if( !(dsrc instanceof LineReader) ) { 212 throw new IVException( Resource.EXPECTSTDCONTEXT ); 213 } 214 215 DataSource ds = new DataSource( (LineReader) dsrc ); 216 return ds.getData(); 217 } 218 219 236 237 public static Context createContext( Context parent, 238 String surl, 239 FlashFile flashFile, 240 boolean useRowStyle ) throws IVException, java.io.IOException 241 { 242 Object dsrc = readContext( surl, flashFile ); 244 245 if( dsrc instanceof LineReader ) 246 { 247 DataSource ds = new DataSource( (LineReader) dsrc ); 249 250 StandardContext context = new StandardContext( parent ); 251 252 if ( useRowStyle ) 253 { 254 setStandardContextData( context, ds.getData(), 1 ); 255 } 256 else 257 { 258 setStandardContextData( context, ds.getData() ); 259 } 260 261 return context; 262 } 263 else 264 { 265 try { 266 return XMLContext.newXMLContext(parent, XMLHelper.getNode((IVUrl)dsrc)); 267 } catch ( Exception e ) { throw new IVException( e ); 269 } 270 } 271 } 272 273 289 290 public static void setStandardContextData( StandardContext context, 291 String [][] data ) 292 throws IVException 293 { 294 int j, k; 295 296 for ( j = 0; j < data[ 0 ].length && ! data[ 0 ][ j ].equalsIgnoreCase( "NAME" ); j++ ); 297 298 if( j == data[0].length ) 299 { 300 throw new IVException( Resource.COLNOTFOUNDCMD, new Object [] {"", "NAME", ""} ); 301 } 302 303 for( k = 0; k < data[ 0 ].length && ! data[ 0 ][ k ].equalsIgnoreCase( "VALUE" ); k++ ); 304 305 if( k == data[ 0 ].length ) 306 { 307 throw new IVException( Resource.COLNOTFOUNDCMD, new Object [] {"", "VALUE", ""} ); 308 } 309 310 for( int i = 1; i < data.length; i++ ) 311 { 312 context.setValue( context.apply( data[i][j] ), 313 context.apply( data[i][k] ) ); 314 } 315 } 316 317 334 335 public static void setStandardContextData( StandardContext context, 336 String [][] data, 337 int row ) 338 throws IVException 339 { 340 for ( int i = 0; i < data[ row ].length; i++ ) 341 { 342 context.setValue( context.apply( data[ 0 ][ i ] ), 343 context.apply( data[ row ][ i ] ) ); 344 } 345 } 346 } 347 | Popular Tags |