1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_xmloff.hxx" 26 #include <com/sun/star/style/TabAlign.hpp> 27 #include <rtl/ustrbuf.hxx> 28 #include <xmloff/xmltkmap.hxx> 29 #include <xmloff/nmspmap.hxx> 30 #include "xmloff/xmlnmspe.hxx" 31 #include <xmloff/xmlimp.hxx> 32 #include <com/sun/star/style/TabStop.hpp> 33 #include <xmloff/xmltoken.hxx> 34 #include "xmloff/i18nmap.hxx" 35 #include <xmloff/xmluconv.hxx> 36 37 #include "xmltabi.hxx" 38 39 #define _SVSTDARR_USHORTS 40 #include <svl/svstdarr.hxx> 41 42 using ::rtl::OUString; 43 using ::rtl::OUStringBuffer; 44 45 using namespace ::com::sun::star; 46 using namespace ::xmloff::token; 47 48 // --- 49 50 enum SvXMLTokenMapAttrs 51 { 52 XML_TOK_TABSTOP_POSITION, 53 XML_TOK_TABSTOP_TYPE, 54 XML_TOK_TABSTOP_CHAR, 55 XML_TOK_TABSTOP_LEADER_STYLE, 56 XML_TOK_TABSTOP_LEADER_TEXT, 57 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN 58 }; 59 60 static __FAR_DATA SvXMLTokenMapEntry aTabsAttributesAttrTokenMap[] = 61 { 62 { XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_TABSTOP_POSITION }, 63 { XML_NAMESPACE_STYLE, XML_TYPE, XML_TOK_TABSTOP_TYPE }, 64 { XML_NAMESPACE_STYLE, XML_CHAR, XML_TOK_TABSTOP_CHAR }, 65 { XML_NAMESPACE_STYLE, XML_LEADER_TEXT, XML_TOK_TABSTOP_LEADER_TEXT }, 66 { XML_NAMESPACE_STYLE, XML_LEADER_STYLE, XML_TOK_TABSTOP_LEADER_STYLE }, 67 XML_TOKEN_MAP_END 68 }; 69 70 // --- 71 72 class SvxXMLTabStopContext_Impl : public SvXMLImportContext 73 { 74 private: 75 style::TabStop aTabStop; 76 77 public: 78 TYPEINFO(); 79 80 SvxXMLTabStopContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx, 81 const OUString& rLName, 82 const uno::Reference< xml::sax::XAttributeList > & xAttrList ); 83 84 virtual ~SvxXMLTabStopContext_Impl(); 85 86 virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, 87 const OUString& rLocalName, 88 const uno::Reference< xml::sax::XAttributeList > & xAttrList ); 89 90 const style::TabStop& getTabStop() const { return aTabStop; } 91 }; 92 93 TYPEINIT1( SvxXMLTabStopContext_Impl, SvXMLImportContext ); 94 95 SvxXMLTabStopContext_Impl::SvxXMLTabStopContext_Impl( 96 SvXMLImport& rImport, sal_uInt16 nPrfx, 97 const OUString& rLName, 98 const uno::Reference< xml::sax::XAttributeList > & xAttrList ) 99 : SvXMLImportContext( rImport, nPrfx, rLName ) 100 { 101 aTabStop.Position = 0; 102 aTabStop.Alignment = style::TabAlign_LEFT; 103 aTabStop.DecimalChar = sal_Unicode( ',' ); 104 aTabStop.FillChar = sal_Unicode( ' ' ); 105 sal_Unicode cTextFillChar = 0; 106 107 SvXMLTokenMap aTokenMap( aTabsAttributesAttrTokenMap ); 108 109 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; 110 for( sal_Int16 i=0; i < nAttrCount; i++ ) 111 { 112 const OUString& rAttrName = xAttrList->getNameByIndex( i ); 113 OUString aLocalName; 114 sal_uInt16 nPrefix = 115 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, 116 &aLocalName ); 117 const OUString& rValue = xAttrList->getValueByIndex( i ); 118 119 sal_Int32 nVal; 120 switch( aTokenMap.Get( nPrefix, aLocalName ) ) 121 { 122 case XML_TOK_TABSTOP_POSITION: 123 if( GetImport().GetMM100UnitConverter().convertMeasure( nVal, 124 rValue ) ) 125 aTabStop.Position = nVal; 126 break; 127 case XML_TOK_TABSTOP_TYPE: 128 if( IsXMLToken( rValue, XML_LEFT ) ) 129 { 130 aTabStop.Alignment = style::TabAlign_LEFT; 131 } 132 else if( IsXMLToken( rValue, XML_RIGHT ) ) 133 { 134 aTabStop.Alignment = style::TabAlign_RIGHT; 135 } 136 else if( IsXMLToken( rValue, XML_CENTER ) ) 137 { 138 aTabStop.Alignment = style::TabAlign_CENTER; 139 } 140 else if( IsXMLToken( rValue, XML_CHAR ) ) 141 { 142 aTabStop.Alignment = style::TabAlign_DECIMAL; 143 } 144 else if( IsXMLToken( rValue, XML_DEFAULT ) ) 145 { 146 aTabStop.Alignment = style::TabAlign_DEFAULT; 147 } 148 break; 149 case XML_TOK_TABSTOP_CHAR: 150 if( 0 != rValue.getLength() ) 151 aTabStop.DecimalChar = rValue[0]; 152 break; 153 case XML_TOK_TABSTOP_LEADER_STYLE: 154 if( IsXMLToken( rValue, XML_NONE ) ) 155 aTabStop.FillChar = ' '; 156 else if( IsXMLToken( rValue, XML_DOTTED ) ) 157 aTabStop.FillChar = '.'; 158 else 159 aTabStop.FillChar = '_'; 160 break; 161 case XML_TOK_TABSTOP_LEADER_TEXT: 162 if( 0 != rValue.getLength() ) 163 cTextFillChar = rValue[0]; 164 break; 165 } 166 } 167 168 if( cTextFillChar != 0 && aTabStop.FillChar != ' ' ) 169 aTabStop.FillChar = cTextFillChar; 170 } 171 172 SvxXMLTabStopContext_Impl::~SvxXMLTabStopContext_Impl() 173 { 174 } 175 176 SvXMLImportContext *SvxXMLTabStopContext_Impl::CreateChildContext( 177 sal_uInt16 nPrefix, 178 const OUString& rLocalName, 179 const uno::Reference< xml::sax::XAttributeList > & ) 180 { 181 return new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 182 } 183 184 185 186 187 typedef SvxXMLTabStopContext_Impl *SvxXMLTabStopContext_Impl_ImplPtr; 188 SV_DECL_PTRARR( SvxXMLTabStopArray_Impl, SvxXMLTabStopContext_Impl_ImplPtr, 20, 5 ) 189 190 191 // --- 192 193 TYPEINIT1( SvxXMLTabStopImportContext, XMLElementPropertyContext ); 194 195 SvxXMLTabStopImportContext::SvxXMLTabStopImportContext( 196 SvXMLImport& rImport, sal_uInt16 nPrfx, 197 const OUString& rLName, 198 const XMLPropertyState& rProp, 199 ::std::vector< XMLPropertyState > &rProps ) 200 : XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps ), 201 mpTabStops( NULL ) 202 { 203 } 204 205 SvxXMLTabStopImportContext::~SvxXMLTabStopImportContext() 206 { 207 if( mpTabStops ) 208 { 209 sal_uInt16 nCount = mpTabStops->Count(); 210 while( nCount ) 211 { 212 nCount--; 213 SvxXMLTabStopContext_Impl *pTabStop = (*mpTabStops)[nCount]; 214 mpTabStops->Remove( nCount, 1 ); 215 pTabStop->ReleaseRef(); 216 } 217 } 218 219 delete mpTabStops; 220 } 221 222 SvXMLImportContext *SvxXMLTabStopImportContext::CreateChildContext( 223 sal_uInt16 nPrefix, 224 const OUString& rLocalName, 225 const uno::Reference< xml::sax::XAttributeList > & xAttrList ) 226 { 227 SvXMLImportContext *pContext = 0; 228 229 if( XML_NAMESPACE_STYLE == nPrefix && IsXMLToken( rLocalName, XML_TAB_STOP ) ) 230 { 231 // create new tabstop import context 232 SvxXMLTabStopContext_Impl *pTabStopContext = 233 new SvxXMLTabStopContext_Impl( GetImport(), nPrefix, rLocalName, 234 xAttrList ); 235 236 // add new tabstop to array of tabstops 237 if( !mpTabStops ) 238 mpTabStops = new SvxXMLTabStopArray_Impl; 239 240 mpTabStops->Insert( pTabStopContext, mpTabStops->Count() ); 241 pTabStopContext->AddRef(); 242 243 pContext = pTabStopContext; 244 } 245 else 246 { 247 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 248 } 249 250 return pContext; 251 } 252 253 void SvxXMLTabStopImportContext::EndElement( ) 254 { 255 sal_uInt16 nCount = mpTabStops ? mpTabStops->Count() : 0; 256 uno::Sequence< style::TabStop> aSeq( nCount ); 257 258 if( mpTabStops ) 259 { 260 sal_uInt16 nNewCount = 0; 261 262 style::TabStop* pTabStops = aSeq.getArray(); 263 for( sal_uInt16 i=0; i < nCount; i++ ) 264 { 265 SvxXMLTabStopContext_Impl *pTabStopContext = (*mpTabStops)[i]; 266 const style::TabStop& rTabStop = pTabStopContext->getTabStop(); 267 sal_Bool bDflt = style::TabAlign_DEFAULT == rTabStop.Alignment; 268 if( !bDflt || 0==i ) 269 { 270 *pTabStops++ = pTabStopContext->getTabStop(); 271 nNewCount++; 272 } 273 if( bDflt && 0==i ) 274 break; 275 } 276 277 if( nCount != nNewCount ) 278 aSeq.realloc( nNewCount ); 279 } 280 aProp.maValue <<= aSeq; 281 282 SetInsert( sal_True ); 283 XMLElementPropertyContext::EndElement(); 284 285 } 286 287 288 289 290