160 lines
5.1 KiB
C++
160 lines
5.1 KiB
C++
/*************************************************************************
|
|
*
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
*
|
|
* Copyright 2008 by Sun Microsystems, Inc.
|
|
*
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
|
*
|
|
* $RCSfile: documentbuilder.cxx,v $
|
|
* $Revision: 1.5 $
|
|
*
|
|
* This file is part of OpenOffice.org.
|
|
*
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
* only, as published by the Free Software Foundation.
|
|
*
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License version 3 for more details
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
* <http://www.openoffice.org/license.html>
|
|
* for a copy of the LGPLv3 License.
|
|
*
|
|
************************************************************************/
|
|
|
|
#include "documentbuilder.hxx"
|
|
#include "node.hxx"
|
|
#include "document.hxx"
|
|
|
|
namespace DOM
|
|
{
|
|
CDocumentBuilder::CDocumentBuilder(const Reference< XMultiServiceFactory >& xFactory)
|
|
: m_aFactory(xFactory)
|
|
{
|
|
// init libxml. libxml will protect itself against multiple
|
|
// initializations so there is no problem here if this gets
|
|
// called multiple times.
|
|
xmlInitParser();
|
|
}
|
|
|
|
Reference< XInterface > CDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
|
|
{
|
|
// XXX
|
|
return static_cast< XDocumentBuilder* >(new CDocumentBuilder(rSMgr));
|
|
}
|
|
|
|
const char* CDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.DocumentBuilder";
|
|
const char* CDocumentBuilder::aSupportedServiceNames[] = {
|
|
"com.sun.star.xml.dom.DocumentBuilder",
|
|
NULL
|
|
};
|
|
|
|
OUString CDocumentBuilder::_getImplementationName()
|
|
{
|
|
return OUString::createFromAscii(aImplementationName);
|
|
}
|
|
Sequence<OUString> CDocumentBuilder::_getSupportedServiceNames()
|
|
{
|
|
Sequence<OUString> aSequence;
|
|
for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
|
|
aSequence.realloc(i+1);
|
|
aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
|
|
}
|
|
return aSequence;
|
|
}
|
|
|
|
Sequence< OUString > SAL_CALL CDocumentBuilder::getSupportedServiceNames()
|
|
throw (RuntimeException)
|
|
{
|
|
return CDocumentBuilder::_getSupportedServiceNames();
|
|
}
|
|
|
|
OUString SAL_CALL CDocumentBuilder::getImplementationName()
|
|
throw (RuntimeException)
|
|
{
|
|
return CDocumentBuilder::_getImplementationName();
|
|
}
|
|
|
|
sal_Bool SAL_CALL CDocumentBuilder::supportsService(const OUString& aServiceName)
|
|
throw (RuntimeException)
|
|
{
|
|
Sequence< OUString > supported = CDocumentBuilder::_getSupportedServiceNames();
|
|
for (sal_Int32 i=0; i<supported.getLength(); i++)
|
|
{
|
|
if (supported[i] == aServiceName) return sal_True;
|
|
}
|
|
return sal_False;
|
|
}
|
|
|
|
Reference< XDOMImplementation > SAL_CALL CDocumentBuilder::getDOMImplementation()
|
|
throw (RuntimeException)
|
|
{
|
|
|
|
return Reference< XDOMImplementation >();
|
|
}
|
|
|
|
sal_Bool SAL_CALL CDocumentBuilder::isNamespaceAware()
|
|
throw (RuntimeException)
|
|
{
|
|
return sal_True;
|
|
}
|
|
|
|
sal_Bool SAL_CALL CDocumentBuilder::isValidating()
|
|
throw (RuntimeException)
|
|
{
|
|
return sal_False;
|
|
}
|
|
|
|
Reference< XDocument > SAL_CALL CDocumentBuilder::newDocument()
|
|
throw (RuntimeException)
|
|
{
|
|
// create a new document
|
|
xmlDocPtr pDocument = xmlNewDoc((const xmlChar*)"1.0");
|
|
return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDocument)));
|
|
}
|
|
|
|
Reference< XDocument > SAL_CALL CDocumentBuilder::parse(const Reference< XInputStream >& is)
|
|
throw (RuntimeException)
|
|
{
|
|
sal_Int32 bsize = 4096;
|
|
char* buffer = (char*)rtl_allocateMemory(bsize);
|
|
sal_Int32 nbytes = 0;
|
|
sal_Int32 nread = 0;
|
|
sal_Int32 csize = bsize;
|
|
Sequence< sal_Int8 > chunk(csize);
|
|
do
|
|
{
|
|
// read mac csize bytes
|
|
nread = is->readBytes(chunk, csize);
|
|
// grow buffer?
|
|
if ((nbytes + nread) > bsize)
|
|
{
|
|
bsize = 2*bsize;
|
|
char *new_buffer = (char*)rtl_reallocateMemory(buffer, bsize);
|
|
if (new_buffer == 0)
|
|
throw RuntimeException();
|
|
buffer = new_buffer;
|
|
}
|
|
// copy bytes to buffer
|
|
rtl_copyMemory(buffer+nbytes, chunk.getConstArray(), nread);
|
|
nbytes += nread;
|
|
}
|
|
while(nread == csize);
|
|
|
|
// try to parse the buffer
|
|
xmlDocPtr pDoc = xmlParseMemory(buffer, nbytes);
|
|
// XXX error checking
|
|
rtl_freeMemory(buffer);
|
|
|
|
return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDoc)));
|
|
|
|
}
|
|
|
|
}
|