diff options
author | Stefan Radomski <github@mintwerk.de> | 2017-01-30 23:12:06 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2017-01-30 23:12:06 (GMT) |
commit | f678b755216a7ea21acec0c8e51a6698719ef776 (patch) | |
tree | 68b9c762d2da279860387d4d4619fda098da980c /src/uscxml/util | |
parent | 7659ef059fa31c780f7c4e0fb028b9fb5b400030 (diff) | |
download | uscxml-f678b755216a7ea21acec0c8e51a6698719ef776.zip uscxml-f678b755216a7ea21acec0c8e51a6698719ef776.tar.gz uscxml-f678b755216a7ea21acec0c8e51a6698719ef776.tar.bz2 |
Introduced constants for XML names
Diffstat (limited to 'src/uscxml/util')
-rw-r--r-- | src/uscxml/util/DOM.cpp | 10 | ||||
-rw-r--r-- | src/uscxml/util/DOM.h | 115 | ||||
-rw-r--r-- | src/uscxml/util/Predicates.cpp | 20 |
3 files changed, 96 insertions, 49 deletions
diff --git a/src/uscxml/util/DOM.cpp b/src/uscxml/util/DOM.cpp index be6012b..d089807 100644 --- a/src/uscxml/util/DOM.cpp +++ b/src/uscxml/util/DOM.cpp @@ -63,8 +63,8 @@ std::string DOMUtils::idForNode(const DOMNode* node) { switch (curr->getNodeType()) { case DOMNode::ELEMENT_NODE: { const DOMElement* elem = dynamic_cast<const DOMElement*>(curr); - if (HAS_ATTR(elem, "id")) { - std::string elementId = ATTR(elem, "id"); + if (HAS_ATTR(elem, kXMLCharId)) { + std::string elementId = ATTR(elem, kXMLCharId); std::replace( elementId.begin(), elementId.end(), '.', '_'); std::replace( elementId.begin(), elementId.end(), ',', '_'); @@ -114,12 +114,12 @@ std::string DOMUtils::xPathForNode(const DOMNode* node, const std::string& ns) { switch (curr->getNodeType()) { case DOMNode::ELEMENT_NODE: { const DOMElement* elem = dynamic_cast<const DOMElement*>(curr); - if (HAS_ATTR(elem, "id")) { + if (HAS_ATTR(elem, kXMLCharId)) { // we assume ids to be unique and return immediately if (ns == "*") { - xPath.insert(0, "//*[local-name() = \"" + TAGNAME(elem) + "\"][@id=\"" + ATTR(elem, "id") + "\"]"); + xPath.insert(0, "//*[local-name() = \"" + TAGNAME(elem) + "\"][@id=\"" + ATTR(elem, kXMLCharId) + "\"]"); } else { - xPath.insert(0, "//" + nsPrefix + TAGNAME(elem) + "[@id=\"" + ATTR(elem, "id") + "\"]"); + xPath.insert(0, "//" + nsPrefix + TAGNAME(elem) + "[@id=\"" + ATTR(elem, kXMLCharId) + "\"]"); } return xPath; } else { diff --git a/src/uscxml/util/DOM.h b/src/uscxml/util/DOM.h index 49dc4de..9697eb8 100644 --- a/src/uscxml/util/DOM.h +++ b/src/uscxml/util/DOM.h @@ -28,9 +28,9 @@ #include <xercesc/util/XMLString.hpp> #include <xercesc/dom/DOM.hpp> -#define HAS_ATTR(elem, attr) (elem)->hasAttribute(X(attr)) +#define HAS_ATTR(elem, attr) (elem)->hasAttribute(attr) #define HAS_ATTR_CAST(elem, attr) HAS_ATTR(static_cast<const DOMElement*>(elem), attr) -#define ATTR(elem, attr) std::string(X((elem)->getAttribute(X(attr)))) +#define ATTR(elem, attr) std::string(X((elem)->getAttribute(attr))) #define ATTR_CAST(elem, attr) ATTR(static_cast<const DOMElement*>(elem), attr) #define TAGNAME(elem) std::string(X((elem)->getTagName())) #define TAGNAME_CAST(elem) TAGNAME(static_cast<const DOMElement*>(elem)) @@ -139,12 +139,6 @@ public : _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm); _deallocOther = true; } - void operator=(X const &other) { // did we maybe leak before? - - _localForm = other._localForm; - _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm); - _deallocOther = true; - } X(const XMLCh* const toTranscode) { @@ -167,6 +161,15 @@ public : } X(const char* const fromTranscode) { + // this is most unfortunate but needed with static XMLChars :( + if (!_xercesIsInit) { + try { + ::xercesc_3_1::XMLPlatformUtils::Initialize(); + _xercesIsInit = true; + } catch (const XERCESC_NS::XMLException& toCatch) { + throw ("Cannot initialize XercesC: " + X(toCatch.getMessage()).str()); + } + } // Call the private transcoding method _localForm = fromTranscode; @@ -198,11 +201,29 @@ public : return _localForm; } - operator const XMLCh* () { + const operator const XMLCh* () const { assert(_unicodeForm != NULL); // constructor with XMLCh return _unicodeForm; } + void operator=(X const &other) { + _localForm = other._localForm; + _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm); + _deallocOther = true; + } + + bool operator==(const X& other) const { + return (_unicodeForm == other._unicodeForm) != 0; + } + + bool operator!=(const X& other) const { + return !(_unicodeForm == other._unicodeForm); + } + + bool operator<(const X& other) const { + return XERCESC_NS::XMLString::compareString(_unicodeForm, other._unicodeForm); + } + operator bool () { return _localForm.size() > 0; } @@ -218,6 +239,7 @@ private: bool _deallocOther; std::string _localForm; XMLCh* _unicodeForm; + bool _xercesIsInit = false; }; #else @@ -293,31 +315,56 @@ private: #endif -#if 0 -static const X kElementScxmlName = X("scxml"); -static const X kElementStateName = X("state"); -static const X kElementParallelName = X("parallel"); -static const X kElementTransitionName = X("transition"); -static const X kElementInitialName = X("initial"); -static const X kElementFinalName = X("final"); -static const X kElementOnEntryName = X("onentry"); -static const X kElementOnExitName = X("onexit"); -static const X kElementHistoryName = X("history"); - -static const X kElementRaiseName = X("raise"); -static const X kElementIfName = X("if"); -static const X kElementElseIfName = X("elseif"); -static const X kElementElseName = X("else"); -static const X kElementForEachName = X("foreach"); -static const X kElementLogName = X("log"); - -static const X kElementDataModelName = X("datamodel"); -static const X kElementDataName = X("data"); -static const X kElementAssignName = X("assign"); -static const X kElementContentName = X("content"); -static const X kElementParamName = X("param"); -static const X kElementScriptName = X("script"); -#endif +static const X kXMLCharScxml = X("scxml"); +static const X kXMLCharState = X("state"); +static const X kXMLCharParallel = X("parallel"); +static const X kXMLCharTransition = X("transition"); +static const X kXMLCharInitial = X("initial"); +static const X kXMLCharFinal = X("final"); +static const X kXMLCharOnEntry = X("onentry"); +static const X kXMLCharOnExit = X("onexit"); +static const X kXMLCharHistory = X("history"); + +static const X kXMLCharRaise = X("raise"); +static const X kXMLCharIf = X("if"); +static const X kXMLCharElseIf = X("elseif"); +static const X kXMLCharElse = X("else"); +static const X kXMLCharForEach = X("foreach"); +static const X kXMLCharLog = X("log"); + +static const X kXMLCharDataModel = X("datamodel"); +static const X kXMLCharData = X("data"); +static const X kXMLCharAssign = X("assign"); +static const X kXMLCharContent = X("content"); +static const X kXMLCharParam = X("param"); +static const X kXMLCharScript = X("script"); + +static const X kXMLCharInvokeId = X("invokeid"); +static const X kXMLCharId = X("id"); +static const X kXMLCharIdLocation = X("idlocation"); +static const X kXMLCharEvent = X("event"); +static const X kXMLCharEventExpr = X("eventexpr"); +static const X kXMLCharTarget = X("target"); +static const X kXMLCharTargetExpr = X("targetexpr"); +static const X kXMLCharSource = X("src"); +static const X kXMLCharSourceExpr = X("srcexpr"); +static const X kXMLCharType = X("type"); +static const X kXMLCharTypeExpr = X("typeexpr"); +static const X kXMLCharDelay = X("delay"); +static const X kXMLCharDelayExpr = X("delayexpr"); +static const X kXMLCharSendId = X("sendid"); +static const X kXMLCharSendIdExpr = X("sendidexpr"); +static const X kXMLCharCond = X("cond"); +static const X kXMLCharLocation = X("location"); +static const X kXMLCharArray = X("array"); +static const X kXMLCharItem = X("item"); +static const X kXMLCharIndex = X("index"); +static const X kXMLCharLabel = X("label"); +static const X kXMLCharExpr = X("expr"); +static const X kXMLCharNameList = X("namelist"); +static const X kXMLCharAutoForward = X("autoforward"); +static const X kXMLCharName = X("name"); +static const X kXMLCharBinding = X("binding"); USCXML_API std::ostream& operator<< (std::ostream& os, const X& xmlString); USCXML_API std::ostream& operator<< (std::ostream& os, const XERCESC_NS::DOMNode& node); diff --git a/src/uscxml/util/Predicates.cpp b/src/uscxml/util/Predicates.cpp index af47c68..c4effdb 100644 --- a/src/uscxml/util/Predicates.cpp +++ b/src/uscxml/util/Predicates.cpp @@ -146,7 +146,7 @@ std::list<DOMElement*> getProperAncestors(const DOMElement* s1, const DOMElement std::list<DOMElement*> getExitSet(const DOMElement* transition, const DOMElement* root) { std::list<DOMElement*> statesToExit; - if (HAS_ATTR(transition, "target")) { + if (HAS_ATTR(transition, kXMLCharTarget)) { DOMElement* domain = getTransitionDomain(transition, root); if (domain == NULL) return statesToExit; @@ -207,7 +207,7 @@ bool isFinal(const DOMElement* state) { std::string localName = LOCALNAME(state); if (iequals("final", localName)) return true; - if (HAS_ATTR(state, "final") && iequals("true", ATTR(state, "final"))) + if (HAS_ATTR(state, kXMLCharFinal) && iequals("true", ATTR(state, kXMLCharFinal))) return true; return false; } @@ -259,8 +259,8 @@ bool isCompound(const DOMElement* state) { std::list<DOMElement*> getTargetStates(const DOMElement* transition, const DOMElement* root) { std::list<DOMElement*> targetStates; - std::string targetId = ATTR(transition, "target"); - std::list<std::string> targetIds = tokenize(ATTR(transition, "target")); +// std::string targetId = ATTR(transition, kXMLCharTarget); + std::list<std::string> targetIds = tokenize(ATTR(transition, kXMLCharTarget)); for (auto targetIter = targetIds.begin(); targetIter != targetIds.end(); targetIter++) { DOMElement* state = getState(*targetIter, root); @@ -277,7 +277,7 @@ DOMElement* getTransitionDomain(const DOMElement* transition, const DOMElement* if (tStates.size() == 0) { return NULL; } - std::string transitionType = (HAS_ATTR(transition, "type") ? ATTR(transition, "type") : "external"); + std::string transitionType = (HAS_ATTR(transition, kXMLCharType) ? ATTR(transition, kXMLCharType) : "external"); DOMElement* source = getSourceState(transition); if (iequals(transitionType, "internal") && isCompound(source)) { @@ -318,7 +318,7 @@ DOMElement* getState(const std::string& stateId, const DOMElement* root) { // std::cout << *curr; - if (HAS_ATTR(curr, "id") && ATTR(curr, "id") == stateId) + if (HAS_ATTR(curr, kXMLCharId) && ATTR(curr, kXMLCharId) == stateId) return (DOMElement*)curr; std::list<DOMElement*> children = getChildStates(curr, false); @@ -354,15 +354,15 @@ std::list<DOMElement*> getInitialStates(const DOMElement* state, const DOMElemen if (isCompound(state)) { // initial attribute at element - if (HAS_ATTR(state, "initial")) { - return getStates(tokenize(ATTR(state, "initial")), root); + if (HAS_ATTR(state, kXMLCharInitial)) { + return getStates(tokenize(ATTR(state, kXMLCharInitial)), root); } // initial element as child std::list<DOMElement*> initElems = DOMUtils::filterChildElements(XML_PREFIX(state).str() + "initial", state); if(initElems.size() > 0 ) { std::list<DOMElement*> initTrans = DOMUtils::filterChildElements(XML_PREFIX(initElems.front()).str() + "transition", initElems.front()); - if (initTrans.size() > 0 && HAS_ATTR(initTrans.front(),"target")) { + if (initTrans.size() > 0 && HAS_ATTR(initTrans.front(), kXMLCharTarget)) { return getTargetStates(initTrans.front(), root); } return std::list<DOMElement*>(); @@ -481,4 +481,4 @@ bool areFromSameMachine(const DOMNode* n1, const DOMNode* n2) { return p1 == p2; } -}
\ No newline at end of file +} |