00001
00002
00003 #include "xmlUtil/id/IdConversion.h"
00004 #include "xmlUtil/id/IdConverter.h"
00005 #include "xmlUtil/id/IdDictMan.h"
00006 #include "xmlUtil/id/IdDict.h"
00007 #include <dom/DOM_Element.hpp>
00008 #include "xml/Dom.h"
00009 #include <algorithm>
00010 #include "xmlUtil/id/IdConverterLessThan.h"
00011
00012 namespace xmlUtil {
00013
00014 IdDictMan * IdConverter::dictMan = 0;
00015
00016 IdConverter::IdConverter(DOM_Element elt) : m_consistent(UNKNOWN),
00017 m_sorted(NO) {
00018 if (!dictMan) dictMan = IdDictMan::getPointer();
00019
00020
00021
00022
00023
00024
00025
00026 DOM_Element child = xml::Dom::getFirstChildElement(elt);
00027
00028 if ( (child.getTagName()).equals(DOMString("constants")) ) {
00029 child = xml::Dom::getSiblingElement(child);
00030 }
00031 while ( (child.getTagName()).equals(DOMString("idDict")) ) {
00032 IdDict* dict = new IdDict(child);
00033 dictMan->registerDict(dict);
00034 child = xml::Dom::getSiblingElement(child);
00035 }
00036
00037
00038
00039 std::string dictName = xml::Dom::getAttribute(elt, "fromDict");
00040 if (dictName.size() > 0 ) {
00041 inputDictName = new std::string(dictName);
00042 inputDict = dictMan->findDict(dictName);
00043 }
00044 dictName = xml::Dom::getAttribute(elt, "toDict");
00045 if (dictName.size() > 0 ) {
00046 outputDictName = new std::string(dictName);
00047 outputDict = dictMan->findDict(dictName);
00048 }
00049
00050
00051 while (child != DOM_Element() ) {
00052 IdConversion *conv = new IdConversion(child);
00053 m_convCol.push_back(conv);
00054 child = xml::Dom::getSiblingElement(child);
00055 }
00056
00057
00058 isConsistent();
00059 }
00060
00061 IdConverter::~IdConverter() {
00062 m_convCol.clear();
00063 }
00064
00065
00066 void IdConverter::sortConvs() {
00067 if (m_sorted != YES) {
00068 ConversionIt start = m_convCol.begin(), last = m_convCol.end();
00069 IdConverterLessThan weakOrder;
00070 std::sort(start, last, weakOrder);
00071 m_sorted = YES;
00072 }
00073 }
00074
00075 bool IdConverter::isConsistent() {
00076 if (m_consistent == UNKNOWN) {
00077 sortConvs();
00078
00079
00080 unsigned int ix, jx;
00081 for (ix = 0; ix < (m_convCol.size() - 1); ix++ ) {
00082 for (jx = ix + 1; jx < (m_convCol.size()); jx++) {
00083
00084
00085
00086 if (m_convCol[ix]->subpathOf(*(m_convCol[jx]))) {
00087 m_consistent = NO;
00088 return m_consistent;
00089 }
00090 }
00091 }
00092 m_consistent = YES;
00093 }
00094 return m_consistent;
00095 }
00096
00097 NamedId *IdConverter::convert(const NamedId *in) const {
00098 unsigned int ix;
00099
00100 if (m_consistent == NO) return 0;
00101
00102
00103 for (ix = 0; ix < m_convCol.size(); ix++) {
00104 if ((m_convCol[ix])->inDomain(*in)) {
00105 return (m_convCol[ix]->convert(*in));
00106 }
00107 }
00108
00109 return new NamedId(*in);
00110 }
00111
00112
00113 Identifier *IdConverter::convert(const Identifier *inIdent) const {
00114 if (!inputDict) return 0;
00115
00116 if (m_consistent == NO) return 0;
00117
00118 NamedId *inNamed = inputDict->getNamedId(*inIdent);
00119 if (inNamed == 0) return 0;
00120
00121 NamedId *outNamed = convert(inNamed);
00122
00123 Identifier *converted = outNamed->stripNames();
00124 delete inNamed;
00125 delete outNamed;
00126 return converted;
00127 }
00128
00129 std::ostream& IdConverter::displayConversions(std::ostream& s) {
00130 Conversions::const_iterator it = m_convCol.begin();
00131
00132 while (it != m_convCol.end() ) {
00133 s << (**it) << std::endl;
00134 ++it;
00135 }
00136 return s;
00137 }
00138 }
00139