Exemple #1
0
void MMISessionManager::received(const NewContextRequest& mmiEvent, const std::string& token) {

	// copy DOM from prototype instance
	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	Arabica::DOM::Document<std::string> newDOM = domFactory.createDocument("", "", 0);
	newDOM.appendChild(newDOM.importNode(_protoInterpreter.getDocument().getDocumentElement(), true));

	// instantiate new interpreter and name it after the context
	std::string contextId = UUID::getUUID();
	Interpreter interpreter = Interpreter::fromDOM(newDOM);
	interpreter.setFactory(_factory);
	interpreter.setName(contextId);
	interpreter.setNameSpaceInfo(_protoInterpreter.getNameSpaceInfo());
	interpreter.setBaseURI(_protoInterpreter.getBaseURI().asString());

	MMISession* session;

	if (token.length() > 0) {
		session = new MMISessionManager::CometMMISession();
		static_cast<MMISessionManager::CometMMISession*>(session)->_token = token;
		_tokens[token] = session;
	} else {
		// todo handle other cases
		session = new MMISessionManager::CometMMISession();
	}
	session->_interpreter = interpreter;

	// save interpreter
	_sessions[contextId] = session;

	interpreter.start();
	interpreter.receive(mmiEvent);

}
   void test4()
   {
      Arabica::DOM::Element<string_type, string_adaptor> root;
      {
        Arabica::DOM::Document<string_type, string_adaptor> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
        root = d.createElement(SA::construct_from_utf8("root"));
        d.appendChild(root);

        root.appendChild(d.createElement(SA::construct_from_utf8("child1")));
        root.appendChild(d.createElement(SA::construct_from_utf8("child2")));
      }
    
      Arabica::DOM::Node<string_type, string_adaptor> c1 = root.getFirstChild();
      Arabica::DOM::Node<string_type, string_adaptor> c2 = root.getLastChild();

      try {
        c1.purgeChild(c2);
      }
      catch(Arabica::DOM::DOMException& e)
      {
        assert(e.code() == Arabica::DOM::DOMException::NOT_FOUND_ERR);
      } // catch

  
      assert(c1 != 0);
      assert(c2 != 0);

      assert(c1.getNextSibling() == c2);
      assert(c2.getPreviousSibling() == c1);
    } // test4
    void test3()
    {
      Arabica::DOM::Element<string_type, string_adaptor> root;
      {
        Arabica::DOM::Document<string_type, string_adaptor> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
        root = d.createElement(SA::construct_from_utf8("root"));
        d.appendChild(root);

        root.appendChild(d.createElement(SA::construct_from_utf8("child1")));
	root.appendChild(d.createElement(SA::construct_from_utf8("child2")));
      }

      Arabica::DOM::Node<string_type, string_adaptor> c = root.getFirstChild();
      assert(c.getNextSibling() == root.getLastChild());

      root.purgeChild(c);

      assert(c == 0);
      assert(root.getLastChild() == root.getFirstChild());
   } // test3
    void test2()
    {
      Arabica::DOM::Element<string_type, string_adaptor> root;
      {
        Arabica::DOM::Document<string_type, string_adaptor> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
        root = d.createElement(SA::construct_from_utf8("root"));
        d.appendChild(root);

        assert(root.getPreviousSibling() == 0);
        assert(root.getNextSibling() == 0);

        Arabica::DOM::ProcessingInstruction<string_type, string_adaptor> p = d.createProcessingInstruction(SA::construct_from_utf8("target"), SA::construct_from_utf8("data"));
        d.insertBefore(p, root);
      }

      Arabica::DOM::Node<string_type, string_adaptor> n = root.getPreviousSibling();
      Arabica::DOM::ProcessingInstruction<string_type, string_adaptor> p = Arabica::DOM::ProcessingInstruction<string_type, string_adaptor>(n);
      assert(p.getTarget() == SA::construct_from_utf8("target"));
      assert(p.getData() == SA::construct_from_utf8("data"));
    } // test2
    void test1()
    {
      Arabica::DOM::Document<string_type, string_adaptor> d = factory.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
      Arabica::DOM::Element<string_type, string_adaptor> root = d.createElement(SA::construct_from_utf8("root"));
      d.appendChild(root);

      assert(root.getPreviousSibling() == 0);
      assert(root.getNextSibling() == 0);

      Arabica::DOM::ProcessingInstruction<string_type, string_adaptor> p = d.createProcessingInstruction(SA::construct_from_utf8("target"), SA::construct_from_utf8("data"));
      d.insertBefore(p, root);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == root);
      assert(root.getPreviousSibling() == p);
      assert(root.getNextSibling() == 0);

      Arabica::DOM::ProcessingInstruction<string_type, string_adaptor> p2 = d.createProcessingInstruction(SA::construct_from_utf8("target"), SA::construct_from_utf8("data"));
      d.insertBefore(p2, root);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == p2);
      assert(p2.getPreviousSibling() == p);
      assert(p2.getNextSibling() == root);
      assert(root.getPreviousSibling() == p2);
      assert(root.getNextSibling() == 0);

      d.removeChild(p2);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == root);
      assert(p2.getPreviousSibling() == 0);
      assert(p2.getNextSibling() == 0);
      assert(root.getPreviousSibling() == p);
      assert(root.getNextSibling() == 0);

      d.insertBefore(p2, root);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == p2);
      assert(p2.getPreviousSibling() == p);
      assert(p2.getNextSibling() == root);
      assert(root.getPreviousSibling() == p2);
      assert(root.getNextSibling() == 0);

      d.removeChild(p);
      assert(d.getFirstChild() == p2);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == 0);
      assert(p2.getPreviousSibling() == 0);
      assert(p2.getNextSibling() == root);
      assert(root.getPreviousSibling() == p2);
      assert(root.getNextSibling() == 0);

      d.replaceChild(p, p2);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == root);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == root);
      assert(p2.getPreviousSibling() == 0);
      assert(p2.getNextSibling() == 0);
      assert(root.getPreviousSibling() == p);
      assert(root.getNextSibling() == 0);

      d.appendChild(p2);
      assert(d.getFirstChild() == p);
      assert(d.getLastChild() == p2);
      assert(p.getPreviousSibling() == 0);
      assert(p.getNextSibling() == root);
      assert(root.getPreviousSibling() == p);
      assert(root.getNextSibling() == p2);
      assert(p2.getPreviousSibling() == root);
      assert(p2.getNextSibling() == 0);
    } // test1
Exemple #6
0
int main(int argc, char** argv) {

	factory_ = Arabica::SimpleDOM::DOMImplementation<string_type, string_adaptor>::getDOMImplementation();
	document_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
	root_ = document_.createElement("root");
	document_.appendChild(root_);
	assert(root_);

	element1_ = document_.createElement(SA::construct_from_utf8("child1"));
	element2_ = document_.createElement(SA::construct_from_utf8("child2"));
	element3_ = document_.createElement(SA::construct_from_utf8("child3"));

	element1_.setAttribute(SA::construct_from_utf8("one"), SA::construct_from_utf8("1"));

	element2_.setAttribute(SA::construct_from_utf8("one"), SA::construct_from_utf8("1"));
	element2_.setAttribute(SA::construct_from_utf8("two"), SA::construct_from_utf8("1"));
	element2_.setAttribute(SA::construct_from_utf8("three"), SA::construct_from_utf8("1"));
	element2_.setAttribute(SA::construct_from_utf8("four"), SA::construct_from_utf8("1"));

	text_ = document_.createTextNode(SA::construct_from_utf8("data"));
	comment_ = document_.createComment(SA::construct_from_utf8("comment"));
	processingInstruction_ = document_.createProcessingInstruction(SA::construct_from_utf8("target"), SA::construct_from_utf8("data"));
	element2_.appendChild(text_);
	spinkle_ = document_.createElement(SA::construct_from_utf8("spinkle"));
	element2_.appendChild(spinkle_);
	element2_.appendChild(comment_);
	element2_.appendChild(processingInstruction_);

	attr_ = element1_.getAttributeNode(SA::construct_from_utf8("one"));

	root_.appendChild(element1_);
	root_.appendChild(element2_);
	root_.appendChild(element3_);

	chapters_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
	chapters_.appendChild(chapters_.createElement(SA::construct_from_utf8("document")));
	chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("one")));
	chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("two")));
	chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("three")));
	chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("four")));
	chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("five")));

	numbers_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
	numbers_.appendChild(numbers_.createElement(SA::construct_from_utf8("doc")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("1")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("2")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("3")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("4")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("5")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("6")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("7")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("8")));
	numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("9")));
	std::cout << document_ << std::endl;
	std::cout << numbers_ << std::endl;
	std::cout << chapters_ << std::endl;

	if (true) {
		using namespace Arabica::XPath;
		using namespace Arabica::DOM;
		XPathValue<string_type, string_adaptor> result = parser.evaluate(SA::construct_from_utf8("//*"), document_);
		for(int i = 0; i < result.asNodeSet().size(); i++) {
			Node<string_type, string_adaptor> node = result.asNodeSet()[i];
			std::string xpathExpr = uscxml::DOMUtils::xPathForNode(node);
			if (xpathExpr.size()) {
				XPathValue<string_type, string_adaptor> innerResult = parser.evaluate(xpathExpr, document_);
				assert(innerResult.asNodeSet().size() > 0);
				assert(innerResult.asNodeSet().size() == 1);
				assert(innerResult.asNodeSet()[0] == node);
			} else {
				assert(node.getNodeType() != Node_base::ELEMENT_NODE);
			}
		}
	}

	if (false) {
		using namespace Arabica::XPath;
		StringVariableResolver svr;
		svr.setVariable(SA::construct_from_utf8("index"), SA::construct_from_utf8("1"));

		parser.setVariableResolver(svr);
		XPathValue<string_type, string_adaptor> result = parser.evaluate(SA::construct_from_utf8("/root/*[@two = $index]"), document_);
		assert(NODE_SET == result.type());
		assert(element2_ == result.asNodeSet()[0]);

		parser.resetVariableResolver();
	} // test18

	if (false) {
		using namespace Arabica::XPath;
		XPathExpression<string_type, string_adaptor> xpath = parser.compile(SA::construct_from_utf8("root/*[position() = 2]"));
		XPathValue<string_type, string_adaptor> result = xpath.evaluate(document_);

		assert(NODE_SET == result.type());
		assert(1 == result.asNodeSet().size());
		Arabica::DOM::Node<string_type, string_adaptor> n = result.asNodeSet()[0];
		assert(element2_ == n);
	} // test19

	if (false) {
		using namespace Arabica::XPath;
		Arabica::DOM::DocumentFragment<string_type, string_adaptor> frag = document_.createDocumentFragment();
		frag.appendChild(document_.createElement(SA::construct_from_utf8("foo")));

		NodeSetVariableResolver svr;
		NodeSet<string_type, string_adaptor> ns;
		ns.push_back(frag);
		svr.setVariable(SA::construct_from_utf8("fruit"), ns);
		parser.setVariableResolver(svr);

		XPathValue<string_type, string_adaptor> result = parser.evaluate_expr(SA::construct_from_utf8("$fruit/foo|/root/child3"), document_);
		assert(NODE_SET == result.type());
		assert(2 == result.asNodeSet().size());
		assert(element3_ == result.asNodeSet()[0]);
	} // testUnion11

	if (false) {
		using namespace Arabica::XPath;
		XPathValue<string_type, string_adaptor> result = parser.evaluate_expr(SA::construct_from_utf8("local-name(/root)"), document_);
		assert(STRING == result.type());
		assert(SA::construct_from_utf8("root") == result.asString());
	} // testLocalNameFn1

	if (0) {
		using namespace Arabica::XPath;
		Arabica::DOM::DocumentFragment<std::string> frag = document_.createDocumentFragment();
		frag.appendChild(document_.createElement("foo"));

		NodeSetVariableResolver svr;
		NodeSet<string_type, string_adaptor> ns;
		ns.push_back(frag);
		svr.setVariable("fruit", ns);
		parser.setVariableResolver(svr);

		XPathValue<string_type, string_adaptor> result = parser.evaluate(SA::construct_from_utf8("local-name($fruit/foo) == 'foo'"), document_);
		std::cout << result.asBool() << std::endl;
	}

}