From 4555948aaef404b50c29a99d638270e15fbc58f3 Mon Sep 17 00:00:00 2001 From: Jez Higgins Date: Wed, 21 Nov 2012 11:12:35 +0000 Subject: [PATCH] OK, I really think that's it --- include/Arabica/StringAdaptor.hpp | 26 ++++ .../handler/xslt_apply_templates_handler.hpp | 2 +- .../XSLT/impl/handler/xslt_output_handler.hpp | 3 +- .../XSLT/impl/xslt_compilation_context.hpp | 12 +- include/XSLT/impl/xslt_execution_context.hpp | 2 +- include/XSLT/impl/xslt_qname.hpp | 2 +- include/XSLT/impl/xslt_sink.hpp | 8 +- tests/XSLT/main_silly.cpp | 2 +- tests/XSLT/xslt_test.hpp | 117 ++++++++++-------- vs2012/test_xslt_silly.vcxproj | 3 +- 10 files changed, 108 insertions(+), 69 deletions(-) diff --git a/include/Arabica/StringAdaptor.hpp b/include/Arabica/StringAdaptor.hpp index 41d1fc2e..5983f813 100644 --- a/include/Arabica/StringAdaptor.hpp +++ b/include/Arabica/StringAdaptor.hpp @@ -176,6 +176,32 @@ public: return static_cast(c); } // makeValueT + template + static inline string_type construct(InputIterator from, InputIterator to) + { + return string_type(from, to); + } + + static inline string_type construct(const_iterator from, const_iterator to) + { + return string_type(from, to); + } + + static inline string_type construct(const std::basic_string& str) + { + return construct(str.begin(), str.end()); + } // construct + + static string_type construct(const value_type* str) + { + return str ? string_type(str) : string_type(); + } + + static std::wstring construct(const std::string& str) + { + return construct_from_utf8(str.c_str()); + } + static std::wstring construct_from_utf8(const char* str) { widener_t w; diff --git a/include/XSLT/impl/handler/xslt_apply_templates_handler.hpp b/include/XSLT/impl/handler/xslt_apply_templates_handler.hpp index 297136ef..af3281ee 100644 --- a/include/XSLT/impl/handler/xslt_apply_templates_handler.hpp +++ b/include/XSLT/impl/handler/xslt_apply_templates_handler.hpp @@ -94,7 +94,7 @@ public: } // characters private: - CompilationContext& context_; + CompilationContext& context_; ApplyTemplates* applyTemplates_; }; // class ApplyTemplatesHandler diff --git a/include/XSLT/impl/handler/xslt_output_handler.hpp b/include/XSLT/impl/handler/xslt_output_handler.hpp index af4a66cb..b78d3bab 100644 --- a/include/XSLT/impl/handler/xslt_output_handler.hpp +++ b/include/XSLT/impl/handler/xslt_output_handler.hpp @@ -70,7 +70,8 @@ private: return elements; string_type norm_cdata_sec_elements = text::normalize_whitespace(cdata_section_elements); - std::basic_istringstream is(string_adaptor::asStdString(norm_cdata_sec_elements)); + std::basic_stringstream is; + is << norm_cdata_sec_elements; while(!is.eof()) { std::basic_string e; diff --git a/include/XSLT/impl/xslt_compilation_context.hpp b/include/XSLT/impl/xslt_compilation_context.hpp index 481300bd..51ff879b 100644 --- a/include/XSLT/impl/xslt_compilation_context.hpp +++ b/include/XSLT/impl/xslt_compilation_context.hpp @@ -79,7 +79,7 @@ public: Disallow current(current_allowed_); return xpath_.compile_match(match); } // xpath_match - std::vector > xpath_match_no_variables(const string_type& match) const + std::vector > xpath_match_no_variables(const string_type& match) const { Disallow variables(variables_allowed_); return xpath_match(match); @@ -190,7 +190,7 @@ public: } // precedence private: - virtual Arabica::XPath::XPathExpression_impl >* + virtual Arabica::XPath::XPathExpression_impl* compileVariable(const string_type& namespace_uri, const string_type& name) const { if(!variables_allowed_) @@ -199,10 +199,10 @@ private: } // compileVariable // FunctionResolver - virtual Arabica::XPath::XPathFunction* resolveFunction( + virtual Arabica::XPath::XPathFunction* resolveFunction( const string_type& namespace_uri, const string_type& name, - const std::vector >& argExprs) const + const std::vector >& argExprs) const { if(!string_adaptor::empty(namespace_uri)) return new UndefinedFunction(namespace_uri, name, argExprs); @@ -273,8 +273,8 @@ private: mutable bool current_allowed_; mutable bool variables_allowed_; Precedence precedence_; - Arabica::XPath::XPath xpath_; - std::stack*> handlerStack_; + Arabica::XPath::XPath xpath_; + std::stack*> handlerStack_; std::stack*> parentStack_; std::map namespaceRemap_; diff --git a/include/XSLT/impl/xslt_execution_context.hpp b/include/XSLT/impl/xslt_execution_context.hpp index 339e71ad..d80e744d 100644 --- a/include/XSLT/impl/xslt_execution_context.hpp +++ b/include/XSLT/impl/xslt_execution_context.hpp @@ -23,7 +23,7 @@ protected: public: virtual const string_type& name() const = 0; - virtual Arabica::XPath::XPathValue value(const DOM::Node& node, + virtual Arabica::XPath::XPathValue value(const DOM::Node& node, ExecutionContext& context, DOMSink& sink) const = 0; virtual const Precedence& precedence() const = 0; diff --git a/include/XSLT/impl/xslt_qname.hpp b/include/XSLT/impl/xslt_qname.hpp index c0185907..e425682f 100644 --- a/include/XSLT/impl/xslt_qname.hpp +++ b/include/XSLT/impl/xslt_qname.hpp @@ -35,7 +35,7 @@ struct QName string_adaptor::append(qname, lN); } // QName - static QName create(const XML::QualifiedName& qName) + static QName create(const XML::QualifiedName& qName) { if(string_adaptor::length(qName.prefix()) && string_adaptor::empty(qName.namespaceUri())) throw SAX::SAXException("Prefix " + string_adaptor::asStdString(qName.prefix()) + " is not declared."); diff --git a/include/XSLT/impl/xslt_sink.hpp b/include/XSLT/impl/xslt_sink.hpp index 1b8add64..6fa49732 100644 --- a/include/XSLT/impl/xslt_sink.hpp +++ b/include/XSLT/impl/xslt_sink.hpp @@ -378,7 +378,7 @@ protected: void do_characters(const string_type& ch) { - DOM::Node lc = current().getLastChild(); + DOM::Node lc = current().getLastChild(); if(lc == 0 || lc.getNodeType() != DOM::Node_base::TEXT_NODE) current().appendChild(document().createTextNode(ch)); else @@ -391,7 +391,7 @@ protected: void do_end_CDATA() { - DOM::Node lc = current().getLastChild(); + DOM::Node lc = current().getLastChild(); if(lc.getNodeType() == DOM::Node_base::TEXT_NODE) current().replaceChild(document().createCDATASection(lc.getNodeValue()), lc); } // do_end_CDATA @@ -434,7 +434,7 @@ protected: } // do_end_element private: - DOM::Document& document() + DOM::Document& document() { if(document_ != 0) return document_; @@ -444,7 +444,7 @@ private: return document_; } // document - DOM::Node& current() + DOM::Node& current() { if(current_ != 0) return current_; diff --git a/tests/XSLT/main_silly.cpp b/tests/XSLT/main_silly.cpp index 791efc8b..7ef62e76 100644 --- a/tests/XSLT/main_silly.cpp +++ b/tests/XSLT/main_silly.cpp @@ -1,5 +1,5 @@ #ifdef _MSC_VER -#pragma warning(disable : 4250) +#pragma warning(disable : 4250 4244) #endif diff --git a/tests/XSLT/xslt_test.hpp b/tests/XSLT/xslt_test.hpp index 67fccc16..73e165f8 100644 --- a/tests/XSLT/xslt_test.hpp +++ b/tests/XSLT/xslt_test.hpp @@ -60,26 +60,27 @@ const std::string SEPERATOR = "/"; template Arabica::DOM::Document buildDOM(const std::string& filename) { - Arabica::SAX::InputSource is(filename); + Arabica::SAX::InputSource is(string_adaptor::construct(filename)); Arabica::SAX2DOM::Parser parser; parser.parse(is); - Arabica::DOM::Documentstring_type, string_adaptor> d = parser.getDocument(); + Arabica::DOM::Document d = parser.getDocument(); if(d != 0) d.normalize(); return d; } // buildDOM -Arabica::DOM::Document buildDOMFromString(const std::string& xml) +template +Arabica::DOM::Document buildDOMFromString(const std::string& xml) { std::stringstream ss; ss << xml; - Arabica::SAX::InputSource is(ss); + Arabica::SAX::InputSource is(ss); - Arabica::SAX2DOM::Parser parser; + Arabica::SAX2DOM::Parser parser; parser.parse(is); - Arabica::DOM::Document d = parser.getDocument(); + Arabica::DOM::Document d = parser.getDocument(); if(d != 0) d.normalize(); return d; @@ -111,13 +112,17 @@ std::string readFragment(const std::string& filename) return frag; } // readFragment -Arabica::XPath::NodeSet selectNodes(const std::string& path, const Arabica::DOM::Node& node) +template +Arabica::XPath::NodeSet selectNodes(const char* path, + const Arabica::DOM::Node& node) { - Arabica::XPath::XPath xpath; - return xpath.evaluate(path, node).asNodeSet(); + Arabica::XPath::XPath xpath; + return xpath.evaluate(string_adaptor::construct(path), node).asNodeSet(); } // selectNodes -Arabica::DOM::Node selectNode(const std::string& path, const Arabica::DOM::Node& node) +template +Arabica::DOM::Node selectNode(const char* path, + const Arabica::DOM::Node& node) { return selectNodes(path, node)[0]; } // selectNode @@ -171,7 +176,7 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + Arabica::SAX::InputSource source(string_adaptor::construct(input_xslt_)); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() != 0) assertImplementation(false, "Expected " + input_xslt_ + " not to compile. But it did :o"); @@ -203,7 +208,7 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + Arabica::SAX::InputSource source(string_adaptor::construct(input_xslt_)); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() == 0) assertImplementation(false, "Failed to compile " + input_xslt_ + " : " + compiler.error()); @@ -211,10 +216,10 @@ protected: Arabica::XSLT::DOMSink output; stylesheet->set_output(output); - std::ostringstream errors; + std::basic_ostringstream errors; stylesheet->set_error_output(errors); - Arabica::DOM::Document document = buildDOM(input_xml_); + Arabica::DOM::Document document = buildDOM(input_xml_); try { stylesheet->execute(document); } @@ -248,7 +253,8 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + string_type input = string_adaptor::construct(input_xslt_); + Arabica::SAX::InputSource source(input); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() == 0) return; @@ -256,10 +262,10 @@ protected: Arabica::XSLT::DOMSink output; stylesheet->set_output(output); - std::ostringstream errors; + std::basic_ostringstream errors; stylesheet->set_error_output(errors); - Arabica::DOM::Document document = buildDOM(input_xml_); + Arabica::DOM::Document document = buildDOM(input_xml_); try { stylesheet->execute(document); } @@ -294,19 +300,19 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + Arabica::SAX::InputSource source(string_adaptor::construct(input_xslt_)); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() == 0) assertImplementation(false, "Failed to compile " + input_xslt_ + " : " + compiler.error()); - std::ostringstream xml_output; + std::basic_ostringstream xml_output; Arabica::XSLT::StreamSink output(xml_output); stylesheet->set_output(output); - std::ostringstream errors; + std::basic_ostringstream errors; stylesheet->set_error_output(errors); - Arabica::DOM::Document document = buildDOM(input_xml_); + Arabica::DOM::Document document = buildDOM(input_xml_); try { stylesheet->execute(document); } @@ -315,7 +321,7 @@ protected: } // catch std::string ref = readFile(output_xml_); - std::string out = xml_output.str(); + std::string out = string_adaptor::asStdString(string_adaptor::construct(xml_output.str())); if(ref == out) return; @@ -338,7 +344,7 @@ protected: private: std::string stripWhitespace(const std::string& str) { - std::string s = Arabica::text::normalize_whitespace(str); + std::string s = Arabica::text::normalize_whitespace >(str); std::string::size_type i = s.find("> "); while(i != std::string::npos) @@ -390,7 +396,7 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + Arabica::SAX::InputSource source(string_adaptor::construct(input_xslt_)); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() == 0) assertImplementation(false, "Failed to compile " + input_xslt_ + " : " + compiler.error()); @@ -398,10 +404,10 @@ protected: Arabica::XSLT::DOMSink output; stylesheet->set_output(output); - std::ostringstream errors; + std::basic_ostringstream errors; stylesheet->set_error_output(errors); - Arabica::DOM::Document document = buildDOM(input_xml_); + Arabica::DOM::Document document = buildDOM(input_xml_); try { stylesheet->execute(document); } @@ -410,7 +416,7 @@ protected: } std::string refStr = "" + readFragment(output_xml_) + ""; - Arabica::DOM::Document refDoc = buildDOMFromString(refStr); + Arabica::DOM::Document refDoc = buildDOMFromString(refStr); if(refDoc == 0) assertImplementation(false, "Couldn't read " + output_xml_ + ". Perhaps it isn't well-formed XML?"); Arabica::DOM::DocumentFragment ref = refDoc.createDocumentFragment(); @@ -439,28 +445,30 @@ protected: std::string docToString(Arabica::DOM::Node node) { - std::ostringstream ss; + std::basic_ostringstream ss; ss << node; - return Arabica::text::normalize_whitespace(ss.str()); + string_type streamed = string_adaptor::construct(ss.str()); + streamed = Arabica::text::normalize_whitespace(streamed); + return string_adaptor::asStdString(streamed); } // docToString void stripWhitespace(Arabica::DOM::Node doc) { - Arabica::XPath::NodeSet textNodes = selectNodes("//text()", doc); + Arabica::XPath::NodeSet textNodes = selectNodes("//text()", doc); for(size_t i = 0; i != textNodes.size(); ++i) { Arabica::DOM::Node t = textNodes[i]; - std::string text = t.getNodeValue(); + string_type text = t.getNodeValue(); text = Arabica::text::normalize_whitespace(text); - size_t index = text.find_first_of(" "); - while(index != std::string::npos) + size_t index = string_adaptor::find(text, Arabica::text::Unicode::SPACE); + while(index != string_adaptor::npos()) { - text.replace(index, 1, ""); - index = text.find_first_of(" "); + text = string_adaptor::concat(string_adaptor::substr(text, 0, index), string_adaptor::substr(text, index+1)); + index = string_adaptor::find(text, Arabica::text::Unicode::SPACE); } t.setNodeValue(text); - if(text.length() == 0) - t.getParentNode().removeChild(t); + if(string_adaptor::length(text) == 0) + t.getParentNode().removeChild(t); } } // stripWhitespace @@ -489,7 +497,7 @@ protected: { Arabica::XSLT::StylesheetCompiler compiler; - Arabica::SAX::InputSource source(input_xslt_); + Arabica::SAX::InputSource source(string_adaptor::construct(input_xslt_)); std::auto_ptr > stylesheet = compiler.compile(source); if(stylesheet.get() == 0) assertImplementation(false, "Failed to compile " + input_xslt_ + " : " + compiler.error()); @@ -497,10 +505,10 @@ protected: Arabica::XSLT::DOMSink output; stylesheet->set_output(output); - std::ostringstream errors; + std::basic_ostringstream errors; stylesheet->set_error_output(errors); - Arabica::DOM::Document document = buildDOM(input_xml_); + Arabica::DOM::Document document = buildDOM(input_xml_); if(document == 0) assertImplementation(false, "Couldn't read " + input_xml_ + ". Perhaps it isn't well-formed XML?"); try { @@ -510,7 +518,7 @@ protected: assertImplementation(false, "Failed to run " + input_xslt_ + " : " + e.what()); } - Arabica::DOM::Document ref = buildDOM(output_xml_); + Arabica::DOM::Document ref = buildDOM(output_xml_); if(ref == 0) assertImplementation(false, "Couldn't read " + output_xml_ + ". Perhaps it isn't well-formed XML?"); Arabica::DOM::Node out = output.node(); @@ -535,9 +543,11 @@ protected: std::string docToString(Arabica::DOM::Node node) { - std::ostringstream ss; + std::basic_ostringstream ss; ss << node; - std::string xml = Arabica::text::normalize_whitespace(ss.str()); + string_type streamed = string_adaptor::construct(ss.str()); + streamed = Arabica::text::normalize_whitespace(streamed); + std::string xml = string_adaptor::asStdString(streamed); if(xml.find(" ") == 0) xml.replace(0, 22, ""); return xml; @@ -545,17 +555,17 @@ protected: void stripWhitespace(Arabica::DOM::Node doc) { - Arabica::XPath::NodeSet textNodes = selectNodes("//text()", doc); + Arabica::XPath::NodeSet textNodes = selectNodes("//text()", doc); for(size_t i = 0; i != textNodes.size(); ++i) { Arabica::DOM::Node t = textNodes[i]; - std::string text = t.getNodeValue(); + string_type text = t.getNodeValue(); text = Arabica::text::normalize_whitespace(text); - size_t index = text.find_first_of(" "); - while(index != std::string::npos) + size_t index = string_adaptor::find(text, Arabica::text::Unicode::SPACE); + while(index != string_adaptor::npos()) { - text.replace(index, 1, ""); - index = text.find_first_of(" "); + text = string_adaptor::concat(string_adaptor::substr(text, 0, index), string_adaptor::substr(text, index+1)); + index = string_adaptor::find(text, Arabica::text::Unicode::SPACE); } t.setNodeValue(text); } @@ -579,8 +589,8 @@ public: return; std::cerr << "Loaded expected fails" << std::endl; - Arabica::DOM::Document fail_doc = buildDOM(PATH_PREFIX + "arabica-expected-fails.xml"); - Arabica::XPath::NodeSet failcases = selectNodes("/test-suite/test-case", fail_doc); + Arabica::DOM::Document fail_doc = buildDOM >(PATH_PREFIX + "arabica-expected-fails.xml"); + Arabica::XPath::NodeSet failcases = selectNodes >("/test-suite/test-case", fail_doc); for(size_t i = 0; i != failcases.size(); ++i) { std::string name = selectString("@id", failcases[i]); @@ -637,7 +647,7 @@ Arabica::DOM::Document Loader::loadCat if(c == 0) { std::cerr << "Loading " << catalog_filename << std::endl; - c = buildDOM(PATH_PREFIX + catalog_filename); + c = buildDOM >(PATH_PREFIX + catalog_filename); catalogs_[catalog_filename] = c; } // if(c == 0) @@ -651,8 +661,9 @@ TestSuite* Loader::suite(const std::string& path, c TestSuite *suiteOfTests = new TestSuite; + std::string testNamesSelector = "/test-suite/test-catalog/test-case[file-path='" + path + "']"; Arabica::XPath::NodeSet tests = - selectNodes("/test-suite/test-catalog/test-case[file-path='" + path + "']", catalog); + selectNodes >(testNamesSelector.c_str(), catalog); std::cerr << "There are " << tests.size() << " " << path << " tests." << std::endl; for(size_t i = 0; i != tests.size(); ++i) { diff --git a/vs2012/test_xslt_silly.vcxproj b/vs2012/test_xslt_silly.vcxproj index 90c99d11..79ace3e7 100644 --- a/vs2012/test_xslt_silly.vcxproj +++ b/vs2012/test_xslt_silly.vcxproj @@ -1,4 +1,4 @@ - + @@ -114,6 +114,7 @@ +