1 5 package com.jofti.config; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Properties ; 14 15 import org.xmlpull.v1.XmlPullParser; 16 import org.xmlpull.v1.XmlPullParserException; 17 import org.xmlpull.v1.XmlPullParserFactory; 18 19 import com.jofti.exception.JoftiException; 20 21 31 public class ConfigFileParser { 32 33 34 42 public Map parseIndexConfig(String configFile) throws JoftiException 43 { 44 45 Map temp =null; 46 47 InputStream stream = null; 48 49 try { 50 stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile); 51 52 if (stream == null){ 53 throw new JoftiException("Unable to find config file '" + configFile +"' on classpath - ensure the file name is correctly configured"); 54 } 55 temp= parseConfig(stream); 56 57 } catch (JoftiException e) { 58 e.printStackTrace(); 59 } catch(Throwable t){ 60 t.printStackTrace(); 61 }finally{ 62 63 if (stream != null){ 64 try{ 65 stream.close(); 66 } catch(Exception e){ 67 } 69 } 70 } 71 return temp; 72 } 73 74 83 public Map parseIndexConfig(InputStream stream) throws JoftiException 84 { 85 86 Map temp =null; 87 88 89 try { 90 91 if (stream == null){ 92 throw new JoftiException("InputStream is null ensure the file name is correctly configured"); 93 } 94 temp= parseConfig(stream); 95 96 } catch (JoftiException e) { 97 e.printStackTrace(); 98 } catch(Throwable t){ 99 t.printStackTrace(); 100 }finally{ 101 102 if (stream != null){ 103 try{ 104 stream.close(); 105 } catch(Exception e){ 106 } 108 } 109 } 110 return temp; 111 } 112 113 114 private XmlPullParser getParser() throws Exception { 115 return XmlPullParserFactory.newInstance("org.kxml2.io.KXmlParser",Thread.currentThread().getContextClassLoader().getClass()).newPullParser(); 116 117 } 118 119 120 private Map parseConfig(InputStream stream) throws JoftiException 121 { 122 Map indexMap = new HashMap (); 123 124 125 126 XmlPullParser parser = null; 127 128 try { 129 130 if (stream == null){ 131 throw new JoftiException("InputStrean is null - ensure the file name is correctly configured"); 132 } 133 134 parser = getParser(); 135 136 parser.setInput(stream, null); 137 138 int eventType = parser.getEventType(); 139 do { 140 if(eventType == XmlPullParser.START_DOCUMENT) { 141 142 indexMap = processIndexes(parser); 143 144 } 145 146 if (parser.getEventType() !=XmlPullParser.END_DOCUMENT ){ 147 eventType = parser.next(); 148 } 149 }while (eventType != XmlPullParser.END_DOCUMENT); 150 parser.setInput (null); 151 } catch (JoftiException e) { 152 153 e.printStackTrace(); 154 } catch (XmlPullParserException e) { 155 156 e.printStackTrace(); 157 } catch (IOException e) { 158 159 e.printStackTrace(); 160 } catch(Throwable t){ 161 t.printStackTrace(); 162 }finally{ 163 164 if (stream != null){ 165 try{ 166 stream.close(); 167 } catch(Exception e){ 168 } 170 } 171 } 172 173 174 return indexMap; 175 } 176 177 178 private Map processIndexes(XmlPullParser parser) throws XmlPullParserException, IOException , JoftiException{ 179 180 181 Map temp = new HashMap (); 182 while(true){ 184 int tagType = parser.nextTag(); 185 186 if (tagType == XmlPullParser.START_TAG){ 187 String name = parser.getName(); 188 189 if ("indexes".equalsIgnoreCase(name)){ 190 if (!parser.isEmptyElementTag()){ 191 tagType = parser.nextTag(); 193 while ( tagType == XmlPullParser.START_TAG && "index".equalsIgnoreCase(parser.getName())){ 195 IndexConfig config = processIndex(parser); 196 temp.put(config.getName(),config); 197 parser.nextTag(); 198 } 199 200 }else{ 201 throw new JoftiException("Indexes definition in config file is empty"); 202 } 203 }else{ 204 throw new JoftiException("Unrecognised tag " + name + " at root of document"); 205 } 206 }else if (tagType == XmlPullParser.END_TAG){ 207 break; 208 } 209 210 parser.require(XmlPullParser.END_TAG,null,"indexes"); 212 break; 213 } 214 return temp; 215 } 216 217 218 IndexConfig processIndex(XmlPullParser parser) 219 throws IOException , XmlPullParserException,JoftiException { 220 221 222 223 DefaultIndexConfig config = new DefaultIndexConfig(); 224 225 String name = parser.getName(); 226 227 228 if ("index".equalsIgnoreCase(name)){ 229 230 int numAttributes = parser.getAttributeCount(); 232 if (numAttributes >= 0){ 233 for (int i=0;i<numAttributes;i++){ 234 String attributeName = parser.getAttributeName(i); 235 if (attributeName.equalsIgnoreCase("name")){ 236 config.setName(parser.getAttributeValue(i)); 237 } else 238 if (attributeName.equalsIgnoreCase("init-method")){ 239 } else 241 if (attributeName.equalsIgnoreCase("destroy-method")){ 242 } 244 else { 245 } 247 } 248 } 249 250 int event = parser.nextTag(); 252 253 name= parser.getName(); 254 while ((event == XmlPullParser.START_TAG) && ("cache".equalsIgnoreCase(name) || 255 "classes".equalsIgnoreCase(name) ||"parser".equalsIgnoreCase(name) || "type".equalsIgnoreCase(name) 256 || "disk-overflow".equalsIgnoreCase(name) 257 || "queries".equalsIgnoreCase(name))) 258 { 259 260 261 if (name.equalsIgnoreCase("cache")){ 262 config = processCache(parser,config); 264 parser.require(XmlPullParser.END_TAG,"","cache"); 265 266 }else if (name.equalsIgnoreCase("classes")){ 267 config = (DefaultIndexConfig)processClasses(parser, config); 268 parser.require(XmlPullParser.END_TAG,"","classes"); 269 270 }else if (name.equalsIgnoreCase("queries")){ 271 config = (DefaultIndexConfig)processQueries(parser, config); 272 parser.require(XmlPullParser.END_TAG,"","queries"); 273 274 }else if (name.equalsIgnoreCase("parser")){ 275 config.setParserType(parser.nextText().trim()); 276 parser.require(XmlPullParser.END_TAG,"","parser"); 277 278 }else if (name.equalsIgnoreCase("type")){ 279 config.setIndexType(parser.nextText().trim()); 280 parser.require(XmlPullParser.END_TAG,"","type"); 281 }else if (name.equalsIgnoreCase("disk-overflow")){ 282 config = (DefaultIndexConfig)processOverflow(parser, config); 283 parser.require(XmlPullParser.END_TAG,"","disk-overflow"); 284 } 285 286 event = parser.nextTag(); 287 name= parser.getName(); 288 289 } 290 291 parser.require(XmlPullParser.END_TAG, "", "index"); 292 293 } 294 return config; 295 } 296 297 private DefaultIndexConfig processCache(XmlPullParser parser, DefaultIndexConfig config) 298 throws IOException , XmlPullParserException, JoftiException{ 299 300 int event = parser.nextTag(); 302 String name = parser.getName(); 303 if(name.equalsIgnoreCase("adapter")){ 304 int adapterAttribs = parser.getAttributeCount(); 305 for (int i=0;i<adapterAttribs;i++){ 306 String temp = parser.getAttributeName(i); 307 if (temp.equalsIgnoreCase("init-method")){ 308 } else 310 if (temp.equalsIgnoreCase("destroy-method")){ 311 } else { 313 } 315 } 316 event = parser.nextTag(); 318 while (event == XmlPullParser.START_TAG){ 319 name = parser.getName(); 321 if (name.equalsIgnoreCase("type")){ 322 String content = parser.nextText(); 323 config.setCacheAdapter(content.trim()); 324 parser.require(XmlPullParser.END_TAG,"","type"); 325 event = parser.nextTag(); 326 name = parser.getName(); 327 } 328 if (name.equalsIgnoreCase("init-properties")){ 329 Properties props = new Properties (); 330 if (!parser.isEmptyElementTag()){ 331 while (parser.nextTag() == XmlPullParser.START_TAG){ 333 name = parser.getName(); 334 if (name.equalsIgnoreCase("property")){ 335 String temp = parser.getAttributeValue(0); 336 if (temp != null){ 337 props.put(temp, parser.nextText().trim()); 338 } 339 } 340 parser.require(XmlPullParser.END_TAG,"",name); 341 } 342 config.setAdapterProperties(props); 343 parser.require(XmlPullParser.END_TAG,"","init-properties"); 345 event = parser.nextTag(); 346 347 } 348 349 } 350 } 351 352 parser.require(XmlPullParser.END_TAG,"","adapter"); 353 parser.nextTag(); 354 } 355 356 return config; 357 } 358 359 360 private IndexConfig processOverflow(XmlPullParser parser, IndexConfig config) 361 throws IOException , XmlPullParserException, JoftiException{ 362 363 Properties props = config.getIndexProperties(); 364 int event = parser.nextTag(); 366 while (event == XmlPullParser.START_TAG){ 367 String name = parser.getName(); 369 if (name.equalsIgnoreCase("provider")){ 370 String content = parser.nextText(); 371 if (content !=null){ 372 content = content.trim(); 373 } 374 props.put("provider",content); 375 parser.require(XmlPullParser.END_TAG,"","provider"); 376 event = parser.nextTag(); 377 name = parser.getName(); 378 } 379 380 if (name.equalsIgnoreCase("init-properties")){ 381 382 if (!parser.isEmptyElementTag()){ 383 while (parser.nextTag() == XmlPullParser.START_TAG){ 385 name = parser.getName(); 386 if (name.equalsIgnoreCase("property")){ 387 String temp = parser.getAttributeValue(0); 388 if (temp != null){ 389 props.put(temp, parser.nextText().trim()); 390 } 391 } 392 parser.require(XmlPullParser.END_TAG,"",name); 393 } 394 395 parser.require(XmlPullParser.END_TAG,"","init-properties"); 397 event = parser.nextTag(); 398 399 } 400 401 } 402 } 403 404 405 406 return config; 407 } 408 private IndexConfig processClasses(XmlPullParser parser, IndexConfig config) 409 throws IOException , XmlPullParserException, JoftiException{ 410 411 Map temp = new HashMap (); 412 while (parser.nextTag() == XmlPullParser.START_TAG){ 414 String name = parser.getName(); 415 if (name.equalsIgnoreCase("class")){ 416 List list = new ArrayList (); 417 String className = parser.getAttributeValue(0); 418 if (!parser.isEmptyElementTag()){ 419 ; 420 while (parser.nextTag() == XmlPullParser.START_TAG){ 422 name = parser.getName(); 423 if (name.equalsIgnoreCase("property")){ 424 list.add(parser.nextText()); 425 } 426 parser.require(XmlPullParser.END_TAG,"",name); 427 } 428 parser.require(XmlPullParser.END_TAG,"","class"); 430 temp.put(className,list); 431 } 432 config.addMapping(className,list); 433 } 434 } 435 436 return config; 437 } 438 439 private IndexConfig processQueries(XmlPullParser parser, IndexConfig config) 440 throws IOException , XmlPullParserException, JoftiException{ 441 442 while (parser.nextTag() == XmlPullParser.START_TAG){ 444 String name = parser.getName(); 445 if (name.equalsIgnoreCase("query")){ 446 String queryText = null; 447 String queryName = parser.getAttributeValue(0); 448 449 queryText = parser.nextText().trim(); 450 451 config.addQuery(queryName,queryText); 452 parser.require(XmlPullParser.END_TAG,"","query"); 453 } 454 } 455 456 return config; 457 } 458 466 public IndexConfig parseClassMapping(IndexConfig config, InputStream stream) throws JoftiException{ 467 return parseClassFromStream(config,stream); 468 } 469 470 479 public IndexConfig parseClassMapping(IndexConfig config, String classFileName) throws JoftiException{ 480 InputStream stream =null; 481 try { 482 stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(classFileName); 483 484 if (stream == null){ 485 throw new JoftiException("Unable to find config file '" + classFileName +"' on classpath - ensure the file name is correctly configured"); 486 } 487 config = parseClassFromStream(config,stream); 488 } catch(Throwable t){ 489 if(t instanceof JoftiException){ 490 throw (JoftiException )t; 491 }else{ 492 throw new JoftiException(t); 493 } 494 } 495 return config; 496 497 } 498 499 private IndexConfig parseClassFromStream(IndexConfig config, InputStream stream) throws JoftiException{ 500 501 502 try { 503 504 if (stream == null){ 505 throw new JoftiException("Input Stream for classes is null"); 506 } 507 508 XmlPullParser parser = getParser(); 509 parser.setInput(stream, null); 510 511 int eventType = parser.getEventType(); 512 513 do { 514 if(eventType == XmlPullParser.START_DOCUMENT){ 515 516 eventType = parser.nextTag(); 517 String name = parser.getName(); 518 while (eventType == XmlPullParser.START_TAG) { 519 520 521 if (eventType == XmlPullParser.START_TAG && ("classes".equalsIgnoreCase(name)) 522 ) { 523 524 525 config = processClasses(parser, config); 526 527 parser.require(XmlPullParser.END_TAG,"","classes"); 528 eventType =parser.next(); 529 name = parser.getName(); 530 531 }else if (eventType == XmlPullParser.START_TAG && ("queries".equalsIgnoreCase(name)) 532 ) { 533 config = processQueries(parser, config); 534 535 parser.require(XmlPullParser.END_TAG,"","queries"); 536 eventType =parser.next(); 537 name = parser.getName(); 538 }else if (eventType == XmlPullParser.START_TAG && ("disk-overflow".equalsIgnoreCase(name)) 539 ) { 540 config =processOverflow(parser, config); 541 parser.require(XmlPullParser.END_TAG,"","disk-overflow"); 542 eventType =parser.next(); 543 name = parser.getName(); 544 }else{ 545 throw new JoftiException("Classes definitions must contain only '<classes>' or '<queries>' or '<disk-overflow>' tag"); 546 } 547 } 548 549 } 550 551 if (eventType !=XmlPullParser.END_DOCUMENT ){ 553 throw new JoftiException("Document not formatted correctly extra tag found "+ parser.getName()); 554 } 555 }while (eventType != XmlPullParser.END_DOCUMENT); 556 parser.setInput (null); 557 } catch (Exception e) { 558 559 throw new JoftiException(e); 560 }finally{ 561 562 if (stream != null){ 563 try{ 564 stream.close(); 565 } catch(Exception e){ 566 } 568 } 569 } 570 return config; 571 } 572 573 574 575 } 576 577 | Popular Tags |