📄 debugxml.c
字号:
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlShellPrintNodeCtxt:
* @ctxt : a non-null shell context
* @node : a non-null node to print to the output FILE
*
* Print node to the output FILE
*/
static void
xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
{
FILE *fp;
if (!node)
return;
if (ctxt == NULL)
fp = stdout;
else
fp = ctxt->output;
if (node->type == XML_DOCUMENT_NODE)
xmlDocDump(fp, (xmlDocPtr) node);
else if (node->type == XML_ATTRIBUTE_NODE)
xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
else
xmlElemDump(fp, node->doc, node);
fprintf(fp, "\n");
}
/**
* xmlShellPrintNode:
* @node : a non-null node to print to the output FILE
*
* Print node to the output FILE
*/
void
xmlShellPrintNode(xmlNodePtr node)
{
xmlShellPrintNodeCtxt(NULL, node);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
* xmlShellPrintXPathResultCtxt:
* @ctxt: a valid shell context
* @list: a valid result generated by an xpath evaluation
*
* Prints result to the output FILE
*/
static void
xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
{
if (!ctxt)
return;
if (list != NULL) {
switch (list->type) {
case XPATH_NODESET:{
#ifdef LIBXML_OUTPUT_ENABLED
int indx;
if (list->nodesetval) {
for (indx = 0; indx < list->nodesetval->nodeNr;
indx++) {
xmlShellPrintNodeCtxt(ctxt,
list->nodesetval->nodeTab[indx]);
}
} else {
xmlGenericError(xmlGenericErrorContext,
"Empty node set\n");
}
break;
#else
xmlGenericError(xmlGenericErrorContext,
"Node set\n");
#endif /* LIBXML_OUTPUT_ENABLED */
}
case XPATH_BOOLEAN:
xmlGenericError(xmlGenericErrorContext,
"Is a Boolean:%s\n",
xmlBoolToText(list->boolval));
break;
case XPATH_NUMBER:
xmlGenericError(xmlGenericErrorContext,
"Is a number:%0g\n", list->floatval);
break;
case XPATH_STRING:
xmlGenericError(xmlGenericErrorContext,
"Is a string:%s\n", list->stringval);
break;
default:
xmlShellPrintXPathError(list->type, NULL);
}
}
}
/**
* xmlShellPrintXPathResult:
* @list: a valid result generated by an xpath evaluation
*
* Prints result to the output FILE
*/
void
xmlShellPrintXPathResult(xmlXPathObjectPtr list)
{
xmlShellPrintXPathResultCtxt(NULL, list);
}
/**
* xmlShellList:
* @ctxt: the shell context
* @arg: unused
* @node: a node
* @node2: unused
*
* Implements the XML shell function "ls"
* Does an Unix like listing of the given node (like a directory)
*
* Returns 0
*/
int
xmlShellList(xmlShellCtxtPtr ctxt,
char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlNodePtr cur;
if (!ctxt)
return (0);
if (node == NULL) {
fprintf(ctxt->output, "NULL\n");
return (0);
}
if ((node->type == XML_DOCUMENT_NODE) ||
(node->type == XML_HTML_DOCUMENT_NODE)) {
cur = ((xmlDocPtr) node)->children;
} else if (node->type == XML_NAMESPACE_DECL) {
xmlLsOneNode(ctxt->output, node);
return (0);
} else if (node->children != NULL) {
cur = node->children;
} else {
xmlLsOneNode(ctxt->output, node);
return (0);
}
while (cur != NULL) {
xmlLsOneNode(ctxt->output, cur);
cur = cur->next;
}
return (0);
}
/**
* xmlShellBase:
* @ctxt: the shell context
* @arg: unused
* @node: a node
* @node2: unused
*
* Implements the XML shell function "base"
* dumps the current XML base of the node
*
* Returns 0
*/
int
xmlShellBase(xmlShellCtxtPtr ctxt,
char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlChar *base;
if (!ctxt)
return 0;
if (node == NULL) {
fprintf(ctxt->output, "NULL\n");
return (0);
}
base = xmlNodeGetBase(node->doc, node);
if (base == NULL) {
fprintf(ctxt->output, " No base found !!!\n");
} else {
fprintf(ctxt->output, "%s\n", base);
xmlFree(base);
}
return (0);
}
#ifdef LIBXML_TREE_ENABLED
/**
* xmlShellSetBase:
* @ctxt: the shell context
* @arg: the new base
* @node: a node
* @node2: unused
*
* Implements the XML shell function "setbase"
* change the current XML base of the node
*
* Returns 0
*/
static int
xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlNodeSetBase(node, (xmlChar*) arg);
return (0);
}
#endif
#ifdef LIBXML_XPATH_ENABLED
/**
* xmlShellRegisterNamespace:
* @ctxt: the shell context
* @arg: a string in prefix=nsuri format
* @node: unused
* @node2: unused
*
* Implements the XML shell function "setns"
* register/unregister a prefix=namespace pair
* on the XPath context
*
* Returns 0 on success and a negative value otherwise.
*/
static int
xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlChar* nsListDup;
xmlChar* prefix;
xmlChar* href;
xmlChar* next;
nsListDup = xmlStrdup((xmlChar *) arg);
next = nsListDup;
while(next != NULL) {
/* skip spaces */
/*while((*next) == ' ') next++;*/
if((*next) == '\0') break;
/* find prefix */
prefix = next;
next = (xmlChar*)xmlStrchr(next, '=');
if(next == NULL) {
fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
xmlFree(nsListDup);
return(-1);
}
*(next++) = '\0';
/* find href */
href = next;
next = (xmlChar*)xmlStrchr(next, ' ');
if(next != NULL) {
*(next++) = '\0';
}
/* do register namespace */
if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
xmlFree(nsListDup);
return(-1);
}
}
xmlFree(nsListDup);
return(0);
}
/**
* xmlShellRegisterRootNamespaces:
* @ctxt: the shell context
* @arg: unused
* @node: the root element
* @node2: unused
*
* Implements the XML shell function "setrootns"
* which registers all namespaces declarations found on the root element.
*
* Returns 0 on success and a negative value otherwise.
*/
static int
xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlNsPtr ns;
if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
(root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
return(-1);
ns = root->nsDef;
while (ns != NULL) {
if (ns->prefix == NULL)
xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
else
xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
ns = ns->next;
}
return(0);
}
#endif
/**
* xmlShellGrep:
* @ctxt: the shell context
* @arg: the string or regular expression to find
* @node: a node
* @node2: unused
*
* Implements the XML shell function "grep"
* dumps informations about the node (namespace, attributes, content).
*
* Returns 0
*/
static int
xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
if (!ctxt)
return (0);
if (node == NULL)
return (0);
if (arg == NULL)
return (0);
#ifdef LIBXML_REGEXP_ENABLED
if ((xmlStrchr((xmlChar *) arg, '?')) ||
(xmlStrchr((xmlChar *) arg, '*')) ||
(xmlStrchr((xmlChar *) arg, '.')) ||
(xmlStrchr((xmlChar *) arg, '['))) {
}
#endif
while (node != NULL) {
if (node->type == XML_COMMENT_NODE) {
if (xmlStrstr(node->content, (xmlChar *) arg)) {
fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
xmlShellList(ctxt, NULL, node, NULL);
}
} else if (node->type == XML_TEXT_NODE) {
if (xmlStrstr(node->content, (xmlChar *) arg)) {
fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
xmlShellList(ctxt, NULL, node->parent, NULL);
}
}
/*
* Browse the full subtree, deep first
*/
if ((node->type == XML_DOCUMENT_NODE) ||
(node->type == XML_HTML_DOCUMENT_NODE)) {
node = ((xmlDocPtr) node)->children;
} else if ((node->children != NULL)
&& (node->type != XML_ENTITY_REF_NODE)) {
/* deep first */
node = node->children;
} else if (node->next != NULL) {
/* then siblings */
node = node->next;
} else {
/* go up to parents->next if needed */
while (node != NULL) {
if (node->parent != NULL) {
node = node->parent;
}
if (node->next != NULL) {
node = node->next;
break;
}
if (node->parent == NULL) {
node = NULL;
break;
}
}
}
}
return (0);
}
/**
* xmlShellDir:
* @ctxt: the shell context
* @arg: unused
* @node: a node
* @node2: unused
*
* Implements the XML shell function "dir"
* dumps informations about the node (namespace, attributes, content).
*
* Returns 0
*/
int
xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
if (!ctxt)
return (0);
if (node == NULL) {
fprintf(ctxt->output, "NULL\n");
return (0);
}
if ((node->type == XML_DOCUMENT_NODE) ||
(node->type == XML_HTML_DOCUMENT_NODE)) {
xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
} else if (node->type == XML_ATTRIBUTE_NODE) {
xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
} else {
xmlDebugDumpOneNode(ctxt->output, node, 0);
}
return (0);
}
/**
* xmlShellSetContent:
* @ctxt: the shell context
* @value: the content as a string
* @node: a node
* @node2: unused
*
* Implements the XML shell function "dir"
* dumps informations about the node (namespace, attributes, content).
*
* Returns 0
*/
static int
xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
char *value, xmlNodePtr node,
xmlNodePtr node2 ATTRIBUTE_UNUSED)
{
xmlNodePtr results;
xmlParserErrors ret;
if (!ctxt)
return (0);
if (node == NULL) {
fprintf(ctxt->output, "NULL\n");
return (0);
}
if (value == NULL) {
fprintf(ctxt->output, "NULL\n");
return (0);
}
ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
if (ret == XML_ERR_OK) {
if (node->children != NULL) {
xmlFreeNodeList(node->children);
node->children = NULL;
node->last = NULL;
}
xmlAddChildList(node, results);
} else {
fprintf(ctxt->output, "failed to parse content\n");
}
return (0);
}
#ifdef LIBXML_SCHEMAS_ENABLED
/**
* xmlShellRNGVa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -