Search in sources :

Example 1 with Sound

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

the class JohnDoeVCard method createVCard.

private static VCard createVCard() throws IOException {
    VCard vcard = new VCard();
    vcard.setKind(Kind.individual());
    vcard.setGender(Gender.male());
    vcard.addLanguage("en-US");
    StructuredName n = new StructuredName();
    n.setFamily("Doe");
    n.setGiven("Jonathan");
    n.getPrefixes().add("Mr");
    vcard.setStructuredName(n);
    vcard.setFormattedName("Jonathan Doe");
    vcard.setNickname("John", "Jonny");
    vcard.addTitle("Widget Engineer");
    vcard.setOrganization("Acme Co. Ltd.", "Widget Department");
    Address adr = new Address();
    adr.setStreetAddress("123 Wall St.");
    adr.setLocality("New York");
    adr.setRegion("NY");
    adr.setPostalCode("12345");
    adr.setCountry("USA");
    adr.setLabel("123 Wall St.\nNew York, NY 12345\nUSA");
    adr.getTypes().add(AddressType.WORK);
    vcard.addAddress(adr);
    adr = new Address();
    adr.setStreetAddress("123 Main St.");
    adr.setLocality("Albany");
    adr.setRegion("NY");
    adr.setPostalCode("54321");
    adr.setCountry("USA");
    adr.setLabel("123 Main St.\nAlbany, NY 54321\nUSA");
    adr.getTypes().add(AddressType.HOME);
    vcard.addAddress(adr);
    vcard.addTelephoneNumber("1-555-555-1234", TelephoneType.WORK);
    vcard.addTelephoneNumber("1-555-555-5678", TelephoneType.WORK, TelephoneType.CELL);
    vcard.addEmail("johndoe@hotmail.com", EmailType.HOME);
    vcard.addEmail("doe.john@acme.com", EmailType.WORK);
    vcard.addUrl("http://www.acme-co.com");
    vcard.setCategories("widgetphile", "biker", "vCard expert");
    vcard.setGeo(37.6, -95.67);
    vcard.setTimezone(new Timezone(new UtcOffset(false, -5, 0), "America/New_York"));
    File file = new File("portrait.jpg");
    Photo photo = new Photo(file, ImageType.JPEG);
    vcard.addPhoto(photo);
    file = new File("pronunciation.ogg");
    Sound sound = new Sound(file, SoundType.OGG);
    vcard.addSound(sound);
    vcard.setUid(Uid.random());
    vcard.setRevision(Revision.now());
    return vcard;
}
Also used : Timezone(ezvcard.property.Timezone) UtcOffset(ezvcard.util.UtcOffset) Address(ezvcard.property.Address) Photo(ezvcard.property.Photo) Sound(ezvcard.property.Sound) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName) File(java.io.File)

Example 2 with Sound

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

the class SoundScribe method _parseHtml.

@Override
protected Sound _parseHtml(HCardElement element, ParseContext context) {
    String elementName = element.tagName();
    if (!"audio".equals(elementName) && !"source".equals(elementName)) {
        return super._parseHtml(element, context);
    }
    if ("audio".equals(elementName)) {
        // parse its child "<source>" element
        org.jsoup.nodes.Element source = element.getElement().getElementsByTag("source").first();
        if (source == null) {
            throw new CannotParseException(16);
        }
        element = new HCardElement(source);
    }
    String src = element.absUrl("src");
    if (src.length() == 0) {
        throw new CannotParseException(17);
    }
    String type = element.attr("type");
    SoundType mediaType = (type.length() == 0) ? null : _mediaTypeFromMediaTypeParameter(type);
    try {
        DataUri uri = DataUri.parse(src);
        mediaType = _mediaTypeFromMediaTypeParameter(uri.getContentType());
        return new Sound(uri.getData(), mediaType);
    } catch (IllegalArgumentException e) {
        // not a data URI
        if (mediaType == null) {
            String extension = getFileExtension(src);
            mediaType = (extension == null) ? null : _mediaTypeFromFileExtension(extension);
        }
        return new Sound(src, mediaType);
    }
}
Also used : SoundType(ezvcard.parameter.SoundType) CannotParseException(ezvcard.io.CannotParseException) Sound(ezvcard.property.Sound) HCardElement(ezvcard.io.html.HCardElement) DataUri(ezvcard.util.DataUri)

