Search in sources :

Example 1 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class LabelScribe method _parseHtml.

@Override
protected Label _parseHtml(HCardElement element, ParseContext context) {
    Label property = new Label(element.value());
    List<String> types = element.types();
    property.getParameters().putAll(VCardParameters.TYPE, types);
    return property;
}
Also used : Label(ezvcard.property.Label)

Example 2 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class VCardReaderTest method label_properties.

/**
 * LABEL properties should be assigned to an ADR and stored in the
 * "Address.getLabel()" field. LABELs that could not be assigned to an ADR
 * should go in "VCard.getOrphanedLabels()".
 */
@Test
public void label_properties() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        // @formatter:off
        VCardAsserter asserter = read("BEGIN:VCARD\r\n" + "VERSION:" + version.getVersion() + "\r\n" + "ADR;TYPE=home;TYPE=parcel:;;123 Main St.;Austin;TX;91827;USA\r\n" + "LABEL;TYPE=parcel;TYPE=home:123 Main St.\\nAustin\\, TX 91827\\nUSA\r\n" + "ADR;TYPE=work:;;200 Broadway;New York;NY;12345;USA\r\n" + "LABEL;TYPE=parcel:200 Broadway\\nNew York\\, NY 12345\\nUSA\r\n" + "END:VCARD\r\n");
        asserter.next(version);
        asserter.address().streetAddress("123 Main St.").locality("Austin").region("TX").postalCode("91827").country("USA").label("123 Main St." + NEWLINE + "Austin, TX 91827" + NEWLINE + "USA").types(AddressType.HOME, AddressType.PARCEL).next().streetAddress("200 Broadway").locality("New York").region("NY").postalCode("12345").country("USA").types(AddressType.WORK).noMore();
        asserter.simpleProperty(Label.class).value("200 Broadway" + NEWLINE + "New York, NY 12345" + NEWLINE + "USA").param("TYPE", "parcel").noMore();
        asserter.done();
    // @formatter:on
    }
}
Also used : Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) VCardAsserter(ezvcard.property.asserter.VCardAsserter) Test(org.junit.Test)

Example 3 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class HCardParser method visit.

private void visit(Element element) {
    boolean visitChildren = true;
    Set<String> classNames = element.classNames();
    for (String className : classNames) {
        className = className.toLowerCase();
        // give special treatment to certain URLs
        if (urlPropertyName.equals(className)) {
            String href = element.attr("href");
            if (href.length() > 0) {
                if (!classNames.contains(emailName) && href.matches("(?i)mailto:.*")) {
                    className = emailName;
                } else if (!classNames.contains(telName) && href.matches("(?i)tel:.*")) {
                    className = telName;
                } else {
                    // try parsing as IMPP
                    VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(Impp.class);
                    context.getWarnings().clear();
                    context.setPropertyName(scribe.getPropertyName());
                    try {
                        VCardProperty property = scribe.parseHtml(new HCardElement(element), context);
                        vcard.addProperty(property);
                        warnings.addAll(context.getWarnings());
                        continue;
                    } catch (SkipMeException e) {
                    // URL is not an instant messenger URL
                    } catch (CannotParseException e) {
                    // URL is not an instant messenger URL
                    }
                }
            }
        }
        // hCard uses a different name for the CATEGORIES property
        if ("category".equals(className)) {
            className = categoriesName;
        }
        VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(className);
        if (scribe == null) {
            // if no scribe is found, and the class name doesn't start with "x-", then it must be an arbitrary CSS class that has nothing to do with vCard
            if (!className.startsWith("x-")) {
                continue;
            }
            scribe = new RawPropertyScribe(className);
        }
        context.getWarnings().clear();
        context.setPropertyName(scribe.getPropertyName());
        VCardProperty property;
        try {
            property = scribe.parseHtml(new HCardElement(element), context);
            warnings.addAll(context.getWarnings());
            // LABELs must be treated specially so they can be matched up with their ADRs
            if (property instanceof Label) {
                labels.add((Label) property);
                continue;
            }
            // add all NICKNAMEs to the same type object
            if (property instanceof Nickname) {
                Nickname nn = (Nickname) property;
                if (nickname == null) {
                    nickname = nn;
                    vcard.addProperty(nickname);
                } else {
                    nickname.getValues().addAll(nn.getValues());
                }
                continue;
            }
            // add all CATEGORIES to the same type object
            if (property instanceof Categories) {
                Categories c = (Categories) property;
                if (categories == null) {
                    categories = c;
                    vcard.addProperty(categories);
                } else {
                    categories.getValues().addAll(c.getValues());
                }
                continue;
            }
        } catch (SkipMeException e) {
            // @formatter:off
            warnings.add(new ParseWarning.Builder(context).message(22, e.getMessage()).build());
            // @formatter:on
            continue;
        } catch (CannotParseException e) {
            // @formatter:off
            warnings.add(new ParseWarning.Builder(context).message(e).build());
            // @formatter:on
            property = new RawProperty(className, element.outerHtml());
        } catch (EmbeddedVCardException e) {
            if (isChildOf(element, embeddedVCards)) {
                // prevents multiple-nested embedded elements from overwriting each other
                continue;
            }
            property = e.getProperty();
            embeddedVCards.add(element);
            HCardParser embeddedReader = new HCardParser(element, pageUrl);
            try {
                VCard embeddedVCard = embeddedReader.readNext();
                e.injectVCard(embeddedVCard);
            } finally {
                warnings.addAll(embeddedReader.getWarnings());
                IOUtils.closeQuietly(embeddedReader);
            }
            visitChildren = false;
        }
        vcard.addProperty(property);
    }
    if (visitChildren) {
        for (Element child : element.children()) {
            visit(child);
        }
    }
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) EmbeddedVCardException(ezvcard.io.EmbeddedVCardException) Categories(ezvcard.property.Categories) Impp(ezvcard.property.Impp) RawPropertyScribe(ezvcard.io.scribe.RawPropertyScribe) Element(org.jsoup.nodes.Element) Label(ezvcard.property.Label) SkipMeException(ezvcard.io.SkipMeException) RawProperty(ezvcard.property.RawProperty) CannotParseException(ezvcard.io.CannotParseException) VCardProperty(ezvcard.property.VCardProperty) VCard(ezvcard.VCard) Nickname(ezvcard.property.Nickname)

