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 "TokenContext.hxx"
21 #include <xmloff/xmltkmap.hxx>
22 #include <xmloff/xmlimp.hxx>
23 #include <xmloff/nmspmap.hxx>
24 #include "xmloff/xmlerror.hxx"
26 #include <tools/debug.hxx>
28 using com::sun::star::uno::Reference
;
29 using com::sun::star::xml::sax::XAttributeList
;
32 struct SvXMLTokenMapEntry aEmptyMap
[1] =
38 TokenContext::TokenContext( SvXMLImport
& rImport
,
40 const OUString
& rLocalName
,
41 const SvXMLTokenMapEntry
* pAttributes
,
42 const SvXMLTokenMapEntry
* pChildren
)
43 : SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
44 mpAttributes( pAttributes
),
45 mpChildren( pChildren
)
49 TokenContext::~TokenContext()
53 void TokenContext::StartElement(
54 const Reference
<XAttributeList
>& xAttributeList
)
56 // iterate over attributes
57 // - if in map: call HandleAttribute
58 // - xmlns:... : ignore
60 DBG_ASSERT( mpAttributes
!= NULL
, "no token map for attributes" );
61 SvXMLTokenMap
aMap( mpAttributes
);
63 sal_Int16 nCount
= xAttributeList
->getLength();
64 for( sal_Int16 i
= 0; i
< nCount
; i
++ )
66 // get key/local-name pair from namespace map
68 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
69 GetKeyByAttrName( xAttributeList
->getNameByIndex(i
), &sLocalName
);
71 // get token from token map
72 sal_uInt16 nToken
= aMap
.Get( nPrefix
, sLocalName
);
75 const OUString
& rValue
= xAttributeList
->getValueByIndex(i
);
77 if( nToken
!= XML_TOK_UNKNOWN
)
79 HandleAttribute( nToken
, rValue
);
81 else if( nPrefix
!= XML_NAMESPACE_XMLNS
)
83 // error handling, for all attribute that are not
84 // namespace declarations
85 GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE
,
91 SvXMLImportContext
* TokenContext::CreateChildContext(
93 const OUString
& rLocalName
,
94 const Reference
<XAttributeList
>& xAttrList
)
96 // call HandleChild for elements in token map. Ignore other content.
98 SvXMLImportContext
* pContext
= NULL
;
100 DBG_ASSERT( mpChildren
!= NULL
, "no token map for child elements" );
101 SvXMLTokenMap
aMap( mpChildren
);
102 sal_uInt16 nToken
= aMap
.Get( nPrefix
, rLocalName
);
103 if( nToken
!= XML_TOK_UNKNOWN
)
105 // call handle child, and pass down arguments
106 pContext
= HandleChild( nToken
, nPrefix
, rLocalName
, xAttrList
);
109 // error handling: create default context and generate warning
110 if( pContext
== NULL
)
112 GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT
, rLocalName
);
113 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
118 static bool lcl_IsWhiteSpace( sal_Unicode c
)
120 return c
== sal_Unicode( ' ' )
121 || c
== sal_Unicode( 0x09 )
122 || c
== sal_Unicode( 0x0A )
123 || c
== sal_Unicode( 0x0D );
126 void TokenContext::Characters( const OUString
& rCharacters
)
128 // get iterators for string data
129 const sal_Unicode
* pBegin
= rCharacters
.getStr();
130 const sal_Unicode
* pEnd
= &( pBegin
[ rCharacters
.getLength() ] );
132 // raise error if non-whitespace character is found
133 if( ::std::find_if( pBegin
, pEnd
, ::std::not1(::std::ptr_fun(lcl_IsWhiteSpace
)) ) != pEnd
)
134 GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS
, rCharacters
);
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */