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

IdConversion.cxx

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/xmlUtil/src/id/IdConversion.cxx,v 1.7 2002/04/05 18:26:44 jrb Exp $
00002 
00003 #include "xmlUtil/id/IdConversion.h"
00004 #include "xmlUtil/id/IdOperation.h"
00005 #include "xml/Dom.h"
00006 #include <dom/DOMString.hpp>
00007 #include "xmlUtil/id/IdOpTruncate.h"
00008 #include "xmlUtil/id/IdOpDisappear.h"
00009 #include "xmlUtil/id/IdOpCompress.h"
00010 
00011 namespace xmlUtil {
00012   IdConversion::IdConversion() : m_path(0), m_condition(0) {
00013     m_op = new IdOperation();
00014   }
00015 
00016   IdConversion::IdConversion(DOM_Element conversion) {
00017 
00018     // Get first child; invoke private function to build path
00019     DOM_Element child = xml::Dom::getFirstChildElement(conversion);
00020     makePath(child);
00021 
00022     // Get next child; save field name in condition component
00023     child = xml::Dom::getSiblingElement(child);
00024     m_condition = new std::string(xml::Dom::getAttribute(child, "name"));
00025 
00026     // Get next child;  build new op component.
00027     child = xml::Dom::getSiblingElement(child);
00028     buildOp(child);
00029   }
00030 
00031   bool IdConversion::inDomain(const NamedId& inputId) {
00032     return inputId.hasSubpath(*m_path);
00033   }
00034 
00035   bool IdConversion::satisfies(const NamedId& inputId) {
00036     if (m_condition == 0) return true;
00037 
00038     return (inputId.hasField(*m_condition) >= 0);
00039   }
00040     
00041   NamedId * IdConversion::convert(const NamedId& inputId) {
00042     if (!inDomain(inputId)) return 0;
00043     return internalConvert(inputId);
00044   }
00045 
00046   NamedId * IdConversion::internalConvert(const NamedId& inputId) {
00047     if (satisfies(inputId)) {  // let the operation do its thing
00048       return m_op->convert(inputId);
00049     }
00050     else { // clone input and return
00051       return new NamedId(inputId);
00052     }
00053   }
00054 
00055   void IdConversion::makePath(const DOM_Element& pathElt) {
00056     
00057     // "path" consists of a list of fields.  Fields have 
00058     // a required attribute "name".  Save its value.
00059     m_path = new NameSeq;
00060     DOM_Element child = xml::Dom::getFirstChildElement(pathElt);
00061 
00062     while (child != DOM_Element()) {
00063       m_path->push_back(new std::string(xml::Dom::getAttribute(child, "name")));
00064       child = xml::Dom::getSiblingElement(child);
00065     }
00066 
00067   }
00068 
00069   // Could maybe do something more elegant than a switch, but it would
00070   // take quite a bit of machinery and would only be worthwhile if
00071   // the set of ops was expected to change often.
00072   void IdConversion::buildOp(const DOM_Element& opElt) {
00073     DOMString opType = opElt.getTagName();
00074 
00075     if (opType.equals("truncate"))
00076     {
00077       m_op = new IdOpTruncate(opElt);
00078     }
00079     else if (opType.equals("disappear")) {
00080       m_op = new IdOpDisappear(opElt);
00081     }
00082     else if (opType.equals(DOMString("compress"))) {
00083       m_op = new IdOpCompress(opElt);
00084     }
00085     else { // default to identity operation, implemented by base class
00086       m_op = new IdOperation(opElt);
00087     }
00088   }
00089 
00090   // return true if our path is subpath of given conversion's path
00091   bool IdConversion::subpathOf(const IdConversion& other) const {
00092     unsigned int ourLen = m_path->size();
00093     if (ourLen > other.m_path->size() ) return false;
00094 
00095     for (unsigned int ix = 0; ix < ourLen; ix++) {
00096       if ((*(other.m_path))[ix]->compare((*(*m_path)[ix])) ) return false;
00097     }
00098     return true;
00099   }
00100 
00101   std::ostream& operator<<(std::ostream& s, const IdConversion& convers) {
00102     s << (*(convers.m_op)) << std::endl << " Path: " << (*(convers.m_path)) << 
00103       " Condition: hasField " << (*(convers.m_condition));
00104     return s;
00105   }
00106 
00107 }

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