📄 ldifutility.java
字号:
currentAtt = (Attribute) allAtts.next();
boolean binary = false;
if (currentAtt instanceof DXAttribute)
binary = !((DXAttribute) currentAtt).isString();
attName = currentAtt.getID();
/*
* Make sure we don't print out 'dn' or objectclass attributes twice
*/
if ((attName.equals("dn") == false) && (attName.equals(oc.getID()) == false))
{
NamingEnumeration values = currentAtt.getAll();
while (values.hasMore())
{
Object value = values.next();
if (value != null)
{
//BY THE TIME IT GETS HERE THE UTF-8 IS HISTORY...
if (debug)
{
System.out.println("value class = " + value.getClass().toString() + " : " + value);
System.out.println(attName + ": " + value.toString());
}
else
{
if (binary)
saveFile.write(attName + ldifEncode(value, attName.length(), true) + "\n");
else
saveFile.write(attName + ldifEncode(value, attName.length()) + "\n");
}
}
}
}
}
if (!debug)
{
saveFile.write("\n");
saveFile.flush();
}
}
/**
* Parse an attribute: value line of an ldif file, and place
* the attribute value pair in an Attributes object.
*
* @param parseableLine a complete ldif text line (unwrapped) to parse
* @param newEntry the partially created entry, which is modified by this
* method.
*/
public void ldifDecode(String parseableLine, DXEntry newEntry)
{
boolean isBinary = false;
int breakpos = parseableLine.indexOf(':');
if (breakpos < 0)
{
log.warning("Error - illegal line in ldif file\n" + parseableLine);
return;
}
String attribute = parseableLine.substring(0, breakpos);
Object value = null;
int attLen = attribute.length();
// auto-translate 'oc' to 'objectClass'
if (attribute.equals("oc")) attribute = "objectClass";
int startpos = 2;
if (parseableLine.length() <= breakpos + 1) // empty value
{
value = "";
}
else if (parseableLine.charAt(breakpos + 1) == ':') // check for base64 encoded isNonString
{
value = getBase64Value(parseableLine, attLen, startpos, attribute); // may return string or byte array!
if (value instanceof String == false)
isBinary = true;
}
else
{
if (parseableLine.charAt(attLen + 1) != ' ') // again, may be a leading space, or may not...
startpos = 1;
value = parseableLine.substring(attLen + startpos);
// expand the value parameters, including the urls
value = expandValueParams(value);
}
if ("dn".equalsIgnoreCase(attribute))
{
if (value instanceof String)
{
DN dn = new DN((String) value);
if (dn.error())
log.warning("Error trying to initialise ldif DN: \n" + dn.getError());
else
newEntry.putDN(dn);
}
else // this code should no longer be triggered, as utf8 conversion is done when data first read...
{
try
{
DN dn = new DN(new String((byte[]) value, "UTF8"));
if (dn.error())
log.log(Level.WARNING, "Error trying to initialise ldif DN: \n", dn.getError());
else
newEntry.putDN(dn);
}
catch (UnsupportedEncodingException e)
{
} // can't happen?: UTF8 is mandatory...
}
}
else if (attribute != null)
{
Attribute existing = newEntry.get(attribute);
if (existing == null)
{
DXAttribute att = new DXAttribute(attribute, value);
att.setString(!isBinary);
newEntry.put(att);
}
else
{
existing.add(value);
newEntry.put(existing);
}
}
}
/**
*
*/
private Object getBase64Value(String parseableLine, int attLen, int startpos, String attribute)
{
byte[] rawBinaryData;
if (parseableLine.charAt(attLen + 2) == ' ') // may be ::XXXX or :: XXXX -> so must adjust for possible space
startpos = 3;
rawBinaryData = CBBase64.stringToBinary(parseableLine.substring(attribute.length() + startpos));
// a bit dodgy - we try to guess whether the isNonString data is UTF-8, or is really isNonString...
// we should probably do some schema checking here, but instead we'll try to make an educated
// guess...
// Create a short array to test for utf-8 ishness... (we don't want to test all of large text files)
byte[] testBytes;
if (rawBinaryData.length > 256)
{
testBytes = new byte[256];
System.arraycopy(rawBinaryData, 0, testBytes, 0, 256);
}
else
testBytes = rawBinaryData;
/*
* Make a (slightly ad-hoc) check to see if it is actually a utf-8 string *pretending* to by bytes...
*/
if (CBParse.isUTF8(testBytes))
{
try
{
return new String(rawBinaryData, "UTF-8");
}
catch (Exception e) // as per String constructor doco, behaviour is 'unspecified' if the above fails...
{
// drop through to return the raw isNonString data instead...
}
}
return rawBinaryData;
}
/**
* Read an entry from LDIF text. Attribute/value pairs are read until
* a blank line is encountered.
*
* @param textReader a buffered Reader to read lines of ldif text from...
* @return the read entry, as a DXAttributes object
* @throws InterruptedIOException if the user hits cancel on the progress bar
*/
public DXEntry readLdifEntry(BufferedReader textReader)
throws IOException
{
DXEntry entry = new DXEntry();
// this is the 'look ahead' current line, read from the ldif file.
String line = "";
// this is the first line of the attribute, read from the ldif file.
String firstLine = "";
/* This is a little tricky. Because lines may be extended by line wrapping,
* we need to look ahead a line until we're sure that we've finished any
* possible wrapping, and only then (when we've already read the 'next' line)
* can we process the old line.
*/
// WARNING - this code is a little messy - trying to make it quick since ldif load is slow :-/.
StringBuffer multiLineText = null; //don't use this unless we need it...
while ((line = textReader.readLine()) != null)
{
if (line.length() > 0 && line.charAt(0) == ' ') // line wrap; normal
{
if (multiLineText == null)
multiLineText = new StringBuffer(firstLine);
multiLineText.append(line.substring(1));
//line = firstLine + line.substring(1); // extend the value...
}
else if (line.length() > 0 && line.charAt(0) == '>') // line wrap; XML (as per draft xml/ldif standard)
{
if (multiLineText == null)
multiLineText = new StringBuffer(firstLine);
multiLineText.append(line.substring(1)).append(cr);
//line = firstLine + line.substring(1) + "\n"; // extend the value...
}
else if (firstLine.length() > 1 && firstLine.charAt(0) == '#')
{
// comment... do nothing.
}
else if (firstLine.length() > 2 || multiLineText != null)
{
if (multiLineText != null)
ldifDecode(multiLineText.toString(), entry);
else
ldifDecode(firstLine, entry);
multiLineText = null;
}
if (line == null || line.equals("")) // end of entry...
{
return entry;
}
firstLine = line;
}
if (entry.getDN().size() > 0) // dn check is for unexpectedly truncated files
{
// unusual - end of file reached, and the file *doesn't* have
// a blank line at the end - hence a special case while we write
// the last entry
if (firstLine != null && firstLine.trim().length() > 0)
ldifDecode(firstLine, entry);
return entry; // should be last entry
}
return null; // finished reading everything...
}
/**
* This method expands the strings inside the ldif file
* that match the list of expandable strings in params list.
*
* @param value value to be expanded
* @return expanded object
*/
public Object expandValueParams(Object value)
{
if (params != null)
{
Enumeration keys = params.keys();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
String keyvalue = (String) params.get(key);
// check for the key
String oldValue = (String) value;
int index = oldValue.indexOf(key);
if (index > -1)
{
String newValue = oldValue.substring(0, index) + keyvalue +
oldValue.substring(index + key.length(), oldValue.length());
System.out.println(newValue);
value = newValue;
}
}
}
// load the file if the value is a url
if (filedir != null)
{
// check if it is a file, i.e. look for "< file:"
String oldValue = (String) value;
String match = "< file://";
int index = (oldValue.toLowerCase()).indexOf(match);
if (index > -1)
{
String filename = filedir + oldValue.substring(index + 9, oldValue.length());
File file = new File(filename);
try
{
FileInputStream input = new FileInputStream(file);
int length = (int) file.length();
if (length > 0)
{
byte[] bytes = new byte[length];
int read = input.read(bytes);
if (read > 0) value = bytes;
}
input.close();
}
catch (IOException e)
{
System.out.println("Error opening the file!" + e);
}
}
}
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -