Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

makeXmlForDoc.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xmlUtil/src/makeXmlForDoc.cxx,v 1.5 2002/04/05 18:25:18 jrb Exp $
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 // Can't literally put in the string we want or CVS will mess it up.
00042 // Instead make a copy of this template, replacing the # with $
00043 const std::string idTemplate("#Id: not committed $");
00050 int main(int argc, char* argv[]) {
00051 
00052   std::ostream *out;
00053   if (argc < 3) {  // instructions
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   // Else successful.   Open output
00070   out = openOut(argv[2]);
00071 
00072   DOM_Element docElt = doc.getDocumentElement();
00073   DOM_DocumentType  doctype = doc.getDoctype(); //  check for gdd?? 
00074 
00075   // For each const child of a derCategory
00076   //   evaluate it, saving value
00077   //   delete all children except <notes>
00078   xmlUtil::Constants *constants = new xmlUtil::Constants(doc);
00079   constants->evalConstants();
00080   constants->pruneConstants(true);  
00081 
00082   // Delete any id dictionaries
00083   DOM_NodeList dicts = docElt.getElementsByTagName(DOMString("idDict"));
00084   //  DOM_Node dictNode = dicts.item(0);
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   // Delete all sections
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     //    secNode = toCome;
00104   }
00105 
00106 
00107   // Add a <source> child to the outer gdd element
00108   xmlUtil::Source * source = 
00109     new xmlUtil::Source(doc, "xmlUtil/v1/src/forDoc.exe", myId.c_str());
00110   source->add();
00111   
00112   // Output the xml declaration and all the text in the DOCTYPE (see DOMPrint)
00113   outProlog(doctype, *out);
00114 
00115   // If have gdd element with CVSid attribute, null it out.  Don't have
00116   // a real CVS id until the file has been committed
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   // Finally output the elements
00123   // May want option to exclude comments here
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 {   // try to open file as ostream
00143     out = new std::ofstream(outfile);
00144   }
00145   return out;
00146 }
00147 
00152 void outProlog(const DOM_DocumentType& doctype, std::ostream& out) {
00153   // write the xml declaration: <?xml version="1.0" ?>
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 }

Generated on Wed Oct 16 14:02:48 2002 by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001