LibreOffice Module xmloff (master)  1
FormPropOOoTContext.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 "MutableAttrList.hxx"
21 #include <xmloff/xmlnmspe.hxx>
22 #include <xmloff/nmspmap.hxx>
23 #include "IgnoreTContext.hxx"
24 #include "ActionMapTypesOOo.hxx"
26 #include "TransformerActions.hxx"
27 #include "TransformerBase.hxx"
28 #include "FormPropOOoTContext.hxx"
29 #include <osl/diagnose.h>
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::xml::sax;
33 using namespace ::xmloff::token;
34 
36 {
37  OUString m_aCharacters;
38  bool m_bIsVoid;
39 
40 public:
41  // element content persistence only
43  const OUString& rQName );
44 
45  virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
46 
47  virtual void EndElement() override;
48 
49  virtual void Characters( const OUString& rChars ) override;
50 
51  virtual bool IsPersistent() const override;
52 
53  bool IsVoid() const { return m_bIsVoid; }
54  const OUString& GetTextContent() const { return m_aCharacters; }
55 };
56 
58  XMLTransformerBase& rTransformer,
59  const OUString& rQName ) :
60  XMLTransformerContext( rTransformer, rQName ),
61  m_bIsVoid( false )
62 {
63 }
64 
66  const Reference< XAttributeList >& rAttrList )
67 {
68  sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
69  for( sal_Int16 i=0; i < nAttrCount; i++ )
70  {
71  const OUString& rAttrName = rAttrList->getNameByIndex( i );
72  OUString aLocalName;
73  sal_uInt16 nPrefix =
75  &aLocalName );
76  if( XML_NAMESPACE_FORM == nPrefix &&
77  IsXMLToken( aLocalName, XML_PROPERTY_IS_VOID ) &&
78  IsXMLToken( rAttrList->getValueByIndex( i ), XML_TRUE ) )
79  m_bIsVoid = true;
80  }
81 }
82 
84 {
85 }
86 
87 void XMLFormPropValueTContext_Impl::Characters( const OUString& rChars )
88 {
89  m_aCharacters += rChars;
90 }
91 
93 {
94  return true;
95 }
96 
98  XMLTransformerBase& rImp,
99  const OUString& rQName ) :
100  XMLTransformerContext( rImp, rQName ),
101  m_aElemQName( rQName ),
102  m_nValueTypeAttr( -1 ),
103  m_eValueToken( XML_VALUE ),
104  m_eValueTypeToken( XML_TOKEN_END ),
105  m_bIsList( false )
106 {
107 }
108 
110 {
111 }
112 
114  sal_uInt16 nPrefix,
115  const OUString& rLocalName,
116  const OUString& rQName,
117  const Reference< XAttributeList >& )
118 {
120 
121  if( XML_NAMESPACE_FORM == nPrefix &&
122  IsXMLToken( rLocalName, XML_PROPERTY_VALUE ) )
123  {
124  if( m_bIsList )
125  {
126  pContext.set(new XMLFormPropValueTContext_Impl( GetTransformer(),
127  rQName ));
128  }
129  else if( !m_xValueContext.is() )
130  {
133  pContext.set(m_xValueContext.get());
134  }
135  }
136 
137  // default is ignore
138  if( !pContext.is() )
139  pContext.set(new XMLIgnoreTransformerContext( GetTransformer(), rQName,
140  true, true ));
141  return pContext;
142 }
143 
145  const Reference< XAttributeList >& rAttrList )
146 {
147 
148  XMLTransformerActions *pActions =
150  OSL_ENSURE( pActions, "go no actions" );
151 
152  XMLMutableAttributeList *pMutableAttrList =
153  new XMLMutableAttributeList( rAttrList, true );
154  m_xAttrList = pMutableAttrList;
155 
156  OUString aValueType;
157  sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
158  for( sal_Int16 i=0; i < nAttrCount; i++ )
159  {
160  const OUString& rAttrName = rAttrList->getNameByIndex( i );
161  OUString aLocalName;
162  sal_uInt16 nPrefix =
164  &aLocalName );
165  XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
166  XMLTransformerActions::const_iterator aIter =
167  pActions->find( aKey );
168  if( aIter != pActions->end() )
169  {
170  const OUString& rAttrValue = rAttrList->getValueByIndex( i );
171  switch( (*aIter).second.m_nActionType )
172  {
173  case XML_ATACTION_RENAME:
174  if( IsXMLToken( aLocalName, XML_PROPERTY_TYPE ) )
175  {
176  aValueType = rAttrValue;
178  }
179  {
180  OUString aNewAttrQName(
181  GetTransformer().GetNamespaceMap().GetQNameByKey(
182  (*aIter).second.GetQNamePrefixFromParam1(),
184  (*aIter).second.GetQNameTokenFromParam1()) ) );
185  pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
186  }
187  break;
188  case XML_ATACTION_REMOVE:
189  if( IsXMLToken( aLocalName, XML_PROPERTY_IS_LIST ) )
190  {
191  m_aElemQName =
194  m_bIsList = true;
195  }
196  pMutableAttrList->RemoveAttributeByIndex( i );
197  --i;
198  --nAttrCount;
199  break;
200  default:
201  OSL_ENSURE( false, "unknown action" );
202  break;
203  }
204  }
205  }
206 
207  if( !aValueType.isEmpty() )
208  {
209  if( IsXMLToken( aValueType, XML_STRING ) )
211  else if( IsXMLToken( aValueType, XML_BOOLEAN ) )
213  else if( IsXMLToken( aValueType, XML_SHORT ) ||
214  IsXMLToken( aValueType, XML_INT ) ||
215  IsXMLToken( aValueType, XML_LONG ) ||
216  IsXMLToken( aValueType, XML_DOUBLE ) )
218  }
219 }
220 
222 {
223  if( m_xValueContext.is() )
224  {
225  if( m_xValueContext->IsVoid() )
226  {
228  }
229  else
230  {
231  OUString aAttrQName(
232  GetTransformer().GetNamespaceMap().GetQNameByKey(
234  static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
235  ->AddAttribute( aAttrQName, m_xValueContext->GetTextContent() );
236  }
237  }
238 
240  {
241  static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
242  ->SetValueByIndex( m_nValueTypeAttr ,
244  }
245 
247  GetTransformer().GetDocHandler()->endElement( m_aElemQName );
248 }
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3428
virtual ~XMLFormPropOOoTransformerContext() override
XMLFormPropOOoTransformerContext(XMLTransformerBase &rTransformer, const OUString &rQName)
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
Definition: nmspmap.cxx:437
XMLFormPropValueTContext_Impl(XMLTransformerBase &rTransformer, const OUString &rQName)
SvXMLNamespaceMap & GetNamespaceMap()
virtual sal_Int16 SAL_CALL getLength() override
::xmloff::token::XMLTokenEnum m_eValueTypeToken
const OUString & GetTextContent() const
int i
void RemoveAttributeByIndex(sal_Int16 i)
void RenameAttributeByIndex(sal_Int16 i, const OUString &rNewName)
OUString GetQNameByKey(sal_uInt16 nKey, const OUString &rLocalName, bool bCache=true) const
Definition: nmspmap.cxx:187
constexpr sal_uInt16 XML_NAMESPACE_FORM
Definition: xmlnmspe.hxx:44
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
virtual XMLTransformerActions * GetUserDefinedActions(sal_uInt16 n)
XMLTransformerBase & GetTransformer()
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3372
Handling of tokens in XML:
::xmloff::token::XMLTokenEnum m_eValueToken
css::uno::Reference< css::xml::sax::XAttributeList > m_xAttrList
virtual void Characters(const OUString &rChars) override
::rtl::Reference< XMLFormPropValueTContext_Impl > m_xValueContext
virtual bool IsPersistent() const override
virtual rtl::Reference< XMLTransformerContext > CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const OUString &rQName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
virtual void EndElement() override
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
Definition: xmlnmspe.hxx:29