LibreOffice Module xmloff (master)  1
TokenContext.cxx
Go to the documentation of this file.
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 <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include "TokenContext.hxx"
24 #include <xmloff/xmltkmap.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/namespacemap.hxx>
27 #include <xmloff/xmlerror.hxx>
28 
29 #include <algorithm>
30 
32 using com::sun::star::xml::sax::XAttributeList;
33 
35 {
37 };
38 
40  sal_uInt16 nPrefix,
41  const OUString& rLocalName,
42  const SvXMLTokenMapEntry* pAttributes,
43  const SvXMLTokenMapEntry* pChildren )
44  : SvXMLImportContext( rImport, nPrefix, rLocalName ),
45  mpAttributes( pAttributes ),
46  mpChildren( pChildren )
47 {
48 }
49 
51  const Reference<XAttributeList>& xAttributeList )
52 {
53  // iterate over attributes
54  // - if in map: call HandleAttribute
55  // - xmlns:... : ignore
56  // - other: warning
57  SAL_WARN_IF( mpAttributes == nullptr, "xmloff", "no token map for attributes" );
59 
60  sal_Int16 nCount = xAttributeList->getLength();
61  for( sal_Int16 i = 0; i < nCount; i++ )
62  {
63  // get key/local-name pair from namespace map
64  OUString sLocalName;
65  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
66  GetKeyByAttrName( xAttributeList->getNameByIndex(i), &sLocalName );
67 
68  // get token from token map
69  sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
70 
71  // and the value...
72  const OUString& rValue = xAttributeList->getValueByIndex(i);
73 
74  if( nToken != XML_TOK_UNKNOWN )
75  {
76  HandleAttribute( nToken, rValue );
77  }
78  else if( nPrefix != XML_NAMESPACE_XMLNS )
79  {
80  // error handling, for all attribute that are not
81  // namespace declarations
83  sLocalName, rValue);
84  }
85  }
86 }
87 
89  sal_uInt16 nPrefix,
90  const OUString& rLocalName,
91  const Reference<XAttributeList>& xAttrList )
92 {
93  // call HandleChild for elements in token map. Ignore other content.
94 
95  SvXMLImportContext* pContext = nullptr;
96 
97  SAL_WARN_IF( mpChildren == nullptr, "xmloff", "no token map for child elements" );
99  sal_uInt16 nToken = aMap.Get( nPrefix, rLocalName );
100  if( nToken != XML_TOK_UNKNOWN )
101  {
102  // call handle child, and pass down arguments
103  pContext = HandleChild( nToken, nPrefix, rLocalName, xAttrList );
104  }
105 
106  // error handling: create default context and generate warning
107  if( pContext == nullptr )
108  {
110  }
111  return pContext;
112 }
113 
115 {
116  return c == ' '
117  || c == u'\x0009'
118  || c == u'\x000A'
119  || c == u'\x000D';
120 }
121 
122 void TokenContext::Characters( const OUString& rCharacters )
123 {
124  // get iterators for string data
125  const sal_Unicode* pBegin = rCharacters.getStr();
126  const sal_Unicode* pEnd = &( pBegin[ rCharacters.getLength() ] );
127 
128  // raise error if non-whitespace character is found
129  if( !::std::all_of( pBegin, pEnd, lcl_IsWhiteSpace ) )
131 }
132 
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define XML_TOK_UNKNOWN
Definition: xmltkmap.hxx:32
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:402
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
HashMap_OWString_Interface aMap
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
const sal_uInt16 XML_NAMESPACE_XMLNS
sal_uInt16 sal_Unicode
const SvXMLTokenMapEntry aEmptyMap[1]
const SvXMLTokenMapEntry * mpAttributes
int nCount
#define XMLERROR_UNKNOWN_ELEMENT
Definition: xmlerror.hxx:63
static bool lcl_IsWhiteSpace(sal_Unicode c)
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
call HandleAttribute for each attribute in the token map; create a warning for all others...
int i
float u
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:99
const SvXMLTokenMapEntry * mpChildren
static token map
virtual SvXMLImportContext * HandleChild(sal_uInt16 nToken, sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)=0
will be called for each child element
virtual void Characters(const OUString &rChars) override
Create a warning for all non-namespace character content.
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
TokenContext(SvXMLImport &rImport, sal_uInt16 nPrefix, const OUString &rLocalName, const SvXMLTokenMapEntry *pAttributes, const SvXMLTokenMapEntry *pChildren)
static token map
DefTokenId nToken
#define SAL_WARN_IF(condition, area, stream)
virtual void HandleAttribute(sal_uInt16 nToken, const OUString &rValue)=0
will be called for each attribute
#define XMLERROR_UNKNOWN_ATTRIBUTE
Definition: xmlerror.hxx:62
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
call HandleChild for each child element in the token map; create a warning for all others...
#define XMLERROR_UNKNOWN_CHARACTERS
Definition: xmlerror.hxx:64