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: TokenContext.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_xmloff.hxx"
33 #include "TokenContext.hxx"
34 #include <xmloff/xmltkmap.hxx>
35 #include <xmloff/xmlimp.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include "xmlerror.hxx"
39 #include <tools/debug.hxx>
42 using com::sun::star::uno::Reference
;
43 using com::sun::star::xml::sax::XAttributeList
;
46 struct SvXMLTokenMapEntry aEmptyMap
[1] =
52 TokenContext::TokenContext( SvXMLImport
& rImport
,
54 const OUString
& rLocalName
,
55 const SvXMLTokenMapEntry
* pAttributes
,
56 const SvXMLTokenMapEntry
* pChildren
)
57 : SvXMLImportContext( rImport
, nPrefix
, rLocalName
),
58 mpAttributes( pAttributes
),
59 mpChildren( pChildren
)
63 TokenContext::~TokenContext()
67 void TokenContext::StartElement(
68 const Reference
<XAttributeList
>& xAttributeList
)
70 // iterate over attributes
71 // - if in map: call HandleAttribute
72 // - xmlns:... : ignore
74 DBG_ASSERT( mpAttributes
!= NULL
, "no token map for attributes" );
75 SvXMLTokenMap
aMap( mpAttributes
);
77 sal_Int16 nCount
= xAttributeList
->getLength();
78 for( sal_Int16 i
= 0; i
< nCount
; i
++ )
80 // get key/local-name pair from namespace map
82 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
83 GetKeyByAttrName( xAttributeList
->getNameByIndex(i
), &sLocalName
);
85 // get token from token map
86 sal_uInt16 nToken
= aMap
.Get( nPrefix
, sLocalName
);
89 const OUString
& rValue
= xAttributeList
->getValueByIndex(i
);
91 if( nToken
!= XML_TOK_UNKNOWN
)
93 HandleAttribute( nToken
, rValue
);
95 else if( nPrefix
!= XML_NAMESPACE_XMLNS
)
97 // error handling, for all attribute that are not
98 // namespace declarations
99 GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE
,
105 SvXMLImportContext
* TokenContext::CreateChildContext(
107 const OUString
& rLocalName
,
108 const Reference
<XAttributeList
>& xAttrList
)
110 // call HandleChild for elements in token map. Ignore other content.
112 SvXMLImportContext
* pContext
= NULL
;
114 DBG_ASSERT( mpChildren
!= NULL
, "no token map for child elements" );
115 SvXMLTokenMap
aMap( mpChildren
);
116 sal_uInt16 nToken
= aMap
.Get( nPrefix
, rLocalName
);
117 if( nToken
!= XML_TOK_UNKNOWN
)
119 // call handle child, and pass down arguments
120 pContext
= HandleChild( nToken
, nPrefix
, rLocalName
, xAttrList
);
123 // error handling: create default context and generate warning
124 if( pContext
== NULL
)
126 GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT
, rLocalName
);
127 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
132 bool lcl_IsWhiteSpace( sal_Unicode c
)
134 return c
== sal_Unicode( ' ' )
135 || c
== sal_Unicode( 0x09 )
136 || c
== sal_Unicode( 0x0A )
137 || c
== sal_Unicode( 0x0D );
140 void TokenContext::Characters( const ::rtl::OUString
& rCharacters
)
142 // get iterators for string data
143 const sal_Unicode
* pBegin
= rCharacters
.getStr();
144 const sal_Unicode
* pEnd
= &( pBegin
[ rCharacters
.getLength() ] );
146 // raise error if non-whitespace character is found
147 if( ::std::find_if( pBegin
, pEnd
, ::std::not1(::std::ptr_fun(lcl_IsWhiteSpace
)) ) != pEnd
)
148 GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS
, rCharacters
);