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 "XMLLineNumberingExport.hxx"
31 #include "com/sun/star/beans/XPropertySet.hpp"
32 #include "com/sun/star/text/XLineNumberingProperties.hpp"
33 #include <com/sun/star/style/LineNumberPosition.hpp>
34 #include <xmloff/xmlexp.hxx>
35 #include <xmloff/xmluconv.hxx>
36 #include "xmloff/xmlnmspe.hxx"
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/xmlnume.hxx>
39 
40 
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star;
43 using namespace ::xmloff::token;
44 
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
47 using ::com::sun::star::beans::XPropertySet;
48 using ::com::sun::star::text::XLineNumberingProperties;
49 
50 
51 XMLLineNumberingExport::XMLLineNumberingExport(SvXMLExport& rExp)
52 :	sCharStyleName(RTL_CONSTASCII_USTRINGPARAM("CharStyleName"))
53 ,	sCountEmptyLines(RTL_CONSTASCII_USTRINGPARAM("CountEmptyLines"))
54 ,	sCountLinesInFrames(RTL_CONSTASCII_USTRINGPARAM("CountLinesInFrames"))
55 ,	sDistance(RTL_CONSTASCII_USTRINGPARAM("Distance"))
56 ,	sInterval(RTL_CONSTASCII_USTRINGPARAM("Interval"))
57 ,	sSeparatorText(RTL_CONSTASCII_USTRINGPARAM("SeparatorText"))
58 ,	sNumberPosition(RTL_CONSTASCII_USTRINGPARAM("NumberPosition"))
59 ,	sNumberingType(RTL_CONSTASCII_USTRINGPARAM("NumberingType"))
60 ,	sIsOn(RTL_CONSTASCII_USTRINGPARAM("IsOn"))
61 ,	sRestartAtEachPage(RTL_CONSTASCII_USTRINGPARAM("RestartAtEachPage"))
62 ,	sSeparatorInterval(RTL_CONSTASCII_USTRINGPARAM("SeparatorInterval"))
63 ,	rExport(rExp)
64 {
65 }
66 
67 SvXMLEnumMapEntry __READONLY_DATA aLineNumberPositionMap[] =
68 {
69 	{ XML_LEFT,	    style::LineNumberPosition::LEFT },
70 	{ XML_RIGHT,	style::LineNumberPosition::RIGHT },
71 	{ XML_INSIDE,	style::LineNumberPosition::INSIDE },
72 	{ XML_OUTSIDE,  style::LineNumberPosition::OUTSIDE },
73 	{ XML_TOKEN_INVALID, 0 }
74 };
75 
76 
77 
78 void XMLLineNumberingExport::Export()
79 {
80 	// export element if we have line numbering info
81 	Reference<XLineNumberingProperties> xSupplier(rExport.GetModel(),
82 												  UNO_QUERY);
83 	if (xSupplier.is())
84 	{
85 		Reference<XPropertySet> xLineNumbering =
86 			xSupplier->getLineNumberingProperties();
87 
88 		if (xLineNumbering.is())
89 		{
90 			Any aAny;
91 
92 			// char style
93 			aAny = xLineNumbering->getPropertyValue(sCharStyleName);
94 			OUString sTmp;
95 			aAny >>= sTmp;
96 			if (sTmp.getLength() > 0)
97 			{
98 				rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME,
99 									 rExport.EncodeStyleName( sTmp ));
100 			}
101 
102 			// enable
103 			aAny = xLineNumbering->getPropertyValue(sIsOn);
104 			if (! *(sal_Bool*)aAny.getValue())
105 			{
106 				rExport.AddAttribute(XML_NAMESPACE_TEXT,
107                                      XML_NUMBER_LINES, XML_FALSE);
108 			}
109 
110 			// count empty lines
111 			aAny = xLineNumbering->getPropertyValue(sCountEmptyLines);
112 			if (! *(sal_Bool*)aAny.getValue())
113 			{
114 				rExport.AddAttribute(XML_NAMESPACE_TEXT,
115                                      XML_COUNT_EMPTY_LINES, XML_FALSE);
116 			}
117 
118 			// count in frames
119 			aAny = xLineNumbering->getPropertyValue(sCountLinesInFrames);
120 			if (*(sal_Bool*)aAny.getValue())
121 			{
122 				rExport.AddAttribute(XML_NAMESPACE_TEXT,
123                                      XML_COUNT_IN_TEXT_BOXES, XML_TRUE);
124 			}
125 
126 			// restart numbering
127 			aAny = xLineNumbering->getPropertyValue(sRestartAtEachPage);
128 			if (*(sal_Bool*)aAny.getValue())
129 			{
130 				rExport.AddAttribute(XML_NAMESPACE_TEXT,
131                                      XML_RESTART_ON_PAGE, XML_TRUE);
132 			}
133 
134 			// Distance
135 			aAny = xLineNumbering->getPropertyValue(sDistance);
136 			sal_Int32 nLength = 0;
137 			aAny >>= nLength;
138 			if (nLength != 0)
139 			{
140 				OUStringBuffer sBuf;
141 				rExport.GetMM100UnitConverter().convertMeasure(sBuf, nLength);
142 				rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_OFFSET,
143 									 sBuf.makeStringAndClear());
144 			}
145 
146 			// NumeringType
147 			OUStringBuffer sNumPosBuf;
148 			aAny = xLineNumbering->getPropertyValue(sNumberingType);
149 			sal_Int16 nFormat = 0;
150 			aAny >>= nFormat;
151 			rExport.GetMM100UnitConverter().convertNumFormat( sNumPosBuf, nFormat );
152 			rExport.AddAttribute(XML_NAMESPACE_STYLE, XML_NUM_FORMAT,
153 								 sNumPosBuf.makeStringAndClear());
154 			rExport.GetMM100UnitConverter().convertNumLetterSync( sNumPosBuf, nFormat );
155 			if( sNumPosBuf.getLength() )
156 			{
157 				rExport.AddAttribute(XML_NAMESPACE_STYLE,
158 									 XML_NUM_LETTER_SYNC,
159 									 sNumPosBuf.makeStringAndClear() );
160 			}
161 
162 			// number position
163 			aAny = xLineNumbering->getPropertyValue(sNumberPosition);
164 			sal_Int16 nPosition = 0;
165 			aAny >>= nPosition;
166 			if (SvXMLUnitConverter::convertEnum(sNumPosBuf, nPosition,
167 												aLineNumberPositionMap))
168 			{
169 				rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_NUMBER_POSITION,
170 									 sNumPosBuf.makeStringAndClear());
171 			}
172 
173 			// sInterval
174 			aAny = xLineNumbering->getPropertyValue(sInterval);
175 			sal_Int16 nLineInterval = 0;
176 			aAny >>= nLineInterval;
177 			OUStringBuffer sBuf;
178 			SvXMLUnitConverter::convertNumber(sBuf,
179 											  (sal_Int32)nLineInterval);
180 			rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_INCREMENT,
181 								 sBuf.makeStringAndClear());
182 
183 			SvXMLElementExport aConfigElem(rExport, XML_NAMESPACE_TEXT,
184 										   XML_LINENUMBERING_CONFIGURATION,
185 										   sal_True, sal_True);
186 
187 			// line separator
188 			aAny = xLineNumbering->getPropertyValue(sSeparatorText);
189 			OUString sSeparator;
190 			aAny >>= sSeparator;
191 			if (sSeparator.getLength() > 0)
192 			{
193 
194 				// SeparatorInterval
195 				aAny = xLineNumbering->getPropertyValue(sSeparatorInterval);
196 				sal_Int16 nLineDistance = 0;
197 				aAny >>= nLineDistance;
198 				SvXMLUnitConverter::convertNumber(sBuf,
199 												  (sal_Int32)nLineDistance);
200 				rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_INCREMENT,
201 									 sBuf.makeStringAndClear());
202 
203 				SvXMLElementExport aSeparatorElem(rExport, XML_NAMESPACE_TEXT,
204 												  XML_LINENUMBERING_SEPARATOR,
205 												  sal_True, sal_False);
206 				rExport.Characters(sSeparator);
207 			}
208 		}
209 		// else: no configuration: don't save -> default
210 	}
211 	// can't even get supplier: don't save -> default
212 }
213