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

eval.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xmlUtil/src/eval.cxx,v 1.4 2002/07/23 20:01:20 jrb Exp $
00017 #include "xml/XmlParser.h"
00018 #include "xml/Dom.h"
00019 #include "xmlUtil/Arith.h"
00020 #include "xmlUtil/Substitute.h"
00021 #include "xmlUtil/Constants.h"
00022 #include "xmlUtil/Source.h"
00023 #include <dom/DOM_Element.hpp>
00024 #include <dom/DOM_NodeList.hpp>
00025 #include <dom/DOM_DocumentType.hpp>
00026 
00027 #include <string>
00028 #include <iostream>
00029 #include <fstream>
00030 
00031 std::ostream *openOut(char * outfile);
00032 void addSourceElt(DOM_Document doc);
00033 void outProlog(const DOM_DocumentType& doctype, std::ostream& out);
00034 char * stripDollar(char *toStrip);
00035 
00036 const char chDoubleQ = 0x22;
00037 const std::string dquote(&chDoubleQ);
00038 const std::string myId("$Id: eval.cxx,v 1.4 2002/07/23 20:01:20 jrb Exp $");
00039 
00046 int main(int argc, char* argv[]) {
00047   std::ostream *out;
00048   if (argc < 3) {  // instructions
00049     std::cout << "Required first argument is xml file to be parsed" 
00050               << std::endl;
00051     std::cout << "Required second argument is output file (- for stdout)"
00052               << std::endl;
00053     exit(0);
00054   }
00055 
00056   xml::XmlParser* parser = new xml::XmlParser();
00057   DOM_Document doc = parser->parse(argv[1]);
00058 
00059   if (doc == 0) {
00060     std::cout << "Document failed to parse correctly" << std::endl;
00061     exit(0);
00062   }
00063 
00064   // Else successful.   Open output
00065   out = openOut(argv[2]);
00066 
00067   DOM_Element docElt = doc.getDocumentElement();
00068   DOM_DocumentType  doctype = doc.getDoctype(); //  check for gdd?? 
00069 
00070   // prim processing
00071   xmlUtil::Constants *constants = new xmlUtil::Constants(doc);
00072   constants->normalizePrimary();
00073 
00074   // No longer need the evaluate step since substitution will
00075   // evaluate any constant which has not yet been evaluated.
00076 
00077   // Search for all dimension and position references, substitute
00078   // value of referenced element.
00079   DOM_NodeList sections = docElt.getElementsByTagName(DOMString("section"));
00080   int nSec = sections.getLength();
00081   int iSec;
00082 
00083   xmlUtil::Substitute* sub = new xmlUtil::Substitute(doc);
00084 
00085   for (iSec = 0; iSec < nSec; iSec++) {
00086     int nSub;
00087     DOM_Node  secNode = sections.item(iSec);
00088     DOM_Element& secElt = static_cast<DOM_Element &> (secNode);
00089     nSub = sub->execute(secElt);
00090     std::cout << "#elements substituted for in this section:  " << nSub
00091               << std::endl;
00092   }
00093 
00094   // In case there were no sections to substitute, do eval here
00095   constants->evalConstants();
00096 
00097   // Get rid of the whole <derived> part of <constants>.  No longer needed.
00098   /*
00099   DOM_Element derived;
00100   if (docElt.getTagName().equals(DOMString("constants"))) {
00101     derived = xml::Dom::findFirstChildByName(docElt, "derived");
00102   }
00103   else {  
00104     derived = xml::Dom::findFirstChildByName(docElt, "constants");
00105     if (derived != DOM_Element() ) {
00106       derived = xml::Dom::findFirstChildByName(derived, "derived");
00107     }
00108   }
00109 
00110 
00111   if (derived != DOM_Element()) {
00112     xml::Dom::prune(derived);
00113     (derived.getParentNode()).removeChild(derived);
00114   }
00115   */
00116 
00117   // Add a <source> child to the outer gdd element
00118   xmlUtil::Source *source = 
00119     new xmlUtil::Source(doc, "xmlUtil/v1/src/eval.exe", "$Id: eval.cxx,v 1.4 2002/07/23 20:01:20 jrb Exp $");
00120   source->add();
00121   
00122   // Output the xml declaration and all the text in the DOCTYPE (see DOMPrint)
00123   outProlog(doctype, *out);
00124 
00125   // Finally output the elements
00126   // May want option to exclude comments here
00127   xml::Dom::prettyPrintElement(docElt, *out, "");
00128 
00129   delete parser;
00130   return(0);
00131 }
00132 
00137 std::ostream *openOut(char * outfile)
00138 {
00139   char  *hyphen = "-";
00140   std::ostream* out;
00141   
00142   if (*outfile == *hyphen) {
00143     out = &std::cout;
00144   }
00145   else {   // try to open file as ostream
00146     out = new std::ofstream(outfile);
00147   }
00148   return out;
00149 }
00150 
00155 void outProlog(const DOM_DocumentType& doctype, std::ostream& out) {
00156   // write the xml declaration: <?xml version="1.0" ?>
00157 
00158   out << "<?xml version=" << dquote << "1.0" << dquote << "?>" << std::endl;
00159   if (doctype != DOM_DocumentType()) {
00160 
00161     out << "<!DOCTYPE " << xml::Dom::transToChar(doctype.getName()) << " ";
00162 
00163     DOMString id = doctype.getPublicId();
00164     if (id != 0)   {
00165       out << " PUBLIC " << dquote << xml::Dom::transToChar(id) << dquote;
00166       id = doctype.getSystemId();
00167       if (id != 0) {
00168         out << " " << dquote << xml::Dom::transToChar(id) << dquote;
00169       }
00170     }
00171     else {
00172       id = doctype.getSystemId();
00173       if (id != 0)   {
00174         out << " SYSTEM " << dquote << xml::Dom::transToChar(id) << dquote;
00175       }
00176     }
00177     id = doctype.getInternalSubset(); 
00178     if (id !=0) {
00179       out << "[" << xml::Dom::transToChar(id) << "]";
00180     }
00181     out << ">" << std::endl;
00182   }
00183 }
00184 
00185 
00186 
00187 
00192 /*
00193 void  addSourceElt(DOM_Document doc) {
00194   DOM_Element source = doc.createElement("source");
00195   DOM_NodeList gdds = doc.getElementsByTagName("gdd");
00196   // must be precisely one <gdd> element
00197   DOM_Node gddNode = gdds.item(0);
00198   DOM_Element& gdd = static_cast<DOM_Element &> (gddNode);
00199 
00200   DOMString idAtt = gdd.getAttribute("CVSid");
00201   const char * raw   = xml::Dom::transToChar(idAtt);
00202   unsigned   len = strlen(raw);
00203   char* myRaw = new char[len+1];
00204   strcpy(myRaw, raw);
00205 
00206   const  char *strippedRaw = stripDollar(myRaw);
00207   DOMString newAtt = DOMString(strippedRaw);
00208 
00209   source.setAttribute("inputId", newAtt);
00210 
00211   // And similarly for our own id.  Make a mutable copy.
00212   len = myId.size();
00213   char * creatorId = new char[len+1];
00214   strcpy(creatorId, myId.c_str());
00215 
00216   const char *creatorStripped = stripDollar(creatorId);
00217   source.setAttribute(DOMString("creatorId"), 
00218                       DOMString(creatorStripped));
00219 
00220   DOM_Text    sourceText = 
00221     doc.createTextNode("This file automatically created by xml/src/eval.cxx");
00222 
00223   source.appendChild(sourceText);
00224 
00225   delete [] creatorId;
00226   delete [] myRaw;
00227   // Finally need to insert this node as the first child of gdd
00228   gdd.insertBefore(source, gdd.getFirstChild());
00229 }
00230 */
00231 
00238 /*
00239 char * stripDollar(char* toStrip) {
00240   const char dollar = '$';
00241 
00242   char   nothing = 0;
00243   unsigned len = strlen(toStrip);
00244 
00245   if (dollar == toStrip[len - 1]) {
00246     len--;
00247     toStrip[len] = nothing;
00248   }
00249   if (dollar == toStrip[0]) {
00250     return (toStrip + 1);
00251   }
00252   return toStrip;
00253 }
00254 */
00255 
00256 
00257 
00258 
00259 
00260 

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