1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <xmloff/HatchStyle.hxx>
22 #include <com/sun/star/drawing/Hatch.hpp>
24 #include <sax/tools/converter.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlexp.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
34 #include <tools/debug.hxx>
35 #include <xmloff/xmltkmap.hxx>
37 using namespace ::com::sun::star
;
39 using namespace ::xmloff::token
;
41 enum SvXMLTokenMapAttrs
44 XML_TOK_HATCH_DISPLAY_NAME
,
47 XML_TOK_HATCH_DISTANCE
,
48 XML_TOK_HATCH_ROTATION
,
49 XML_TOK_TABSTOP_END
=XML_TOK_UNKNOWN
52 SvXMLEnumMapEntry
const pXML_HatchStyle_Enum
[] =
54 { XML_HATCHSTYLE_SINGLE
, drawing::HatchStyle_SINGLE
},
55 { XML_HATCHSTYLE_DOUBLE
, drawing::HatchStyle_DOUBLE
},
56 { XML_HATCHSTYLE_TRIPLE
, drawing::HatchStyle_TRIPLE
},
57 { XML_TOKEN_INVALID
, 0 }
62 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport
& rImp
)
67 XMLHatchStyleImport::~XMLHatchStyleImport()
71 bool XMLHatchStyleImport::importXML(
72 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
,
76 static const SvXMLTokenMapEntry aHatchAttrTokenMap
[] =
78 { XML_NAMESPACE_DRAW
, XML_NAME
, XML_TOK_HATCH_NAME
},
79 { XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
, XML_TOK_HATCH_DISPLAY_NAME
},
80 { XML_NAMESPACE_DRAW
, XML_STYLE
, XML_TOK_HATCH_STYLE
},
81 { XML_NAMESPACE_DRAW
, XML_COLOR
, XML_TOK_HATCH_COLOR
},
82 { XML_NAMESPACE_DRAW
, XML_HATCH_DISTANCE
, XML_TOK_HATCH_DISTANCE
},
83 { XML_NAMESPACE_DRAW
, XML_ROTATION
, XML_TOK_HATCH_ROTATION
},
87 bool bHasName
= false;
88 bool bHasStyle
= false;
89 bool bHasColor
= false;
90 bool bHasDist
= false;
91 OUString aDisplayName
;
93 drawing::Hatch aHatch
;
94 aHatch
.Style
= drawing::HatchStyle_SINGLE
;
99 SvXMLTokenMap
aTokenMap( aHatchAttrTokenMap
);
100 SvXMLNamespaceMap rNamespaceMap
= rImport
.GetNamespaceMap();
101 SvXMLUnitConverter
& rUnitConverter
= rImport
.GetMM100UnitConverter();
103 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
104 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
106 const OUString
& rFullAttrName
= xAttrList
->getNameByIndex( i
);
107 OUString aStrAttrName
;
108 sal_uInt16 nPrefix
= rNamespaceMap
.GetKeyByAttrName( rFullAttrName
, &aStrAttrName
);
109 const OUString
& rStrValue
= xAttrList
->getValueByIndex( i
);
111 switch( aTokenMap
.Get( nPrefix
, aStrAttrName
) )
113 case XML_TOK_HATCH_NAME
:
115 rStrName
= rStrValue
;
119 case XML_TOK_HATCH_DISPLAY_NAME
:
120 aDisplayName
= rStrValue
;
122 case XML_TOK_HATCH_STYLE
:
125 bHasStyle
= SvXMLUnitConverter::convertEnum( eValue
, rStrValue
, pXML_HatchStyle_Enum
);
127 aHatch
.Style
= (drawing::HatchStyle
) eValue
;
130 case XML_TOK_HATCH_COLOR
:
132 bHasColor
= ::sax::Converter::convertColor(
133 aHatch
.Color
, rStrValue
);
136 case XML_TOK_HATCH_DISTANCE
:
137 bHasDist
= rUnitConverter
.convertMeasureToCore(
138 (sal_Int32
&)aHatch
.Distance
, rStrValue
);
140 case XML_TOK_HATCH_ROTATION
:
143 ::sax::Converter::convertNumber(nValue
, rStrValue
, 0, 3600);
144 aHatch
.Angle
= sal_Int16( nValue
);
149 SAL_INFO("xmloff.style", "Unknown token at import hatch style");
155 if( !aDisplayName
.isEmpty() )
157 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID
, rStrName
,
159 rStrName
= aDisplayName
;
162 bool bRet
= bHasName
&& bHasStyle
&& bHasColor
&& bHasDist
;
169 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport
& rExp
)
174 XMLHatchStyleExport::~XMLHatchStyleExport()
178 bool XMLHatchStyleExport::exportXML(
179 const OUString
& rStrName
,
180 const uno::Any
& rValue
)
183 drawing::Hatch aHatch
;
185 if( !rStrName
.isEmpty() )
187 if( rValue
>>= aHatch
)
192 SvXMLUnitConverter
& rUnitConverter
=
193 rExport
.GetMM100UnitConverter();
196 if( !SvXMLUnitConverter::convertEnum( aOut
, aHatch
.Style
, pXML_HatchStyle_Enum
) )
203 bool bEncoded
= false;
204 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
,
205 rExport
.EncodeStyleName( rStrName
,
208 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_DISPLAY_NAME
,
211 aStrValue
= aOut
.makeStringAndClear();
212 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_STYLE
, aStrValue
);
215 ::sax::Converter::convertColor(aOut
, aHatch
.Color
);
216 aStrValue
= aOut
.makeStringAndClear();
217 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_COLOR
, aStrValue
);
220 rUnitConverter
.convertMeasureToXML( aOut
, aHatch
.Distance
);
221 aStrValue
= aOut
.makeStringAndClear();
222 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_HATCH_DISTANCE
, aStrValue
);
225 ::sax::Converter::convertNumber(aOut
, sal_Int32(aHatch
.Angle
));
226 aStrValue
= aOut
.makeStringAndClear();
227 rExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_ROTATION
, aStrValue
);
230 SvXMLElementExport
rElem( rExport
, XML_NAMESPACE_DRAW
, XML_HATCH
,
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */