25 using namespace KLDAP;
27 class Ldif::LdifPrivate
31 bool mDelOldRdn, mUrl;
33 QString mAttr, mNewRdn, mNewSuperior, mOid;
34 QByteArray mLdif, mValue;
37 bool mIsNewLine, mIsComment, mCritical;
38 ParseValue mLastParseValue;
39 uint mPos, mLineNumber;
43 Ldif::Ldif() : d( new LdifPrivate )
48 Ldif::Ldif(
const Ldif &that ) : d( new LdifPrivate )
55 Ldif &Ldif::operator=(
const Ldif &that )
57 if (
this == &that ) {
72 const QByteArray &value,
73 uint linelen,
bool url )
80 result = fieldname.toUtf8() +
":< " +
value;
82 isDn = fieldname.toLower() == QLatin1String(
"dn");
84 if ( value.size() > 0 && value[0] > 0 && value[0] !=
'\n' &&
85 value[0] !=
'\r' && value[0] !=
':' && value[0] !=
'<' ) safe =
true;
89 for (
int i=1; i < value.size(); i++ ) {
91 if ( ( isDn && value[i] == 0 ) ||
92 ( !isDn && value[i] <= 0 ) ||
93 value[i] ==
'\r' || value[i] ==
'\n' ) {
100 if ( value.size() == 0 ) {
105 result = fieldname.toUtf8() +
": " +
value;
107 result = fieldname.toUtf8() +
":: " + value.toBase64();
111 int i = (uint)( fieldname.length() + 2 ) > linelen ? fieldname.length() + 2 : linelen;
112 while ( i < result.length() ) {
113 result.insert( i,
"\n " );
122 uint linelen,
bool url )
124 return assembleLine( fieldname, value.toUtf8(), linelen, url );
135 position = line.indexOf(
":" );
136 if ( position == -1 ) {
138 fieldname = QLatin1String(
"");
139 value = line.trimmed();
144 linelen = line.size();
145 fieldname = QString::fromUtf8( line.left( position ).trimmed() );
147 if ( linelen > ( position + 1 ) && line[ position + 1 ] ==
':' ) {
149 if ( linelen <= ( position + 3 ) ) {
153 value = QByteArray::fromBase64( line.mid( position + 3 ) );
157 if ( linelen > ( position + 1 ) && line[ position + 1 ] ==
'<' ) {
159 if ( linelen <= ( position + 3 ) ) {
163 value = QByteArray::fromBase64( line.mid( position + 3 ) );
167 if ( linelen <= ( position + 2 ) ) {
171 value = line.mid( position + 2 );
180 bool url =
splitLine( line, tmp, value );
182 kDebug() <<
"value:" << QString::fromUtf8( value, value.size() );
183 if ( tmp.isEmpty() ) {
184 tmp = QString::fromUtf8( value, value.size() );
187 if ( tmp.endsWith( QLatin1String(
"true" ) ) ) {
189 tmp.truncate( tmp.length() - 5 );
190 }
else if ( tmp.endsWith( QLatin1String(
"false" ) ) ) {
192 tmp.truncate( tmp.length() - 6 );
201 if ( d->mIsComment ) {
205 ParseValue retval = None;
206 if ( d->mLastParseValue == EndEntry ) {
207 d->mEntryType = Entry_None;
210 d->mUrl =
splitLine( d->mLine, d->mAttr, d->mValue );
212 QString attrLower = d->mAttr.toLower();
214 switch ( d->mEntryType ) {
216 if ( attrLower == QLatin1String(
"version" ) ) {
217 if ( !d->mDn.isEmpty() ) {
220 }
else if ( attrLower == QLatin1String(
"dn" ) ) {
221 kDebug() <<
"ldapentry dn:" << QString::fromUtf8( d->mValue, d->mValue.size() );
222 d->mDn = LdapDN( QString::fromUtf8( d->mValue, d->mValue.size() ) );
223 d->mModType = Mod_None;
225 }
else if ( attrLower == QLatin1String(
"changetype" ) ) {
226 if ( d->mDn.isEmpty() ) {
229 QString tmpval = QString::fromUtf8( d->mValue, d->mValue.size() );
230 kDebug() <<
"changetype:" << tmpval;
231 if ( tmpval == QLatin1String(
"add" ) ) {
232 d->mEntryType = Entry_Add;
233 }
else if ( tmpval == QLatin1String(
"delete" ) ) {
234 d->mEntryType = Entry_Del;
235 }
else if ( tmpval == QLatin1String(
"modrdn" ) || tmpval == QLatin1String(
"moddn" ) ) {
237 d->mNewSuperior.clear();
238 d->mDelOldRdn =
true;
239 d->mEntryType = Entry_Modrdn;
240 }
else if ( tmpval == QLatin1String(
"modify" ) ) {
241 d->mEntryType = Entry_Mod;
246 }
else if ( attrLower == QLatin1String(
"control" ) ) {
247 d->mUrl =
splitControl( d->mValue, d->mOid, d->mCritical, d->mValue );
249 }
else if ( !d->mAttr.isEmpty() && d->mValue.size() > 0 ) {
250 d->mEntryType = Entry_Add;
255 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
262 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
269 if ( d->mModType == Mod_None ) {
270 kDebug() <<
"new modtype" << d->mAttr;
271 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
273 }
else if ( attrLower == QLatin1String(
"add" ) ) {
274 d->mModType = Mod_Add;
275 }
else if ( attrLower == QLatin1String(
"replace" ) ) {
276 d->mModType = Mod_Replace;
277 d->mAttr = QString::fromUtf8( d->mValue, d->mValue.size() );
278 d->mValue = QByteArray();
280 }
else if ( attrLower == QLatin1String(
"delete" ) ) {
281 d->mModType = Mod_Del;
282 d->mAttr = QString::fromUtf8( d->mValue, d->mValue.size() );
283 d->mValue = QByteArray();
289 if ( d->mAttr.isEmpty() ) {
290 if ( QString::fromUtf8( d->mValue, d->mValue.size() ) == QLatin1String(
"-") ) {
291 d->mModType = Mod_None;
292 }
else if ( d->mValue.size() == 0 ) {
303 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
305 }
else if ( attrLower == QLatin1String(
"newrdn" ) ) {
306 d->mNewRdn = QString::fromUtf8( d->mValue, d->mValue.size() );
307 }
else if ( attrLower == QLatin1String(
"newsuperior" ) ) {
308 d->mNewSuperior = QString::fromUtf8( d->mValue, d->mValue.size() );
309 }
else if ( attrLower == QLatin1String(
"deleteoldrdn" ) ) {
310 if ( d->mValue.size() > 0 && d->mValue[0] ==
'0' ) {
311 d->mDelOldRdn =
false;
312 }
else if ( d->mValue.size() > 0 && d->mValue[0] ==
'1' ) {
313 d->mDelOldRdn =
true;
327 ParseValue retval = None;
330 while ( retval == None ) {
331 if ( d->mPos < (uint)d->mLdif.size() ) {
332 c = d->mLdif[d->mPos];
334 if ( d->mIsNewLine && c ==
'\r' ) {
337 if ( d->mIsNewLine && ( c ==
' ' || c ==
'\t' ) ) {
338 d->mIsNewLine =
false;
341 if ( d->mIsNewLine ) {
342 d->mIsNewLine =
false;
344 d->mLastParseValue = retval;
345 d->mLine.resize( 0 );
346 d->mIsComment = ( c ==
'#' );
348 if ( c ==
'\n' || c ==
'\r' ) {
350 d->mIsNewLine =
true;
358 if ( !d->mIsComment ) {
367 QByteArray tmp( 3,
'\n' );
374 d->mPos = d->mLineNumber = 0;
375 d->mDelOldRdn =
false;
376 d->mEntryType = Entry_None;
377 d->mModType = Mod_None;
380 d->mNewSuperior.clear();
381 d->mLine = QByteArray();
382 d->mIsNewLine =
false;
383 d->mIsComment =
false;
384 d->mLastParseValue = None;
395 return d->mEntryType;
415 return d->mNewSuperior;
420 return d->mDelOldRdn;
450 return d->mLineNumber;
QString oid() const
Returns the OID when modType() returned Control.
uint lineNumber() const
Returns the line number which the parser processes.
QString newRdn() const
Returns the new Relative Distinguished Name if modType() returned Entry_Modrdn.
bool isUrl() const
Returns if val() is an url.
void endLdif()
Indicates the end of the Ldif file/stream.
ParseValue processLine()
Process one Ldif line.
static QByteArray assembleLine(const QString &fieldname, const QByteArray &value, uint linelen=0, bool url=false)
Assembles fieldname and value into a valid Ldif line, BASE64 encodes the value if necessary and optio...
QByteArray value() const
Returns the attribute value.
void startParsing()
Starts the parsing of a new Ldif.
int modType() const
Returns the LDAP modify request type if entryType() returned Entry_Mod.
QString attr() const
Returns the attribute name.
static bool splitLine(const QByteArray &line, QString &fieldname, QByteArray &value)
Splits one line from an Ldif file to attribute and value components.
LdapDN dn() const
Returns the Distinguished Name of the current entry.
void setLdif(const QByteArray &ldif)
Sets a chunk of Ldif.
bool delOldRdn() const
Returns if the delete of the old RDN is required.
ParseValue nextItem()
Process the Ldif until a complete item can be returned.
QString newSuperior() const
Returns the new parent of the entry if modType() returned Entry_Modrdn.
EntryType entryType() const
Returns the requested LDAP operation extracted from the current entry.
static bool splitControl(const QByteArray &line, QString &oid, bool &critical, QByteArray &value)
Splits a control specification (without the "control:" directive)
bool isCritical() const
Returns the criticality level when modType() returned Control.