diff options
author | Stefan Radomski <sradomski@mintwerk.de> | 2016-02-23 11:28:18 (GMT) |
---|---|---|
committer | Stefan Radomski <sradomski@mintwerk.de> | 2016-02-23 11:28:18 (GMT) |
commit | 7212d5a3dbbd2845d09df96b2c345132c8a24931 (patch) | |
tree | 194bf3525bc7b607013301b79dde6c9950ccbf2e /src/uscxml/dom | |
parent | 8e62f3801b98bf4b7f7f85b848b2fe6339c99162 (diff) | |
download | uscxml-7212d5a3dbbd2845d09df96b2c345132c8a24931.zip uscxml-7212d5a3dbbd2845d09df96b2c345132c8a24931.tar.gz uscxml-7212d5a3dbbd2845d09df96b2c345132c8a24931.tar.bz2 |
Some refactoring for improved compile times
Diffstat (limited to 'src/uscxml/dom')
-rw-r--r-- | src/uscxml/dom/DOMUtils.cpp | 288 | ||||
-rw-r--r-- | src/uscxml/dom/DOMUtils.h | 111 | ||||
-rw-r--r-- | src/uscxml/dom/NameSpacingParser.cpp | 71 | ||||
-rw-r--r-- | src/uscxml/dom/NameSpacingParser.h | 67 |
4 files changed, 537 insertions, 0 deletions
diff --git a/src/uscxml/dom/DOMUtils.cpp b/src/uscxml/dom/DOMUtils.cpp new file mode 100644 index 0000000..137d0ef --- /dev/null +++ b/src/uscxml/dom/DOMUtils.cpp @@ -0,0 +1,288 @@ +/** + * @file + * @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "uscxml/Common.h" +#include "uscxml/UUID.h" +#include "uscxml/dom/DOMUtils.h" +#include "uscxml/Convenience.h" + +#include <glog/logging.h> +#include <boost/algorithm/string.hpp> + +namespace uscxml { + +using namespace Arabica::XPath; +using namespace Arabica::DOM; + +bool DOMUtils::attributeIsTrue(const::std::string& value) { + return stringIsTrue(value.c_str()); +} + +std::string DOMUtils::idForNode(const Arabica::DOM::Node<std::string>& node) { + std::string nodeId; + std::string seperator; + Arabica::DOM::Node<std::string> curr = node; + while(curr) { + switch (curr.getNodeType()) { + case Arabica::DOM::Node_base::ELEMENT_NODE: { + Arabica::DOM::Element<std::string> elem = Arabica::DOM::Element<std::string>(curr); + if (HAS_ATTR(elem, "id") && !UUID::isUUID(ATTR(elem, "id"))) { + std::string elementId = ATTR(elem, "id"); + boost::replace_all(elementId, ".", "_"); + boost::replace_all(elementId, ",", "_"); + nodeId.insert(0, elementId + seperator); + seperator = "_"; + return nodeId; + } else { + Arabica::DOM::Node<std::string> sibling = curr.getPreviousSibling(); + int index = 0; + while(sibling) { + if (sibling.getNodeType() == Arabica::DOM::Node_base::ELEMENT_NODE) { + if (iequals(TAGNAME_CAST(sibling), TAGNAME(elem))) { + index++; + } + } + sibling = sibling.getPreviousSibling(); + } + nodeId.insert(0, TAGNAME(elem) + toStr(index) + seperator); + seperator = "_"; + } + break; + } + case Arabica::DOM::Node_base::DOCUMENT_NODE: + return nodeId; + } + + curr = curr.getParentNode(); + } + return nodeId; +} + +std::string DOMUtils::xPathForNode(const Arabica::DOM::Node<std::string>& node, const std::string& ns) { + std::string xPath; + std::string nsPrefix; + + if (ns.size() > 0) { + nsPrefix = ns + ":"; + } + + if (!node || node.getNodeType() != Arabica::DOM::Node_base::ELEMENT_NODE) + return xPath; + + Arabica::DOM::Node<std::string> curr = node; + while(curr) { + switch (curr.getNodeType()) { + case Arabica::DOM::Node_base::ELEMENT_NODE: { + Arabica::DOM::Element<std::string> elem = Arabica::DOM::Element<std::string>(curr); + if (HAS_ATTR(elem, "id") && !UUID::isUUID(ATTR(elem, "id"))) { + // we assume ids to be unique and return immediately + if (ns == "*") { + xPath.insert(0, "//*[local-name() = \"" + TAGNAME(elem) + "\"][@id=\"" + ATTR(elem, "id") + "\"]"); + } else { + xPath.insert(0, "//" + nsPrefix + TAGNAME(elem) + "[@id=\"" + ATTR(elem, "id") + "\"]"); + } + return xPath; + } else { + // check previous siblings to count our index + Arabica::DOM::Node<std::string> sibling = curr.getPreviousSibling(); + int index = 1; // xpath indices start at 1 + while(sibling) { + if (sibling.getNodeType() == Arabica::DOM::Node_base::ELEMENT_NODE) { + if (iequals(TAGNAME_CAST(sibling), TAGNAME(elem))) { + index++; + } + } + sibling = sibling.getPreviousSibling(); + } + if (ns == "*") { + xPath.insert(0, "/*[local-name() = \"" + TAGNAME(elem) + "\"][" + toStr(index) + "]"); + } else { + xPath.insert(0, "/" + nsPrefix + TAGNAME(elem) + "[" + toStr(index) + "]"); + } + } + break; + } + case Arabica::DOM::Node_base::DOCUMENT_NODE: + return xPath; + default: + LOG(ERROR) << "Only nodes of type element supported for now"; + return ""; + break; + } + curr = curr.getParentNode(); + } + return xPath; +} + +NodeSet<std::string> DOMUtils::inPostFixOrder(const std::set<std::string>& elements, + const Element<std::string>& root, + const bool includeEmbeddedDoc) { + NodeSet<std::string> nodes; + inPostFixOrder(elements, root, includeEmbeddedDoc, nodes); + return nodes; +} + +void DOMUtils::inPostFixOrder(const std::set<std::string>& elements, + const Element<std::string>& root, + const bool includeEmbeddedDoc, + NodeSet<std::string>& nodes) { + NodeList<std::string> children = root.getChildNodes(); + for (size_t i = 0; i < children.getLength(); i++) { + if (children.item(i).getNodeType() != Node_base::ELEMENT_NODE) + continue; + Arabica::DOM::Element<std::string> childElem(children.item(i)); + if (!includeEmbeddedDoc && LOCALNAME(childElem) == "scxml") + continue; + inPostFixOrder(elements, childElem, includeEmbeddedDoc, nodes); + + } + for (size_t i = 0; i < children.getLength(); i++) { + if (children.item(i).getNodeType() != Node_base::ELEMENT_NODE) + continue; + Arabica::DOM::Element<std::string> childElem(children.item(i)); + if (!includeEmbeddedDoc && LOCALNAME(childElem) == "scxml") + continue; + + if (elements.find(TAGNAME(childElem)) != elements.end()) { + nodes.push_back(childElem); + } + } +} + +NodeSet<std::string> DOMUtils::inDocumentOrder(const std::set<std::string>& elements, + const Element<std::string>& root, + const bool includeEmbeddedDoc) { + NodeSet<std::string> nodes; + inDocumentOrder(elements, root, includeEmbeddedDoc, nodes); + return nodes; +} + +void DOMUtils::inDocumentOrder(const std::set<std::string>& elements, + const Element<std::string>& root, + const bool includeEmbeddedDoc, + NodeSet<std::string>& nodes) { + if (elements.find(TAGNAME(root)) != elements.end()) { + nodes.push_back(root); + } + + NodeList<std::string> children = root.getChildNodes(); + for (size_t i = 0; i < children.getLength(); i++) { + if (children.item(i).getNodeType() != Node_base::ELEMENT_NODE) + continue; + if (!includeEmbeddedDoc && LOCALNAME_CAST(children.item(i)) == "scxml") + continue; + Arabica::DOM::Element<std::string> childElem(children.item(i)); + inDocumentOrder(elements, childElem, includeEmbeddedDoc, nodes); + } +} + +std::list<Arabica::DOM::Node<std::string> > DOMUtils::getElementsByType(const Arabica::DOM::Node<std::string>& root, Arabica::DOM::Node_base::Type type) { + std::list<Arabica::DOM::Node<std::string> > result; + std::list<Arabica::DOM::Node<std::string> > stack; + std::list<Arabica::DOM::Node<std::string> >::iterator stackIter; + + if (!root) + return result; + + stack.push_back(root); + while(stack.size() > 0) { +// for(stackIter = stack.begin(); stackIter != stack.end(); stackIter++) { +// std::cout << stackIter->getNodeType() << " " << stackIter->getLocalName() << " " << stackIter->getNodeValue() << std::endl; +// } +// std::cout << std::endl; + + Arabica::DOM::Node<std::string> currNode = stack.back(); + if (currNode.hasChildNodes()) { + stack.push_back(currNode.getFirstChild()); + continue; + } + + // roll back stack and pop everyone without next sibling + do { + currNode = stack.back(); + if (currNode.getNodeType() == type) + result.push_back(currNode); + stack.pop_back(); + if (currNode.getNextSibling()) { + stack.push_back(currNode.getNextSibling()); + break; + } + } while(stack.size() > 0); + } + return result; +} + + +NodeSet<std::string> DOMUtils::filterChildElements(const std::string& tagName, const NodeSet<std::string>& nodeSet, bool recurse) { + NodeSet<std::string> filteredChildElems; + for (unsigned int i = 0; i < nodeSet.size(); i++) { + filteredChildElems.push_back(filterChildElements(tagName, nodeSet[i], recurse)); + } + return filteredChildElems; +} + +NodeSet<std::string> DOMUtils::filterChildElements(const std::string& tagName, const Node<std::string>& node, bool recurse) { + NodeSet<std::string> filteredChildElems; + + if (!node) + return filteredChildElems; + + NodeList<std::string> childs = node.getChildNodes(); + for (unsigned int i = 0; i < childs.getLength(); i++) { + if (childs.item(i).getNodeType() != Node_base::ELEMENT_NODE) + continue; + // std::cerr << TAGNAME(childs.item(i)) << std::endl; + if(iequals(TAGNAME_CAST(childs.item(i)), tagName)) { + filteredChildElems.push_back(childs.item(i)); + } + if (recurse) { + filteredChildElems.push_back(filterChildElements(tagName, childs.item(i), recurse)); + } + } + return filteredChildElems; +} + + +NodeSet<std::string> DOMUtils::filterChildType(const Node_base::Type type, const NodeSet<std::string>& nodeSet, bool recurse) { + NodeSet<std::string> filteredChildType; + for (unsigned int i = 0; i < nodeSet.size(); i++) { + filteredChildType.push_back(filterChildType(type, nodeSet[i], recurse)); + } + return filteredChildType; +} + +NodeSet<std::string> DOMUtils::filterChildType(const Node_base::Type type, const Node<std::string>& node, bool recurse) { + NodeSet<std::string> filteredChildTypes; + + if (!node) + return filteredChildTypes; + + NodeList<std::string> childs = node.getChildNodes(); + for (unsigned int i = 0; i < childs.getLength(); i++) { + if (childs.item(i).getNodeType() == type) + filteredChildTypes.push_back(childs.item(i)); + if (recurse) { + filteredChildTypes.push_back(filterChildType(type, childs.item(i), recurse)); + } + } + return filteredChildTypes; +} + + +}
\ No newline at end of file diff --git a/src/uscxml/dom/DOMUtils.h b/src/uscxml/dom/DOMUtils.h new file mode 100644 index 0000000..5c61f6d --- /dev/null +++ b/src/uscxml/dom/DOMUtils.h @@ -0,0 +1,111 @@ +/** + * @file + * @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef DOMUTILS_H_WK0WAEA7 +#define DOMUTILS_H_WK0WAEA7 + +#include "uscxml/Common.h" +#include <vector> +#include <iostream> +#include <DOM/Document.hpp> +#include <DOM/io/Stream.hpp> // operator<< for nodes +#include <XPath/XPath.hpp> + +#define TAGNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getTagName() +#define LOCALNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getLocalName() +#define ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttribute(attr) +#define ATTR_NODE_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttributeNode(attr) +#define HAS_ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).hasAttribute(attr) + +#define TAGNAME(elem) elem.getTagName() +#define LOCALNAME(elem) elem.getLocalName() +#define ATTR(elem, attr) elem.getAttribute(attr) +#define ATTR_NODE(elem, attr) elem.getAttributeNode(attr) +#define HAS_ATTR(elem, attr) elem.hasAttribute(attr) + +namespace uscxml { + +class USCXML_API DOMUtils { +public: + + static std::string xPathForNode(const Arabica::DOM::Node<std::string>& node, const std::string& ns = ""); + static std::list<Arabica::DOM::Node<std::string> > getElementsByType(const Arabica::DOM::Node<std::string>& root, Arabica::DOM::Node_base::Type type); + static std::string idForNode(const Arabica::DOM::Node<std::string>& node); + // deprecated, use stringIsTrue from Convenience.h instead + DEPRECATED static bool attributeIsTrue(const::std::string& value); + + static Arabica::XPath::NodeSet<std::string> inPostFixOrder(const std::string& element, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc = false) { + std::set<std::string> elements; + elements.insert(element); + return inPostFixOrder(elements, root, includeEmbeddedDoc); + } + + static Arabica::XPath::NodeSet<std::string> inPostFixOrder(const std::set<std::string>& elements, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc = false); + + static Arabica::XPath::NodeSet<std::string> inDocumentOrder(const std::string& element, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc = false) { + std::set<std::string> elements; + elements.insert(element); + return inDocumentOrder(elements, root, includeEmbeddedDoc); + } + + static Arabica::XPath::NodeSet<std::string> inDocumentOrder(const std::set<std::string>& elements, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc = false); + + static Arabica::XPath::NodeSet<std::string> filterChildElements(const std::string& tagname, + const Arabica::DOM::Node<std::string>& node, + bool recurse = false); + + static Arabica::XPath::NodeSet<std::string> filterChildElements(const std::string& tagName, + const Arabica::XPath::NodeSet<std::string>& nodeSet, + bool recurse = false); + + static Arabica::XPath::NodeSet<std::string> filterChildType(const Arabica::DOM::Node_base::Type type, + const Arabica::DOM::Node<std::string>& node, + bool recurse = false); + + static Arabica::XPath::NodeSet<std::string> filterChildType(const Arabica::DOM::Node_base::Type type, + const Arabica::XPath::NodeSet<std::string>& nodeSet, + bool recurse = false); + +protected: + static void inPostFixOrder(const std::set<std::string>& elements, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc, + Arabica::XPath::NodeSet<std::string>& nodes); + + static void inDocumentOrder(const std::set<std::string>& elements, + const Arabica::DOM::Element<std::string>& root, + const bool includeEmbeddedDoc, + Arabica::XPath::NodeSet<std::string>& nodes); + + +}; + + +} + + +#endif /* end of include guard: DOMUTILS_H_WK0WAEA7 */ diff --git a/src/uscxml/dom/NameSpacingParser.cpp b/src/uscxml/dom/NameSpacingParser.cpp new file mode 100644 index 0000000..0e26b55 --- /dev/null +++ b/src/uscxml/dom/NameSpacingParser.cpp @@ -0,0 +1,71 @@ +/** + * @file + * @author 2016 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "uscxml/dom/NameSpacingParser.h" +#include <uscxml/Convenience.h> +#include <glog/logging.h> +#include <SAX/helpers/InputSourceResolver.hpp> + +namespace uscxml { + +using namespace Arabica::XPath; +using namespace Arabica::DOM; + +NameSpacingParser NameSpacingParser::fromFile(const std::string& file) { + Arabica::SAX::InputSource<std::string> inputSource; + inputSource.setSystemId(file); + return fromInputSource(inputSource); +} + +NameSpacingParser NameSpacingParser::fromXML(const std::string& xml) { + std::stringstream* ss = new std::stringstream(); + (*ss) << xml; + // we need an auto_ptr for arabica to assume ownership + std::auto_ptr<std::istream> ssPtr(ss); + Arabica::SAX::InputSource<std::string> inputSource; + inputSource.setByteStream(ssPtr); + return fromInputSource(inputSource); +} + +NameSpacingParser NameSpacingParser::fromInputSource(Arabica::SAX::InputSource<std::string>& source) { + NameSpacingParser parser; + if(!parser.parse(source) || !parser.getDocument().hasChildNodes()) { + if(parser._errorHandler.errorsReported()) { +// LOG(ERROR) << "could not parse input:"; +// LOG(ERROR) << parser._errorHandler.errors() << std::endl; + } else { + Arabica::SAX::InputSourceResolver resolver(source, Arabica::default_string_adaptor<std::string>()); + if (!resolver.resolve()) { + LOG(ERROR) << source.getSystemId() << ": no such file"; + } + } + } + return parser; +} + +NameSpacingParser::NameSpacingParser() { + setErrorHandler(_errorHandler); +} + +void NameSpacingParser::startPrefixMapping(const std::string& prefix, const std::string& uri) { + nameSpace.insert(std::make_pair(uri, prefix)); +} + + +}
\ No newline at end of file diff --git a/src/uscxml/dom/NameSpacingParser.h b/src/uscxml/dom/NameSpacingParser.h new file mode 100644 index 0000000..1aa85a2 --- /dev/null +++ b/src/uscxml/dom/NameSpacingParser.h @@ -0,0 +1,67 @@ +/** + * @file + * @author 2016 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef NAMESPACINGPARSER_H_BD6902F6 +#define NAMESPACINGPARSER_H_BD6902F6 + +#include "uscxml/Common.h" +#include <iostream> +#include <XPath/XPath.hpp> +#include <DOM/io/Stream.hpp> // operator<< for nodes +#include <DOM/SAX2DOM/SAX2DOM.hpp> +#include <SAX/helpers/DefaultHandler.hpp> +#include <SAX/helpers/CatchErrorHandler.hpp> + +namespace uscxml { + +class ScriptEntityResolver : public Arabica::SAX::EntityResolver<std::string> { + virtual InputSourceT resolveEntity(const std::string& publicId, const std::string& systemId) { + Arabica::SAX::InputSource<std::string> is; + return is; + } +}; + +class USCXML_API NameSpacingParser : public Arabica::SAX2DOM::Parser<std::string> { +public: + NameSpacingParser(); + NameSpacingParser(const NameSpacingParser& other) {} + static NameSpacingParser fromFile(const std::string& file); + static NameSpacingParser fromXML(const std::string& xml); + static NameSpacingParser fromInputSource(Arabica::SAX::InputSource<std::string>& source); + + void startPrefixMapping(const std::string& prefix, const std::string& uri); + + std::map<std::string, std::string> nameSpace; + + virtual bool errorsReported() { + return _errorHandler.errorsReported(); + } + + virtual const std::string& errors() { + return _errorHandler.errors(); + } + +private: + Arabica::SAX::CatchErrorHandler<std::string> _errorHandler; +}; + +} + + +#endif /* end of include guard: NAMESPACINGPARSER_H_BD6902F6 */ |