1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLTextPContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
37 #include "XMLTextPContext.hxx"
38 #include "xmlimprt.hxx"
39 #include "xmlcelli.hxx"
40 #include <xmloff/xmlnmspe.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/nmspmap.hxx>
43 #include <tools/debug.hxx>
44 #include <com/sun/star/text/XTextCursor.hpp>
46 using namespace com::sun::star
;
47 using namespace xmloff::token
;
49 class ScXMLTextTContext
: public SvXMLImportContext
51 const ScXMLImport
& GetScImport() const { return (const ScXMLImport
&)GetImport(); }
52 ScXMLImport
& GetScImport() { return (ScXMLImport
&)GetImport(); }
54 ScXMLTextTContext( ScXMLImport
& rImport
, USHORT nPrfx
,
55 const ::rtl::OUString
& rLName
,
56 const ::com::sun::star::uno::Reference
<
57 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
,
58 ScXMLTextPContext
* pTextPContext
);
60 virtual ~ScXMLTextTContext();
64 ScXMLTextTContext::ScXMLTextTContext( ScXMLImport
& rImport
,
66 const ::rtl::OUString
& rLName
,
67 const ::com::sun::star::uno::Reference
<
68 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
,
69 ScXMLTextPContext
* pTextPContext
) :
70 SvXMLImportContext( rImport
, nPrfx
, rLName
)
74 sal_Int16
nAttrCount(xAttrList
.is() ? xAttrList
->getLength() : 0);
75 rtl::OUString aLocalName
;
77 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
79 sal_uInt16
nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
80 xAttrList
->getNameByIndex( i
), &aLocalName
));
81 const rtl::OUString
& sValue(xAttrList
->getValueByIndex( i
));
83 if ((nPrefix
== XML_NAMESPACE_TEXT
) && IsXMLToken(aLocalName
, XML_C
))
84 nCount
= sValue
.toInt32();
86 pTextPContext
->AddSpaces(nCount
);
90 ScXMLTextTContext::~ScXMLTextTContext()
94 //------------------------------------------------------------------
96 ScXMLTextPContext::ScXMLTextPContext( ScXMLImport
& rImport
,
98 const ::rtl::OUString
& rLName
,
99 const ::com::sun::star::uno::Reference
<
100 ::com::sun::star::xml::sax::XAttributeList
>& xTempAttrList
,
101 ScXMLTableRowCellContext
* pTempCellContext
) :
102 SvXMLImportContext( rImport
, nPrfx
, rLName
),
103 xAttrList(xTempAttrList
),
105 pCellContext(pTempCellContext
),
108 pContentBuffer(NULL
),
112 // here are no attributes
115 ScXMLTextPContext::~ScXMLTextPContext()
118 delete pTextPContext
;
120 delete pContentBuffer
;
123 void ScXMLTextPContext::AddSpaces(sal_Int32 nSpaceCount
)
125 // use pContentBuffer
126 if ( !pContentBuffer
)
127 pContentBuffer
= new rtl::OUStringBuffer( sSimpleContent
);
129 sal_Char
* pChars
= new sal_Char
[nSpaceCount
];
130 memset(pChars
, ' ', nSpaceCount
);
131 pContentBuffer
->appendAscii(pChars
, nSpaceCount
);
134 SvXMLImportContext
*ScXMLTextPContext::CreateChildContext( USHORT nTempPrefix
,
135 const ::rtl::OUString
& rLName
,
136 const ::com::sun::star::uno::Reference
<
137 ::com::sun::star::xml::sax::XAttributeList
>& xTempAttrList
)
139 SvXMLImportContext
*pContext(NULL
);
140 if (!pTextPContext
&&
141 (nTempPrefix
== XML_NAMESPACE_TEXT
) &&
142 IsXMLToken(rLName
, XML_S
))
143 pContext
= new ScXMLTextTContext( GetScImport(), nTempPrefix
, rLName
, xTempAttrList
, this);
148 rtl::OUString sSetString
;
149 if ( pContentBuffer
)
150 sSetString
= pContentBuffer
->makeStringAndClear();
152 sSetString
= sSimpleContent
;
154 sal_Unicode
cNonSpace(0);
156 sal_Int32 nLength
= sSetString
.getLength();
159 sal_Unicode cLast
= sSetString
.getStr()[ nLength
- 1 ];
160 if ( cLast
!= (sal_Unicode
)' ' )
162 // #i53253# To keep XMLParaContext's whitespace handling in sync,
163 // if there's a non-space character at the end of the existing string,
164 // it has to be processed by XMLParaContext.
167 sSetString
= sSetString
.copy( 0, nLength
- 1 ); // remove from the string for SetCursorOnTextImport
171 pCellContext
->SetCursorOnTextImport( sSetString
);
173 pTextPContext
= GetScImport().GetTextImport()->CreateTextChildContext(
174 GetScImport(), nPrefix
, sLName
, xAttrList
);
176 if ( cNonSpace
!= 0 )
178 // pass non-space character through XMLParaContext, so a following space isn't ignored
179 pTextPContext
->Characters( rtl::OUString( cNonSpace
) );
183 pContext
= pTextPContext
->CreateChildContext(nTempPrefix
, rLName
, xTempAttrList
);
187 pContext
= new SvXMLImportContext( GetScImport(), nTempPrefix
, rLName
);
192 void ScXMLTextPContext::Characters( const ::rtl::OUString
& rChars
)
196 // For the first call to an empty context, copy (ref-counted) the OUString.
197 // The OUStringBuffer is used only if there is more complex content.
199 if ( !pContentBuffer
&& sSimpleContent
.getLength() == 0 )
200 sSimpleContent
= rChars
;
203 if ( !pContentBuffer
)
204 pContentBuffer
= new rtl::OUStringBuffer( sSimpleContent
);
205 pContentBuffer
->append(rChars
);
209 pTextPContext
->Characters(rChars
);
212 void ScXMLTextPContext::EndElement()
216 if ( pContentBuffer
)
217 pCellContext
->SetString(pContentBuffer
->makeStringAndClear());
219 pCellContext
->SetString(sSimpleContent
);
223 pTextPContext
->EndElement();
224 GetScImport().SetRemoveLastChar(sal_True
);