Example 4 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class VCardReaderTest method nested_vcard_with_labels.

@Test
public void nested_vcard_with_labels() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        /*
			 * Test against all versions, even though nested vCards are only
			 * supported by 2.1.
			 */
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "ADR;TYPE=home:;;;;;\r\n" + "ADR;TYPE=work:;;;;;\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "LABEL;TYPE=home:home label\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "ADR;TYPE=dom:;;;;;\r\n" + "LABEL;TYPE=dom:dom label\r\n" + "END:VCARD\r\n" + "ADR;TYPE=dom:;;;;;\r\n" + "END:VCARD\r\n" + "LABEL;TYPE=work:work label\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        VCard vcard = reader.readNext();
        assertVersion(version, vcard);
        assertPropertyCount(3, vcard);
        Iterator<Address> adrs = vcard.getAddresses().iterator();
        Address adr = adrs.next();
        assertEquals(Arrays.asList(AddressType.HOME), adr.getTypes());
        assertNull(adr.getLabel());
        adr = adrs.next();
        assertEquals(Arrays.asList(AddressType.WORK), adr.getTypes());
        assertEquals("work label", adr.getLabel());
        {
            VCard agentVCard = vcard.getAgent().getVCard();
            assertVersion(version, agentVCard);
            assertPropertyCount(3, agentVCard);
            adr = agentVCard.getAddresses().get(0);
            assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes());
            assertNull(adr.getLabel());
            Label label = agentVCard.getOrphanedLabels().get(0);
            assertEquals(Arrays.asList(AddressType.HOME), label.getTypes());
            {
                VCard agentAgentVCard = agentVCard.getAgent().getVCard();
                assertVersion(version, agentAgentVCard);
                assertPropertyCount(1, agentAgentVCard);
                adr = agentAgentVCard.getAddresses().get(0);
                assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes());
                assertEquals("dom label", adr.getLabel());
            }
        }
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : Address(ezvcard.property.Address) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 5 with Label

use of ezvcard.property.Label in project ez-vcard by mangstadt.

the class StreamWriterTest method labels.

@Test
public void labels() throws Throwable {
    writer.setAddProdId(false);
    // address with label and type
    Address adr = new Address();
    adr.setLabel("value1");
    adr.getTypes().add(AddressType.HOME);
    vcard.addAddress(adr);
    // address with label
    adr = new Address();
    adr.setLabel("value2");
    vcard.addAddress(adr);
    // address
    adr = new Address();
    vcard.addAddress(adr);
    for (VCardVersion version : each(V2_1, V3_0)) {
        writer.write(vcard, version);
        assertEquals(5, writer.count());
        assertEquals(3, writer.count(Address.class));
        assertEquals(2, writer.count(Label.class));
        Label label = writer.get(Label.class).get(0);
        assertEquals("value1", label.getValue());
        assertEquals(Arrays.asList(AddressType.HOME), label.getTypes());
        label = writer.get(Label.class).get(1);
        assertEquals("value2", label.getValue());
        assertEquals(Arrays.asList(), label.getTypes());
    }
    writer.write(vcard, V4_0);
    assertEquals(3, writer.count());
    assertEquals(3, writer.count(Address.class));
}
Also used : Address(ezvcard.property.Address) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) Test(org.junit.Test)

Aggregations

Label (ezvcard.property.Label)8 VCardVersion (ezvcard.VCardVersion)4 Address (ezvcard.property.Address)4 Test (org.junit.Test)4 VCard (ezvcard.VCard)2 RawProperty (ezvcard.property.RawProperty)2 VCardProperty (ezvcard.property.VCardProperty)2 VCardAsserter (ezvcard.property.asserter.VCardAsserter)2 HashSet (java.util.HashSet)2 CannotParseException (ezvcard.io.CannotParseException)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 SkipMeException (ezvcard.io.SkipMeException)1 RawPropertyScribe (ezvcard.io.scribe.RawPropertyScribe)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 AddressType (ezvcard.parameter.AddressType)1 Categories (ezvcard.property.Categories)1 Impp (ezvcard.property.Impp)1 Nickname (ezvcard.property.Nickname)1 ProductId (ezvcard.property.ProductId)1 ArrayList (java.util.ArrayList)1