bjavaloader: added -A option to the save command
[barry.git] / src / ldif.h
blobcc602eeb53cf8aa6323d432441030e462af647e6
1 ///
2 /// \file ldif.h
3 /// Routines for reading and writing LDAP LDIF data.
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_LDIF_H__
23 #define __BARRY_LDIF_H__
25 #include "dll.h"
26 #include <string>
27 #include <map>
29 // forward declarations
30 namespace Barry {
31 class Contact;
34 namespace Barry {
37 // ContactLdif
39 /// Class for generating LDIF output based on a Barry::Contact record object.
40 /// This class supports LDIF attribute mapping, and a heuristics mechanism
41 /// for parsing LDIF fields that may not have consistent data.
42 ///
43 /// To use this class, create an instance of it, then call DumpLdif(), passing
44 /// the Contact record object to base the work on. Output will be written
45 /// to the stream you provide. ReadLdif() goes in the other direction.
46 ///
47 /// To override LDIF attribute mapping, call Map() or Unmap() as appropriate.
48 ///
49 /// To get a list of supported Barry::Contact field names, call GetFieldNames().
50 /// This function returns a pointer to an array of ContactLdif::NameToFunc
51 /// structures, ending with NameToFunc::name as null. You can cycle through the
52 /// array with code like this:
53 ///
54 /// <pre>
55 /// for( ContactLdif::NameToFunc *n = o.GetFieldNames(); n->name; n++ ) {
56 /// ...
57 /// }
58 /// </pre>
59 ///
60 /// Note that all Get/Set functions used in attribute mapping are virtual,
61 /// and can be overridden by a derived class. This includes the heuristics
62 /// functions, which are called by DumpLdif().
63 ///
64 class BXEXPORT ContactLdif
66 public:
67 typedef std::string (ContactLdif::*GetFunctionType)(const Barry::Contact&) const;
68 typedef void (ContactLdif::*SetFunctionType)(Barry::Contact&, const std::string &) const;
70 /// Used to create a List of supported Barry field names, including
71 /// calculated names, such as full postal address.
72 struct NameToFunc
74 const char *name;
75 const char *description;
76 GetFunctionType read;
77 SetFunctionType write;
80 struct LdifAttribute
82 std::string name;
83 std::string objectClass;
84 int order;
86 LdifAttribute() : order(0) {}
87 LdifAttribute(const char *name, const std::string &oc = "")
88 : name(name), objectClass(oc), order(0)
90 LdifAttribute(const std::string &name, const std::string &oc = "")
91 : name(name), objectClass(oc), order(0)
94 bool operator<(const LdifAttribute &other) const;
95 bool operator==(const LdifAttribute &other) const;
98 struct AccessPair
100 GetFunctionType read;
101 SetFunctionType write;
103 AccessPair() : read(0), write(0) {}
104 AccessPair(GetFunctionType r, SetFunctionType w)
105 : read(r), write(w)
109 typedef std::map<LdifAttribute, AccessPair> AccessMapType;
110 typedef std::map<std::string, std::string*> HookMapType;
112 protected:
113 static const NameToFunc FieldMap[];
114 AccessMapType m_map;
115 std::string m_baseDN;
116 HookMapType m_hookMap;
117 LdifAttribute m_dnAttr;
119 void DoWrite(Barry::Contact &con, const std::string &attr,
120 const std::string &data);
122 // Array getter state
123 mutable unsigned int m_emailIndex;
125 // name heuristics
126 std::string m_cn, m_displayName, m_sn, m_givenName;
128 // heuristics hooking - saves each found value in the variable
129 // pointed at by var
130 void Hook(const std::string &ldifname, std::string *var);
132 public:
133 explicit ContactLdif(const std::string &baseDN);
134 virtual ~ContactLdif();
136 const NameToFunc* GetFieldNames() const { return FieldMap; }
137 const NameToFunc* GetField(const std::string &fieldname) const;
138 std::string GetFieldReadName(GetFunctionType read) const;
139 std::string GetFieldWriteName(SetFunctionType write) const;
141 bool Map(const LdifAttribute &ldifname, const std::string &readField,
142 const std::string &writeField);
143 void Map(const LdifAttribute &ldifname, GetFunctionType read,
144 SetFunctionType write);
145 void Unmap(const LdifAttribute &ldifname);
147 void SetBaseDN(const std::string &baseDN) { m_baseDN = baseDN; }
148 bool SetDNAttr(const LdifAttribute &name);
149 bool SetObjectClass(const LdifAttribute &name, const std::string &objectClass);
150 bool SetObjectOrder(const LdifAttribute &name, int order);
153 // Access functions
156 virtual std::string Email(const Barry::Contact &con) const;
157 virtual std::string Phone(const Barry::Contact &con) const;
158 virtual std::string Fax(const Barry::Contact &con) const;
159 virtual std::string WorkPhone(const Barry::Contact &con) const;
160 virtual std::string HomePhone(const Barry::Contact &con) const;
161 virtual std::string MobilePhone(const Barry::Contact &con) const;
162 virtual std::string Pager(const Barry::Contact &con) const;
163 virtual std::string PIN(const Barry::Contact &con) const;
164 virtual std::string FirstName(const Barry::Contact &con) const;
165 virtual std::string LastName(const Barry::Contact &con) const;
166 virtual std::string Company(const Barry::Contact &con) const;
167 virtual std::string DefaultCommunicationsMethod(const Barry::Contact &con) const;
168 virtual std::string WorkAddress1(const Barry::Contact &con) const;
169 virtual std::string WorkAddress2(const Barry::Contact &con) const;
170 virtual std::string WorkAddress3(const Barry::Contact &con) const;
171 virtual std::string WorkCity(const Barry::Contact &con) const;
172 virtual std::string WorkProvince(const Barry::Contact &con) const;
173 virtual std::string WorkPostalCode(const Barry::Contact &con) const;
174 virtual std::string WorkCountry(const Barry::Contact &con) const;
175 virtual std::string JobTitle(const Barry::Contact &con) const;
176 virtual std::string PublicKey(const Barry::Contact &con) const;
177 virtual std::string Notes(const Barry::Contact &con) const;
178 virtual std::string Image(const Barry::Contact &con) const;
179 // calculated values...
180 virtual std::string WorkPostalAddress(const Barry::Contact &con) const;
181 virtual std::string HomePostalAddress(const Barry::Contact &con) const;
182 virtual std::string FullName(const Barry::Contact &con) const;
183 virtual std::string FQDN(const Barry::Contact &con) const;
186 // Array modifier functions for above Access functions
189 virtual bool IsArrayFunc(GetFunctionType getf) const;
190 void ClearArrayState() const;
193 // Write functions
196 virtual void SetEmail(Barry::Contact &con, const std::string &val) const;
197 virtual void SetPhone(Barry::Contact &con, const std::string &val) const;
198 virtual void SetFax(Barry::Contact &con, const std::string &val) const;
199 virtual void SetWorkPhone(Barry::Contact &con, const std::string &val) const;
200 virtual void SetHomePhone(Barry::Contact &con, const std::string &val) const;
201 virtual void SetMobilePhone(Barry::Contact &con, const std::string &val) const;
202 virtual void SetPager(Barry::Contact &con, const std::string &val) const;
203 virtual void SetPIN(Barry::Contact &con, const std::string &val) const;
204 virtual void SetFirstName(Barry::Contact &con, const std::string &val) const;
205 virtual void SetLastName(Barry::Contact &con, const std::string &val) const;
206 virtual void SetCompany(Barry::Contact &con, const std::string &val) const;
207 virtual void SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const;
208 virtual void SetWorkAddress1(Barry::Contact &con, const std::string &val) const;
209 virtual void SetWorkAddress2(Barry::Contact &con, const std::string &val) const;
210 virtual void SetWorkAddress3(Barry::Contact &con, const std::string &val) const;
211 virtual void SetWorkCity(Barry::Contact &con, const std::string &val) const;
212 virtual void SetWorkProvince(Barry::Contact &con, const std::string &val) const;
213 virtual void SetWorkPostalCode(Barry::Contact &con, const std::string &val) const;
214 virtual void SetWorkCountry(Barry::Contact &con, const std::string &val) const;
215 virtual void SetJobTitle(Barry::Contact &con, const std::string &val) const;
216 virtual void SetPublicKey(Barry::Contact &con, const std::string &val) const;
217 virtual void SetNotes(Barry::Contact &con, const std::string &val) const;
218 virtual void SetImage(Barry::Contact &con, const std::string &val) const;
219 virtual void SetWorkPostalAddress(Barry::Contact &con, const std::string &val) const;
220 virtual void SetHomePostalAddress(Barry::Contact &con, const std::string &val) const;
221 virtual void SetFullName(Barry::Contact &con, const std::string &val) const;
222 virtual void SetFQDN(Barry::Contact &con, const std::string &val) const;
226 // Name heuristics
229 virtual void ClearHeuristics();
230 virtual bool RunHeuristics(Barry::Contact &con);
233 // Operations
236 void DumpLdif(std::ostream &os, const Barry::Contact &contact) const;
237 bool ReadLdif(std::istream &is, Barry::Contact &contact);
238 // returns true on success
239 void DumpMap(std::ostream &os) const;
242 static std::string MakeLdifData(const std::string &str);
243 static bool NeedsEncoding(const std::string &str);
246 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const ContactLdif &ldif) {
247 ldif.DumpMap(os);
248 return os;
251 } // namespace Barry
253 #endif