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

  1 martin 1.14 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.15 //
  3 martin 1.14 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.15 //
 10 martin 1.14 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.15 //
 17 martin 1.14 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.15 //
 20 martin 1.14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.15 //
 28 martin 1.14 //////////////////////////////////////////////////////////////////////////
 29 karl   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32 dave.sudlik 1.12 #include <Pegasus/Common/PegasusAssert.h>
 33 karl        1.1  #include <Pegasus/Common/Config.h>
 34                  #include <Pegasus/Common/FileSystem.h>
 35                  #include <Pegasus/Common/XmlWriter.h>
 36 dave.sudlik 1.12 #include <Pegasus/Common/XmlReader.h>
 37 karl        1.1  #include <Pegasus/Repository/CIMRepository.h>
 38                  
 39 kumpf       1.4  PEGASUS_USING_PEGASUS;
 40                  PEGASUS_USING_STD;
 41 karl        1.1  
 42 dave.sudlik 1.12 String namespaceName;
 43                  static Boolean verbose = false;
 44 karl        1.1  
 45                  //------------------------------------------------------------------------------
 46                  // ProcessValueObjectElement()
 47                  //
 48                  //     <!ELEMENT VALUE.OBJECT (CLASS|INSTANCE)>
 49                  //
 50                  // ATTN: Nothing handled but CLASS.
 51                  //
 52                  //------------------------------------------------------------------------------
 53                  
 54                  Boolean ProcessValueObjectElement(CIMRepository& repository, XmlParser& parser)
 55                  {
 56                      XmlEntry entry;
 57                  
 58                      if (!XmlReader::testStartTag(parser, entry, "VALUE.OBJECT"))
 59 dave.sudlik 1.12     {
 60                          return false;
 61                      }
 62 karl        1.1  
 63 dave.sudlik 1.12     CIMClass cimClass, tmpClass;
 64 karl        1.1      CIMQualifierDecl qualifierDecl;
 65                  
 66                      if (XmlReader::getClassElement(parser, cimClass))
 67                      {
 68 kumpf       1.16         if (verbose)
 69 dave.sudlik 1.12         {
 70                              cout << "Creating: class ";
 71 kumpf       1.13             cout << cimClass.getClassName().getString() << endl;
 72 dave.sudlik 1.12         }
 73 karl        1.1  
 74 dave.sudlik 1.12         try
 75                          {
 76                              repository.createClass(namespaceName, cimClass);
 77                          }
 78                          catch (Exception &e)
 79                          {
 80                              // Ignore if cimClass already exists
 81                              if (e.getMessage() != cimClass.getClassName())
 82                              {
 83                                  throw;
 84                              }
 85                          }
 86 karl        1.1      }
 87                      else if (XmlReader::getQualifierDeclElement(parser, qualifierDecl))
 88                      {
 89 kumpf       1.16         if (verbose)
 90 dave.sudlik 1.12         {
 91                              cout << "Creating: qualifier ";
 92 kumpf       1.13             cout << qualifierDecl.getName().getString() << endl;
 93 dave.sudlik 1.12         }
 94 karl        1.1  
 95 dave.sudlik 1.12         try
 96                          {
 97                              repository.setQualifier(namespaceName, qualifierDecl);
 98                          }
 99                          catch (Exception &e)
100                          {
101                              // Ignore if qualifierDecl already exists
102                              if (e.getMessage() != qualifierDecl.getName())
103                              {
104                                  throw;
105                              }
106                          }
107 karl        1.1      }
108                      XmlReader::expectEndTag(parser, "VALUE.OBJECT");
109                  
110                      return true;
111                  }
112                  
113                  //------------------------------------------------------------------------------
114                  // ProcessDeclGroupElement()
115                  //
116                  //     <!ELEMENT DECLGROUP ((LOCALNAMESPACEPATH|NAMESPACEPATH)?,
117                  //         QUALIFIER.DECLARATION*,VALUE.OBJECT*)>
118                  //
119                  // ATTN: Nothing handled but VALUE.OBJECT:
120                  //
121                  //------------------------------------------------------------------------------
122                  
123                  Boolean ProcessDeclGroupElement(CIMRepository& repository, XmlParser& parser)
124                  {
125                      XmlEntry entry;
126                  
127                      if (!XmlReader::testStartTag(parser, entry, "DECLGROUP"))
128 dave.sudlik 1.12     {
129                          return false;
130 kumpf       1.16     }
131 karl        1.1  
132                      while (ProcessValueObjectElement(repository, parser))
133 dave.sudlik 1.12         ;
134 karl        1.1  
135                      XmlReader::expectEndTag(parser, "DECLGROUP");
136                  
137                      return true;
138                  }
139                  
140                  //------------------------------------------------------------------------------
141                  // ProcessDeclarationElement()
142                  //
143                  //     <!ELEMENT DECLARATION (DECLGROUP|DECLGROUP.WITHNAME|DECLGROUP.WITHPATH)*>
144                  //
145                  // ATTN: DECLGROUP.WITHNAME ande DECLGROUP.WITHPATH not handled:
146                  //
147                  //------------------------------------------------------------------------------
148                  
149                  Boolean ProcessDeclarationElement(CIMRepository& repository, XmlParser& parser)
150                  {
151                      XmlEntry entry;
152                  
153                      if (!XmlReader::testStartTag(parser, entry, "DECLARATION"))
154 dave.sudlik 1.12     {
155                          return false;
156                      }
157 karl        1.1  
158                      while(ProcessDeclGroupElement(repository, parser))
159 dave.sudlik 1.12         ;
160 karl        1.1  
161                      XmlReader::expectEndTag(parser, "DECLARATION");
162                  
163                      return true;
164                  }
165                  
166                  //------------------------------------------------------------------------------
167                  // ProcessCimElement()
168                  //
169                  //     <!ELEMENT CIM (MESSAGE|DECLARATION)>
170                  //     <!ATTLIST CIM
171                  //         CIMVERSION CDATA #REQUIRED
172                  //         DTDVERSION CDATA #REQUIRED>
173                  //
174                  // ATTN: only declarations are handled here!
175                  //
176                  //------------------------------------------------------------------------------
177                  
178                  Boolean ProcessCimElement(CIMRepository& repository, XmlParser& parser)
179                  {
180                      XmlEntry entry;
181 karl        1.1  
182                      if (!parser.next(entry) || entry.type != XmlEntry::XML_DECLARATION)
183                      {
184 dave.sudlik 1.12         throw(parser.getLine(), "expected XML declaration");
185 karl        1.1      }
186                  
187                      if (!XmlReader::testStartTag(parser, entry, "CIM"))
188 dave.sudlik 1.12     {
189                          return false;
190                      }
191 karl        1.1      String cimVersion;
192                  
193                      if (!entry.getAttributeValue("CIMVERSION", cimVersion))
194                      {
195 kumpf       1.16         throw XmlValidationError(parser.getLine(),
196 dave.sudlik 1.12             "missing CIM.CIMVERSION attribute");
197 karl        1.1      }
198                  
199                      String dtdVersion;
200                  
201                      if (!entry.getAttributeValue("DTDVERSION", dtdVersion))
202                      {
203 kumpf       1.16         throw XmlValidationError(parser.getLine(),
204 dave.sudlik 1.12             "missing CIM.DTDVERSION attribute");
205 karl        1.1      }
206                  
207                      if (!ProcessDeclarationElement(repository, parser))
208                      {
209 kumpf       1.16         throw XmlValidationError(parser.getLine(),
210 dave.sudlik 1.12             "Expected DECLARATION element");
211 karl        1.1      }
212                  
213                      XmlReader::expectEndTag(parser, "CIM");
214                  
215                      return true;
216                  }
217                  
218                  //------------------------------------------------------------------------------
219                  //
220                  // _processFile()
221                  //
222                  //------------------------------------------------------------------------------
223                  
224 dave.sudlik 1.12 static void _processFile(const char* repositoryRoot, const char* xmlFileName)
225 karl        1.1  {
226                      // Create the parser:
227                  
228 dave.sudlik 1.12     Buffer text;
229                      text.reserveCapacity(1024 * 1024);
230 karl        1.1      FileSystem::loadFileToMemory(text, xmlFileName);
231                      XmlParser parser((char*)text.getData());
232                  
233                      CIMRepository repository(repositoryRoot);
234 dave.sudlik 1.12     try
235                      {
236                          repository.createNameSpace(namespaceName);
237                      }
238                      catch (Exception &e)
239                      {
240                          // Ignore if Namespace already exists
241                          if (e.getMessage () != namespaceName)
242                          {
243                              throw;
244                          }
245                      }
246 karl        1.1  
247                      if (!ProcessCimElement(repository, parser))
248                      {
249 dave.sudlik 1.12         cerr << "CIM root element missing" << endl;
250                          exit(1);
251 karl        1.1      }
252                  }
253                  
254                  //------------------------------------------------------------------------------
255                  //
256                  // main()
257                  //
258                  //------------------------------------------------------------------------------
259                  
260                  int main(int argc, char** argv)
261                  {
262 dave.sudlik 1.12     if (argc != 4)
263 karl        1.1      {
264 kumpf       1.16         cerr << "Usage: " << argv[0]
265 dave.sudlik 1.12             << " repository-root xmlfile namespace" << endl;
266                          exit(1);
267 karl        1.1      }
268                  
269 dave.sudlik 1.12     verbose = getenv("PEGASUS_TEST_VERBOSE") ? true : false;
270 karl        1.1  
271                      try
272                      {
273 kumpf       1.16         namespaceName = argv[3];
274 dave.sudlik 1.12         cout << argv[0] << " loading " << argv[2] << " to namespace "
275                              << namespaceName << " into repository " << argv[1] << endl;
276                          _processFile(argv[1], argv[2]);
277                          cout << argv[0] << " loaded." << endl;
278                      }
279 kumpf       1.16 
280 dave.sudlik 1.12     catch (const Exception& e)
281 karl        1.1      {
282 dave.sudlik 1.12         cerr << e.getMessage() << endl;
283                          exit(1);
284 karl        1.1      }
285                  
286                      return 0;
287                  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2