/** * Prepare XSLT stylesheet based on command line options */ int selPrepareXslt(xmlDocPtr style, selOptionsPtr ops, xmlChar *ns_arr[], int start, int argc, char **argv) { int i, t, ns, use_inputfile = 0, use_value_of = 0; xmlNodePtr root, root_template = NULL; xmlNsPtr xslns; xmlBufferPtr attr_buf; root = xmlNewDocRawNode(style, NULL, BAD_CAST "stylesheet", NULL); xmlDocSetRootElement(style, root); xmlNewProp(root, BAD_CAST "version", BAD_CAST "1.0"); xslns = xmlNewNs(root, XSLT_NAMESPACE, BAD_CAST "xsl"); xmlSetNs(root, xslns); ns = 0; while(ns_arr[ns]) { xmlNewNs(root, ns_arr[ns+1], xmlStrlen(ns_arr[ns])?ns_arr[ns] : NULL); ns += 2; } cleanupNSArr(ns_arr); { xmlNodePtr output; output = xmlNewChild(root, xslns, BAD_CAST "output", NULL); xmlNewProp(output, BAD_CAST "omit-xml-declaration", BAD_CAST ((ops->no_omit_decl)?"no":"yes")); xmlNewProp(output, BAD_CAST "indent", BAD_CAST ((ops->indent)?"yes":"no")); if (ops->encoding) xmlNewProp(output, BAD_CAST "encoding", ops->encoding); if (ops->outText) xmlNewProp(output, BAD_CAST "method", BAD_CAST "text"); } for (i = start, t = 0; i < argc; i++) if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--template")) t++; /* * At least one -t option must be found */ if (t == 0) { fprintf(stderr, "error in arguments:"); fprintf(stderr, " no -t or --template options found\n"); exit(EXIT_BAD_ARGS); } if (t > 1) root_template = xmlNewChild(root, xslns, BAD_CAST "template", NULL); t = 0; i = start; while(i < argc) { if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--template")) { xmlNodePtr call_template, template; int lastTempl = 0; t++;
/** * Output document */ static void edOutput(const char* filename, const XmlEdAction* ops, int ops_count, const edOptions* g_ops) { xmlDocPtr doc; int save_options = #if LIBXML_VERSION >= 20708 (g_ops->noblanks? 0 : XML_SAVE_WSNONSIG) | #endif (g_ops->preserveFormat? 0 : XML_SAVE_FORMAT) | (g_ops->omit_decl? XML_SAVE_NO_DECL : 0); int read_options = (g_ops->nonet? XML_PARSE_NONET : 0); xmlSaveCtxtPtr save; doc = xmlReadFile(filename, NULL, read_options); if (!doc) { cleanupNSArr(ns_arr); xmlCleanupParser(); xmlCleanupGlobals(); exit(EXIT_BAD_FILE); } edProcess(doc, ops, ops_count); /* avoid getting ASCII CRs in UTF-16/UCS-(2,4) text */ if ((xmlStrcasestr(doc->encoding, BAD_CAST "UTF") == 0 && xmlStrcasestr(doc->encoding, BAD_CAST "16") == 0) || (xmlStrcasestr(doc->encoding, BAD_CAST "UCS") == 0 && (xmlStrcasestr(doc->encoding, BAD_CAST "2") == 0 || xmlStrcasestr(doc->encoding, BAD_CAST "4") == 0))) { set_stdout_binary(); } save = xmlSaveToFilename(g_ops->inplace? filename : "-", NULL, save_options); xmlSaveDoc(save, doc); xmlSaveClose(save); xmlFreeDoc(doc); }
/** * This is the main function for 'edit' option */ int edMain(int argc, char **argv) { int i, ops_count, max_ops_count = 8, n, start = 0; XmlEdAction* ops = xmlMalloc(sizeof(XmlEdAction) * max_ops_count); static edOptions g_ops; int nCount = 0; if (argc < 3) edUsage(argv[0], EXIT_BAD_ARGS); edInitOptions(&g_ops); start = edParseOptions(&g_ops, argc, argv); parseNSArr(ns_arr, &nCount, argc-start, argv+start); /* * Parse command line and fill array of operations */ ops_count = 0; i = start + nCount; while (i < argc) { const char *arg = nextArg(argv, &i); if (arg[0] == '-') { if (ops_count >= max_ops_count) { max_ops_count *= 2; ops = xmlRealloc(ops, sizeof(XmlEdAction) * max_ops_count); } ops[ops_count].type = XML_UNDEFINED; if (!strcmp(arg, "-d") || !strcmp(arg, "--delete")) { ops[ops_count].op = XML_ED_DELETE; ops[ops_count].arg1 = nextArg(argv, &i); ops[ops_count].arg2 = 0; } else if (!strcmp(arg, "--var")) { ops[ops_count].op = XML_ED_VAR; ops[ops_count].arg1 = nextArg(argv, &i); ops[ops_count].arg2 = nextArg(argv, &i); } else if (!strcmp(arg, "-m") || !strcmp(arg, "--move")) { ops[ops_count].op = XML_ED_MOVE; ops[ops_count].arg1 = nextArg(argv, &i); ops[ops_count].arg2 = nextArg(argv, &i); } else if (!strcmp(arg, "-u") || !strcmp(arg, "--update")) { ops[ops_count].op = XML_ED_UPDATE; ops[ops_count].arg1 = nextArg(argv, &i); ops[ops_count].type = parseNextArg(argv, &i, OPT_VAL_OR_EXP); ops[ops_count].arg2 = nextArg(argv, &i); } else if (!strcmp(arg, "-r") || !strcmp(arg, "--rename")) { ops[ops_count].op = XML_ED_RENAME; ops[ops_count].arg1 = nextArg(argv, &i); ops[ops_count].type = parseNextArg(argv, &i, OPT_JUST_VAL); ops[ops_count].arg2 = nextArg(argv, &i); } else if (!strcmp(arg, "-i") || !strcmp(arg, "--insert")) { parseInsertionArgs(XML_ED_INSERT, &ops[ops_count], argv, &i); } else if (!strcmp(arg, "-a") || !strcmp(arg, "--append")) { parseInsertionArgs(XML_ED_APPEND, &ops[ops_count], argv, &i); } else if (!strcmp(arg, "-s") || !strcmp(arg, "--subnode")) { parseInsertionArgs(XML_ED_SUBNODE, &ops[ops_count], argv, &i); } else { fprintf(stderr, "Warning: unrecognized option '%s'\n", arg); } ops_count++; } else { i--; /* it was a filename, we didn't use it */ break; } } xmlKeepBlanksDefault(0); if ((!g_ops.noblanks) || g_ops.preserveFormat) xmlKeepBlanksDefault(1); if (i >= argc) { edOutput("-", ops, ops_count, &g_ops); } for (n=i; n<argc; n++) { edOutput(argv[n], ops, ops_count, &g_ops); } xmlFree(ops); cleanupNSArr(ns_arr); xmlCleanupParser(); xmlCleanupGlobals(); return 0; }