/** * Initializes this mapping with the regular expression used to map header * names and the {@link BeanField} they should be mapped to. * * @param pattern A valid regular expression against which potential header * names are matched * @param field The {@link BeanField} this mapping maps to * @param errorLocale The locale for error messages */ public RegexToBeanField(final String pattern, final BeanField<T> field, final Locale errorLocale) { super(field, errorLocale); regex = OpencsvUtils.compilePattern(pattern, Pattern.CASE_INSENSITIVE, BeanFieldJoin.class, this.errorLocale); }
/** * Simply calls the same constructor in the base class. * @param field A {@link java.lang.reflect.Field} object. * @param required Whether or not this field is required in input * @param errorLocale The errorLocale to use for error messages. * @param converter The converter to be used to perform the actual data * conversion * @param capture See {@link CsvBindByName#capture()} * @param format The format string used for packaging values to be written. * If {@code null} or empty, it is ignored. * @see AbstractBeanField#AbstractBeanField(java.lang.reflect.Field, boolean, java.util.Locale, com.opencsv.bean.CsvConverter) */ public BeanFieldSingleValue(Field field, boolean required, Locale errorLocale, CsvConverter converter, String capture, String format) { super(field, required, errorLocale, converter); this.capture = OpencsvUtils.compilePatternAtLeastOneGroup( capture, 0, BeanFieldSingleValue.class, this.errorLocale); this.writeFormat = format; // Verify that the format string works as expected OpencsvUtils.verifyFormatString(this.writeFormat, BeanFieldSingleValue.class, this.errorLocale); }
this.splitOn = OpencsvUtils.compilePattern(splitOn, 0, BeanFieldSplit.class, this.errorLocale); this.capture = OpencsvUtils.compilePatternAtLeastOneGroup(capture, 0, BeanFieldSplit.class, this.errorLocale); OpencsvUtils.verifyFormatString(this.writeFormat, BeanFieldSplit.class, this.errorLocale);
@Override public void run() { try { OpencsvUtils.queueRefuseToAcceptDefeat(resultantLineQueue, new OrderedObject<>(lineNumber, mappingStrategy.transmuteBean(bean))); } catch (CsvException e) { e.setLineNumber(lineNumber); if(throwExceptions) { throw new RuntimeException(e); } OpencsvUtils.queueRefuseToAcceptDefeat(thrownExceptionsQueue, new OrderedObject<>(lineNumber, e)); } catch(CsvRuntimeException csvre) { // Rethrowing exception here because I do not want the CsvRuntimeException caught and rewrapped in the catch below. throw csvre; } catch(Exception t) { throw new RuntimeException(t); } }
/** * Custodial tasks that must be performed before beans are written to a CSV * destination for the first time. * @param bean Any bean to be written. Used to determine the mapping * strategy automatically. The bean itself is not written to the output by * this method. * @throws CsvRequiredFieldEmptyException If a required header is missing * while attempting to write. Since every other header is hard-wired * through the bean fields and their associated annotations, this can only * happen with multi-valued fields. */ private void beforeFirstWrite(T bean) throws CsvRequiredFieldEmptyException { // Determine mapping strategy if(mappingStrategy == null) { mappingStrategy = OpencsvUtils.<T>determineMappingStrategy((Class<T>)bean.getClass(), errorLocale); } // Build CSVWriter if (csvwriter == null) { csvwriter = new CSVWriter(writer, separator, quotechar, escapechar, lineEnd); } // Write the header String[] header = mappingStrategy.generateHeader(bean); if(header.length > 0) { csvwriter.writeNext(header, applyQuotesToAll); } headerWritten = true; }
@Override public void run() { try { if (filter == null || filter.allowLine(line)) { T obj = processLine(); ListIterator<BeanVerifier<T>> verifierList = verifiers.listIterator(); boolean keep = true; while(keep && verifierList.hasNext()) { keep = verifierList.next().verifyBean(obj); } if (keep) { OpencsvUtils.queueRefuseToAcceptDefeat( resultantBeanQueue, new OrderedObject<>(lineNumber, obj)); } } } catch (CsvException e) { e.setLineNumber(lineNumber); e.setLine(line); if (throwExceptions) { throw new RuntimeException(e); } OpencsvUtils.queueRefuseToAcceptDefeat(thrownExceptionsQueue, new OrderedObject<>(lineNumber, e)); } catch (Exception e) { throw new RuntimeException(e); } }
mappingStrategy = OpencsvUtils.determineMappingStrategy(type, errorLocale);
Pattern tempPattern = compilePattern(regex, regexFlags, callingClass, errorLocale); errorLocale = errorLocale == null ? Locale.getDefault() : errorLocale;