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