00001
00019 #include "xml/XmlParser.h"
00020 #include "xml/Dom.h"
00021 #include "xmlUtil/Source.h"
00022 #include "xmlUtil/Arith.h"
00023 #include "xmlUtil/Constants.h"
00024 #include <dom/DOM_Element.hpp>
00025 #include <dom/DOM_NodeList.hpp>
00026 #include <dom/DOM_DocumentType.hpp>
00027
00028 #include <string>
00029 #include <iostream>
00030 #include <fstream>
00031
00032 std::ostream *openOut(char * outfile);
00033
00034 void outProlog(const DOM_DocumentType& doctype, std::ostream& out);
00035
00036 char * stripDollar(char *toStrip);
00037
00038 const char chDoubleQ[2] = {0x22, 0x0};
00039 const std::string dquote(&chDoubleQ[0]);
00040 const std::string myId("$Id: makeXmlForDoc.cxx,v 1.5 2002/04/05 18:25:18 jrb Exp $");
00041
00042
00043 const std::string idTemplate("#Id: not committed $");
00050 int main(int argc, char* argv[]) {
00051
00052 std::ostream *out;
00053 if (argc < 3) {
00054 std::cout << "Required first argument is xml file to be parsed"
00055 << std::endl;
00056 std::cout << "Required second argument is output file (- for stdout)"
00057 << std::endl;
00058 exit(0);
00059 }
00060
00061 xml::XmlParser* parser = new xml::XmlParser();
00062 DOM_Document doc = parser->parse(argv[1]);
00063
00064 if (doc == 0) {
00065 std::cout << "Document failed to parse correctly" << std::endl;
00066 exit(0);
00067 }
00068
00069
00070 out = openOut(argv[2]);
00071
00072 DOM_Element docElt = doc.getDocumentElement();
00073 DOM_DocumentType doctype = doc.getDoctype();
00074
00075
00076
00077
00078 xmlUtil::Constants *constants = new xmlUtil::Constants(doc);
00079 constants->evalConstants();
00080 constants->pruneConstants(true);
00081
00082
00083 DOM_NodeList dicts = docElt.getElementsByTagName(DOMString("idDict"));
00084
00085
00086 unsigned nDict = dicts.getLength();
00087 for (unsigned iDict = 0; iDict < nDict; iDict++) {
00088 DOM_Node dictNode = dicts.item(iDict);
00089 DOM_Element& dictElt = static_cast<DOM_Element &> (dictNode);
00090
00091 xml::Dom::prune(dictElt);
00092 (dictElt.getParentNode()).removeChild(dictElt);
00093 }
00094
00095
00096 DOM_NodeList sections = docElt.getElementsByTagName(DOMString("section"));
00097 unsigned nSec = sections.getLength();
00098 for (unsigned iSec = 0; iSec < nSec; iSec++) {
00099 DOM_Node secNode = sections.item(iSec);
00100 DOM_Element& secElt = static_cast<DOM_Element &> (secNode);
00101 xml::Dom::prune(secElt);
00102 (secElt.getParentNode()).removeChild(secElt);
00103
00104 }
00105
00106
00107
00108 xmlUtil::Source * source =
00109 new xmlUtil::Source(doc, "xmlUtil/v1/src/forDoc.exe", myId.c_str());
00110 source->add();
00111
00112
00113 outProlog(doctype, *out);
00114
00115
00116
00117 if (docElt.getAttribute("CVSid") != DOMString() ) {
00118 std::string noId(idTemplate);
00119 noId.replace(0, 1, "$");
00120 docElt.setAttribute("CVSid", noId.c_str());
00121 }
00122
00123
00124 xml::Dom::prettyPrintElement(docElt, *out, "");
00125
00126 delete parser;
00127 return(0);
00128 }
00129
00134 std::ostream *openOut(char * outfile)
00135 {
00136 char *hyphen = "-";
00137 std::ostream* out;
00138
00139 if (*outfile == *hyphen) {
00140 out = &std::cout;
00141 }
00142 else {
00143 out = new std::ofstream(outfile);
00144 }
00145 return out;
00146 }
00147
00152 void outProlog(const DOM_DocumentType& doctype, std::ostream& out) {
00153
00154
00155 out << "<?xml version=" << dquote << "1.0" << dquote << "?>" << std::endl;
00156 if (doctype != DOM_DocumentType()) {
00157
00158 out << "<!DOCTYPE " << xml::Dom::transToChar(doctype.getName()) << " ";
00159
00160 DOMString id = doctype.getPublicId();
00161 if (id != 0) {
00162 out << " PUBLIC " << dquote << xml::Dom::transToChar(id) << dquote;
00163 id = doctype.getSystemId();
00164 if (id != 0) {
00165 out << " " << dquote << xml::Dom::transToChar(id) << dquote;
00166 }
00167 }
00168 else {
00169 id = doctype.getSystemId();
00170 if (id != 0) {
00171 out << " SYSTEM " << dquote << xml::Dom::transToChar(id) << dquote;
00172 }
00173 }
00174 id = doctype.getInternalSubset();
00175 if (id !=0) {
00176 out << "[" << xml::Dom::transToChar(id) << "]";
00177 }
00178 out << ">" << std::endl;
00179 }
00180 }