Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "TokenContext.hxx"
21 : #include <xmloff/xmltkmap.hxx>
22 : #include <xmloff/xmlimp.hxx>
23 : #include <xmloff/nmspmap.hxx>
24 : #include <xmloff/xmlerror.hxx>
25 :
26 : #include <tools/debug.hxx>
27 :
28 : using com::sun::star::uno::Reference;
29 : using com::sun::star::xml::sax::XAttributeList;
30 :
31 :
32 : struct SvXMLTokenMapEntry aEmptyMap[1] =
33 : {
34 : XML_TOKEN_MAP_END
35 : };
36 :
37 :
38 0 : TokenContext::TokenContext( SvXMLImport& rImport,
39 : sal_uInt16 nPrefix,
40 : const OUString& rLocalName,
41 : const SvXMLTokenMapEntry* pAttributes,
42 : const SvXMLTokenMapEntry* pChildren )
43 : : SvXMLImportContext( rImport, nPrefix, rLocalName ),
44 : mpAttributes( pAttributes ),
45 0 : mpChildren( pChildren )
46 : {
47 0 : }
48 :
49 0 : TokenContext::~TokenContext()
50 : {
51 0 : }
52 :
53 0 : void TokenContext::StartElement(
54 : const Reference<XAttributeList>& xAttributeList )
55 : {
56 : // iterate over attributes
57 : // - if in map: call HandleAttribute
58 : // - xmlns:... : ignore
59 : // - other: warning
60 : DBG_ASSERT( mpAttributes != NULL, "no token map for attributes" );
61 0 : SvXMLTokenMap aMap( mpAttributes );
62 :
63 0 : sal_Int16 nCount = xAttributeList->getLength();
64 0 : for( sal_Int16 i = 0; i < nCount; i++ )
65 : {
66 : // get key/local-name pair from namespace map
67 0 : OUString sLocalName;
68 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
69 0 : GetKeyByAttrName( xAttributeList->getNameByIndex(i), &sLocalName );
70 :
71 : // get token from token map
72 0 : sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
73 :
74 : // and the value...
75 0 : const OUString& rValue = xAttributeList->getValueByIndex(i);
76 :
77 0 : if( nToken != XML_TOK_UNKNOWN )
78 : {
79 0 : HandleAttribute( nToken, rValue );
80 : }
81 0 : else if( nPrefix != XML_NAMESPACE_XMLNS )
82 : {
83 : // error handling, for all attribute that are not
84 : // namespace declarations
85 0 : GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE,
86 0 : sLocalName, rValue);
87 : }
88 0 : }
89 0 : }
90 :
91 0 : SvXMLImportContext* TokenContext::CreateChildContext(
92 : sal_uInt16 nPrefix,
93 : const OUString& rLocalName,
94 : const Reference<XAttributeList>& xAttrList )
95 : {
96 : // call HandleChild for elements in token map. Ignore other content.
97 :
98 0 : SvXMLImportContext* pContext = NULL;
99 :
100 : DBG_ASSERT( mpChildren != NULL, "no token map for child elements" );
101 0 : SvXMLTokenMap aMap( mpChildren );
102 0 : sal_uInt16 nToken = aMap.Get( nPrefix, rLocalName );
103 0 : if( nToken != XML_TOK_UNKNOWN )
104 : {
105 : // call handle child, and pass down arguments
106 0 : pContext = HandleChild( nToken, nPrefix, rLocalName, xAttrList );
107 : }
108 :
109 : // error handling: create default context and generate warning
110 0 : if( pContext == NULL )
111 : {
112 0 : GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT, rLocalName );
113 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
114 : }
115 0 : return pContext;
116 : }
117 :
118 0 : static bool lcl_IsWhiteSpace( sal_Unicode c )
119 : {
120 : return c == ' '
121 0 : || c == sal_Unicode( 0x09 )
122 0 : || c == sal_Unicode( 0x0A )
123 0 : || c == sal_Unicode( 0x0D );
124 : }
125 :
126 0 : void TokenContext::Characters( const OUString& rCharacters )
127 : {
128 : // get iterators for string data
129 0 : const sal_Unicode* pBegin = rCharacters.getStr();
130 0 : const sal_Unicode* pEnd = &( pBegin[ rCharacters.getLength() ] );
131 :
132 : // raise error if non-whitespace character is found
133 0 : if( ::std::find_if( pBegin, pEnd, ::std::not1(::std::ptr_fun(lcl_IsWhiteSpace)) ) != pEnd )
134 0 : GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS, rCharacters );
135 0 : }
136 :
137 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|