Back to index

openldap  2.4.31
LdifWriter.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2008-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include "LdifWriter.h"
00008 #include "StringList.h"
00009 #include "LDAPAttribute.h"
00010 #include "debug.h"
00011 #include <sstream>
00012 #include <stdexcept>
00013 
00014 LdifWriter::LdifWriter( std::ostream& output, int version ) :
00015         m_ldifstream(output), m_version(version), m_addSeparator(false)
00016 {
00017     if ( version )
00018     {
00019         if ( version == 1 )
00020         {
00021             m_ldifstream << "version: " << version << std::endl;
00022             m_addSeparator = true;
00023         } else {
00024             std::ostringstream err;
00025             err << "Unsuported LDIF Version";
00026             throw( std::runtime_error(err.str()) );
00027         }
00028     }
00029     
00030 }
00031 
00032 void LdifWriter::writeRecord(const LDAPEntry& le)
00033 {
00034     std::ostringstream line;
00035 
00036     if ( m_addSeparator )
00037     {
00038         m_ldifstream << std::endl;
00039     } else {
00040         m_addSeparator = true;
00041     }
00042 
00043     line << "dn: " << le.getDN();
00044     this->breakline( line.str(), m_ldifstream );
00045 
00046     const LDAPAttributeList *al = le.getAttributes();
00047     LDAPAttributeList::const_iterator i = al->begin();
00048     for ( ; i != al->end(); i++ )
00049     {
00050         StringList values = i->getValues();
00051         StringList::const_iterator j = values.begin();
00052         for( ; j != values.end(); j++)
00053         {
00054             // clear output stream
00055             line.str("");
00056             line << i->getName() << ": " << *j;
00057             this->breakline( line.str(), m_ldifstream );
00058         }
00059     }
00060 }
00061 
00062 void LdifWriter::writeIncludeRecord( const std::string& target )
00063 {
00064     DEBUG(LDAP_DEBUG_TRACE, "writeIncludeRecord: " << target << std::endl);
00065     std::string scheme = target.substr( 0, sizeof("file:")-1 );
00066     
00067     if ( m_version == 1 )
00068     {
00069         std::ostringstream err;
00070         err << "\"include\" not allowed in LDIF version 1.";
00071         throw( std::runtime_error(err.str()) );
00072     }
00073     
00074     if ( m_addSeparator )
00075     {
00076         m_ldifstream << std::endl;
00077     } else {
00078         m_addSeparator = true;
00079     }
00080 
00081     m_ldifstream << "include: ";
00082     if ( scheme != "file:" )
00083     {
00084         m_ldifstream << "file://";
00085     }
00086 
00087     m_ldifstream << target << std::endl;
00088 }
00089 
00090 void LdifWriter::breakline( const std::string &line, std::ostream &out )
00091 {
00092     std::string::size_type pos = 0;
00093     std::string::size_type linelength = 76;
00094     bool first = true;
00095     
00096     if ( line.length() >= linelength )
00097     {
00098         while ( pos < line.length() )
00099         {
00100             if (! first )
00101             {
00102                 out << " ";
00103             }
00104             out << line.substr(pos, linelength) << std::endl;
00105             pos += linelength;
00106             if ( first )
00107             {
00108                 first = false;
00109                 linelength--; //account for the leading space
00110             }
00111         }
00112     } else {
00113         out << line << std::endl;
00114     }
00115 }
00116