提交 8d8815ea 编写于 作者: J Jiri Denemark

Use common XML parsing functions

上级 26be7a0a
...@@ -4168,99 +4168,35 @@ error: ...@@ -4168,99 +4168,35 @@ error:
} }
/* Called from SAX on parsing errors in the XML. */ static virDomainDefPtr
static void virDomainDefParse(const char *xmlStr,
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) const char *filename,
virCapsPtr caps,
int flags)
{ {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDocPtr xml;
virDomainDefPtr def = NULL;
if (ctxt) { if ((xml = virXMLParse(filename, xmlStr, "domain.xml"))) {
if (virGetLastError() == NULL && def = virDomainDefParseNode(caps, xml, xmlDocGetRootElement(xml), flags);
ctxt->lastError.level == XML_ERR_FATAL && xmlFreeDoc(xml);
ctxt->lastError.message != NULL) {
virDomainReportError(VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
} }
return def;
} }
virDomainDefPtr virDomainDefParseString(virCapsPtr caps, virDomainDefPtr virDomainDefParseString(virCapsPtr caps,
const char *xmlStr, const char *xmlStr,
int flags) int flags)
{ {
xmlParserCtxtPtr pctxt; return virDomainDefParse(xmlStr, NULL, caps, flags);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virDomainDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "domain.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virDomainReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virDomainDefParseNode(caps, xml, root, flags);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
virDomainDefPtr virDomainDefParseFile(virCapsPtr caps, virDomainDefPtr virDomainDefParseFile(virCapsPtr caps,
const char *filename, int flags) const char *filename,
int flags)
{ {
xmlParserCtxtPtr pctxt; return virDomainDefParse(NULL, filename, caps, flags);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virDomainDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virDomainReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virDomainDefParseNode(caps, xml, root, flags);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
...@@ -4296,38 +4232,14 @@ cleanup: ...@@ -4296,38 +4232,14 @@ cleanup:
virDomainObjPtr virDomainObjParseFile(virCapsPtr caps, virDomainObjPtr virDomainObjParseFile(virCapsPtr caps,
const char *filename) const char *filename)
{ {
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr root;
virDomainObjPtr obj = NULL; virDomainObjPtr obj = NULL;
/* Set up a parser context so we can catch the details of XML errors. */ if ((xml = virXMLParseFile(filename))) {
pctxt = xmlNewParserCtxt (); obj = virDomainObjParseNode(caps, xml, xmlDocGetRootElement(xml));
if (!pctxt || !pctxt->sax) xmlFreeDoc(xml);
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virDomainReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
} }
obj = virDomainObjParseNode(caps, xml, root);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return obj; return obj;
} }
......
...@@ -851,96 +851,29 @@ cleanup: ...@@ -851,96 +851,29 @@ cleanup:
return def; return def;
} }
/* Called from SAX on parsing errors in the XML. */ static virInterfaceDefPtr
static void virInterfaceDefParse(const char *xmlStr,
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) const char *filename)
{ {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDocPtr xml;
virInterfaceDefPtr def = NULL;
if (ctxt) { if ((xml = virXMLParse(filename, xmlStr, "interface.xml"))) {
if (virGetLastError() == NULL && def = virInterfaceDefParseNode(xml, xmlDocGetRootElement(xml));
ctxt->lastError.level == XML_ERR_FATAL && xmlFreeDoc(xml);
ctxt->lastError.message != NULL) {
virInterfaceReportError (VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
} }
return def;
} }
virInterfaceDefPtr virInterfaceDefParseString(const char *xmlStr) virInterfaceDefPtr virInterfaceDefParseString(const char *xmlStr)
{ {
xmlParserCtxtPtr pctxt; return virInterfaceDefParse(xmlStr, NULL);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virInterfaceDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "interface.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virInterfaceReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virInterfaceDefParseNode(xml, root);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
virInterfaceDefPtr virInterfaceDefParseFile(const char *filename) virInterfaceDefPtr virInterfaceDefParseFile(const char *filename)
{ {
xmlParserCtxtPtr pctxt; return virInterfaceDefParse(NULL, filename);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virInterfaceDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virInterfaceReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virInterfaceDefParseNode(xml, root);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
static int static int
......
...@@ -504,96 +504,29 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt) ...@@ -504,96 +504,29 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
return NULL; return NULL;
} }
/* Called from SAX on parsing errors in the XML. */ static virNetworkDefPtr
static void virNetworkDefParse(const char *xmlStr,
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) const char *filename)
{ {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDocPtr xml;
virNetworkDefPtr def = NULL;
if (ctxt) { if ((xml = virXMLParse(filename, xmlStr, "network.xml"))) {
if (virGetLastError() == NULL && def = virNetworkDefParseNode(xml, xmlDocGetRootElement(xml));
ctxt->lastError.level == XML_ERR_FATAL && xmlFreeDoc(xml);
ctxt->lastError.message != NULL) {
virNetworkReportError(VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
} }
return def;
} }
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr) virNetworkDefPtr virNetworkDefParseString(const char *xmlStr)
{ {
xmlParserCtxtPtr pctxt; return virNetworkDefParse(xmlStr, NULL);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virNetworkDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "network.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virNetworkReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virNetworkDefParseNode(xml, root);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
virNetworkDefPtr virNetworkDefParseFile(const char *filename) virNetworkDefPtr virNetworkDefParseFile(const char *filename)
{ {
xmlParserCtxtPtr pctxt; return virNetworkDefParse(NULL, filename);
xmlDocPtr xml = NULL;
xmlNodePtr root;
virNetworkDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml) {
if (virGetLastError() == NULL)
virNetworkReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
def = virNetworkDefParseNode(xml, root);
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc (xml);
return def;
} }
......
...@@ -1224,71 +1224,19 @@ cleanup: ...@@ -1224,71 +1224,19 @@ cleanup:
return def; return def;
} }
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt) {
if (virGetLastError() == NULL &&
ctxt->lastError.level == XML_ERR_FATAL &&
ctxt->lastError.message != NULL) {
virNodeDeviceReportError(VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
}
}
static virNodeDeviceDefPtr static virNodeDeviceDefPtr
virNodeDeviceDefParse(const char *str, virNodeDeviceDefParse(const char *str,
const char *filename, const char *filename,
int create) int create)
{ {
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr root;
virNodeDeviceDefPtr def = NULL; virNodeDeviceDefPtr def = NULL;
/* Set up a parser context so we can catch the details of XML errors. */ if ((xml = virXMLParse(filename, str, "device.xml"))) {
pctxt = xmlNewParserCtxt (); def = virNodeDeviceDefParseNode(xml, xmlDocGetRootElement(xml), create);
if (!pctxt || !pctxt->sax) xmlFreeDoc(xml);
goto cleanup;
pctxt->sax->error = catchXMLError;
if (filename) {
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
} else {
xml = xmlCtxtReadDoc (pctxt, BAD_CAST str,
"device.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
}
if (!xml) {
if (virGetLastError() == NULL)
virNodeDeviceReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
if ((root = xmlDocGetRootElement(xml)) == NULL) {
virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
goto cleanup;
} }
def = virNodeDeviceDefParseNode(xml, root, create);
cleanup:
xmlFreeParserCtxt(pctxt);
xmlFreeDoc(xml);
return def; return def;
} }
......
...@@ -187,62 +187,18 @@ secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root) ...@@ -187,62 +187,18 @@ secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root)
return ret; return ret;
} }
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt) {
if (virGetLastError() == NULL &&
ctxt->lastError.level == XML_ERR_FATAL &&
ctxt->lastError.message != NULL) {
virSecretReportError(VIR_ERR_XML_DETAIL, _("at line %d: %s"),
ctxt->lastError.line, ctxt->lastError.message);
}
}
}
static virSecretDefPtr static virSecretDefPtr
virSecretDefParse(const char *xmlStr, const char *filename) virSecretDefParse(const char *xmlStr,
const char *filename)
{ {
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr root;
virSecretDefPtr ret = NULL; virSecretDefPtr ret = NULL;
pctxt = xmlNewParserCtxt(); if ((xml = virXMLParse(filename, xmlStr, "secret.xml"))) {
if (pctxt == NULL || pctxt->sax == NULL) ret = secretXMLParseNode(xml, xmlDocGetRootElement(xml));
goto cleanup; xmlFreeDoc(xml);
pctxt->sax->error = catchXMLError;
if (filename != NULL)
xml = xmlCtxtReadFile(pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
else
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, "secret.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (xml == NULL) {
if (virGetLastError() == NULL)
virSecretReportError(VIR_ERR_XML_ERROR, "%s",
_("failed to parse xml document"));
goto cleanup;
}
root = xmlDocGetRootElement(xml);
if (root == NULL) {
virSecretReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing root element"));
goto cleanup;
} }
ret = secretXMLParseNode(xml, root);
cleanup:
xmlFreeDoc(xml);
xmlFreeParserCtxt(pctxt);
return ret; return ret;
} }
......
...@@ -717,24 +717,6 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) { ...@@ -717,24 +717,6 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt) {
return NULL; return NULL;
} }
/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt) {
if (virGetLastError() == NULL &&
ctxt->lastError.level == XML_ERR_FATAL &&
ctxt->lastError.message != NULL) {
virStorageReportError (VIR_ERR_XML_DETAIL,
_("at line %d: %s"),
ctxt->lastError.line,
ctxt->lastError.message);
}
}
}
virStoragePoolDefPtr virStoragePoolDefPtr
virStoragePoolDefParseNode(xmlDocPtr xml, virStoragePoolDefParseNode(xmlDocPtr xml,
xmlNodePtr root) { xmlNodePtr root) {
...@@ -764,52 +746,14 @@ static virStoragePoolDefPtr ...@@ -764,52 +746,14 @@ static virStoragePoolDefPtr
virStoragePoolDefParse(const char *xmlStr, virStoragePoolDefParse(const char *xmlStr,
const char *filename) { const char *filename) {
virStoragePoolDefPtr ret = NULL; virStoragePoolDefPtr ret = NULL;
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr node = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
if (filename) {
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
} else {
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
"storage.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
}
if (!xml) { if ((xml = virXMLParse(filename, xmlStr, "storage.xml"))) {
if (virGetLastError() == NULL) ret = virStoragePoolDefParseNode(xml, xmlDocGetRootElement(xml));
virStorageReportError(VIR_ERR_XML_ERROR,
"%s",_("failed to parse xml document"));
goto cleanup;
}
node = xmlDocGetRootElement(xml);
if (node == NULL) {
virStorageReportError(VIR_ERR_XML_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
ret = virStoragePoolDefParseNode(xml, node);
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
}
return ret; return ret;
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml);
return NULL;
} }
virStoragePoolDefPtr virStoragePoolDefPtr
...@@ -1163,52 +1107,14 @@ virStorageVolDefParse(virStoragePoolDefPtr pool, ...@@ -1163,52 +1107,14 @@ virStorageVolDefParse(virStoragePoolDefPtr pool,
const char *xmlStr, const char *xmlStr,
const char *filename) { const char *filename) {
virStorageVolDefPtr ret = NULL; virStorageVolDefPtr ret = NULL;
xmlParserCtxtPtr pctxt; xmlDocPtr xml;
xmlDocPtr xml = NULL;
xmlNodePtr node = NULL;
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt ();
if (!pctxt || !pctxt->sax)
goto cleanup;
pctxt->sax->error = catchXMLError;
if (filename) {
xml = xmlCtxtReadFile (pctxt, filename, NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
} else {
xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr,
"storage.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
}
if (!xml) {
if (virGetLastError() == NULL)
virStorageReportError(VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document"));
goto cleanup;
}
node = xmlDocGetRootElement(xml);
if (node == NULL) {
virStorageReportError(VIR_ERR_XML_ERROR,
"%s", _("missing root element"));
goto cleanup;
}
ret = virStorageVolDefParseNode(pool, xml, node); if ((xml = virXMLParse(filename, xmlStr, "storage.xml"))) {
ret = virStorageVolDefParseNode(pool, xml, xmlDocGetRootElement(xml));
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
}
return ret; return ret;
cleanup:
xmlFreeParserCtxt (pctxt);
xmlFreeDoc(xml);
return NULL;
} }
virStorageVolDefPtr virStorageVolDefPtr
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册