1 |
/* Copyright (C) 2003 Stephane Thiell |
2 |
* |
3 |
* This file is part of pxyscand (from pxys) |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
* |
19 |
*/ |
20 |
#define RCSID "$Id: PXXMLXPathLoaderImp.cc,v 1.2 2004/01/02 00:33:03 mbuna Exp $" |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#include "PXXMLXPathLoaderImp.h" |
27 |
|
28 |
#include <sys/types.h> |
29 |
#include <sys/socket.h> |
30 |
#include <netinet/in.h> |
31 |
#include <arpa/inet.h> |
32 |
|
33 |
PXXMLXPathLoaderImp::PXXMLXPathLoaderImp(const char *inFile) |
34 |
{ |
35 |
mDoc = xmlParseFile(inFile); |
36 |
if (mDoc == NULL) |
37 |
PXXMLException::Throw("XML document parsing failed"); |
38 |
|
39 |
mCx = xmlXPathNewContext(mDoc); |
40 |
} |
41 |
|
42 |
PXXMLXPathLoaderImp::~PXXMLXPathLoaderImp() |
43 |
{ |
44 |
xmlXPathFreeContext(mCx); |
45 |
} |
46 |
|
47 |
xmlXPathObjectPtr |
48 |
PXXMLXPathLoaderImp::Eval(const char *xpath) |
49 |
{ |
50 |
xmlXPathObjectPtr result; |
51 |
result = xmlXPathEvalExpression((xmlChar *)xpath, mCx); |
52 |
|
53 |
if (result && xmlXPathNodeSetIsEmpty(result->nodesetval)) |
54 |
{ |
55 |
xmlXPathFreeObject(result); |
56 |
return NULL; |
57 |
} |
58 |
return result; |
59 |
} |
60 |
|
61 |
xmlXPathObjectPtr |
62 |
PXXMLXPathLoaderImp::EvalUnique(const char *xpath) |
63 |
{ |
64 |
xmlXPathObjectPtr o = this->Eval(xpath); |
65 |
|
66 |
if (!o || !o->nodesetval->nodeNr) |
67 |
PXXMLException::Throw("Missing parameter", xpath); |
68 |
if (o->nodesetval->nodeNr > 1) |
69 |
PXXMLException::Throw("Duplicate parameter", xpath); |
70 |
|
71 |
return o; |
72 |
} |
73 |
|
74 |
void |
75 |
PXXMLXPathLoaderImp::GetAddress(const char *xpath, int idx, int mincnt, |
76 |
const char *debugpath, struct in_addr *outAddr) |
77 |
{ |
78 |
xmlChar *str; |
79 |
|
80 |
if (!(str = CopyString(xpath, idx, mincnt))) |
81 |
outAddr->s_addr = INADDR_ANY; |
82 |
else |
83 |
{ |
84 |
int res = inet_pton(AF_INET, (char *)str, outAddr); |
85 |
xmlFree(str); |
86 |
if (res == -1) |
87 |
PXXMLException::Throw("Bad IP address", debugpath); |
88 |
} |
89 |
} |
90 |
|
91 |
int |
92 |
PXXMLXPathLoaderImp::GetInteger(const char *xpath, int idx, int mincnt) |
93 |
{ |
94 |
int result; |
95 |
xmlChar *s; |
96 |
|
97 |
if ((s = this->CopyString(xpath, idx, mincnt)) != NULL) |
98 |
{ |
99 |
result = atoi((char *)s); |
100 |
xmlFree(s); |
101 |
} |
102 |
else |
103 |
result = 0; |
104 |
return result; |
105 |
} |
106 |
|
107 |
xmlChar * |
108 |
PXXMLXPathLoaderImp::CopyString(const char *xpath, int idx, int mincnt) |
109 |
{ |
110 |
xmlXPathObjectPtr result; |
111 |
xmlNodePtr node; |
112 |
xmlChar *s; |
113 |
|
114 |
result = this->Eval(xpath); |
115 |
if (!result || !result->nodesetval->nodeNr) |
116 |
{ |
117 |
if (result) |
118 |
xmlXPathFreeObject(result); |
119 |
if (mincnt > 0) |
120 |
PXXMLException::Throw("Missing parameter", xpath); |
121 |
return NULL; |
122 |
} |
123 |
if (idx == 0 && mincnt > 0 && result->nodesetval->nodeNr > mincnt) |
124 |
PXXMLException::Throw("Duplicate parameter", xpath); |
125 |
|
126 |
if (!(node = result->nodesetval->nodeTab[idx]->xmlChildrenNode)) |
127 |
PXXMLException::Throw("Internal error (PXXMLXPathLoaderImp::CopyString)", |
128 |
xpath); |
129 |
s = xmlNodeListGetString(mDoc, node, 1); |
130 |
xmlXPathFreeObject(result); |
131 |
if (!s && mincnt > 0) |
132 |
PXXMLException::Throw("Empty parameter", xpath); |
133 |
return s; |
134 |
} |
135 |
|