Example 3 with Sound

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

the class HCardPageTest method createFullVCard.

private VCard createFullVCard() throws IOException {
    VCard vcard = new VCard();
    StructuredName n = new StructuredName();
    n.setFamily("Claus");
    n.setGiven("Santa");
    n.getAdditionalNames().add("Saint Nicholas");
    n.getAdditionalNames().add("Father Christmas");
    n.getPrefixes().add("Mr");
    n.getPrefixes().add("Dr");
    n.getSuffixes().add("M.D.");
    n.setSortAs("Claus");
    vcard.setStructuredName(n);
    vcard.setClassification("public");
    vcard.setMailer("Thunderbird");
    vcard.setFormattedName("Santa Claus");
    vcard.setNickname("Kris Kringle");
    vcard.addTitle("Manager");
    vcard.addRole("Executive");
    vcard.addRole("Team Builder");
    vcard.addEmail("johndoe@hotmail.com", EmailType.HOME, EmailType.WORK);
    vcard.addEmail("doe.john@company.com", EmailType.WORK);
    Telephone tel = new Telephone(new TelUri.Builder("+1-555-222-3333").extension("101").build());
    vcard.addTelephoneNumber(tel);
    tel = new Telephone(new TelUri.Builder("+1-555-333-4444").build());
    tel.getTypes().add(TelephoneType.WORK);
    vcard.addTelephoneNumber(tel);
    vcard.addTelephoneNumber("(555) 111-2222", TelephoneType.HOME, TelephoneType.VOICE, TelephoneType.PREF);
    Address adr = new Address();
    adr.setStreetAddress("123 Main St");
    adr.setExtendedAddress("Apt 11");
    adr.setLocality("Austin");
    adr.setRegion("Tx");
    adr.setPostalCode("12345");
    adr.setCountry("USA");
    adr.setLabel("123 Main St." + NEWLINE + "Austin TX, 12345" + NEWLINE + "USA");
    adr.getTypes().add(AddressType.HOME);
    vcard.addAddress(adr);
    adr = new Address();
    adr.setPoBox("123");
    adr.setStreetAddress("456 Wall St.");
    adr.setLocality("New York");
    adr.setRegion("NY");
    adr.setPostalCode("11111");
    adr.setCountry("USA");
    adr.getTypes().add(AddressType.PREF);
    adr.getTypes().add(AddressType.WORK);
    vcard.addAddress(adr);
    vcard.setOrganization("Google", "GMail");
    Birthday bday = new Birthday(date("1970-03-08"), false);
    vcard.setBirthday(bday);
    vcard.addUrl("http://company.com");
    vcard.setCategories("business owner", "jolly");
    vcard.addImpp(Impp.aim("myhandle"));
    vcard.addImpp(Impp.yahoo("myhandle@yahoo.com"));
    vcard.addNote("I am proficient in Tiger-Crane Style," + NEWLINE + "and I am more than proficient in the exquisite art of the Samurai sword.");
    vcard.setGeo(123.456, -98.123);
    vcard.setTimezone(new Timezone(new UtcOffset(false, -6, 0), "America/Chicago"));
    InputStream in = getClass().getResourceAsStream("hcard-portrait.jpg");
    Photo photo = new Photo(in, ImageType.JPEG);
    vcard.addPhoto(photo);
    in = getClass().getResourceAsStream("hcard-sound.ogg");
    Sound sound = new Sound(in, SoundType.OGG);
    vcard.addSound(sound);
    vcard.setUid(new Uid("urn:uuid:ffce1595-cbe9-4418-9d0d-b015655d45f6"));
    vcard.setRevision(date("2000-03-10 13:22:44"));
    return vcard;
}
Also used : Timezone(ezvcard.property.Timezone) Telephone(ezvcard.property.Telephone) Address(ezvcard.property.Address) InputStream(java.io.InputStream) TelUri(ezvcard.util.TelUri) Birthday(ezvcard.property.Birthday) Photo(ezvcard.property.Photo) Sound(ezvcard.property.Sound) UtcOffset(ezvcard.util.UtcOffset) Uid(ezvcard.property.Uid) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName)

