1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include "TokenContext.hxx"
24 #include <xmloff/xmltkmap.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlerror.hxx>
31 using com::sun::star::uno::Reference
;
32 using com::sun::star::xml::sax::XAttributeList
;
34 const SvXMLTokenMapEntry aEmptyMap
[1] =
39 TokenContext::TokenContext( SvXMLImport
& rImport
,
41 const OUString
& rLocalName
,
42 const SvXMLTokenMapEntry
* pAttributes
,
43 const SvXMLTokenMapEntry
* pChildren
)
44 : SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
45 mpAttributes( pAttributes
),
46 mpChildren( pChildren
)
50 void TokenContext::StartElement(
51 const Reference
<XAttributeList
>& xAttributeList
)
53 // iterate over attributes
54 // - if in map: call HandleAttribute
55 // - xmlns:... : ignore
57 SAL_WARN_IF( mpAttributes
== nullptr, "xmloff", "no token map for attributes" );
58 SvXMLTokenMap
aMap( mpAttributes
);
60 sal_Int16 nCount
= xAttributeList
->getLength();
61 for( sal_Int16 i
= 0; i
< nCount
; i
++ )
63 // get key/local-name pair from namespace map
65 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
66 GetKeyByAttrName( xAttributeList
->getNameByIndex(i
), &sLocalName
);
68 // get token from token map
69 sal_uInt16 nToken
= aMap
.Get( nPrefix
, sLocalName
);
72 const OUString
& rValue
= xAttributeList
->getValueByIndex(i
);
74 if( nToken
!= XML_TOK_UNKNOWN
)
76 HandleAttribute( nToken
, rValue
);
78 else if( nPrefix
!= XML_NAMESPACE_XMLNS
)
80 // error handling, for all attribute that are not
81 // namespace declarations
82 GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE
,
88 SvXMLImportContextRef
TokenContext::CreateChildContext(
90 const OUString
& rLocalName
,
91 const Reference
<XAttributeList
>& xAttrList
)
93 // call HandleChild for elements in token map. Ignore other content.
95 SvXMLImportContext
* pContext
= nullptr;
97 SAL_WARN_IF( mpChildren
== nullptr, "xmloff", "no token map for child elements" );
98 SvXMLTokenMap
aMap( mpChildren
);
99 sal_uInt16 nToken
= aMap
.Get( nPrefix
, rLocalName
);
100 if( nToken
!= XML_TOK_UNKNOWN
)
102 // call handle child, and pass down arguments
103 pContext
= HandleChild( nToken
, nPrefix
, rLocalName
, xAttrList
);
106 // error handling: create default context and generate warning
107 if( pContext
== nullptr )
109 GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT
, rLocalName
);
110 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
115 static bool lcl_IsWhiteSpace( sal_Unicode c
)
123 void TokenContext::Characters( const OUString
& rCharacters
)
125 // get iterators for string data
126 const sal_Unicode
* pBegin
= rCharacters
.getStr();
127 const sal_Unicode
* pEnd
= &( pBegin
[ rCharacters
.getLength() ] );
129 // raise error if non-whitespace character is found
130 if( !::std::all_of( pBegin
, pEnd
, lcl_IsWhiteSpace
) )
131 GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS
, rCharacters
);
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */