在我的IT公司实习期间,我被分配了编写脚本或java程序的任务,将CSV文件转换为LDIF文件格式,我相信很多组织用来填充/修改/删除他们的目录很多用户我正在尝试编写一个java程序,它可以帮助我将CSV文件转换为LDIF文件。然后将此LDIF文件导入到电子目录中以添加新用户。目前,我有一个简单的程序,但需要进行重大改进,但我无法提供。
示例CSV文件的屏幕截图(第一行是标题):
csv file sample
Notepad ++中的CSV文件示例:
csv file in Notepad++
(第一行是标题。一行中的每个元素用逗号(,)分隔)
使用下面给出的示例代码生成的样本LDIF文件(OUTPUT):
dn: cn=demotest1, ou=Data, o=Data
changetype: add
ou: Data
objectClass: dt1
objectClass: test_demo1
objectClass: demotest1
objectClass: Employee
cn: demotest1
uid: test_demo1
SAMAccountName: demt1
givenName: demotest1
sn: dt1
dn: cn=demotest2, ou=Data, o=Data
changetype: add
ou: Data
objectClass: dt2
objectClass: test_demo2
objectClass: demotest2
objectClass: Employee
cn: demotest2
uid: test_demo2
SAMAccountName: demt2
givenName: demotest2
sn: dt2
注意:第一行作为标题从输出中排除。 CSV文件中的每一行都转换为一组数据(lines = dn:to sn :),每组数据用空行分隔。
以下是我用于生成上述LDIF文件的代码示例:
package readcsv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
*
* @author Dorjee
*/
public class ReadCSV {
public static void main(String[] args) {
ReadCSV obj = new ReadCSV();
obj.run();
}
public void run() {
String csvFile = "/Users/Dorjee/Desktop/sampleCSV.csv"; //Path of file to be read.
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
String[] column;
int count = 0;
try {
PrintStream out = new PrintStream(new FileOutputStream("OutputLDIFFile.ldif"));
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// using comma as separator
column = line.split(csvSplitBy);
//End format of the ouput file.
//Change according to .CSV file.
//Count used to exclude the reading of the first line.
if (count > 0) {
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ "\nou: " + column[7]
+ "\nobjectClass: " + column[3]
+ "\nobjectClass: " + column[4]
+ "\nobjectClass: " + column[5]
+ "\nobjectClass: " + column[6]
+ "\ncn: " + column[5]
+ "\nuid: "+column[4]
+ "\nSAMAccountName: "+column[1]
+ "\ngivenName: "+column[0]
+ "\nsn: "+column[3]
+ "\n"
);
}
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
这是一个简单的代码,可帮助从CSV文件生成LDIF文件。这很有用,因为这里生成的数据集可以跨越数千行。但是这段代码显然不够用。正如您所看到的,每当需要将具有不同列数的不同CSV文件转换为LDIF文件时,我将不得不在上面突出显示的区域更改代码。它占用了大量的时间,因为我遇到了超过50列的文件,并且在我手动更改代码时增加了出错的可能性。最重要的是,一些列具有空值(需要从输出中排除)。
•如何排除输出中的空值?
•即使CSV文件具有不同的列数,是否有办法自动生成输出?我尝试过ArrayList,但我找不到解决问题的方法。
答案 0 :(得分:1)
这样的事情应该跳过空列:
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ ((column[7].length()>0)?"\nou: " + column[7]:"")
+ ((column[3].length()>0)?"\nobjectClass: " + column[3]:"")
+ ((column[4].length()>0)?"\nobjectClass: " + column[4]:"")
+ ((column[5].length()>0)?"\nobjectClass: " + column[5]:"")
+ ((column[6].length()>0)?"\nobjectClass: " + column[6]:"")
+ ((column[5].length()>0)?"\ncn: " + column[5]:"")
+ ((column[4].length()>0)?"\nuid: "+column[4]:"")
+ ((column[1].length()>0)?"\nSAMAccountName: "+column[1]:"")
+ ((column[0].length()>0)?"\ngivenName: "+column[0]:"")
+ ((column[3].length()>0)?"\nsn: "+column[3]:"")
+ "\n"
);
如果您还需要检查列数:
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ ((column.length > 7 && column[7].length()>0)?"\nou: " + column[7]:"")
+ ((column.length > 3 && column[3].length()>0)?"\nobjectClass: " + column[3]:"")
+ ((column.length > 4 && column[4].length()>0)?"\nobjectClass: " + column[4]:"")
+ ((column.length > 5 && column[5].length()>0)?"\nobjectClass: " + column[5]:"")
+ ((column.length > 6 && column[6].length()>0)?"\nobjectClass: " + column[6]:"")
+ ((column.length > 5 && column[5].length()>0)?"\ncn: " + column[5]:"")
+ ((column.length > 4 && column[4].length()>0)?"\nuid: "+column[4]:"")
+ ((column.length > 1 && column[1].length()>0)?"\nSAMAccountName: "+column[1]:"")
+ ((column.length > 0 && column[0].length()>0)?"\ngivenName: "+column[0]:"")
+ ((column.length > 3 && column[3].length()>0)?"\nsn: "+column[3]:"")
+ "\n"
);
看起来有点难看,使用辅助函数可能会更容易。
static public String check(String column[],String line,int index) {
return ((column.length > index && column[index].length()>0)?line + column[index]:"");
}
...
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ check(column,"\nou: ",7)
+ check(column,"\nobjectClass: " ,3)
+ check(column,"\nobjectClass: ",4)
+ check(column,"\nobjectClass: ",5)
+ check(column,"\nobjectClass: ",6)
+ check(column,"\ncn: ",5)
+ check(column,"\nuid: ",4)
+ check(column,"\nSAMAccountName: ",1)
+ check(column,"\ngivenName: ",0)
+ check(column,"\nsn: ",3)
+ "\n"
);
答案 1 :(得分:1)
我将对之前/接受的答案发表评论,但我没有声誉能够发表评论。相反,我会添加一个非回答的回复,希望能帮助任何人在未来看到这个......
在处理可能出现的任何可能数据时,提供的解决方案似乎不完整。如果你看RFC2849,你会找到像(第5页)这样的建议:
4) ... Any value that contains characters other than those defined as "SAFE-CHAR", or begins with a character other than those defined as "SAFE-INIT-CHAR", above, MUST be base-64 encoded. Other values MAY be base-64 encoded.
其中SAFE-INIT-CHAR定义为
SAFE-INIT-CHAR = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B / %x3D-7F ; any value <= 127 except NUL, LF, CR, ; SPACE, colon (":", ASCII 58 decimal) ; and less-than ("<" , ASCII 60 decimal)
和
8) Values or distinguished names that end with SPACE SHOULD be base-64 encoded.
如果您需要更完整的解决方案,则应更新已接受的解决方案以正确处理编码。