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] !=
'<' ) {
91 for (
int i=1; i < value.size(); i++ ) {
93 if ( ( isDn && value[i] == 0 ) ||
94 ( !isDn && value[i] <= 0 ) ||
95 value[i] ==
'\r' || value[i] ==
'\n' ) {
102 if ( value.size() == 0 ) {
107 result = fieldname.toUtf8() +
": " + value;
109 result = fieldname.toUtf8() +
":: " + value.toBase64();
113 int i = (uint)( fieldname.length() + 2 ) > linelen ? fieldname.length() + 2 : linelen;
114 while ( i < result.length() ) {
115 result.insert( i,
"\n " );
124 uint linelen,
bool url )
126 return assembleLine( fieldname, value.toUtf8(), linelen, url );
137 position = line.indexOf(
":" );
138 if ( position == -1 ) {
140 fieldname = QLatin1String(
"" );
141 value = line.trimmed();
146 linelen = line.size();
147 fieldname = QString::fromUtf8( line.left( position ).trimmed() );
149 if ( linelen > ( position + 1 ) && line[ position + 1 ] ==
':' ) {
151 if ( linelen <= ( position + 3 ) ) {
155 value = QByteArray::fromBase64( line.mid( position + 3 ) );
159 if ( linelen > ( position + 1 ) && line[ position + 1 ] ==
'<' ) {
161 if ( linelen <= ( position + 3 ) ) {
165 value = QByteArray::fromBase64( line.mid( position + 3 ) );
169 if ( linelen <= ( position + 2 ) ) {
173 value = line.mid( position + 2 );
182 bool url = splitLine( line, tmp, value );
184 kDebug() <<
"value:" << QString::fromUtf8( value, value.size() );
185 if ( tmp.isEmpty() ) {
186 tmp = QString::fromUtf8( value, value.size() );
189 if ( tmp.endsWith( QLatin1String(
"true" ) ) ) {
191 tmp.truncate( tmp.length() - 5 );
192 }
else if ( tmp.endsWith( QLatin1String(
"false" ) ) ) {
194 tmp.truncate( tmp.length() - 6 );
203 if ( d->mIsComment ) {
207 ParseValue retval = None;
208 if ( d->mLastParseValue == EndEntry ) {
209 d->mEntryType = Entry_None;
212 d->mUrl = splitLine( d->mLine, d->mAttr, d->mValue );
214 QString attrLower = d->mAttr.toLower();
216 switch ( d->mEntryType ) {
218 if ( attrLower == QLatin1String(
"version" ) ) {
219 if ( !d->mDn.isEmpty() ) {
222 }
else if ( attrLower == QLatin1String(
"dn" ) ) {
223 kDebug() <<
"ldapentry dn:" << QString::fromUtf8( d->mValue, d->mValue.size() );
224 d->mDn = LdapDN( QString::fromUtf8( d->mValue, d->mValue.size() ) );
225 d->mModType = Mod_None;
227 }
else if ( attrLower == QLatin1String(
"changetype" ) ) {
228 if ( d->mDn.isEmpty() ) {
231 QString tmpval = QString::fromUtf8( d->mValue, d->mValue.size() );
232 kDebug() <<
"changetype:" << tmpval;
233 if ( tmpval == QLatin1String(
"add" ) ) {
234 d->mEntryType = Entry_Add;
235 }
else if ( tmpval == QLatin1String(
"delete" ) ) {
236 d->mEntryType = Entry_Del;
237 }
else if ( tmpval == QLatin1String(
"modrdn" ) || tmpval == QLatin1String(
"moddn" ) ) {
238 d->mNewRdn = QLatin1String(
"" );
239 d->mNewSuperior = QLatin1String(
"" );
240 d->mDelOldRdn =
true;
241 d->mEntryType = Entry_Modrdn;
242 }
else if ( tmpval == QLatin1String(
"modify" ) ) {
243 d->mEntryType = Entry_Mod;
248 }
else if ( attrLower == QLatin1String(
"control" ) ) {
249 d->mUrl = splitControl( d->mValue, d->mOid, d->mCritical, d->mValue );
251 }
else if ( !d->mAttr.isEmpty() && d->mValue.size() > 0 ) {
252 d->mEntryType = Entry_Add;
257 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
264 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
271 if ( d->mModType == Mod_None ) {
272 kDebug() <<
"new modtype" << d->mAttr;
273 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
275 }
else if ( attrLower == QLatin1String(
"add" ) ) {
276 d->mModType = Mod_Add;
277 }
else if ( attrLower == QLatin1String(
"replace" ) ) {
278 d->mModType = Mod_Replace;
279 d->mAttr = QString::fromUtf8( d->mValue, d->mValue.size() );
280 d->mValue = QByteArray();
282 }
else if ( attrLower == QLatin1String(
"delete" ) ) {
283 d->mModType = Mod_Del;
284 d->mAttr = QString::fromUtf8( d->mValue, d->mValue.size() );
285 d->mValue = QByteArray();
291 if ( d->mAttr.isEmpty() ) {
292 if ( QString::fromUtf8( d->mValue, d->mValue.size() ) == QLatin1String(
"-" ) ) {
293 d->mModType = Mod_None;
294 }
else if ( d->mValue.size() == 0 ) {
305 if ( d->mAttr.isEmpty() && d->mValue.size() == 0 ) {
307 }
else if ( attrLower == QLatin1String(
"newrdn" ) ) {
308 d->mNewRdn = QString::fromUtf8( d->mValue, d->mValue.size() );
309 }
else if ( attrLower == QLatin1String(
"newsuperior" ) ) {
310 d->mNewSuperior = QString::fromUtf8( d->mValue, d->mValue.size() );
311 }
else if ( attrLower == QLatin1String(
"deleteoldrdn" ) ) {
312 if ( d->mValue.size() > 0 && d->mValue[0] ==
'0' ) {
313 d->mDelOldRdn =
false;
314 }
else if ( d->mValue.size() > 0 && d->mValue[0] ==
'1' ) {
315 d->mDelOldRdn =
true;
329 ParseValue retval = None;
332 while ( retval == None ) {
333 if ( d->mPos < (uint)d->mLdif.size() ) {
334 c = d->mLdif[d->mPos];
336 if ( d->mIsNewLine && c ==
'\r' ) {
339 if ( d->mIsNewLine && ( c ==
' ' || c ==
'\t' ) ) {
340 d->mIsNewLine =
false;
343 if ( d->mIsNewLine ) {
344 d->mIsNewLine =
false;
345 retval = processLine();
346 d->mLastParseValue = retval;
347 d->mLine.resize( 0 );
348 d->mIsComment = ( c ==
'#' );
350 if ( c ==
'\n' || c ==
'\r' ) {
352 d->mIsNewLine =
true;
360 if ( !d->mIsComment ) {
369 QByteArray tmp( 3,
'\n' );
376 d->mPos = d->mLineNumber = 0;
377 d->mDelOldRdn =
false;
378 d->mEntryType = Entry_None;
379 d->mModType = Mod_None;
382 d->mNewSuperior.clear();
383 d->mLine = QByteArray();
384 d->mIsNewLine =
false;
385 d->mIsComment =
false;
386 d->mLastParseValue = None;
397 return d->mEntryType;
417 return d->mNewSuperior;
422 return d->mDelOldRdn;
452 return d->mLineNumber;
QByteArray value() const
Returns the attribute value.
void endLdif()
Indicates the end of the Ldif file/stream.
uint lineNumber() const
Returns the line number which the parser processes.
bool isCritical() const
Returns the criticality level when modType() returned Control.
ParseValue nextItem()
Process the Ldif until a complete item can be returned.
static bool splitControl(const QByteArray &line, QString &oid, bool &critical, QByteArray &value)
Splits a control specification (without the "control:" directive)
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...
QString newSuperior() const
Returns the new parent of the entry if modType() returned Entry_Modrdn.
void setLdif(const QByteArray &ldif)
Sets a chunk of Ldif.
int modType() const
Returns the LDAP modify request type if entryType() returned Entry_Mod.
QString attr() const
Returns the attribute name.
ParseValue processLine()
Process one Ldif line.
void startParsing()
Starts the parsing of a new Ldif.
bool isUrl() const
Returns if val() is an url.
QString oid() const
Returns the OID when modType() returned Control.
LdapDN dn() const
Returns the Distinguished Name of the current entry.
QString newRdn() const
Returns the new Relative Distinguished Name if modType() returned Entry_Modrdn.
EntryType entryType() const
Returns the requested LDAP operation extracted from the current entry.
bool delOldRdn() const
Returns if the delete of the old RDN is required.
static bool splitLine(const QByteArray &line, QString &fieldname, QByteArray &value)
Splits one line from an Ldif file to attribute and value components.