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

makeXmlForProg.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xmlUtil/src/makeXmlForProg.cxx,v 1.7 2002/04/05 18:25:18 jrb Exp $
00021 #include "xml/XmlParser.h"
00022 #include "xml/Dom.h"
00023 #include "xmlUtil/Source.h"
00024 #include "xmlUtil/Constants.h"
00025 #include "xmlUtil/Arith.h"
00026 #include "xmlUtil/Substitute.h"
00027 #include <dom/DOM_Element.hpp>
00028 #include <dom/DOM_NodeList.hpp>
00029 #include <dom/DOM_NamedNodeMap.hpp>
00030 #include <dom/DOM_DocumentType.hpp>
00031 
00032 #include <string>
00033 #include <iostream>
00034 #include <fstream>
00035 
00036 std::ostream *openOut(char * outfile);
00037 void outProlog(const DOM_DocumentType& doctype, std::ostream& out);
00038 
00039 const char chDoubleQ[2] = {0x22, 0x0};
00040 const std::string dquote(&chDoubleQ[0]);
00041 const std::string myId("$Id: makeXmlForProg.cxx,v 1.7 2002/04/05 18:25:18 jrb Exp $");
00042 
00043 // Can't literally put in the string we want or CVS will mess it up.
00044 // Instead make a copy of this template, replacing the # with $
00045 const std::string idTemplate("#Id: not committed $");
00046 
00053 int main(int argc, char* argv[]) {
00054   std::ostream *out;
00055   if (argc < 3) {  // instructions
00056     std::cout << "Required first argument is xml file to be parsed" 
00057               << std::endl;
00058     std::cout << "Required second argument is output file (- for stdout)"
00059               << std::endl;
00060     exit(0);
00061   }
00062 
00063   xml::XmlParser* parser = new xml::XmlParser();
00064   DOM_Document doc = parser->parse(argv[1]);
00065 
00066   if (doc == 0) {
00067     std::cout << "Document failed to parse correctly" << std::endl;
00068     exit(0);
00069   }
00070 
00071   // Else successful.   Open output
00072   out = openOut(argv[2]);
00073 
00074   DOM_Element docElt = doc.getDocumentElement();
00075   DOM_DocumentType  doctype = doc.getDoctype(); //  check for gdd?? 
00076   xmlUtil::Constants   *constants = new xmlUtil::Constants(doc);
00077 
00078   // prim processing
00079   constants->normalizePrimary();
00080 
00081   // Evaluate all constants.  Most would be evaluated even without
00082   // this step because of the use of Substitute, but if there happens
00083   // to be a constant not actually used in the <section> part of
00084   // the file, Substitute would not cause that constant to be evaluated.
00085   constants->evalConstants();
00086 
00087   // Search for all dimension and position references, substitute
00088   // value of referenced element.
00089   DOM_NodeList sections = docElt.getElementsByTagName(DOMString("section"));
00090   int nSec = sections.getLength();
00091   int iSec;
00092 
00093   xmlUtil::Substitute* sub = new xmlUtil::Substitute(doc);
00094 
00095   DOM_Node dict = (docElt.getElementsByTagName(DOMString("idDict"))).item(0);
00096   DOM_Element& dictElt = static_cast<DOM_Element &> (dict);
00097   int nDictSub = sub->execute(dictElt);
00098     std::cout << "#elements substituted for id dictionary:  " << nDictSub
00099               << std::endl;
00100 
00101   for (iSec = 0; iSec < nSec; iSec++) {
00102     int nSub;
00103     DOM_Node  secNode = sections.item(iSec);
00104     DOM_Element& secElt = static_cast<DOM_Element &> (secNode);
00105     nSub = sub->execute(secElt);
00106     std::cout << "#elements substituted for in this section:  " << nSub
00107               << std::endl;
00108   }
00109   
00110   // Get rid of any <derCategory> without the save attribute set to "true".
00111   // For the rest, prune element content from all <const> children;
00112   // it's no longer needed.
00113   DOM_Element derived = xml::Dom::findFirstChildByName(docElt, "constants");
00114   if (derived != DOM_Element() ) {
00115     derived = xml::Dom::findFirstChildByName(derived, "derived");
00116     
00117     if (derived != DOM_Element()) {
00118       DOM_NodeList cats = derived.getElementsByTagName("derCategory");
00119       int nCat = cats.getLength();
00120       int iCat;
00121       for (iCat=0; iCat < nCat; iCat++) {
00122         DOM_Node node = cats.item(iCat);
00123         DOM_Node attNode = (node.getAttributes()).getNamedItem("save");
00124         if ((attNode.getNodeValue()).equals(DOMString("false")) ) {  // remove
00125           DOM_Element& toPrune = static_cast<DOM_Element &>(node);
00126           xml::Dom::prune(toPrune);
00127           (toPrune.getParentNode()).removeChild(toPrune);
00128           // compensate for changing node list
00129           iCat--;  nCat--;
00130         }
00131       }
00132 
00133       // Now just get rid of all content for <const> children 
00134       // in remaining derived categories
00135       constants->pruneConstants();
00136     }
00137   }
00138 
00139   // Add a <source> child to the outer gdd element
00140   xmlUtil::Source * source = 
00141     new xmlUtil::Source(doc, "xmlUtil/v1/src/forProg.exe", myId.c_str());
00142   source->add();
00143   
00144   // Output the xml declaration and all the text in the DOCTYPE (see DOMPrint)
00145   outProlog(doctype, *out);
00146 
00147   // If have gdd element with CVSid attribute, null it out.  Don't have
00148   // a real CVS id until the file has been committed
00149   if (docElt.getAttribute("CVSid") != DOMString() ) {
00150     std::string noId(idTemplate);
00151     noId.replace(0, 1, "$");
00152     docElt.setAttribute("CVSid", noId.c_str());
00153   }
00154 
00155   // Finally output the elements
00156   // May want option to exclude comments here
00157   xml::Dom::prettyPrintElement(docElt, *out, "");
00158 
00159   delete parser;
00160   return(0);
00161 }
00162 
00167 std::ostream *openOut(char * outfile)
00168 {
00169   char  *hyphen = "-";
00170   std::ostream* out;
00171   
00172   if (*outfile == *hyphen) {
00173     out = &std::cout;
00174   }
00175   else {   // try to open file as ostream
00176     out = new std::ofstream(outfile);
00177   }
00178   return out;
00179 }
00180 
00185 void outProlog(const DOM_DocumentType& doctype, std::ostream& out) {
00186   // write the xml declaration: <?xml version="1.0" ?>
00187 
00188   out << "<?xml version=" << dquote << "1.0" << dquote << "?>" << std::endl;
00189   if (doctype != DOM_DocumentType()) {
00190     char* transcoded = xml::Dom::transToChar(doctype.getName());
00191     if (transcoded != 0) {
00192       //    out << "<!DOCTYPE " << xml::Dom::transToChar(doctype.getName()) << " ";
00193       out << "<!DOCTYPE " << transcoded << " ";
00194     }
00195     else 
00196     {
00197       std::cout << "Failed to transcode doctype " << std::endl;
00198       return;
00199     }
00200 
00201     DOMString id = doctype.getPublicId();
00202     if (id != 0)   {
00203       transcoded = xml::Dom::transToChar(id);
00204       //      out << " PUBLIC " << dquote << xml::Dom::transToChar(id) << dquote;
00205       if (transcoded != 0) {
00206         out << " PUBLIC " << dquote << transcoded << dquote;
00207       }
00208       else  {
00209         std::cout << "Failed to transcode public id " << std::endl;
00210         return;
00211       }
00212       id = doctype.getSystemId();
00213       if (id != 0) {
00214         transcoded = xml::Dom::transToChar(id);
00215         if (transcoded != 0) {
00216           out << " " << dquote << transcoded << dquote;
00217         }
00218         else  {
00219           std::cout << "Failed to transcode system id " << std::endl;
00220           return;
00221         }
00222       }
00223     }
00224     else {
00225       id = doctype.getSystemId();
00226       if (id != 0)   {
00227         //        out << " SYSTEM " << dquote << xml::Dom::transToChar(id) << dquote;
00228         transcoded = xml::Dom::transToChar(id);
00229         if (transcoded != 0) {
00230           out << " " << dquote << transcoded << dquote;
00231         }
00232         else  {
00233           std::cout << "Failed to transcode system id " << std::endl;
00234           return;
00235         }
00236       }
00237     }
00238     id = doctype.getInternalSubset(); 
00239     if (id !=0) {
00240       transcoded = xml::Dom::transToChar(id);
00241       if (transcoded != 0) {
00242         //        out << "[" << xml::Dom::transToChar(id) << "]";
00243         out << "[" << transcoded << "]";
00244       }
00245       else {
00246         std::cout << "Failed to transcode internal subset" << std::endl;
00247         return;
00248       }
00249     }
00250     out << ">" << std::endl;
00251   }
00252 }

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