Example 4 with Sound

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

the class HCardPageTest method create_then_parse.

@Test
public void create_then_parse() throws Exception {
    // create template
    VCard expected = createFullVCard();
    HCardPage template = new HCardPage();
    template.add(expected);
    String html = template.write();
    // write to file for manual inspection
    FileWriter writer = new FileWriter(new File("target", "vcard.html"));
    writer.write(html);
    writer.close();
    // parse template
    HCardParser reader = new HCardParser(html);
    VCard actual = reader.readNext();
    reader.close();
    assertEquals("Claus", actual.getSortString().getValue());
    assertEquals(expected.getClassification().getValue(), actual.getClassification().getValue());
    assertEquals(expected.getMailer().getValue(), actual.getMailer().getValue());
    assertEquals(expected.getFormattedName().getValue(), actual.getFormattedName().getValue());
    assertEquals(expected.getUid().getValue(), actual.getUid().getValue());
    assertEquals(expected.getNickname().getValues(), actual.getNickname().getValues());
    assertEquals(expected.getOrganization().getValues(), actual.getOrganization().getValues());
    assertEquals(expected.getCategories().getValues(), actual.getCategories().getValues());
    assertEquals(expected.getBirthday().getDate(), actual.getBirthday().getDate());
    assertEquals(expected.getRevision().getValue(), actual.getRevision().getValue());
    assertEquals(expected.getGeo().getLatitude(), actual.getGeo().getLatitude());
    assertEquals(expected.getGeo().getLongitude(), actual.getGeo().getLongitude());
    assertEquals(expected.getTimezone().getOffset(), actual.getTimezone().getOffset());
    // text value is not written
    assertNull(actual.getTimezone().getText());
    {
        StructuredName e = expected.getStructuredName();
        StructuredName a = actual.getStructuredName();
        assertEquals(e.getFamily(), a.getFamily());
        assertEquals(e.getGiven(), a.getGiven());
        assertEquals(e.getAdditionalNames(), a.getAdditionalNames());
        assertEquals(e.getPrefixes(), a.getPrefixes());
        assertEquals(e.getSuffixes(), a.getSuffixes());
        assertTrue(a.getSortAs().isEmpty());
    }
    assertEquals(expected.getTitles().size(), actual.getTitles().size());
    for (int i = 0; i < expected.getTitles().size(); i++) {
        Title e = expected.getTitles().get(i);
        Title a = actual.getTitles().get(i);
        assertEquals(e.getValue(), a.getValue());
    }
    assertEquals(expected.getRoles().size(), actual.getRoles().size());
    for (int i = 0; i < expected.getRoles().size(); i++) {
        Role e = expected.getRoles().get(i);
        Role a = actual.getRoles().get(i);
        assertEquals(e.getValue(), a.getValue());
    }
    assertEquals(expected.getNotes().size(), actual.getNotes().size());
    for (int i = 0; i < expected.getNotes().size(); i++) {
        Note e = expected.getNotes().get(i);
        Note a = actual.getNotes().get(i);
        assertEquals(e.getValue(), a.getValue());
    }
    assertEquals(expected.getUrls().size(), actual.getUrls().size());
    for (int i = 0; i < expected.getUrls().size(); i++) {
        Url e = expected.getUrls().get(i);
        Url a = actual.getUrls().get(i);
        assertEquals(e.getValue(), a.getValue());
    }
    assertEquals(expected.getImpps().size(), actual.getImpps().size());
    for (int i = 0; i < expected.getImpps().size(); i++) {
        Impp e = expected.getImpps().get(i);
        Impp a = actual.getImpps().get(i);
        assertEquals(e.getUri(), a.getUri());
    }
    assertEquals(expected.getEmails().size(), actual.getEmails().size());
    for (int i = 0; i < expected.getEmails().size(); i++) {
        Email e = expected.getEmails().get(i);
        Email a = actual.getEmails().get(i);
        assertEquals(e.getValue(), a.getValue());
        assertEquals(e.getTypes(), a.getTypes());
    }
    assertEquals(expected.getTelephoneNumbers().size(), actual.getTelephoneNumbers().size());
    for (int i = 0; i < expected.getTelephoneNumbers().size(); i++) {
        Telephone e = expected.getTelephoneNumbers().get(i);
        Telephone a = actual.getTelephoneNumbers().get(i);
        if (e.getText() != null) {
            assertEquals(e.getText(), a.getText());
        } else {
            TelUri uri = e.getUri();
            if (uri.getExtension() == null) {
                assertEquals(e.getUri().getNumber(), a.getText());
            } else {
                assertEquals(e.getUri().getNumber() + " x" + uri.getExtension(), a.getText());
            }
        }
        assertEquals(e.getTypes(), a.getTypes());
    }
    assertEquals(expected.getAddresses().size(), actual.getAddresses().size());
    for (int i = 0; i < expected.getAddresses().size(); i++) {
        Address e = expected.getAddresses().get(i);
        Address a = actual.getAddresses().get(i);
        assertEquals(e.getPoBox(), a.getPoBox());
        assertEquals(e.getExtendedAddress(), a.getExtendedAddress());
        assertEquals(e.getStreetAddress(), a.getStreetAddress());
        assertEquals(e.getLocality(), a.getLocality());
        assertEquals(e.getRegion(), a.getRegion());
        assertEquals(e.getPostalCode(), a.getPostalCode());
        assertEquals(e.getCountry(), a.getCountry());
        assertEquals(e.getLabel(), a.getLabel());
        assertEquals(e.getTypes(), a.getTypes());
    }
    assertEquals(expected.getPhotos().size(), actual.getPhotos().size());
    for (int i = 0; i < expected.getPhotos().size(); i++) {
        Photo e = expected.getPhotos().get(i);
        Photo a = actual.getPhotos().get(i);
        assertEquals(e.getContentType(), a.getContentType());
        assertArrayEquals(e.getData(), a.getData());
    }
    assertEquals(expected.getSounds().size(), actual.getSounds().size());
    for (int i = 0; i < expected.getSounds().size(); i++) {
        Sound e = expected.getSounds().get(i);
        Sound a = actual.getSounds().get(i);
        assertEquals(e.getContentType(), a.getContentType());
        assertArrayEquals(e.getData(), a.getData());
    }
    assertEquals("ez-vcard " + Ezvcard.VERSION, actual.getProductId().getValue());
}
Also used : Email(ezvcard.property.Email) Telephone(ezvcard.property.Telephone) Address(ezvcard.property.Address) Impp(ezvcard.property.Impp) FileWriter(java.io.FileWriter) Title(ezvcard.property.Title) TelUri(ezvcard.util.TelUri) Photo(ezvcard.property.Photo) Sound(ezvcard.property.Sound) SortString(ezvcard.property.SortString) Url(ezvcard.property.Url) Role(ezvcard.property.Role) Note(ezvcard.property.Note) VCard(ezvcard.VCard) File(java.io.File) StructuredName(ezvcard.property.StructuredName) Test(org.junit.Test)

Aggregations

Sound (ezvcard.property.Sound)4 VCard (ezvcard.VCard)3 Address (ezvcard.property.Address)3 Photo (ezvcard.property.Photo)3 StructuredName (ezvcard.property.StructuredName)3 Telephone (ezvcard.property.Telephone)2 Timezone (ezvcard.property.Timezone)2 TelUri (ezvcard.util.TelUri)2 UtcOffset (ezvcard.util.UtcOffset)2 File (java.io.File)2 CannotParseException (ezvcard.io.CannotParseException)1 HCardElement (ezvcard.io.html.HCardElement)1 SoundType (ezvcard.parameter.SoundType)1 Birthday (ezvcard.property.Birthday)1 Email (ezvcard.property.Email)1 Impp (ezvcard.property.Impp)1 Note (ezvcard.property.Note)1 Role (ezvcard.property.Role)1 SortString (ezvcard.property.SortString)1 Title (ezvcard.property.Title)1