Exemplo n.º 1
   * This example has been prepared with Outlook 2003, it is full of errors, but still the library
   * should be able to parse it as well as possible.
   * <p>This test also makes use of a custom ParameterRegistry, that allows me to work around the
   * Outlook quirk, that places the TYPE parameter values without the TYPE string, i.e. instead of
   * TYPE=HOME,WORK, we have only HOME,WORK.
   * @throws ParserException
   * @throws IOException
   * @throws ValidationException
   * @throws DecoderException
  public void testOutlookExample()
      throws IOException, ParserException, ValidationException, DecoderException {
    File file = new File("src/test/resources/samples/vcard-antoni-outlook2003.vcf");
    Reader reader = new FileReader(file);
    GroupRegistry groupRegistry = new GroupRegistry();
    PropertyFactoryRegistry propReg = new PropertyFactoryRegistry();
    ParameterFactoryRegistry parReg = new ParameterFactoryRegistry();

     * The custom registry allows the file to be parsed correctly. It's the
     * first workaround for the Outlook problem.
    VCardBuilder builder = new VCardBuilder(reader, groupRegistry, propReg, parReg);

    VCard card = builder.build();
    assertEquals("Antoni Jozef Mylka jun.", card.getProperty(Id.FN).getValue());

     * To test whether the file has really been parsed correctly, we
     * generate a string out of it. Before writing this test, the builder
     * contained a bug. The file contains non-standard folding. The LABEL
     * property has two lines, but the second line is not indented properly.
     * The builder used to interpret it as a separate property. Since it
     * didn't know it, it used to insert NULL into the property list. This
     * NULL yielded a NullPointerException when trying to serialize the file
     * back.
     * If we can't preserve all data we should still have "something"
     * note: we use non-validating outputter, since the ENCODING parameter
     * has been deprecated in the newest versions
    VCardOutputter outputter = new VCardOutputter(false);
    StringWriter writer = new StringWriter();
    outputter.output(card, writer);

     * We don't support quoted printable, and we don't try to support
     * the crappy Outlook 2003 folding, but we would still like to
     * get something.
    Property labelProperty = card.getProperty(Id.LABEL);
    String labelvalue = labelProperty.getValue();
        "3.10=0D=0ATrippstadter Str. 122=0D=0AKaiserslautern, " + "Rheinland-Pfalz 67663=",

     * A workaround for the limitation above, a utility method, that
     * checks the encoding of a property, and returns an un-encoded
     * value.
        "3.10\r\nTrippstadter Str. 122\r\nKaiserslautern, " + "Rheinland-Pfalz 67663",

     * Another issue found, the BDAY property is parsed, but the
     * value is not converted to a date, and te BDay.getDate() method
     * returns null.
    BDay bday = (BDay) card.getProperty(Id.BDAY);
    assertEquals("19800118", bday.getValue());

     * Yet another issue. The entry in PropertyFactoryRegistry for the ORG
     * property was invalid. There should be TWO values for this file
     * and the org property.
    String[] vals = ((Org) card.getProperty(Id.ORG)).getValues();
    assertEquals(2, vals.length);
    assertEquals("DFKI", vals[0]);
    assertEquals("Knowledge-Management", vals[1]);
 public void testOutput() throws IOException, ValidationException {
   StringWriter out = new StringWriter();
   outputter.output(card, out);
   assertEquals(expectedOutput, out.toString().replaceAll("\\r\\n ", ""));