Update git submodules
[LibreOffice.git] / xmloff / source / transform / FormPropOASISTContext.cxx
blob042b57ae07e55eef16aa6bf40faa114b8d2ffe56
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "MutableAttrList.hxx"
21 #include <xmloff/xmlnamespace.hxx>
22 #include <xmloff/namespacemap.hxx>
23 #include "ActionMapTypesOASIS.hxx"
24 #include "AttrTransformerAction.hxx"
25 #include "TransformerActions.hxx"
26 #include "TransformerBase.hxx"
27 #include "FormPropOASISTContext.hxx"
28 #include <osl/diagnose.h>
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::xml::sax;
32 using namespace ::xmloff::token;
34 XMLTokenEnum XMLFormPropOASISTransformerContext::GetValueType(
35 std::u16string_view rValue )
37 XMLTokenEnum eRet = XML_DOUBLE;
38 bool bNeg = false;
39 sal_uInt32 nVal = 0;
41 sal_Int32 nPos = 0;
42 sal_Int32 nLen = rValue.size();
44 // skip white space
45 while( nPos < nLen && ' ' == rValue[nPos] )
46 nPos++;
48 if( nPos < nLen && '-' == rValue[nPos] )
50 bNeg = true;
51 nPos++;
54 // get number
55 bool bOverflow = false;
56 while( nPos < nLen &&
57 '0' <= rValue[nPos] &&
58 '9' >= rValue[nPos] )
60 nVal *= 10;
61 nVal += (rValue[nPos] - '0');
62 bOverflow |= (nVal > (bNeg ? 2147483648UL : 2147483647UL));
63 nPos++;
66 // skip white space
67 while( nPos < nLen && ' ' == rValue[nPos] )
68 nPos++;
70 if( nPos == nLen )
72 // It's an integer number
73 if( bOverflow )
74 eRet = XML_LONG;
75 else if( nVal > (bNeg ? 32768UL : 32767UL) )
76 eRet = XML_INT;
77 else
78 eRet = XML_SHORT;
81 return eRet;
84 XMLFormPropOASISTransformerContext::XMLFormPropOASISTransformerContext(
85 XMLTransformerBase& rImp,
86 const OUString& rQName,
87 XMLTokenEnum eLocalName ) :
88 XMLRenameElemTransformerContext( rImp, rQName, XML_NAMESPACE_FORM,
89 XML_PROPERTY ),
90 m_bIsList( XML_LIST_PROPERTY == eLocalName),
91 m_bIsListValue( XML_LIST_VALUE == eLocalName)
95 XMLFormPropOASISTransformerContext::~XMLFormPropOASISTransformerContext()
99 void XMLFormPropOASISTransformerContext::StartElement(
100 const Reference< XAttributeList >& rAttrList )
103 XMLTransformerActions *pActions =
104 GetTransformer().GetUserDefinedActions( OASIS_FORM_PROP_ACTIONS );
105 OSL_ENSURE( pActions, "go no actions" );
107 rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
108 new XMLMutableAttributeList( rAttrList );
109 Reference< XAttributeList > xAttrList( pMutableAttrList );
111 sal_Int16 nValueTypeAttr = -1;
112 OUString aValue;
113 bool bIsVoid = false;
114 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
115 for( sal_Int16 i=0; i < nAttrCount; i++ )
117 const OUString aAttrName = xAttrList->getNameByIndex( i );
118 OUString aLocalName;
119 sal_uInt16 nPrefix =
120 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
121 &aLocalName );
122 XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
123 XMLTransformerActions::const_iterator aIter =
124 pActions->find( aKey );
125 if( aIter != pActions->end() )
127 const OUString aAttrValue = xAttrList->getValueByIndex( i );
128 switch( (*aIter).second.m_nActionType )
130 case XML_ATACTION_RENAME:
131 if( IsXMLToken( aLocalName, XML_VALUE_TYPE ) )
133 if( IsXMLToken( aAttrValue, XML_FLOAT ) )
135 nValueTypeAttr = i;
137 else if( IsXMLToken( aAttrValue, XML_VOID ) )
139 pMutableAttrList->SetValueByIndex( i,
140 GetXMLToken( XML_SHORT ) );
141 bIsVoid = true;
145 OUString aNewAttrQName(
146 GetTransformer().GetNamespaceMap().GetQNameByKey(
147 (*aIter).second.GetQNamePrefixFromParam1(),
148 ::xmloff::token::GetXMLToken(
149 (*aIter).second.GetQNameTokenFromParam1()) ) );
150 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
152 break;
153 case XML_ATACTION_REMOVE:
154 if( !IsXMLToken( aLocalName, XML_CURRENCY ) )
155 aValue = aAttrValue;
156 pMutableAttrList->RemoveAttributeByIndex( i );
157 --i;
158 --nAttrCount;
159 break;
160 default:
161 OSL_ENSURE( false, "unknown action" );
162 break;
166 if( m_bIsList )
168 OUString aNewAttrQName(
169 GetTransformer().GetNamespaceMap().GetQNameByKey(
170 XML_NAMESPACE_FORM,
171 GetXMLToken( XML_PROPERTY_IS_LIST ) ) );
172 pMutableAttrList->AddAttribute( aNewAttrQName,
173 GetXMLToken( XML_TRUE ) );
176 if( nValueTypeAttr != -1 )
177 pMutableAttrList->SetValueByIndex( nValueTypeAttr,
178 GetXMLToken( GetValueType( aValue ) ) );
180 if( !m_bIsListValue )
181 XMLRenameElemTransformerContext::StartElement( xAttrList );
182 if( m_bIsList )
183 return;
185 pMutableAttrList = new XMLMutableAttributeList;
186 xAttrList = pMutableAttrList;
187 if( bIsVoid )
189 OUString aNewAttrQName(
190 GetTransformer().GetNamespaceMap().GetQNameByKey(
191 XML_NAMESPACE_FORM, GetXMLToken( XML_PROPERTY_IS_VOID ) ) );
192 pMutableAttrList->AddAttribute( aNewAttrQName,
193 GetXMLToken( XML_TRUE ) );
196 OUString aValueElemQName(
197 GetTransformer().GetNamespaceMap().GetQNameByKey(
198 XML_NAMESPACE_FORM, GetXMLToken( XML_PROPERTY_VALUE ) ) );
199 GetTransformer().GetDocHandler()->startElement( aValueElemQName,
200 xAttrList );
201 GetTransformer().GetDocHandler()->characters( aValue );
202 GetTransformer().GetDocHandler()->endElement( aValueElemQName );
205 void XMLFormPropOASISTransformerContext::EndElement()
207 if( !m_bIsListValue )
208 XMLRenameElemTransformerContext::EndElement();
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */