1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <tools/debug.hxx>
31 #include <xmloff/xmluconv.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <xmloff/XMLConstantsPropertyHandler.hxx>
35 
36 using namespace ::com::sun::star::uno;
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 
40 using ::xmloff::token::XMLTokenEnum;
41 
42 XMLConstantsPropertyHandler::XMLConstantsPropertyHandler(
43 	const SvXMLEnumMapEntry *pM,
44 	enum XMLTokenEnum eDflt ) :
45 		pMap( pM ),
46 		eDefault( eDflt )
47 {
48 }
49 
50 XMLConstantsPropertyHandler::~XMLConstantsPropertyHandler()
51 {
52 }
53 
54 sal_Bool XMLConstantsPropertyHandler::importXML(
55 	const OUString& rStrImpValue,
56 	Any& rValue,
57 	const SvXMLUnitConverter& ) const
58 {
59 	sal_uInt16 nEnum;
60 	sal_Bool bRet = SvXMLUnitConverter::convertEnum(
61         nEnum, rStrImpValue, pMap );
62 
63 	if( bRet )
64 		rValue <<= (sal_Int16)nEnum;
65 
66 	return bRet;
67 }
68 
69 sal_Bool XMLConstantsPropertyHandler::exportXML(
70 	OUString& rStrExpValue,
71 	const Any& rValue,
72 	const SvXMLUnitConverter& ) const
73 {
74 	OUStringBuffer aOut;
75 
76 	sal_Bool bRet = false;
77 
78 	sal_Int32 nEnum = 0;
79 
80 	if( rValue.hasValue() && (rValue.getValueTypeClass() == TypeClass_ENUM))
81 	{
82 		nEnum = *((sal_Int32*)rValue.getValue());
83 		bRet = true;
84 	}
85 	else
86 	{
87 		bRet = (rValue >>= nEnum );
88 	}
89 
90 	if( bRet )
91 	{
92 		if( (nEnum >= 0) && (nEnum <= 0xffff) )
93 		{
94 			sal_uInt16 nConst = static_cast<sal_uInt16>( nEnum );
95 
96 			bRet = SvXMLUnitConverter::convertEnum(
97                 aOut, nConst, pMap, eDefault );
98 
99 			rStrExpValue = aOut.makeStringAndClear();
100 		}
101 		else
102 		{
103 			DBG_ERROR("XMLConstantsPropertyHandler::exportXML() constant is out of range for implementation using sal_uInt16");
104 		}
105 	}
106 	else
107 	{
108 		DBG_ERROR("XMLConstantsPropertyHandler::exportXML() could not convert any to sal_Int32");
109 	}
110 
111 	return bRet;
112 }
113 
114