00001
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
00019 DOM_Element child = xml::Dom::getFirstChildElement(conversion);
00020 makePath(child);
00021
00022
00023 child = xml::Dom::getSiblingElement(child);
00024 m_condition = new std::string(xml::Dom::getAttribute(child, "name"));
00025
00026
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)) {
00048 return m_op->convert(inputId);
00049 }
00050 else {
00051 return new NamedId(inputId);
00052 }
00053 }
00054
00055 void IdConversion::makePath(const DOM_Element& pathElt) {
00056
00057
00058
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
00070
00071
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 {
00086 m_op = new IdOperation(opElt);
00087 }
00088 }
00089
00090
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 }