Skip to content

Commit

Permalink
Merge pull request #33 from JavaLuigi/master
Browse files Browse the repository at this point in the history
Some small changes/additions
  • Loading branch information
berkholz authored Jul 26, 2021
2 parents a409fd0 + 68d503c commit f36253f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
18 changes: 6 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<dependency>
<groupId>com.googlecode.ez-vcard</groupId>
<artifactId>ez-vcard</artifactId>
<version>0.8.5</version>
<version>0.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<version>3.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
Expand Down Expand Up @@ -72,17 +72,11 @@
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>

<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.2</version>
</dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/org/berkholz/vcard2fritzXML/CommandOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.VCardVersion;
import ezvcard.parameters.EmailTypeParameter;
import ezvcard.parameters.TelephoneTypeParameter;
import ezvcard.types.FormattedNameType;
import ezvcard.types.StructuredNameType;
import ezvcard.parameter.EmailType;
import ezvcard.parameter.TelephoneType;
import ezvcard.property.FormattedName;
import ezvcard.property.StructuredName;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -50,6 +50,7 @@ public class CommandOptions {
String[] args;
boolean skipEmptyContacts;
boolean reversedOrder;
boolean useUTF8Reader;
private String filetype;

/**
Expand All @@ -66,6 +67,7 @@ public CommandOptions(String[] args) throws ParseException {
this.phonebookName = "Privat";
this.reversedOrder = false;
this.skipEmptyContacts = false;
this.useUTF8Reader = false;
this.options = new Options();
this.args = args;
this.filetype = "vcf";
Expand All @@ -79,8 +81,8 @@ public void setOptions() {
// options definitions
this.options.addOption("h", "help", false, "Show help.");
this.options.addOption("d", "directory", true,
"Directory to search for vcards/CSVs. Every contact is given in a single vcard/CSV file.");
this.options.addOption("f", "file", true, "Read all contacts from Vcard/CSV file or from stdin.");
"Directory to search for vCards/CSVs. Every contact is given in a single vCard/CSV file.");
this.options.addOption("f", "file", true, "Read all contacts from vCard/CSV file or from stdin.");
this.options.addOption("o", "outfile", true, "Save XML output to file.");
this.options.addOption("t", "filetype", true, "Specify the file format. Possible values are: csv, vcf. Default: vcf.");
this.options.addOption("n", "phonebookname", true, "Rename phonebook to given name.");
Expand All @@ -89,6 +91,7 @@ public void setOptions() {
"Reverse the default order of the fullname. Default order: <surname> <name>.");

this.options.addOption("s", "skip-empty-contacts", false, "Skip contacts with no mail address and no telephone numbers.");
this.options.addOption("u", "use-utf8-reader ", false, "Set the vCard reader to UTF-8 character set");
this.options.addOption("v", "verbose", false, "Be verbose. [NOT YET IMPLEMENTED.]");

// instantiate parser with our options
Expand Down Expand Up @@ -182,6 +185,10 @@ public void checkOptions() {
if (cmd.hasOption("r")) {
reversedOrder = true;
}

if (cmd.hasOption("u")) {
useUTF8Reader = true;
}
}

private void generateTemplateFile() {
Expand Down Expand Up @@ -209,13 +216,13 @@ private static String generateCSVTemplate() {
private static String generateVcardTemplate() {
VCard vcardExample = new VCard();

vcardExample.addFormattedName(new FormattedNameType("John Doe"));
vcardExample.addEmail("john.doe@example.com", EmailTypeParameter.HOME);
vcardExample.addTelephoneNumber("0821/37097123", TelephoneTypeParameter.HOME);
vcardExample.addTelephoneNumber("0821/37097122", TelephoneTypeParameter.WORK);
vcardExample.addTelephoneNumber("0171/37097121", TelephoneTypeParameter.CELL);
vcardExample.addTelephoneNumber("030/55512345", TelephoneTypeParameter.FAX);
StructuredNameType sn = new StructuredNameType();
vcardExample.addFormattedName(new FormattedName("John Doe"));
vcardExample.addEmail("john.doe@example.com", EmailType.HOME);
vcardExample.addTelephoneNumber("0821/37097123", TelephoneType.HOME);
vcardExample.addTelephoneNumber("0821/37097122", TelephoneType.WORK);
vcardExample.addTelephoneNumber("0171/37097121", TelephoneType.CELL);
vcardExample.addTelephoneNumber("030/55512345", TelephoneType.FAX);
StructuredName sn = new StructuredName();
sn.setGiven("John");
sn.setFamily("Doe");
vcardExample.setStructuredName(sn);
Expand Down
43 changes: 30 additions & 13 deletions src/main/java/org/berkholz/vcard2fritzXML/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
import org.apache.commons.csv.CSVRecord;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.types.EmailType;
import ezvcard.types.TelephoneType;
import ezvcard.parameter.EmailType;
import ezvcard.property.Telephone;
import ezvcard.util.Utf8Reader;
import java.io.FilenameFilter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

/**
* @author Marcel Berkholz
Expand All @@ -50,7 +53,7 @@ public class Main {
* VARIABLES
*/
List<VCard> vcard;

List<CSVRecord> csv;

// Options object including file and directory name
Expand Down Expand Up @@ -151,7 +154,10 @@ protected void readInVCards() throws IOException {

File file = new File(this.cmdOptions.inFile);
// get all vcard entries from file
this.vcard = Ezvcard.parse(file).all();
if(this.cmdOptions.useUTF8Reader)
this.vcard = Ezvcard.parse(new Utf8Reader(file)).all();
else
this.vcard = Ezvcard.parse(file).all();
} catch (IOException e) {
System.out.println("Error while opening file: " + this.cmdOptions.inFile + " StackTrace:\n" + Arrays.toString(e.getStackTrace()));
}
Expand All @@ -160,12 +166,16 @@ protected void readInVCards() throws IOException {
File dir = new File(this.cmdOptions.inDirectory);
this.vcard = new ArrayList<>();
if (dir.exists() && dir.isDirectory()) {
ArrayList<File> files = new ArrayList();
// ArrayList<File> files = new ArrayList();

for (File f : dir.listFiles(Main.getFileNameFilter("vcf"))) {
if (f.isFile()) {
// files.add(f);
List<VCard> tmpVcard = Ezvcard.parse(f).all();
List<VCard> tmpVcard;
if(this.cmdOptions.useUTF8Reader)
tmpVcard = Ezvcard.parse(new Utf8Reader(f)).all();
else
tmpVcard = Ezvcard.parse(f).all();
this.vcard.addAll(tmpVcard);
}
}
Expand All @@ -174,7 +184,11 @@ protected void readInVCards() throws IOException {
} else {
// we get our vcard infos over stdin
try {
InputStreamReader inStreamReader = new InputStreamReader(System.in);
InputStreamReader inStreamReader;
if(this.cmdOptions.useUTF8Reader)
inStreamReader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
else
inStreamReader = new InputStreamReader(System.in);
// get all vcard entries from stdin
this.vcard = Ezvcard.parse(inStreamReader).all();
} catch (IOException ioe) {
Expand Down Expand Up @@ -263,7 +277,10 @@ protected void createContactsFromVCards() {
// skip contact with no mail address and telephone numbers if
// command line option -s is given
if (cmdOptions.skipEmptyContacts && tp.isEmpty() && c1.getServices().getEmail().isEmpty()) {
System.out.println("Skipping: " + vcardElement.getFormattedName().getValue());
if (!Objects.isNull(vcardElement.getFormattedName()))
System.out.println("Skipping: " + vcardElement.getFormattedName().getValue());
if (!Objects.isNull(vcardElement.getOrganization()))
System.out.println("Skipping: " + vcardElement.getOrganization());
continue;
}

Expand All @@ -286,7 +303,7 @@ protected void createContactsFromVCards() {
? "" : vcardElement.getFormattedName().getValue();
}
}

// check if strictured name F is not null and not empty
if (vcardElement.getStructuredName() != null) {
// Family name has value, but given name is null
Expand All @@ -301,7 +318,7 @@ protected void createContactsFromVCards() {
// family and givenname is not null, but maybe empty
}
}

// now we have first the organisation, then the formatted name and last the structured name
// if all of them are empty we take "" as family and given name
p.setRealName(given, family, cmdOptions.reversedOrder);
Expand Down Expand Up @@ -374,7 +391,7 @@ public static String getEmailAddress(List<EmailType> emailList, String mailType)
// System.out.println(mail.getTypes());
// TODO: Vergleich geht auf die Adresse des Objektes und nicht auf
// den String
if (mail.getTypes().toString() == "[TYPE=" + mailType) {
if (mail.getValue() == null ? "[TYPE=" + mailType == null : mail.getValue().equals("[TYPE=" + mailType)) {
System.out.println(mail.getValue());
// return mail.getValue();
}
Expand All @@ -389,9 +406,9 @@ public static String getEmailAddress(List<EmailType> emailList, String mailType)
* @param telephoneType
* @return The first telephone number found in the telephoneList.
*/
public static String getTelephoneNumberByType(List<TelephoneType> telephoneList, String telephoneType) {
public static String getTelephoneNumberByType(List<Telephone> telephoneList, String telephoneType) {
// TODO: difference between work fax and home fax is not recognized
for (TelephoneType tel : telephoneList) {
for (Telephone tel : telephoneList) {
if (tel.getTypes().toString().contains(telephoneType)) {
return tel.getText();
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/berkholz/vcard2fritzXML/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public void setRealName(String realName) {
}

/**
* Sets the realname of the Person object in the specified name order.
* Default name order is "givenname name". Reversed order is "name
* givenname". Names will be trimmed (spaces are deleted at beginning and
* end).
* Sets the realname of the Person object in the specified name order.Default name order is "givenname name".
* Reversed order is "name givenname". Names will be trimmed (spaces are deleted at beginning and end).
*
* @param givenName The given name of the person.
* @param familyName The family name of the person.
Expand All @@ -71,7 +69,10 @@ public void setRealName(String givenName, String familyName, boolean reversedNam
if (reversedNameOrder) {
givenName = checkNameNull(givenName);
if (!givenName.isEmpty()) {
realName = (checkNameNull(familyName) + ", " + givenName).trim();
if(!familyName.isEmpty())
realName = (checkNameNull(familyName) + ", " + givenName).trim();
else
realName = givenName.trim();
} else {
realName = checkNameNull(familyName).trim();
}
Expand Down

0 comments on commit f36253f

Please sign in to comment.