📄 xmllint.c
字号:
endTimer("Parsing and validating"); else#endif#ifdef LIBXML_VALID_ENABLED if (valid) endTimer("Parsing and validating"); else#endif endTimer("Parsing"); }#ifdef LIBXML_VALID_ENABLED if (valid) { if (xmlTextReaderIsValid(reader) != 1) { xmlGenericError(xmlGenericErrorContext, "Document %s does not validate\n", filename); progresult = XMLLINT_ERR_VALID; } }#endif /* LIBXML_VALID_ENABLED */#ifdef LIBXML_SCHEMAS_ENABLED if (relaxng != NULL) { if (xmlTextReaderIsValid(reader) != 1) { fprintf(stderr, "%s fails to validate\n", filename); progresult = XMLLINT_ERR_VALID; } else { fprintf(stderr, "%s validates\n", filename); } }#endif /* * Done, cleanup and status */ xmlFreeTextReader(reader); if (ret != 0) { fprintf(stderr, "%s : failed to parse\n", filename); progresult = XMLLINT_ERR_UNCLASS; } } else { fprintf(stderr, "Unable to open %s\n", filename); progresult = XMLLINT_ERR_UNCLASS; }#ifdef LIBXML_PATTERN_ENABLED if (patstream != NULL) { xmlFreeStreamCtxt(patstream); patstream = NULL; }#endif#ifdef HAVE_SYS_MMAN_H if (memory) { xmlFreeParserInputBuffer(input); munmap((char *) base, info.st_size); close(fd); }#endif}static void walkDoc(xmlDocPtr doc) { xmlTextReaderPtr reader; int ret;#ifdef LIBXML_PATTERN_ENABLED xmlNodePtr root; const xmlChar *namespaces[22]; int i; xmlNsPtr ns; root = xmlDocGetRootElement(doc); for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) { namespaces[i++] = ns->href; namespaces[i++] = ns->prefix; } namespaces[i++] = NULL; namespaces[i++] = NULL; if (pattern != NULL) { patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict, 0, &namespaces[0]); if (patternc == NULL) { xmlGenericError(xmlGenericErrorContext, "Pattern %s failed to compile\n", pattern); progresult = XMLLINT_ERR_SCHEMAPAT; pattern = NULL; } } if (patternc != NULL) { patstream = xmlPatternGetStreamCtxt(patternc); if (patstream != NULL) { ret = xmlStreamPush(patstream, NULL, NULL); if (ret < 0) { fprintf(stderr, "xmlStreamPush() failure\n"); xmlFreeStreamCtxt(patstream); patstream = NULL; } } }#endif /* LIBXML_PATTERN_ENABLED */ reader = xmlReaderWalker(doc); if (reader != NULL) { if ((timing) && (!repeat)) { startTimer(); } ret = xmlTextReaderRead(reader); while (ret == 1) { if ((debug)#ifdef LIBXML_PATTERN_ENABLED || (patternc)#endif ) processNode(reader); ret = xmlTextReaderRead(reader); } if ((timing) && (!repeat)) { endTimer("walking through the doc"); } xmlFreeTextReader(reader); if (ret != 0) { fprintf(stderr, "failed to walk through the doc\n"); progresult = XMLLINT_ERR_UNCLASS; } } else { fprintf(stderr, "Failed to crate a reader from the document\n"); progresult = XMLLINT_ERR_UNCLASS; }#ifdef LIBXML_PATTERN_ENABLED if (patstream != NULL) { xmlFreeStreamCtxt(patstream); patstream = NULL; }#endif}#endif /* LIBXML_READER_ENABLED *//************************************************************************ * * * Tree Test processing * * * ************************************************************************/static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) { xmlDocPtr doc = NULL;#ifdef LIBXML_TREE_ENABLED xmlDocPtr tmp;#endif /* LIBXML_TREE_ENABLED */ if ((timing) && (!repeat)) startTimer(); #ifdef LIBXML_TREE_ENABLED if (filename == NULL) { if (generate) { xmlNodePtr n; doc = xmlNewDoc(BAD_CAST "1.0"); n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL); xmlNodeSetContent(n, BAD_CAST "abc"); xmlDocSetRootElement(doc, n); } }#endif /* LIBXML_TREE_ENABLED */#ifdef LIBXML_HTML_ENABLED#ifdef LIBXML_PUSH_ENABLED else if ((html) && (push)) { FILE *f;#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) f = fopen(filename, "rb");#else f = fopen(filename, "r");#endif if (f != NULL) { int res, size = 3; char chars[4096]; htmlParserCtxtPtr ctxt; /* if (repeat) */ size = 4096; res = fread(chars, 1, 4, f); if (res > 0) { ctxt = htmlCreatePushParserCtxt(NULL, NULL, chars, res, filename, XML_CHAR_ENCODING_NONE); while ((res = fread(chars, 1, size, f)) > 0) { htmlParseChunk(ctxt, chars, res, 0); } htmlParseChunk(ctxt, chars, 0, 1); doc = ctxt->myDoc; htmlFreeParserCtxt(ctxt); } fclose(f); } }#endif /* LIBXML_PUSH_ENABLED */ else if (html) { doc = htmlReadFile(filename, NULL, options); }#endif /* LIBXML_HTML_ENABLED */ else {#ifdef LIBXML_PUSH_ENABLED /* * build an XML tree from a string; */ if (push) { FILE *f; /* '-' Usually means stdin -<sven@zen.org> */ if ((filename[0] == '-') && (filename[1] == 0)) { f = stdin; } else {#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) f = fopen(filename, "rb");#else f = fopen(filename, "r");#endif } if (f != NULL) { int ret; int res, size = 1024; char chars[1024]; xmlParserCtxtPtr ctxt; /* if (repeat) size = 1024; */ res = fread(chars, 1, 4, f); if (res > 0) { ctxt = xmlCreatePushParserCtxt(NULL, NULL, chars, res, filename); xmlCtxtUseOptions(ctxt, options); while ((res = fread(chars, 1, size, f)) > 0) { xmlParseChunk(ctxt, chars, res, 0); } xmlParseChunk(ctxt, chars, 0, 1); doc = ctxt->myDoc; ret = ctxt->wellFormed; xmlFreeParserCtxt(ctxt); if (!ret) { xmlFreeDoc(doc); doc = NULL; } } } } else#endif /* LIBXML_PUSH_ENABLED */ if (testIO) { if ((filename[0] == '-') && (filename[1] == 0)) { doc = xmlReadFd(0, NULL, NULL, options); } else { FILE *f;#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) f = fopen(filename, "rb");#else f = fopen(filename, "r");#endif if (f != NULL) { if (rectxt == NULL) doc = xmlReadIO((xmlInputReadCallback) myRead, (xmlInputCloseCallback) myClose, f, filename, NULL, options); else doc = xmlCtxtReadIO(rectxt, (xmlInputReadCallback) myRead, (xmlInputCloseCallback) myClose, f, filename, NULL, options); } else doc = NULL; } } else if (htmlout) { xmlParserCtxtPtr ctxt; if (rectxt == NULL) ctxt = xmlNewParserCtxt(); else ctxt = rectxt; if (ctxt == NULL) { doc = NULL; } else { ctxt->sax->error = xmlHTMLError; ctxt->sax->warning = xmlHTMLWarning; ctxt->vctxt.error = xmlHTMLValidityError; ctxt->vctxt.warning = xmlHTMLValidityWarning; doc = xmlCtxtReadFile(ctxt, filename, NULL, options); if (rectxt == NULL) xmlFreeParserCtxt(ctxt); }#ifdef HAVE_SYS_MMAN_H } else if (memory) { int fd; struct stat info; const char *base; if (stat(filename, &info) < 0) return; if ((fd = open(filename, O_RDONLY)) < 0) return; base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ; if (base == (void *) MAP_FAILED) return; if (rectxt == NULL) doc = xmlReadMemory((char *) base, info.st_size, filename, NULL, options); else doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size, filename, NULL, options); munmap((char *) base, info.st_size);#endif#ifdef LIBXML_VALID_ENABLED } else if (valid) { xmlParserCtxtPtr ctxt = NULL; if (rectxt == NULL) ctxt = xmlNewParserCtxt(); else ctxt = rectxt; if (ctxt == NULL) { doc = NULL; } else { doc = xmlCtxtReadFile(ctxt, filename, NULL, options); if (ctxt->valid == 0) progresult = XMLLINT_ERR_RDFILE; if (rectxt == NULL) xmlFreeParserCtxt(ctxt); }#endif /* LIBXML_VALID_ENABLED */ } else { if (rectxt != NULL) doc = xmlCtxtReadFile(rectxt, filename, NULL, options); else doc = xmlReadFile(filename, NULL, options); } } /* * If we don't have a document we might as well give up. Do we * want an error message here? <sven@zen.org> */ if (doc == NULL) { progresult = XMLLINT_ERR_UNCLASS; return; } if ((timing) && (!repeat)) { endTimer("Parsing"); } /* * Remove DOCTYPE nodes */ if (dropdtd) { xmlDtdPtr dtd; dtd = xmlGetIntSubset(doc); if (dtd != NULL) { xmlUnlinkNode((xmlNodePtr)dtd); xmlFreeDtd(dtd); } }#ifdef LIBXML_XINCLUDE_ENABLED if (xinclude) { if ((timing) && (!repeat)) { startTimer(); } if (xmlXIncludeProcessFlags(doc, options) < 0) progresult = XMLLINT_ERR_UNCLASS; if ((timing) && (!repeat)) { endTimer("Xinclude processing"); } }#endif#ifdef LIBXML_DEBUG_ENABLED#ifdef LIBXML_XPATH_ENABLED /* * shell interaction */ if (shell) xmlShell(doc, filename, xmlShellReadline, stdout);#endif#endif#ifdef LIBXML_TREE_ENABLED /* * test intermediate copy if needed. */ if (copy) { tmp = doc; if (timing) { startTimer(); } doc = xmlCopyDoc(doc, 1); if (timing) { endTimer("Copying"); } if (timing) { startTimer(); } xmlFreeDoc(tmp); if (timing) { endTimer("Freeing original"); } }#endif /* LIBXML_TREE_ENABLED */#ifdef LIBXML_VALID_ENABLED if ((insert) && (!html)) { const xmlChar* list[256]; int nb, i; xmlNodePtr node; if (doc->children != NULL) { node = doc->children; while ((node != NULL) && (node->last == NULL)) node = node->next; if (node != NULL) { nb = xmlValidGetValidElements(node->last, NULL, list, 256); if (nb < 0) { fprintf(stderr, "could not get valid list of elements\n"); } else if (nb == 0) { fprintf(stderr, "No element can be inserted under root\n"); } else { fprintf(stderr, "%d element types can be inserted under root:\n", nb); for (i = 0;i < nb;i++) { fprintf(stderr, "%s\n", (char *) list[i]); } } } } }else#endif /* LIBXML_VALID_ENABLED */#ifdef LIBXML_READER_ENABLED if (walker) { walkDoc(doc); }#endif /* LIBXML_READER_ENABLED */#ifdef LIBXML_OUTPUT_ENABLED if (noout == 0) { int ret; /* * print it. */#ifdef LIBXML_DEBUG_ENABLED if (!debug) {#endif if ((timing) && (!repeat)) { startTimer(); }#ifdef LIBXML_HTML_ENABLED if ((html) && (!xmlout)) { if (compress) { htmlSaveFile(output ? output : "-", doc); } else if (encoding != NULL) { if ( format ) { htmlSaveFileFormat(output ? output : "-", doc, encoding, 1); } else { htmlSaveFileFormat(output ? output : "-", doc, encoding, 0); } } else if (format) { htmlSaveFileFormat(output ? output : "-", doc, NULL, 1); } else { FILE *out; if (output == NULL) out = stdout; else { out = fopen(output,"wb"); } if (out != NULL) { if (htmlDocDump(out, doc) < 0) progresult = XMLLINT_ERR_OUT; if (output != NULL) fclose(out); } else { fprintf(stderr, "failed to open %s\n", output); progresult = XMLLINT_ERR_OUT; } } if ((timing) && (!repeat)) { endTimer("Saving"); } } else#endif#ifdef LIBXML_C14N_ENABLED if (canonical) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -