package org.argeo.naming;
-import static org.argeo.osgi.useradmin.LdifName.dn;
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
/** Basic LDIF parser. */
public class LdifParser {
private final static Log log = LogFactory.getLog(LdifParser.class);
+ private final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
protected Attributes addAttributes(SortedMap<LdapName, Attributes> res, int lineNumber, LdapName currentDn,
Attributes currentAttributes) {
}
}
+ /** With UTF-8 charset */
public SortedMap<LdapName, Attributes> read(InputStream in) throws IOException {
+ try (Reader reader = new InputStreamReader(in, DEFAULT_CHARSET)) {
+ return read(reader);
+ } finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ if (log.isTraceEnabled())
+ log.error("Cannot close stream", e);
+ }
+ }
+ }
+
+ /** Will close the reader. */
+ public SortedMap<LdapName, Attributes> read(Reader reader) throws IOException {
SortedMap<LdapName, Attributes> res = new TreeMap<LdapName, Attributes>();
try {
List<String> lines = new ArrayList<>();
- try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
+ try (BufferedReader br = new BufferedReader(reader)) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
String attributeId = attrId.toString();
+ // TODO should we really trim the end of the string as well?
String cleanValueStr = currentEntry.toString().trim();
Object attributeValue = isBase64 ? Base64.getDecoder().decode(cleanValueStr) : cleanValueStr;
// manage DN attributes
- if (attributeId.equals(dn.name()) || isLastLine) {
+ if (attributeId.equals(LdapAttrs.DN) || isLastLine) {
if (currentDn != null) {
//
// ADD
}
}
- if (attributeId.equals(dn.name()))
+ if (attributeId.equals(LdapAttrs.DN))
try {
currentDn = new LdapName(attributeValue.toString());
currentAttributes = new BasicAttributes(true);
currentEntry.append(line);
}
} finally {
- in.close();
+ try {
+ reader.close();
+ } catch (IOException e) {
+ if (log.isTraceEnabled())
+ log.error("Cannot close stream", e);
+ }
}
return res;
}