/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
namespace com::sun::star::ucb { class XCommandEnvironment; class XContentEventListener;
}
namespace {
// This class only implements that subset of functionality of a proper // css::ucb::Content that is known to be needed here: class Content: public cppu::WeakImplHelper<
css::ucb::XContent, css::ucb::XCommandProcessor >
{ public: explicit Content(
css::uno::Reference< css::ucb::XContentIdentifier > const & identifier);
css::uno::Any Content::execute(
css::ucb::Command const & command, sal_Int32,
css::uno::Reference< css::ucb::XCommandEnvironment > const &)
{ if ( command.Name != "getCasePreservingURL" )
{ throw css::uno::RuntimeException();
} // If any non-empty segment starts with anything but '0', '1', or '2', fail; // otherwise, if the last non-empty segment starts with '1', add a final // slash, and if the last non-empty segment starts with '2', remove a final // slash (if any); also, turn the given uri into all-lowercase:
OUString uri(m_identifier->getContentIdentifier());
sal_Unicode c = '0'; for (sal_Int32 i = RTL_CONSTASCII_LENGTH(m_prefix); i != -1;) {
OUString seg(uri.getToken(0, '/', i)); if (seg.getLength() > 0) {
c = seg[0]; if (c < '0' || c > '2') { throw css::uno::Exception();
}
}
} switch (c) { case'1':
uri += "/"; break; case'2': if (uri.endsWith("/")) {
uri = uri.copy(0, uri.getLength() -1);
} break;
} return css::uno::Any(uri.toAsciiLowerCase());
}
void Test::setUp() { // For whatever reason, on W32 it does not work to create/destroy a fresh // component context for each test in Test::setUp/tearDown; therefore, a // single component context is used for all tests and destroyed in the last // pseudo-test "finish": if (!m_context.is()) {
m_context = cppu::defaultBootstrap_InitialComponentContext();
}
}
void Test::testFindFirstDOIInText() { struct Data { charconst * input; charconst * result;
sal_Int32 begin;
sal_Int32 end;
}; static Data const tests[] = {
{ "doi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with only digits
{ "Doi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
{ "DoI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
{ "DOI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
{ "dOI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
{ "dOi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
{ "doi:10.1038/nature03001", "https://doi.org/10.1038/nature03001", 0, 23 }, // valid doi suffix with alphanumeric characters
{ "doi:10.1093/ajae/aaq063", "https://doi.org/10.1093/ajae/aaq063", 0, 23 }, // valid doi suffix with multiple slash
{ "doi:10.1016/S0735-1097(98)00347-7", "https://doi.org/10.1016/S0735-1097(98)00347-7", 0, 33 }, // valid doi suffix with characters apart from alphanumeric
{ "doi:10.109/ajae/aaq063", nullptr, 0, 0 }, // # of digits after doi;10. is not between 4 and 9
{ "doi:10.1234567890/ajae/aaq063", nullptr, 0, 0 }, // # of digits after doi;10. is not between 4 and 9
{ "doi:10.1093/ajae/aaq063/", nullptr, 0, 0 }, // nothing after slash
{ "doi:10.1093", nullptr, 0, 0 }, // no slash
{ "doi:11.1093/ajae/aaq063", nullptr, 0, 0 }, // doesn't begin with doi:10.
};
CharClass charClass( m_context, LanguageTag( css::lang::Locale(u"en"_ustr, u"US"_ustr, u""_ustr))); for (autoconst[pInput, pResult, nBegin, nEnd] : tests)
{
OUString input(OUString::createFromAscii(pInput));
sal_Int32 begin = 0;
sal_Int32 end = input.getLength();
OUString result(
URIHelper::FindFirstDOIInText(input, begin, end, charClass)); bool ok = pResult == nullptr
? (result.getLength() == 0 && begin == input.getLength() && end == input.getLength())
: (result.equalsAscii(pResult) && begin == nBegin && end == nEnd);
OString msg; if (!ok)
{
OStringBuffer buf;
buf.append(OString::Concat("\"") + pInput + "\" -> ");
buf.append(pResult == nullptr ? "none" : pResult);
buf.append(" (" + OString::number(nBegin) + ", " + OString::number(nEnd)
+ ")" " != "
+ OUStringToOString(result, RTL_TEXTENCODING_UTF8) + " ("
+ OString::number(begin) + ", " + OString::number(end) +")");
msg = buf.makeStringAndClear();
}
CPPUNIT_ASSERT_MESSAGE(msg.getStr(), ok);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.