00001
00002
00003 #include <vector>
00004 #include <string>
00005 #include "xmlUtil/Substitute.h"
00006 #include "xml/Dom.h"
00007 #include "xmlUtil/Arith.h"
00008 #include <dom/DOM_Element.hpp>
00009 #include <dom/DOM_TreeWalker.hpp>
00010 #include <dom/DOM_NamedNodeMap.hpp>
00011 #include <vector>
00012
00013 namespace xmlUtil {
00014
00015 Substitute::Substitute(DOM_Document doc, std::string suffix) :
00016 m_doc(doc), m_count(0), m_suffix(suffix) {
00017 m_suffixLen = suffix.size();
00018 }
00019
00020 int Substitute::execute(DOM_Element top) {
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 DOM_Element docElt = m_doc.getDocumentElement();
00040 if ((docElt.getAttribute("substituted")).equals(DOMString("true")))
00041 return 0;
00042
00043 if ((top.getAttribute("substituted")).equals(DOMString("true"))) return 0;
00044 DOM_Node curNode = top;
00045
00046
00047
00048
00049 unsigned long whatToShow = 1 << (DOM_Node::ELEMENT_NODE -1);
00050
00051 DOM_TreeWalker walker = m_doc.createTreeWalker(top, whatToShow, 0, 0);
00052 m_count = 0;
00053 while (curNode != DOM_Node() ) {
00054 sub(static_cast<DOM_Element &> (curNode));
00055 curNode = walker.nextNode();
00056 }
00057 DOM_Attr subNode = top.getAttributeNode("substituted");
00058 if (subNode != DOM_Attr()) {
00059 subNode.setValue("true");
00060 }
00061
00062 return m_count;
00063 }
00064
00065 void Substitute::sub(DOM_Element elt) {
00066 DOM_NamedNodeMap attMap = elt.getAttributes();
00067 int nAtt = attMap.getLength();
00068 int iAtt;
00069 std::vector<DOMString> toProcess;
00070 std::vector<unsigned> toProcessPos;
00071
00072
00073 for (iAtt = 0; iAtt <nAtt; iAtt++) {
00074 DOM_Node att = attMap.item(iAtt);
00075
00076 DOMString attString = att.getNodeName();
00077 std::string attName = std::string(xml::Dom::transToChar(attString));
00078
00079
00080 unsigned pos = attName.find(m_suffix, attName.size() - m_suffixLen);
00081 if (pos < attName.size()) {
00082 toProcess.push_back(attString);
00083 toProcessPos.push_back(pos);
00084 }
00085 }
00086
00087
00088 while (toProcess.size() > 0) {
00089 DOMString oldAttString = toProcess.back();
00090 std::string oldAttName =
00091 std::string(xml::Dom::transToChar(oldAttString));
00092 unsigned pos = toProcessPos.back();
00093 toProcess.pop_back();
00094 toProcessPos.pop_back();
00095 DOM_Element constElt =
00096 m_doc.getElementById(elt.getAttribute(oldAttString));
00097 if (constElt == DOM_Element() ) {
00098 m_notFound++;
00099 continue;
00100 }
00101
00102 DOMString val = constElt.getAttribute(DOMString("value"));
00103
00104
00105
00106
00107 if (val == DOMString()) {
00108 Arith curArith(constElt);
00109 double evalValue = curArith.evaluate();
00110 curArith.saveValue();
00111 val = constElt.getAttribute(DOMString("value"));
00112 }
00113
00114 std::string newAttName = oldAttName.erase(pos, oldAttName.size() );
00115 elt.setAttribute(DOMString(newAttName.c_str()), val);
00116 elt.removeAttribute(oldAttString);
00117 m_count++;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 }
00127 }