/* -*- 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 .
*/
CDocumentBuilder::CDocumentBuilder()
: m_xEntityResolver(new CDefaultEntityResolver)
{ // init libxml. libxml will protect itself against multiple // initializations so there is no problem here if this gets // called multiple times.
xmlInitParser();
}
staticint xmlIO_read_func( void *context, char *buffer, int len)
{ // get the context...
context_t *pctx = static_cast<context_t*>(context); if (!pctx->rInputStream.is()) return -1; try { // try to read the requested number of bytes
Sequence< sal_Int8 > chunk(len); int nread = pctx->rInputStream->readBytes(chunk, len);
// resolve the entity
InputSource src = resolver->resolveEntity(pubid, sysid);
// create IO context on heap because this call will no longer be on the stack // when IO is actually performed through the callbacks. The close function must // free the memory which is indicated by the freeOnClose field in the context struct
context_t *c = new context_t;
c->rInputStream = src.aInputStream;
c->close = true;
c->freeOnClose = true;
// set up the inputBuffer and inputPtr for libxml
xmlParserInputBufferPtr pBuffer =
xmlParserInputBufferCreateIO(xmlIO_read_func, xmlIO_close_func, c, XML_CHAR_ENCODING_NONE);
xmlParserInputPtr pInput =
xmlNewIOInputStream(ctxt, pBuffer, XML_CHAR_ENCODING_NONE); return pInput;
}
#if 0 static xmlParserInputPtr external_entity_loader(constchar *URL, constchar * /*ID*/, xmlParserCtxtPtr ctxt)
{ // just call our resolver function using the URL as systemId return resolve_func(ctxt, 0, (const xmlChar*)URL);
} #endif
Reference<XErrorHandler> xErrorHandler = pDocBuilder->getErrorHandler(); if (xErrorHandler.is()) // if custom error handler is set (using setErrorHandler ())
{ // Prepare SAXParseException to be passed to custom XErrorHandler::warning function const xmlError* lastError = xmlCtxtGetLastError(pctx);
css::xml::sax::SAXParseException saxex(make_error_message(pctx), {}, {}, {}, {},
lastError->line, lastError->int2);
// Call custom warning function
xErrorHandler->warning(::css::uno::Any(saxex));
}
} catch (const css::uno::Exception &)
{ // Protect lib2xml from UNO Exception
TOOLS_WARN_EXCEPTION("unoxml", "DOM::warning_func");
}
}
Reference<XErrorHandler> xErrorHandler = pDocBuilder->getErrorHandler(); if (xErrorHandler.is()) // if custom error handler is set (using setErrorHandler ())
{ // Prepare SAXParseException to be passed to custom XErrorHandler::error function const xmlError* lastError = xmlCtxtGetLastError(pctx);
css::xml::sax::SAXParseException saxex(make_error_message(pctx), {}, {}, {}, {},
lastError->line, lastError->int2);
// Call custom warning function
xErrorHandler->error(::css::uno::Any(saxex));
}
} catch (const css::uno::Exception &)
{ // Protect lib2xml from UNO Exception
TOOLS_WARN_EXCEPTION("unoxml", "DOM::error_func");
}
}
} // extern "C"
// IO context struct. Must outlive pContext, as destroying that via // xmlFreeParserCtxt may still access this context_t
context_t c;
c.rInputStream = is; // we did not open the stream, thus we do not close it.
c.close = false;
c.freeOnClose = false;
// if we failed to parse the URI as a simple file, let's try via a ucb stream. // For Android file:///assets/ URLs which must go via the osl/ file API. if (pDoc == nullptr) {
Reference < XSimpleFileAccess3 > xStreamAccess(
SimpleFileAccess::create( comphelper::getProcessComponentContext() ) );
Reference< XInputStream > xInStream = xStreamAccess->openFileRead( sUri ); if (!xInStream.is())
throwEx(pContext.get());
// loop over every layout entry in current file
xRet = parse( xInStream );
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.