bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / transform / FormPropOASISTContext.cxx
blob8807ee564e8dbbfd773f8beccba5257cac41019d
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/xmlnmspe.hxx>
22 #include <xmloff/nmspmap.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 TYPEINIT1( XMLFormPropOASISTransformerContext,
35 XMLRenameElemTransformerContext );
37 XMLTokenEnum XMLFormPropOASISTransformerContext::GetValueType(
38 const OUString& rValue )
40 XMLTokenEnum eRet = XML_DOUBLE;
41 bool bNeg = false;
42 sal_uInt32 nVal = 0;
44 sal_Int32 nPos = 0;
45 sal_Int32 nLen = rValue.getLength();
47 // skip white space
48 while( nPos < nLen && ' ' == rValue[nPos] )
49 nPos++;
51 if( nPos < nLen && '-' == rValue[nPos] )
53 bNeg = true;
54 nPos++;
57 // get number
58 bool bOverflow = false;
59 while( nPos < nLen &&
60 '0' <= rValue[nPos] &&
61 '9' >= rValue[nPos] )
63 nVal *= 10;
64 nVal += (rValue[nPos] - '0');
65 bOverflow |= (nVal > (bNeg ? 2147483648UL : 2147483647UL));
66 nPos++;
69 // skip white space
70 while( nPos < nLen && ' ' == rValue[nPos] )
71 nPos++;
73 if( nPos == nLen )
75 // It's a integer number
76 if( bOverflow )
77 eRet = XML_LONG;
78 else if( nVal > (bNeg ? 32768UL : 32767UL) )
79 eRet = XML_INT;
80 else
81 eRet = XML_SHORT;
84 return eRet;
87 XMLFormPropOASISTransformerContext::XMLFormPropOASISTransformerContext(
88 XMLTransformerBase& rImp,
89 const OUString& rQName,
90 XMLTokenEnum eLocalName ) :
91 XMLRenameElemTransformerContext( rImp, rQName, XML_NAMESPACE_FORM,
92 XML_PROPERTY ),
93 m_bIsList( XML_LIST_PROPERTY == eLocalName),
94 m_bIsListValue( XML_LIST_VALUE == eLocalName)
98 XMLFormPropOASISTransformerContext::~XMLFormPropOASISTransformerContext()
102 void XMLFormPropOASISTransformerContext::StartElement(
103 const Reference< XAttributeList >& rAttrList )
106 XMLTransformerActions *pActions =
107 GetTransformer().GetUserDefinedActions( OASIS_FORM_PROP_ACTIONS );
108 OSL_ENSURE( pActions, "go no actions" );
110 XMLMutableAttributeList *pMutableAttrList =
111 new XMLMutableAttributeList( rAttrList );
112 Reference< XAttributeList > xAttrList( pMutableAttrList );
114 sal_Int16 nValueTypeAttr = -1;
115 OUString aValue;
116 bool bIsVoid = false;
117 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
118 for( sal_Int16 i=0; i < nAttrCount; i++ )
120 const OUString& rAttrName = xAttrList->getNameByIndex( i );
121 OUString aLocalName;
122 sal_uInt16 nPrefix =
123 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
124 &aLocalName );
125 XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
126 XMLTransformerActions::const_iterator aIter =
127 pActions->find( aKey );
128 if( !(aIter == pActions->end() ) )
130 const OUString& rAttrValue = xAttrList->getValueByIndex( i );
131 switch( (*aIter).second.m_nActionType )
133 case XML_ATACTION_RENAME:
134 if( IsXMLToken( aLocalName, XML_VALUE_TYPE ) )
136 if( IsXMLToken( rAttrValue, XML_FLOAT ) )
138 nValueTypeAttr = i;
140 else if( IsXMLToken( rAttrValue, XML_VOID ) )
142 pMutableAttrList->SetValueByIndex( i,
143 GetXMLToken( XML_SHORT ) );
144 bIsVoid = true;
148 OUString aNewAttrQName(
149 GetTransformer().GetNamespaceMap().GetQNameByKey(
150 (*aIter).second.GetQNamePrefixFromParam1(),
151 ::xmloff::token::GetXMLToken(
152 (*aIter).second.GetQNameTokenFromParam1()) ) );
153 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
155 break;
156 case XML_ATACTION_REMOVE:
157 if( !IsXMLToken( aLocalName, XML_CURRENCY ) )
158 aValue = rAttrValue;
159 pMutableAttrList->RemoveAttributeByIndex( i );
160 --i;
161 --nAttrCount;
162 break;
163 default:
164 OSL_ENSURE( false, "unknown action" );
165 break;
169 if( m_bIsList )
171 OUString aNewAttrQName(
172 GetTransformer().GetNamespaceMap().GetQNameByKey(
173 XML_NAMESPACE_FORM,
174 GetXMLToken( XML_PROPERTY_IS_LIST ) ) );
175 pMutableAttrList->AddAttribute( aNewAttrQName,
176 GetXMLToken( XML_TRUE ) );
179 if( nValueTypeAttr != -1 )
180 pMutableAttrList->SetValueByIndex( nValueTypeAttr,
181 GetXMLToken( GetValueType( aValue ) ) );
183 if( !m_bIsListValue )
184 XMLRenameElemTransformerContext::StartElement( xAttrList );
185 if( !m_bIsList )
187 pMutableAttrList =
188 new XMLMutableAttributeList;
189 xAttrList = pMutableAttrList;
190 if( bIsVoid )
192 OUString aNewAttrQName(
193 GetTransformer().GetNamespaceMap().GetQNameByKey(
194 XML_NAMESPACE_FORM, GetXMLToken( XML_PROPERTY_IS_VOID ) ) );
195 pMutableAttrList->AddAttribute( aNewAttrQName,
196 GetXMLToken( XML_TRUE ) );
199 OUString aValueElemQName(
200 GetTransformer().GetNamespaceMap().GetQNameByKey(
201 XML_NAMESPACE_FORM, GetXMLToken( XML_PROPERTY_VALUE ) ) );
202 GetTransformer().GetDocHandler()->startElement( aValueElemQName,
203 xAttrList );
204 GetTransformer().GetDocHandler()->characters( aValue );
205 GetTransformer().GetDocHandler()->endElement( aValueElemQName );
209 void XMLFormPropOASISTransformerContext::EndElement()
211 if( !m_bIsListValue )
212 XMLRenameElemTransformerContext::EndElement();
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */