001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.csv; 014 015import java.lang.annotation.*; 016import java.lang.reflect.*; 017import java.nio.charset.*; 018import java.util.*; 019import java.util.concurrent.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.internal.*; 023import org.apache.juneau.parser.*; 024import org.apache.juneau.utils.*; 025 026/** 027 * TODO - Work in progress. CSV parser. 028 * 029 * <h5 class='section'>Notes:</h5><ul> 030 * <li class='note'>This class is thread safe and reusable. 031 * </ul> 032 * 033 * <h5 class='section'>See Also:</h5><ul> 034 035 * </ul> 036 */ 037public class CsvParser extends ReaderParser implements CsvMetaProvider { 038 039 //------------------------------------------------------------------------------------------------------------------- 040 // Static 041 //------------------------------------------------------------------------------------------------------------------- 042 043 /** Default parser, all default settings.*/ 044 public static final CsvParser DEFAULT = new CsvParser(create()); 045 046 /** 047 * Creates a new builder for this object. 048 * 049 * @return A new builder. 050 */ 051 public static Builder create() { 052 return new Builder(); 053 } 054 055 //------------------------------------------------------------------------------------------------------------------- 056 // Builder 057 //------------------------------------------------------------------------------------------------------------------- 058 059 /** 060 * Builder class. 061 */ 062 @FluentSetters 063 public static class Builder extends ReaderParser.Builder { 064 065 private static final Cache<HashKey,CsvParser> CACHE = Cache.of(HashKey.class, CsvParser.class).build(); 066 067 /** 068 * Constructor, default settings. 069 */ 070 protected Builder() { 071 super(); 072 consumes("text/csv"); 073 } 074 075 /** 076 * Copy constructor. 077 * 078 * @param copyFrom The bean to copy from. 079 */ 080 protected Builder(CsvParser copyFrom) { 081 super(copyFrom); 082 } 083 084 /** 085 * Copy constructor. 086 * 087 * @param copyFrom The builder to copy from. 088 */ 089 protected Builder(Builder copyFrom) { 090 super(copyFrom); 091 } 092 093 @Override /* Context.Builder */ 094 public Builder copy() { 095 return new Builder(this); 096 } 097 098 @Override /* Context.Builder */ 099 public CsvParser build() { 100 return cache(CACHE).build(CsvParser.class); 101 } 102 103 @Override /* Context.Builder */ 104 public HashKey hashKey() { 105 return super.hashKey(); 106 } 107 108 //----------------------------------------------------------------------------------------------------------------- 109 // Properties 110 //----------------------------------------------------------------------------------------------------------------- 111 112 // <FluentSetters> 113 114 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 115 public Builder annotations(Annotation...values) { 116 super.annotations(values); 117 return this; 118 } 119 120 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 121 public Builder apply(AnnotationWorkList work) { 122 super.apply(work); 123 return this; 124 } 125 126 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 127 public Builder applyAnnotations(java.lang.Class<?>...fromClasses) { 128 super.applyAnnotations(fromClasses); 129 return this; 130 } 131 132 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 133 public Builder applyAnnotations(Method...fromMethods) { 134 super.applyAnnotations(fromMethods); 135 return this; 136 } 137 138 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 139 public Builder cache(Cache<HashKey,? extends org.apache.juneau.Context> value) { 140 super.cache(value); 141 return this; 142 } 143 144 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 145 public Builder debug() { 146 super.debug(); 147 return this; 148 } 149 150 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 151 public Builder debug(boolean value) { 152 super.debug(value); 153 return this; 154 } 155 156 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 157 public Builder impl(Context value) { 158 super.impl(value); 159 return this; 160 } 161 162 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 163 public Builder type(Class<? extends org.apache.juneau.Context> value) { 164 super.type(value); 165 return this; 166 } 167 168 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 169 public Builder beanClassVisibility(Visibility value) { 170 super.beanClassVisibility(value); 171 return this; 172 } 173 174 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 175 public Builder beanConstructorVisibility(Visibility value) { 176 super.beanConstructorVisibility(value); 177 return this; 178 } 179 180 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 181 public Builder beanContext(BeanContext value) { 182 super.beanContext(value); 183 return this; 184 } 185 186 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 187 public Builder beanContext(BeanContext.Builder value) { 188 super.beanContext(value); 189 return this; 190 } 191 192 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 193 public Builder beanDictionary(java.lang.Class<?>...values) { 194 super.beanDictionary(values); 195 return this; 196 } 197 198 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 199 public Builder beanFieldVisibility(Visibility value) { 200 super.beanFieldVisibility(value); 201 return this; 202 } 203 204 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 205 public Builder beanInterceptor(Class<?> on, Class<? extends org.apache.juneau.swap.BeanInterceptor<?>> value) { 206 super.beanInterceptor(on, value); 207 return this; 208 } 209 210 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 211 public Builder beanMapPutReturnsOldValue() { 212 super.beanMapPutReturnsOldValue(); 213 return this; 214 } 215 216 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 217 public Builder beanMethodVisibility(Visibility value) { 218 super.beanMethodVisibility(value); 219 return this; 220 } 221 222 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 223 public Builder beanProperties(Map<String,Object> values) { 224 super.beanProperties(values); 225 return this; 226 } 227 228 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 229 public Builder beanProperties(Class<?> beanClass, String properties) { 230 super.beanProperties(beanClass, properties); 231 return this; 232 } 233 234 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 235 public Builder beanProperties(String beanClassName, String properties) { 236 super.beanProperties(beanClassName, properties); 237 return this; 238 } 239 240 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 241 public Builder beanPropertiesExcludes(Map<String,Object> values) { 242 super.beanPropertiesExcludes(values); 243 return this; 244 } 245 246 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 247 public Builder beanPropertiesExcludes(Class<?> beanClass, String properties) { 248 super.beanPropertiesExcludes(beanClass, properties); 249 return this; 250 } 251 252 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 253 public Builder beanPropertiesExcludes(String beanClassName, String properties) { 254 super.beanPropertiesExcludes(beanClassName, properties); 255 return this; 256 } 257 258 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 259 public Builder beanPropertiesReadOnly(Map<String,Object> values) { 260 super.beanPropertiesReadOnly(values); 261 return this; 262 } 263 264 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 265 public Builder beanPropertiesReadOnly(Class<?> beanClass, String properties) { 266 super.beanPropertiesReadOnly(beanClass, properties); 267 return this; 268 } 269 270 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 271 public Builder beanPropertiesReadOnly(String beanClassName, String properties) { 272 super.beanPropertiesReadOnly(beanClassName, properties); 273 return this; 274 } 275 276 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 277 public Builder beanPropertiesWriteOnly(Map<String,Object> values) { 278 super.beanPropertiesWriteOnly(values); 279 return this; 280 } 281 282 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 283 public Builder beanPropertiesWriteOnly(Class<?> beanClass, String properties) { 284 super.beanPropertiesWriteOnly(beanClass, properties); 285 return this; 286 } 287 288 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 289 public Builder beanPropertiesWriteOnly(String beanClassName, String properties) { 290 super.beanPropertiesWriteOnly(beanClassName, properties); 291 return this; 292 } 293 294 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 295 public Builder beansRequireDefaultConstructor() { 296 super.beansRequireDefaultConstructor(); 297 return this; 298 } 299 300 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 301 public Builder beansRequireSerializable() { 302 super.beansRequireSerializable(); 303 return this; 304 } 305 306 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 307 public Builder beansRequireSettersForGetters() { 308 super.beansRequireSettersForGetters(); 309 return this; 310 } 311 312 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 313 public Builder dictionaryOn(Class<?> on, java.lang.Class<?>...values) { 314 super.dictionaryOn(on, values); 315 return this; 316 } 317 318 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 319 public Builder disableBeansRequireSomeProperties() { 320 super.disableBeansRequireSomeProperties(); 321 return this; 322 } 323 324 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 325 public Builder disableIgnoreMissingSetters() { 326 super.disableIgnoreMissingSetters(); 327 return this; 328 } 329 330 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 331 public Builder disableIgnoreTransientFields() { 332 super.disableIgnoreTransientFields(); 333 return this; 334 } 335 336 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 337 public Builder disableIgnoreUnknownNullBeanProperties() { 338 super.disableIgnoreUnknownNullBeanProperties(); 339 return this; 340 } 341 342 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 343 public Builder disableInterfaceProxies() { 344 super.disableInterfaceProxies(); 345 return this; 346 } 347 348 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 349 public <T> Builder example(Class<T> pojoClass, T o) { 350 super.example(pojoClass, o); 351 return this; 352 } 353 354 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 355 public <T> Builder example(Class<T> pojoClass, String json) { 356 super.example(pojoClass, json); 357 return this; 358 } 359 360 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 361 public Builder findFluentSetters() { 362 super.findFluentSetters(); 363 return this; 364 } 365 366 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 367 public Builder findFluentSetters(Class<?> on) { 368 super.findFluentSetters(on); 369 return this; 370 } 371 372 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 373 public Builder ignoreInvocationExceptionsOnGetters() { 374 super.ignoreInvocationExceptionsOnGetters(); 375 return this; 376 } 377 378 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 379 public Builder ignoreInvocationExceptionsOnSetters() { 380 super.ignoreInvocationExceptionsOnSetters(); 381 return this; 382 } 383 384 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 385 public Builder ignoreUnknownBeanProperties() { 386 super.ignoreUnknownBeanProperties(); 387 return this; 388 } 389 390 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 391 public Builder ignoreUnknownEnumValues() { 392 super.ignoreUnknownEnumValues(); 393 return this; 394 } 395 396 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 397 public Builder implClass(Class<?> interfaceClass, Class<?> implClass) { 398 super.implClass(interfaceClass, implClass); 399 return this; 400 } 401 402 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 403 public Builder implClasses(Map<Class<?>,Class<?>> values) { 404 super.implClasses(values); 405 return this; 406 } 407 408 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 409 public Builder interfaceClass(Class<?> on, Class<?> value) { 410 super.interfaceClass(on, value); 411 return this; 412 } 413 414 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 415 public Builder interfaces(java.lang.Class<?>...value) { 416 super.interfaces(value); 417 return this; 418 } 419 420 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 421 public Builder locale(Locale value) { 422 super.locale(value); 423 return this; 424 } 425 426 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 427 public Builder mediaType(MediaType value) { 428 super.mediaType(value); 429 return this; 430 } 431 432 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 433 public Builder notBeanClasses(java.lang.Class<?>...values) { 434 super.notBeanClasses(values); 435 return this; 436 } 437 438 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 439 public Builder notBeanPackages(String...values) { 440 super.notBeanPackages(values); 441 return this; 442 } 443 444 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 445 public Builder propertyNamer(Class<? extends org.apache.juneau.PropertyNamer> value) { 446 super.propertyNamer(value); 447 return this; 448 } 449 450 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 451 public Builder propertyNamer(Class<?> on, Class<? extends org.apache.juneau.PropertyNamer> value) { 452 super.propertyNamer(on, value); 453 return this; 454 } 455 456 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 457 public Builder sortProperties() { 458 super.sortProperties(); 459 return this; 460 } 461 462 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 463 public Builder sortProperties(java.lang.Class<?>...on) { 464 super.sortProperties(on); 465 return this; 466 } 467 468 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 469 public Builder stopClass(Class<?> on, Class<?> value) { 470 super.stopClass(on, value); 471 return this; 472 } 473 474 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 475 public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction) { 476 super.swap(normalClass, swappedClass, swapFunction); 477 return this; 478 } 479 480 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 481 public <T, S> Builder swap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction) { 482 super.swap(normalClass, swappedClass, swapFunction, unswapFunction); 483 return this; 484 } 485 486 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 487 public Builder swaps(java.lang.Class<?>...values) { 488 super.swaps(values); 489 return this; 490 } 491 492 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 493 public Builder timeZone(TimeZone value) { 494 super.timeZone(value); 495 return this; 496 } 497 498 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 499 public Builder typeName(Class<?> on, String value) { 500 super.typeName(on, value); 501 return this; 502 } 503 504 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 505 public Builder typePropertyName(String value) { 506 super.typePropertyName(value); 507 return this; 508 } 509 510 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 511 public Builder typePropertyName(Class<?> on, String value) { 512 super.typePropertyName(on, value); 513 return this; 514 } 515 516 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 517 public Builder useEnumNames() { 518 super.useEnumNames(); 519 return this; 520 } 521 522 @Override /* GENERATED - org.apache.juneau.BeanContextable.Builder */ 523 public Builder useJavaBeanIntrospector() { 524 super.useJavaBeanIntrospector(); 525 return this; 526 } 527 528 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 529 public Builder autoCloseStreams() { 530 super.autoCloseStreams(); 531 return this; 532 } 533 534 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 535 public Builder autoCloseStreams(boolean value) { 536 super.autoCloseStreams(value); 537 return this; 538 } 539 540 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 541 public Builder consumes(String value) { 542 super.consumes(value); 543 return this; 544 } 545 546 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 547 public Builder debugOutputLines(int value) { 548 super.debugOutputLines(value); 549 return this; 550 } 551 552 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 553 public Builder listener(Class<? extends org.apache.juneau.parser.ParserListener> value) { 554 super.listener(value); 555 return this; 556 } 557 558 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 559 public Builder strict() { 560 super.strict(); 561 return this; 562 } 563 564 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 565 public Builder strict(boolean value) { 566 super.strict(value); 567 return this; 568 } 569 570 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 571 public Builder trimStrings() { 572 super.trimStrings(); 573 return this; 574 } 575 576 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 577 public Builder trimStrings(boolean value) { 578 super.trimStrings(value); 579 return this; 580 } 581 582 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 583 public Builder unbuffered() { 584 super.unbuffered(); 585 return this; 586 } 587 588 @Override /* GENERATED - org.apache.juneau.parser.Parser.Builder */ 589 public Builder unbuffered(boolean value) { 590 super.unbuffered(value); 591 return this; 592 } 593 594 @Override /* GENERATED - org.apache.juneau.parser.ReaderParser.Builder */ 595 public Builder fileCharset(Charset value) { 596 super.fileCharset(value); 597 return this; 598 } 599 600 @Override /* GENERATED - org.apache.juneau.parser.ReaderParser.Builder */ 601 public Builder streamCharset(Charset value) { 602 super.streamCharset(value); 603 return this; 604 } 605 606 // </FluentSetters> 607 } 608 609 //------------------------------------------------------------------------------------------------------------------- 610 // Instance 611 //------------------------------------------------------------------------------------------------------------------- 612 613 private final Map<ClassMeta<?>,CsvClassMeta> csvClassMetas = new ConcurrentHashMap<>(); 614 private final Map<BeanPropertyMeta,CsvBeanPropertyMeta> csvBeanPropertyMetas = new ConcurrentHashMap<>(); 615 616 /** 617 * Constructor. 618 * 619 * @param builder The builder for this object. 620 */ 621 public CsvParser(Builder builder) { 622 super(builder); 623 } 624 625 @Override /* Context */ 626 public Builder copy() { 627 return new Builder(this); 628 } 629 630 @Override /* Context */ 631 public CsvParserSession.Builder createSession() { 632 return CsvParserSession.create(this); 633 } 634 635 @Override /* Context */ 636 public CsvParserSession getSession() { 637 return createSession().build(); 638 } 639 640 //----------------------------------------------------------------------------------------------------------------- 641 // Extended metadata 642 //----------------------------------------------------------------------------------------------------------------- 643 644 @Override /* CsvMetaProvider */ 645 public CsvClassMeta getCsvClassMeta(ClassMeta<?> cm) { 646 CsvClassMeta m = csvClassMetas.get(cm); 647 if (m == null) { 648 m = new CsvClassMeta(cm, this); 649 csvClassMetas.put(cm, m); 650 } 651 return m; 652 } 653 654 @Override /* CsvMetaProvider */ 655 public CsvBeanPropertyMeta getCsvBeanPropertyMeta(BeanPropertyMeta bpm) { 656 if (bpm == null) 657 return CsvBeanPropertyMeta.DEFAULT; 658 CsvBeanPropertyMeta m = csvBeanPropertyMetas.get(bpm); 659 if (m == null) { 660 m = new CsvBeanPropertyMeta(bpm.getDelegateFor(), this); 661 csvBeanPropertyMetas.put(bpm, m); 662 } 663 return m; 664 } 665}