LibreOffice Module xmloff (master)  1
SchXMLParagraphContext.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 
22 
23 #include <sal/log.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/xmlnamespace.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/namespacemap.hxx>
28 
29 #include <com/sun/star/xml/sax/XAttributeList.hpp>
30 
31 using namespace com::sun::star;
32 using namespace ::xmloff::token;
33 
35  const OUString& rLocalName,
36  OUString& rText,
37  OUString * pOutId /* = 0 */ ) :
38  SvXMLImportContext( rImport, XML_NAMESPACE_TEXT, rLocalName ),
39  mrText( rText ),
40  mpId( pOutId )
41 {
42 }
43 
45  OUString& rText ) :
46  SvXMLImportContext( rImport ),
47  mrText( rText ),
48  mpId( nullptr )
49 {
50 }
51 
53 {}
54 
55 void SchXMLParagraphContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
56 {
57  // remember the id. It is used for storing the original cell range string in
58  // a local table (cached data)
59  if( !mpId )
60  return;
61 
62  sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
63  bool bHaveXmlId( false );
64 
65  for( sal_Int16 i = 0; i < nAttrCount; i++ )
66  {
67  OUString sAttrName = xAttrList->getNameByIndex( i );
68  OUString aLocalName;
69  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
70 
71  if (IsXMLToken(aLocalName, XML_ID))
72  {
73  if (nPrefix == XML_NAMESPACE_XML)
74  {
75  (*mpId) = xAttrList->getValueByIndex( i );
76  bHaveXmlId = true;
77  }
78  if (nPrefix == XML_NAMESPACE_TEXT)
79  { // text:id shall be ignored if xml:id exists
80  if (!bHaveXmlId)
81  {
82  (*mpId) = xAttrList->getValueByIndex( i );
83  }
84  }
85  }
86  }
87 }
88 
90 {
91  mrText = maBuffer.makeStringAndClear();
92 }
93 
95  sal_Int32 /*nElement*/,
96  const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
97 {
98  // remember the id. It is used for storing the original cell range string in
99  // a local table (cached data)
100  if( !mpId )
101  return;
102 
103  bool bHaveXmlId( false );
104 
105  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
106  {
107  switch(aIter.getToken())
108  {
109  case XML_ELEMENT(XML, XML_ID):
110  (*mpId) = aIter.toString();
111  bHaveXmlId = true;
112  break;
113  case XML_ELEMENT(TEXT, XML_ID):
114  { // text:id shall be ignored if xml:id exists
115  if (!bHaveXmlId)
116  {
117  (*mpId) = aIter.toString();
118  }
119  break;
120  }
121  default:
122  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << aIter.toString());
123  }
124  }
125 }
126 
128 {
129  mrText = maBuffer.makeStringAndClear();
130 }
131 
133  sal_uInt16 nPrefix,
134  const OUString& rLocalName,
135  const uno::Reference< xml::sax::XAttributeList >& )
136 {
137  if( nPrefix == XML_NAMESPACE_TEXT )
138  {
140  {
141  maBuffer.append( u'\x0009'); // tabulator
142  }
143  else if( rLocalName == ::xmloff::token::GetXMLToken( ::xmloff::token::XML_LINE_BREAK ))
144  {
145  maBuffer.append( u'\x000A'); // linefeed
146  }
147  }
148 
149  return nullptr;
150 }
151 
152 void SchXMLParagraphContext::Characters( const OUString& rChars )
153 {
154  maBuffer.append( rChars );
155 }
156 
157 void SchXMLParagraphContext::characters( const OUString& rChars )
158 {
159  maBuffer.append( rChars );
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:394
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2000
constexpr sal_uInt16 XML_NAMESPACE_XML
SchXMLParagraphContext(SvXMLImport &rImport, const OUString &rLocalName, OUString &rText, OUString *pOutId=nullptr)
int i
constexpr sal_uInt16 XML_NAMESPACE_TEXT
float u
virtual ~SchXMLParagraphContext() override
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
virtual void Characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3378
Handling of tokens in XML:
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
#define SAL_WARN(area, stream)
virtual void SAL_CALL characters(const OUString &rChars) override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
TEXT