(file) Return to test_wsman.c CVS log (file) (dir) Up to [OMI] / omi / xml / wsman

  1 mike  1.1 /*
  2           **==============================================================================
  3           **
  4           ** Open Management Infrastructure (OMI)
  5           **
  6           ** Copyright (c) Microsoft Corporation
  7           ** 
  8           ** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  9           ** use this file except in compliance with the License. You may obtain a copy 
 10           ** of the License at 
 11           **
 12           **     http://www.apache.org/licenses/LICENSE-2.0 
 13           **
 14           ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15           ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
 16           ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
 17           ** MERCHANTABLITY OR NON-INFRINGEMENT. 
 18           **
 19           ** See the Apache 2 License for the specific language governing permissions 
 20           ** and limitations under the License.
 21           **
 22 mike  1.1 **==============================================================================
 23           */
 24           
 25           #include <sys/stat.h>
 26           #include <unistd.h>
 27           #include <sys/stat.h>
 28           #include <stdio.h>
 29           #include <stdlib.h>
 30           #include <string.h>
 31           #include <xml/xml.h>
 32           
 33           const char* arg0;
 34           
 35           int Inhale(const char* path, char** dataOut, size_t* sizeOut)
 36           {
 37               FILE* is;
 38               char* data;
 39               size_t size;
 40               size_t n;
 41           
 42               /* Get file size */
 43 mike  1.1     {
 44                   struct stat st;
 45           
 46                   if (stat(path, &st) != 0)
 47                       return -1;
 48           
 49                   size = st.st_size;
 50               }
 51           
 52               /* Open file */
 53               {
 54                   is = fopen(path, "rb");
 55           
 56                   if (!is)
 57                       return -1;
 58               }
 59           
 60               /* Allocate buffer */
 61               {
 62 krisbash 1.3         data = (char*)PAL_Malloc(size + 1);
 63 mike     1.1 
 64                      if (!data)
 65                      {
 66                          fclose(is);
 67                          return -1;
 68                      }
 69              
 70                      *dataOut = data;
 71                      *sizeOut = size;
 72                  }
 73              
 74                  /* Read file into buffer */
 75                  while ((n = fread(data, 1, size, is)) != 0)
 76                  {
 77                      data += n;
 78                      size -= n;
 79                  }
 80              
 81                  data[0] = '\0';
 82                  fclose(is);
 83              
 84 mike     1.1     return 0;
 85              }
 86              
 87              static int ParseSoapEnvelope(XML* xml)
 88              {
 89                  XML_Elem e;
 90              
 91                  /* Ignore the processing instruction (if any) */
 92                  {
 93                      if (XML_Next(xml, &e) != 0)
 94                      {
 95                          XML_Raise(xml, "missing root element");
 96                          return -1;
 97                      }
 98              
 99                      if (e.type != XML_INST)
100                      {
101                          if (XML_PutBack(xml, &e) != 0)
102                              return -1;
103                      }
104                  }
105 mike     1.1 
106                  /* Expect <s:Envelope> */
107                  if (XML_Expect(xml, &e, XML_START, "s:Envelope") != 0)
108                      return -1;
109              
110                  /* Expect <s:Header> */
111                  if (XML_Expect(xml, &e, XML_START, "s:Header") != 0)
112                      return -1;
113              
114                  for (;;)
115                  {
116                      if (XML_Next(xml, &e) != 0)
117                          return -1;
118              
119                      if (e.type == XML_END && strcmp(e.data, "s:Header") == 0)
120                      {
121                          printf("DONE\n");
122                          break;
123                      }
124                      else if (e.type == XML_START && strcmp(e.data, "a:To") == 0)
125                      {
126 mike     1.1             if (XML_Expect(xml, &e, XML_CHARS, NULL) != 0)
127                              return -1;
128              
129                          printf("a:To{%s}\n", e.data);
130              
131                          if (XML_Expect(xml, &e, XML_END, "a:To") != 0)
132                              return -1;
133                      }
134                      else if (e.type == XML_START && strcmp(e.data, "w:ResourceURI") == 0)
135                      {
136                          if (XML_Expect(xml, &e, XML_CHARS, NULL) != 0)
137                              return -1;
138              
139                          printf("w:ResourceURI{%s}\n", e.data);
140              
141                          if (XML_Expect(xml, &e, XML_END, "w:ResourceURI") != 0)
142                              return -1;
143                      }
144                      else if (e.type == XML_START && strcmp(e.data, "a:ReplyTo") == 0)
145                      {
146                          if (XML_Expect(xml, &e, XML_START, "a:Address") != 0)
147 mike     1.1                 return -1;
148              
149                          if (XML_Expect(xml, &e, XML_CHARS, NULL) != 0)
150                              return -1;
151              
152                          printf("a:Address{%s}\n", e.data);
153              
154                          if (XML_Expect(xml, &e, XML_END, "a:Address") != 0)
155                              return -1;
156              
157                          if (XML_Expect(xml, &e, XML_END, "a:ReplyTo") != 0)
158                              return -1;
159                      }
160                      else if (e.type == XML_START && strcmp(e.data, "a:Action") == 0)
161                      {
162                          if (XML_Expect(xml, &e, XML_CHARS, NULL) != 0)
163                              return -1;
164              
165                          printf("a:Action{%s}\n", e.data);
166              
167                          if (XML_Expect(xml, &e, XML_END, "a:Action") != 0)
168 mike     1.1                 return -1;
169                      }
170                      else if (e.type == XML_START && strcmp(e.data, "a:MessageID") == 0)
171                      {
172                          if (XML_Expect(xml, &e, XML_CHARS, NULL) != 0)
173                              return -1;
174              
175                          printf("a:MessageID{%s}\n", e.data);
176              
177                          if (XML_Expect(xml, &e, XML_END, "a:MessageID") != 0)
178                              return -1;
179                      }
180                      else
181                      {
182                          if (XML_Skip(xml) != 0)
183                              return -1;
184                      }
185                  }
186              
187                  /* Expect </s:Body> */
188                  if (XML_Expect(xml, &e, XML_START, "s:Body") != 0)
189 mike     1.1         return -1;
190              
191                  if (XML_Skip(xml) != 0)
192                      return -1;
193              
194                  /* Expect </s:Body> */
195              //    if (XML_Expect(xml, &e, XML_END, "s:Body") != 0)
196              //        return -1;
197              
198                  /* Expect </s:Envelope> */
199                  if (XML_Expect(xml, &e, XML_END, "s:Envelope") != 0)
200                      return -1;
201              
202                  return 0;
203              }
204              
205              static void Parse(const char* path)
206              {
207                  XML xml;
208                  char* data;
209                  size_t size;
210 mike     1.1 
211                  if (Inhale(path, &data, &size) != 0)
212                  {
213                      fprintf(stderr, "%s: failed to open file: %s\n", arg0, path);
214                      exit(1);
215                  }
216              
217                  XML_Init(&xml, &data[0]);
218              
219                  XML_RegisterNameSpace(&xml, 's', 
220                      "http://www.w3.org/2003/05/soap-envelope");
221              
222                  XML_RegisterNameSpace(&xml, 'a', 
223                      "http://schemas.xmlsoap.org/ws/2004/08/addressing");
224              
225                  XML_RegisterNameSpace(&xml, 'w', 
226                      "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd");
227              
228                  ParseSoapEnvelope(&xml);
229              
230                  if (xml.status)
231 mike     1.1     {
232                      XML_PutError(&xml);
233                      exit(1);
234                  }
235              
236              #if 0
237                  while ((r = XML_Next(&xml, &e)) == 0)
238                  {
239                      XML_Elem_Dump(&e);
240                  }
241              
242                  if (r != 1)
243                  {
244                      char msg[1024];
245                      XML_FormatError(&xml, msg, sizeof(msg));
246                      printf("%s:%s\n", path, msg);
247                      exit(1);
248                  }
249              #endif
250              }
251              
252 mike     1.1 int main(int argc, char** argv)
253              {
254                  arg0 = argv[0];
255              
256                  /* Check arguments */
257                  if (argc < 2)
258                  {
259                      fprintf(stderr, "Usage: %s path\n", arg0);
260                      exit(1);
261                  }
262              
263                  /* Parse the file */
264                  Parse(argv[1]);
265              
266                  return 0;
267              }

ViewCVS 0.9.2