(file) Return to AssocClassTable.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Repository

  1 mike  1.4 //%/////////////////////////////////////////////////////////////////////////////
  2           //
  3 kumpf 1.6 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
  4           // The Open Group, Tivoli Systems
  5 mike  1.4 //
  6           // Permission is hereby granted, free of charge, to any person obtaining a copy
  7 kumpf 1.6 // of this software and associated documentation files (the "Software"), to
  8           // deal in the Software without restriction, including without limitation the
  9           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10 mike  1.4 // sell copies of the Software, and to permit persons to whom the Software is
 11           // furnished to do so, subject to the following conditions:
 12           // 
 13 kumpf 1.6 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14 mike  1.4 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16 kumpf 1.6 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19 mike  1.4 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21           //
 22           //==============================================================================
 23           //
 24           // Author: Mike Brasher (mbrasher@bmc.com)
 25           //
 26 kumpf 1.10 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
 27            //                (carolann_graves@hp.com)
 28 mday  1.12.4.1 //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 29 mike  1.4      //
 30                //%/////////////////////////////////////////////////////////////////////////////
 31                
 32 mike  1.5      #include <Pegasus/Common/Config.h>
 33 mike  1.4      #include <fstream>
 34 kumpf 1.8      #include <Pegasus/Common/InternalException.h>
 35 mike  1.4      #include <Pegasus/Common/FileSystem.h>
 36                #include <Pegasus/Common/Exception.h>
 37                #include "AssocClassTable.h"
 38                
 39                PEGASUS_USING_STD;
 40                
 41                PEGASUS_NAMESPACE_BEGIN
 42                
 43                #define ASSOC_CLASS_NAME_INDEX 0
 44                #define FROM_CLASS_NAME_INDEX 1
 45                #define FROM_PROPERTY_NAME_INDEX 2
 46                #define TO_CLASS_NAME_INDEX 3
 47                #define TO_PROPERTY_NAME_INDEX 4
 48                #define NUM_FIELDS 5
 49                
 50                static inline Boolean _MatchNoCase(const String& x, const String& pattern)
 51                {
 52                    return pattern.size() == 0 || String::equalNoCase(x, pattern);
 53                }
 54                
 55 karl  1.11     static inline Boolean _ContainsClass(const Array<CIMName>& classNames, const String& match ) 
 56                {
 57                    Uint32 n = classNames.size();
 58                
 59                    for (Uint32 i = 0; i < n; i++)
 60                    {
 61                	if (_MatchNoCase(classNames[i].getString(), match))
 62                	    return true;
 63                    }
 64                
 65                    return false;
 66                }
 67                
 68                
 69 mike  1.4      static String _Escape(const String& str)
 70                {
 71                    String result;
 72                
 73                    for (Uint32 i = 0, n = str.size(); i < n; i++)
 74                    {
 75                	Char16 c = str[i];
 76                
 77                	switch (c)
 78                	{
 79                	    case '\n':
 80 kumpf 1.9      		result.append("\\n");
 81 mike  1.4      		break;
 82                
 83                	    case '\r':
 84 kumpf 1.9      		result.append("\\r");
 85 mike  1.4      		break;
 86                
 87                	    case '\t':
 88 kumpf 1.9      		result.append("\\t");
 89 mike  1.4      		break;
 90                
 91                	    case '\f':
 92 kumpf 1.9      		result.append("\\f");
 93 mike  1.4      		break;
 94                
 95                	    case '\\':
 96 kumpf 1.9      		result.append("\\\\");
 97 mike  1.4      		break;
 98                
 99                	    default:
100 kumpf 1.9      		result.append(c);
101 mike  1.4      	}
102                    }
103                
104                    return result;
105                }
106                
107                static String _Unescape(const String& str)
108                {
109                    String result;
110                
111                    for (Uint32 i = 0, n = str.size(); i < n; i++)
112                    {
113                	Char16 c = str[i];
114                
115                	if (c == '\\')
116                	{
117                	    if (i + 1 == n)
118                		break;
119                
120                	    c = str[i + 1];
121                
122 mike  1.4      	    switch (c)
123                	    {
124                		case 'n':
125 kumpf 1.9      		    result.append("\n");
126 mike  1.4      		    break;
127                
128                		case 'r':
129 kumpf 1.9      		    result.append("\r");
130 mike  1.4      		    break;
131                
132                		case 't':
133 kumpf 1.9      		    result.append("\t");
134 mike  1.4      		    break;
135                
136                		case 'f':
137 kumpf 1.9      		    result.append("\f");
138 mike  1.4      		    break;
139                
140                		default:
141 kumpf 1.9      		    result.append(c);
142 mike  1.4      	    }
143                
144                	    i++;
145                	}
146                	else
147 kumpf 1.9      	    result.append(c);
148 mike  1.4          }
149                
150                    return result;
151                }
152                
153                static Boolean _GetRecord(ifstream& is, Array<String>& fields)
154                {
155                    fields.clear();
156                    String line;
157                
158                    for (Uint32 i = 0; i < NUM_FIELDS; i++)
159                    {
160                	if (!GetLine(is, line))
161                	    return false;
162                
163                	fields.append(_Unescape(line));
164                    }
165                
166                    // Skip the blank line:
167                
168                    if (!GetLine(is, line))
169 mike  1.4      	return false;
170                
171                    return true;
172                }
173                
174                static void _PutRecord(ofstream& os, Array<String>& fields)
175                {
176                    for (Uint32 i = 0, n = fields.size(); i < n; i++)
177                	os << _Escape(fields[i]) << endl;
178                    os << endl;
179                }
180                
181                void AssocClassTable::append(
182                    PEGASUS_STD(ofstream)& os,
183 kumpf 1.10         const CIMName& assocClassName,
184                    const CIMName& fromClassName,
185                    const CIMName& fromPropertyName,
186                    const CIMName& toClassName,
187                    const CIMName& toPropertyName)
188 mike  1.4      {
189                    Array<String> fields;
190 kumpf 1.7          fields.reserveCapacity(5);
191 kumpf 1.10         fields.append(assocClassName.getString());
192                    fields.append(fromClassName.getString());
193                    fields.append(fromPropertyName.getString());
194                    fields.append(toClassName.getString());
195                    fields.append(toPropertyName.getString());
196 mike  1.4      
197                    _PutRecord(os, fields);
198                }
199                
200                void AssocClassTable::append(
201                    const String& path,
202 kumpf 1.10         const CIMName& assocClassName,
203                    const CIMName& fromClassName,
204                    const CIMName& fromPropertyName,
205                    const CIMName& toClassName,
206                    const CIMName& toPropertyName)
207 mike  1.4      {
208                    // Open input file:
209                    
210                    ofstream os;
211                
212                    if (!OpenAppend(os, path))
213                	throw CannotOpenFile(path);
214                
215                    // Insert the entry:
216                
217                    Array<String> fields;
218 kumpf 1.7          fields.reserveCapacity(5);
219 kumpf 1.10         fields.append(assocClassName.getString());
220                    fields.append(fromClassName.getString());
221                    fields.append(fromPropertyName.getString());
222                    fields.append(toClassName.getString());
223                    fields.append(toPropertyName.getString());
224 mike  1.4      
225                    _PutRecord(os, fields);
226                }
227                
228                Boolean AssocClassTable::deleteAssociation(
229                    const String& path,
230 kumpf 1.10         const CIMName& assocClassName)
231 mike  1.4      {
232                    // Open input file:
233                
234                    ifstream is;
235                
236                    if (!Open(is, path))
237                	return false;
238                
239                    // Open output file:
240                
241                    String tmpPath = path + ".tmp";
242                    ofstream os;
243                
244                    if (!Open(os, tmpPath))
245                	throw CannotOpenFile(tmpPath);
246                
247                    // Copy over all lines except ones with the given association instance name:
248                
249                    Array<String> fields;
250                    Boolean found = false;
251                
252 mike  1.4          while (_GetRecord(is, fields))
253                    {
254 kumpf 1.10     	if (assocClassName.getString() != fields[ASSOC_CLASS_NAME_INDEX])
255 mike  1.4      	{
256                	    _PutRecord(os, fields);
257                	    found = true;
258                	}
259                    }
260                
261                    // Close both files:
262                
263                    is.close();
264                    os.close();
265                
266                    // Remove orginal file:
267                
268                    if (!FileSystem::removeFile(path))
269                	throw CannotRemoveFile(path);
270                
271                    // Rename back to original name:
272                
273                    if (!FileSystem::renameFile(tmpPath, path))
274                	throw CannotRenameFile(path);
275                
276 mike  1.4          return found;
277                }
278                
279                Boolean AssocClassTable::getAssociatorNames(
280                    const String& path,
281 mday  1.12.4.1     const Array<CIMName>& classList,
282                    const Array<CIMName>& assocClassList,
283                    const Array<CIMName>& resultClassList,
284 mike  1.4          const String& role,
285                    const String& resultRole,
286                    Array<String>& associatorNames)
287                {
288                    // Open input file:
289                    ifstream is;
290                
291                    if (!Open(is, path))
292 mday  1.12.4.1         return false;
293 mike  1.4      
294                    Array<String> fields;
295                    Boolean found = false;
296                
297 mday  1.12.4.1     // For each line in the associations table:
298 mike  1.4          while (_GetRecord(is, fields))
299                    {
300 mday  1.12.4.1         // Process associations from the right end class and with right roles
301                        if (_ContainsClass(classList, fields[FROM_CLASS_NAME_INDEX]) &&
302                            _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role) &&
303                            _MatchNoCase(fields[TO_PROPERTY_NAME_INDEX], resultRole))
304                        {
305                            // Skip classes that do not appear in the association class list
306                            if ((assocClassList.size() != 0) &&
307                                (!_ContainsClass(assocClassList,
308                                                 fields[ASSOC_CLASS_NAME_INDEX])))
309                            {
310                                continue;
311                            }
312                
313                            // Skip classes that do not appear in the result class list
314                            if ((resultClassList.size() != 0) &&
315                                (!_ContainsClass(resultClassList,
316                                                 fields[TO_CLASS_NAME_INDEX])))
317                            {
318                                continue;
319                            }
320                
321 mday  1.12.4.1             // This class qualifies; add it to the list (skipping duplicates)
322                            if (!Contains(associatorNames, fields[TO_CLASS_NAME_INDEX]))
323                            {
324                                associatorNames.append(fields[TO_CLASS_NAME_INDEX]);
325                            }
326                            found = true;
327                        }
328 mike  1.4          }
329                
330                    return found;
331                }
332                
333                Boolean AssocClassTable::getReferenceNames(
334                    const String& path,
335 karl  1.11         const Array<CIMName>& classList,
336 mday  1.12.4.1     const Array<CIMName>& resultClassList,
337 mike  1.4          const String& role,
338                    Array<String>& referenceNames)
339                {
340                    // Open input file:
341                    ifstream is;
342                
343                    if (!Open(is, path))
344 mday  1.12.4.1         return false;
345 mike  1.4      
346 karl  1.11         Array<String> fields;
347                    Boolean found = false;
348                
349 mday  1.12.4.1     // For each line in the associations table:
350 karl  1.11         while (_GetRecord(is, fields))
351                    {
352 mday  1.12.4.1         // Process associations from the right end class and with right role
353                        if (_ContainsClass(classList, fields[FROM_CLASS_NAME_INDEX]) &&
354                            _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role))
355 karl  1.11             {
356 mday  1.12.4.1             // Skip classes that do not appear in the result class list
357                            if ((resultClassList.size() != 0) &&
358                                (!_ContainsClass(resultClassList,
359                                                 fields[ASSOC_CLASS_NAME_INDEX])))
360                            {
361                                continue;
362                            }
363                
364                            // This class qualifies; add it to the list (skipping duplicates)
365                            if (!Contains(referenceNames, fields[ASSOC_CLASS_NAME_INDEX]))
366 karl  1.11                 {
367 mday  1.12.4.1                 referenceNames.append(fields[ASSOC_CLASS_NAME_INDEX]);
368 karl  1.11                 }
369 mday  1.12.4.1             found = true;
370 karl  1.11             }
371 mike  1.4          }
372                
373                    return found;
374                }
375                
376                PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2