OK, I really think that's it

This commit is contained in:
Jez Higgins 2012-11-21 11:12:35 +00:00
parent 1ed500107b
commit 4555948aae
10 changed files with 108 additions and 69 deletions

View file

@ -176,6 +176,32 @@ public:
return static_cast<wchar_t>(c);
} // makeValueT
template<class InputIterator>
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<value_type>& 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;

View file

@ -94,7 +94,7 @@ public:
} // characters
private:
CompilationContext<string_type>& context_;
CompilationContext<string_type, string_adaptor>& context_;
ApplyTemplates<string_type, string_adaptor>* applyTemplates_;
}; // class ApplyTemplatesHandler

View file

@ -70,7 +70,8 @@ private:
return elements;
string_type norm_cdata_sec_elements = text::normalize_whitespace<string_type, string_adaptor>(cdata_section_elements);
std::basic_istringstream<typename string_adaptor::value_type> is(string_adaptor::asStdString(norm_cdata_sec_elements));
std::basic_stringstream<typename string_adaptor::value_type> is;
is << norm_cdata_sec_elements;
while(!is.eof())
{
std::basic_string<typename string_adaptor::value_type> e;

View file

@ -79,7 +79,7 @@ public:
Disallow current(current_allowed_);
return xpath_.compile_match(match);
} // xpath_match
std::vector<Arabica::XPath::MatchExpr<string_type> > xpath_match_no_variables(const string_type& match) const
std::vector<Arabica::XPath::MatchExpr<string_type, string_adaptor> > 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<string_type, Arabica::default_string_adaptor<string_type> >*
virtual Arabica::XPath::XPathExpression_impl<string_type, string_adaptor>*
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<string_type>* resolveFunction(
virtual Arabica::XPath::XPathFunction<string_type, string_adaptor>* resolveFunction(
const string_type& namespace_uri,
const string_type& name,
const std::vector<Arabica::XPath::XPathExpression<string_type> >& argExprs) const
const std::vector<Arabica::XPath::XPathExpression<string_type, string_adaptor> >& argExprs) const
{
if(!string_adaptor::empty(namespace_uri))
return new UndefinedFunction<string_type, string_adaptor>(namespace_uri, name, argExprs);
@ -273,8 +273,8 @@ private:
mutable bool current_allowed_;
mutable bool variables_allowed_;
Precedence precedence_;
Arabica::XPath::XPath<string_type> xpath_;
std::stack<SAX::DefaultHandler<string_type>*> handlerStack_;
Arabica::XPath::XPath<string_type, string_adaptor> xpath_;
std::stack<SAX::DefaultHandler<string_type, string_adaptor>*> handlerStack_;
std::stack<ItemContainer<string_type, string_adaptor>*> parentStack_;
std::map<string_type, Namespace> namespaceRemap_;

View file

@ -23,7 +23,7 @@ protected:
public:
virtual const string_type& name() const = 0;
virtual Arabica::XPath::XPathValue<string_type> value(const DOM::Node<string_type, string_adaptor>& node,
virtual Arabica::XPath::XPathValue<string_type, string_adaptor> value(const DOM::Node<string_type, string_adaptor>& node,
ExecutionContext<string_type, string_adaptor>& context,
DOMSink<string_type, string_adaptor>& sink) const = 0;
virtual const Precedence& precedence() const = 0;

View file

@ -35,7 +35,7 @@ struct QName
string_adaptor::append(qname, lN);
} // QName
static QName create(const XML::QualifiedName<string_type>& qName)
static QName create(const XML::QualifiedName<string_type, string_adaptor>& qName)
{
if(string_adaptor::length(qName.prefix()) && string_adaptor::empty(qName.namespaceUri()))
throw SAX::SAXException("Prefix " + string_adaptor::asStdString(qName.prefix()) + " is not declared.");

View file

@ -378,7 +378,7 @@ protected:
void do_characters(const string_type& ch)
{
DOM::Node<string_type> lc = current().getLastChild();
DOM::Node<string_type, string_adaptor> 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<string_type> lc = current().getLastChild();
DOM::Node<string_type, string_adaptor> 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<string_type>& document()
DOM::Document<string_type, string_adaptor>& document()
{
if(document_ != 0)
return document_;
@ -444,7 +444,7 @@ private:
return document_;
} // document
DOM::Node<string_type>& current()
DOM::Node<string_type, string_adaptor>& current()
{
if(current_ != 0)
return current_;

View file

@ -1,5 +1,5 @@
#ifdef _MSC_VER
#pragma warning(disable : 4250)
#pragma warning(disable : 4250 4244)
#endif

View file

@ -60,26 +60,27 @@ const std::string SEPERATOR = "/";
template<class string_type, class string_adaptor>
Arabica::DOM::Document<string_type, string_adaptor> buildDOM(const std::string& filename)
{
Arabica::SAX::InputSource<string_type, string_adaptor> is(filename);
Arabica::SAX::InputSource<string_type, string_adaptor> is(string_adaptor::construct(filename));
Arabica::SAX2DOM::Parser<string_type, string_adaptor> parser;
parser.parse(is);
Arabica::DOM::Documentstring_type, string_adaptor> d = parser.getDocument();
Arabica::DOM::Document<string_type, string_adaptor> d = parser.getDocument();
if(d != 0)
d.normalize();
return d;
} // buildDOM
Arabica::DOM::Document<std::string> buildDOMFromString(const std::string& xml)
template<class string_type, class string_adaptor>
Arabica::DOM::Document<string_type, string_adaptor> buildDOMFromString(const std::string& xml)
{
std::stringstream ss;
ss << xml;
Arabica::SAX::InputSource<std::string> is(ss);
Arabica::SAX::InputSource<string_type, string_adaptor> is(ss);
Arabica::SAX2DOM::Parser<std::string> parser;
Arabica::SAX2DOM::Parser<string_type, string_adaptor> parser;
parser.parse(is);
Arabica::DOM::Document<std::string> d = parser.getDocument();
Arabica::DOM::Document<string_type, string_adaptor> 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<std::string> selectNodes(const std::string& path, const Arabica::DOM::Node<std::string>& node)
template<class string_type, class string_adaptor>
Arabica::XPath::NodeSet<string_type, string_adaptor> selectNodes(const char* path,
const Arabica::DOM::Node<string_type, string_adaptor>& node)
{
Arabica::XPath::XPath<std::string> xpath;
return xpath.evaluate(path, node).asNodeSet();
Arabica::XPath::XPath<string_type, string_adaptor> xpath;
return xpath.evaluate(string_adaptor::construct(path), node).asNodeSet();
} // selectNodes
Arabica::DOM::Node<std::string> selectNode(const std::string& path, const Arabica::DOM::Node<std::string>& node)
template<class string_type, class string_adaptor>
Arabica::DOM::Node<string_type, string_adaptor> selectNode(const char* path,
const Arabica::DOM::Node<string_type, string_adaptor>& node)
{
return selectNodes(path, node)[0];
} // selectNode
@ -171,7 +176,7 @@ protected:
{
Arabica::XSLT::StylesheetCompiler<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(string_adaptor::construct(input_xslt_));
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > 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<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(string_adaptor::construct(input_xslt_));
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > 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<string_type, string_adaptor> output;
stylesheet->set_output(output);
std::ostringstream errors;
std::basic_ostringstream<typename string_adaptor::value_type> errors;
stylesheet->set_error_output(errors);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM(input_xml_);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM<string_type, string_adaptor>(input_xml_);
try {
stylesheet->execute(document);
}
@ -248,7 +253,8 @@ protected:
{
Arabica::XSLT::StylesheetCompiler<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
string_type input = string_adaptor::construct(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(input);
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > stylesheet = compiler.compile(source);
if(stylesheet.get() == 0)
return;
@ -256,10 +262,10 @@ protected:
Arabica::XSLT::DOMSink<string_type, string_adaptor> output;
stylesheet->set_output(output);
std::ostringstream errors;
std::basic_ostringstream<typename string_adaptor::value_type> errors;
stylesheet->set_error_output(errors);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM(input_xml_);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM<string_type, string_adaptor>(input_xml_);
try {
stylesheet->execute(document);
}
@ -294,19 +300,19 @@ protected:
{
Arabica::XSLT::StylesheetCompiler<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(string_adaptor::construct(input_xslt_));
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > stylesheet = compiler.compile(source);
if(stylesheet.get() == 0)
assertImplementation(false, "Failed to compile " + input_xslt_ + " : " + compiler.error());
std::ostringstream xml_output;
std::basic_ostringstream<typename string_adaptor::value_type> xml_output;
Arabica::XSLT::StreamSink<string_type, string_adaptor> output(xml_output);
stylesheet->set_output(output);
std::ostringstream errors;
std::basic_ostringstream<typename string_adaptor::value_type> errors;
stylesheet->set_error_output(errors);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM(input_xml_);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM<string_type, string_adaptor>(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<string_type, string_adaptor>(str);
std::string s = Arabica::text::normalize_whitespace<std::string, Arabica::default_string_adaptor<std::string> >(str);
std::string::size_type i = s.find("> ");
while(i != std::string::npos)
@ -390,7 +396,7 @@ protected:
{
Arabica::XSLT::StylesheetCompiler<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(string_adaptor::construct(input_xslt_));
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > 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<string_type, string_adaptor> output;
stylesheet->set_output(output);
std::ostringstream errors;
std::basic_ostringstream<typename string_adaptor::value_type> errors;
stylesheet->set_error_output(errors);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM(input_xml_);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM<string_type, string_adaptor>(input_xml_);
try {
stylesheet->execute(document);
}
@ -410,7 +416,7 @@ protected:
}
std::string refStr = "<wrapper>" + readFragment(output_xml_) + "</wrapper>";
Arabica::DOM::Document<string_type, string_adaptor> refDoc = buildDOMFromString(refStr);
Arabica::DOM::Document<string_type, string_adaptor> refDoc = buildDOMFromString<string_type, string_adaptor>(refStr);
if(refDoc == 0)
assertImplementation(false, "Couldn't read " + output_xml_ + ". Perhaps it isn't well-formed XML?");
Arabica::DOM::DocumentFragment<string_type, string_adaptor> ref = refDoc.createDocumentFragment();
@ -439,27 +445,29 @@ protected:
std::string docToString(Arabica::DOM::Node<string_type, string_adaptor> node)
{
std::ostringstream ss;
std::basic_ostringstream<typename string_adaptor::value_type> ss;
ss << node;
return Arabica::text::normalize_whitespace<string_type, string_adaptor>(ss.str());
string_type streamed = string_adaptor::construct(ss.str());
streamed = Arabica::text::normalize_whitespace<string_type, string_adaptor>(streamed);
return string_adaptor::asStdString(streamed);
} // docToString
void stripWhitespace(Arabica::DOM::Node<string_type, string_adaptor> doc)
{
Arabica::XPath::NodeSet<string_type, string_adaptor> textNodes = selectNodes("//text()", doc);
Arabica::XPath::NodeSet<string_type, string_adaptor> textNodes = selectNodes<string_type, string_adaptor>("//text()", doc);
for(size_t i = 0; i != textNodes.size(); ++i)
{
Arabica::DOM::Node<string_type, string_adaptor> t = textNodes[i];
std::string text = t.getNodeValue();
string_type text = t.getNodeValue();
text = Arabica::text::normalize_whitespace<string_type, string_adaptor>(text);
size_t index = text.find_first_of(" ");
while(index != std::string::npos)
size_t index = string_adaptor::find(text, Arabica::text::Unicode<typename string_adaptor::value_type>::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<typename string_adaptor::value_type>::SPACE);
}
t.setNodeValue(text);
if(text.length() == 0)
if(string_adaptor::length(text) == 0)
t.getParentNode().removeChild(t);
}
} // stripWhitespace
@ -489,7 +497,7 @@ protected:
{
Arabica::XSLT::StylesheetCompiler<string_type, string_adaptor> compiler;
Arabica::SAX::InputSource<string_type, string_adaptor> source(input_xslt_);
Arabica::SAX::InputSource<string_type, string_adaptor> source(string_adaptor::construct(input_xslt_));
std::auto_ptr<Arabica::XSLT::Stylesheet<string_type, string_adaptor> > 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<string_type, string_adaptor> output;
stylesheet->set_output(output);
std::ostringstream errors;
std::basic_ostringstream<typename string_adaptor::value_type> errors;
stylesheet->set_error_output(errors);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM(input_xml_);
Arabica::DOM::Document<string_type, string_adaptor> document = buildDOM<string_type, string_adaptor>(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<string_type, string_adaptor> ref = buildDOM(output_xml_);
Arabica::DOM::Document<string_type, string_adaptor> ref = buildDOM<string_type, string_adaptor>(output_xml_);
if(ref == 0)
assertImplementation(false, "Couldn't read " + output_xml_ + ". Perhaps it isn't well-formed XML?");
Arabica::DOM::Node<string_type, string_adaptor> out = output.node();
@ -535,9 +543,11 @@ protected:
std::string docToString(Arabica::DOM::Node<string_type, string_adaptor> node)
{
std::ostringstream ss;
std::basic_ostringstream<typename string_adaptor::value_type> ss;
ss << node;
std::string xml = Arabica::text::normalize_whitespace<string_type, string_adaptor>(ss.str());
string_type streamed = string_adaptor::construct(ss.str());
streamed = Arabica::text::normalize_whitespace<string_type, string_adaptor>(streamed);
std::string xml = string_adaptor::asStdString(streamed);
if(xml.find("<?xml version=\"1.0\"?> ") == 0)
xml.replace(0, 22, "");
return xml;
@ -545,17 +555,17 @@ protected:
void stripWhitespace(Arabica::DOM::Node<string_type, string_adaptor> doc)
{
Arabica::XPath::NodeSet<string_type, string_adaptor> textNodes = selectNodes("//text()", doc);
Arabica::XPath::NodeSet<string_type, string_adaptor> textNodes = selectNodes<string_type, string_adaptor>("//text()", doc);
for(size_t i = 0; i != textNodes.size(); ++i)
{
Arabica::DOM::Node<string_type, string_adaptor> t = textNodes[i];
std::string text = t.getNodeValue();
string_type text = t.getNodeValue();
text = Arabica::text::normalize_whitespace<string_type, string_adaptor>(text);
size_t index = text.find_first_of(" ");
while(index != std::string::npos)
size_t index = string_adaptor::find(text, Arabica::text::Unicode<typename string_adaptor::value_type>::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<typename string_adaptor::value_type>::SPACE);
}
t.setNodeValue(text);
}
@ -579,8 +589,8 @@ public:
return;
std::cerr << "Loaded expected fails" << std::endl;
Arabica::DOM::Document<std::string> fail_doc = buildDOM<std::string>(PATH_PREFIX + "arabica-expected-fails.xml");
Arabica::XPath::NodeSet<std::string> failcases = selectNodes("/test-suite/test-case", fail_doc);
Arabica::DOM::Document<std::string> fail_doc = buildDOM<std::string, Arabica::default_string_adaptor<std::string> >(PATH_PREFIX + "arabica-expected-fails.xml");
Arabica::XPath::NodeSet<std::string> failcases = selectNodes<std::string, Arabica::default_string_adaptor<std::string> >("/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<std::string> Loader<string_type, string_adaptor>::loadCat
if(c == 0)
{
std::cerr << "Loading " << catalog_filename << std::endl;
c = buildDOM(PATH_PREFIX + catalog_filename);
c = buildDOM<std::string, Arabica::default_string_adaptor<std::string> >(PATH_PREFIX + catalog_filename);
catalogs_[catalog_filename] = c;
} // if(c == 0)
@ -651,8 +661,9 @@ TestSuite* Loader<string_type, string_adaptor>::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<std::string> tests =
selectNodes("/test-suite/test-catalog/test-case[file-path='" + path + "']", catalog);
selectNodes<std::string, Arabica::default_string_adaptor<std::string> >(testNamesSelector.c_str(), catalog);
std::cerr << "There are " << tests.size() << " " << path << " tests." << std::endl;
for(size_t i = 0; i != tests.size(); ++i)
{

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@ -114,6 +114,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\tests\silly_string\silly_string.cpp" />
<ClCompile Include="..\tests\Xslt\main_silly.cpp" />
</ItemGroup>
<ItemGroup>