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 "XMLLineNumberingImportContext.hxx"
31 #include "XMLLineNumberingSeparatorImportContext.hxx"
32 #include "com/sun/star/beans/XPropertySet.hpp"
33 #include "com/sun/star/text/XLineNumberingProperties.hpp"
34 #include <com/sun/star/style/LineNumberPosition.hpp>
35 #include <com/sun/star/style/NumberingType.hpp>
36 #include <xmloff/xmlimp.hxx>
37 #include <xmloff/xmluconv.hxx>
38 #include "xmloff/xmlnmspe.hxx"
39 #include <xmloff/nmspmap.hxx>
40 #include <xmloff/xmltoken.hxx>
41 #include <xmloff/xmlnumi.hxx>
42 
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::style;
47 using namespace ::xmloff::token;
48 
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::xml::sax::XAttributeList;
51 using ::com::sun::star::text::XLineNumberingProperties;
52 using ::rtl::OUString;
53 
54 TYPEINIT1( XMLLineNumberingImportContext, SvXMLStyleContext );
55 
56 
57 XMLLineNumberingImportContext::XMLLineNumberingImportContext(
58 	SvXMLImport& rImport,
59 	sal_uInt16 nPrfx,
60 	const OUString& rLocalName,
61 	const Reference<XAttributeList> & xAttrList)
62 :	SvXMLStyleContext(rImport, nPrfx, rLocalName, xAttrList, XML_STYLE_FAMILY_TEXT_LINENUMBERINGCONFIG)
63 ,	sCharStyleName(RTL_CONSTASCII_USTRINGPARAM("CharStyleName"))
64 ,	sCountEmptyLines(RTL_CONSTASCII_USTRINGPARAM("CountEmptyLines"))
65 ,	sCountLinesInFrames(RTL_CONSTASCII_USTRINGPARAM("CountLinesInFrames"))
66 ,	sDistance(RTL_CONSTASCII_USTRINGPARAM("Distance"))
67 ,	sInterval(RTL_CONSTASCII_USTRINGPARAM("Interval"))
68 ,	sSeparatorText(RTL_CONSTASCII_USTRINGPARAM("SeparatorText"))
69 ,	sNumberPosition(RTL_CONSTASCII_USTRINGPARAM("NumberPosition"))
70 ,	sNumberingType(RTL_CONSTASCII_USTRINGPARAM("NumberingType"))
71 ,	sIsOn(RTL_CONSTASCII_USTRINGPARAM("IsOn"))
72 ,	sRestartAtEachPage(RTL_CONSTASCII_USTRINGPARAM("RestartAtEachPage"))
73 ,	sSeparatorInterval(RTL_CONSTASCII_USTRINGPARAM("SeparatorInterval"))
74 ,	sNumFormat(GetXMLToken(XML_1))
75 ,	sNumLetterSync(GetXMLToken(XML_FALSE))
76 ,	nOffset(-1)
77 ,	nNumberPosition(style::LineNumberPosition::LEFT)
78 ,	nIncrement(-1)
79 ,	nSeparatorIncrement(-1)
80 ,	bNumberLines(sal_True)
81 ,	bCountEmptyLines(sal_True)
82 ,	bCountInFloatingFrames(sal_False)
83 ,	bRestartNumbering(sal_False)
84 {
85 }
86 
87 XMLLineNumberingImportContext::~XMLLineNumberingImportContext()
88 {
89 }
90 
91 void XMLLineNumberingImportContext::StartElement(
92 	const Reference<XAttributeList> & xAttrList)
93 {
94     static SvXMLTokenMapEntry aLineNumberingTokenMap[] =
95     {
96 	    { XML_NAMESPACE_TEXT, XML_STYLE_NAME, XML_TOK_LINENUMBERING_STYLE_NAME },
97 	    { XML_NAMESPACE_TEXT, XML_NUMBER_LINES,
98 		      XML_TOK_LINENUMBERING_NUMBER_LINES },
99 	    { XML_NAMESPACE_TEXT, XML_COUNT_EMPTY_LINES,
100 		      XML_TOK_LINENUMBERING_COUNT_EMPTY_LINES },
101 	    { XML_NAMESPACE_TEXT, XML_COUNT_IN_TEXT_BOXES,
102 		      XML_TOK_LINENUMBERING_COUNT_IN_TEXT_BOXES },
103 	    { XML_NAMESPACE_TEXT, XML_RESTART_ON_PAGE,
104 		      XML_TOK_LINENUMBERING_RESTART_NUMBERING },
105 	    { XML_NAMESPACE_TEXT, XML_OFFSET, XML_TOK_LINENUMBERING_OFFSET },
106 	    { XML_NAMESPACE_STYLE, XML_NUM_FORMAT, XML_TOK_LINENUMBERING_NUM_FORMAT },
107 	    { XML_NAMESPACE_STYLE, XML_NUM_LETTER_SYNC,
108 		      XML_TOK_LINENUMBERING_NUM_LETTER_SYNC },
109 	    { XML_NAMESPACE_TEXT, XML_NUMBER_POSITION,
110 		      XML_TOK_LINENUMBERING_NUMBER_POSITION },
111 	    { XML_NAMESPACE_TEXT, XML_INCREMENT, XML_TOK_LINENUMBERING_INCREMENT },
112     //	{ XML_NAMESPACE_TEXT, XML_LINENUMBERING_CONFIGURATION,
113     //		  XML_TOK_LINENUMBERING_LINENUMBERING_CONFIGURATION },
114     //	{ XML_NAMESPACE_TEXT, XML_INCREMENT, XML_TOK_LINENUMBERING_INCREMENT },
115     //	{ XML_NAMESPACE_TEXT, XML_LINENUMBERING_SEPARATOR,
116     //		  XML_TOK_LINENUMBERING_LINENUMBERING_SEPARATOR },
117 
118 	    XML_TOKEN_MAP_END
119     };
120 
121 	SvXMLTokenMap aTokenMap(aLineNumberingTokenMap);
122 
123 	// process attributes
124 	sal_Int16 nLength = xAttrList->getLength();
125 	for(sal_Int16 i=0; i<nLength; i++)
126 	{
127 		OUString sLocalName;
128 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
129 			GetKeyByAttrName( xAttrList->getNameByIndex(i), &sLocalName );
130 
131 		ProcessAttribute(
132 			(enum LineNumberingToken)aTokenMap.Get(nPrefix, sLocalName),
133 			xAttrList->getValueByIndex(i));
134 	}
135 }
136 
137 void XMLLineNumberingImportContext::ProcessAttribute(
138 	enum LineNumberingToken eToken,
139 	OUString sValue)
140 {
141 	sal_Bool bTmp;
142 	sal_Int32 nTmp;
143 
144 	switch (eToken)
145 	{
146 		case XML_TOK_LINENUMBERING_STYLE_NAME:
147 			sStyleName = sValue;
148 			break;
149 
150 		case XML_TOK_LINENUMBERING_NUMBER_LINES:
151 			if (SvXMLUnitConverter::convertBool(bTmp, sValue))
152 			{
153 				bNumberLines = bTmp;
154 			}
155 			break;
156 
157 		case XML_TOK_LINENUMBERING_COUNT_EMPTY_LINES:
158 			if (SvXMLUnitConverter::convertBool(bTmp, sValue))
159 			{
160 				bCountEmptyLines = bTmp;
161 			}
162 			break;
163 
164 		case XML_TOK_LINENUMBERING_COUNT_IN_TEXT_BOXES:
165 			if (SvXMLUnitConverter::convertBool(bTmp, sValue))
166 			{
167 				bCountInFloatingFrames = bTmp;
168 			}
169 			break;
170 
171 		case XML_TOK_LINENUMBERING_RESTART_NUMBERING:
172 			if (SvXMLUnitConverter::convertBool(bTmp, sValue))
173 			{
174 				bRestartNumbering = bTmp;
175 			}
176 			break;
177 
178 		case XML_TOK_LINENUMBERING_OFFSET:
179 			if (GetImport().GetMM100UnitConverter().
180 				convertMeasure(nTmp, sValue))
181 			{
182 				nOffset = nTmp;
183 			}
184 			break;
185 
186 		case XML_TOK_LINENUMBERING_NUM_FORMAT:
187 			sNumFormat = sValue;
188 			break;
189 
190 		case XML_TOK_LINENUMBERING_NUM_LETTER_SYNC:
191 			sNumLetterSync = sValue;
192 			break;
193 
194 		case XML_TOK_LINENUMBERING_NUMBER_POSITION:
195 		{
196             static const SvXMLEnumMapEntry aLineNumberPositionMap[] =
197             {
198 	            { XML_LEFT,	    style::LineNumberPosition::LEFT },
199 	            { XML_RIGHT,	style::LineNumberPosition::RIGHT },
200 	            { XML_INSIDE,	style::LineNumberPosition::INSIDE },
201 	            { XML_OUTSIDE,  style::LineNumberPosition::OUTSIDE },
202 	            { XML_TOKEN_INVALID, 0 }
203             };
204 
205 			sal_uInt16 nTmp16;
206 			if (SvXMLUnitConverter::convertEnum(nTmp16, sValue,
207 												aLineNumberPositionMap))
208 			{
209 				nNumberPosition = nTmp16;
210 			}
211 			break;
212 		}
213 
214 		case XML_TOK_LINENUMBERING_INCREMENT:
215 			if (SvXMLUnitConverter::convertNumber(nTmp, sValue, 0))
216 			{
217 				nIncrement = (sal_Int16)nTmp;
218 			}
219 			break;
220 	}
221 }
222 
223 void XMLLineNumberingImportContext::CreateAndInsert(sal_Bool)
224 {
225 	// insert and block mode is handled in insertStyleFamily
226 
227 	// we'll try to get the LineNumberingProperties
228 	Reference<XLineNumberingProperties> xSupplier(GetImport().GetModel(),
229 												  UNO_QUERY);
230 	if (xSupplier.is())
231 	{
232 		Reference<XPropertySet> xLineNumbering =
233 			xSupplier->getLineNumberingProperties();
234 
235 		if (xLineNumbering.is())
236 		{
237 			Any aAny;
238 
239             // set style name (if it exists)
240             if ( GetImport().GetStyles()->FindStyleChildContext(
241                             XML_STYLE_FAMILY_TEXT_TEXT, sStyleName ) != NULL )
242             {
243                 aAny <<= GetImport().GetStyleDisplayName(
244 							XML_STYLE_FAMILY_TEXT_TEXT,	sStyleName );
245                 xLineNumbering->setPropertyValue(sCharStyleName, aAny);
246             }
247 
248 			aAny <<= sSeparator;
249 			xLineNumbering->setPropertyValue(sSeparatorText, aAny);
250 
251 			aAny <<= nOffset;
252 			xLineNumbering->setPropertyValue(sDistance, aAny);
253 
254 			aAny <<= nNumberPosition;
255 			xLineNumbering->setPropertyValue(sNumberPosition, aAny);
256 
257 			if (nIncrement >= 0)
258 			{
259 				aAny <<= nIncrement;
260 				xLineNumbering->setPropertyValue(sInterval, aAny);
261 			}
262 
263 			if (nSeparatorIncrement >= 0)
264 			{
265 				aAny <<= nSeparatorIncrement;
266 				xLineNumbering->setPropertyValue(sSeparatorInterval, aAny);
267 			}
268 
269 			aAny.setValue(&bNumberLines, ::getBooleanCppuType());
270 			xLineNumbering->setPropertyValue(sIsOn, aAny);
271 
272 			aAny.setValue(&bCountEmptyLines, ::getBooleanCppuType());
273 			xLineNumbering->setPropertyValue(sCountEmptyLines, aAny);
274 
275 			aAny.setValue(&bCountInFloatingFrames, ::getBooleanCppuType());
276 			xLineNumbering->setPropertyValue(sCountLinesInFrames, aAny);
277 
278 			aAny.setValue(&bRestartNumbering, ::getBooleanCppuType());
279 			xLineNumbering->setPropertyValue(sRestartAtEachPage, aAny);
280 
281 			sal_Int16 nNumType = NumberingType::ARABIC;
282 			GetImport().GetMM100UnitConverter().convertNumFormat( nNumType,
283 													sNumFormat,
284 													sNumLetterSync );
285 			aAny <<= nNumType;
286 			xLineNumbering->setPropertyValue(sNumberingType, aAny);
287 		}
288 	}
289 }
290 
291 SvXMLImportContext* XMLLineNumberingImportContext::CreateChildContext(
292 	sal_uInt16 nPrefix,
293 	const OUString& rLocalName,
294 	const Reference<XAttributeList> & xAttrList )
295 {
296 	if ( (nPrefix == XML_NAMESPACE_TEXT) &&
297 		 IsXMLToken(rLocalName, XML_LINENUMBERING_SEPARATOR) )
298 	{
299 		return new XMLLineNumberingSeparatorImportContext(GetImport(),
300 														  nPrefix, rLocalName,
301 														  *this);
302 	}
303 	else
304 	{
305 		// unknown element: default context
306 		return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
307 													  xAttrList);
308 	}
309 }
310 
311 void XMLLineNumberingImportContext::SetSeparatorText(
312 	const OUString& sText)
313 {
314 	sSeparator = sText;
315 }
316 
317 void XMLLineNumberingImportContext::SetSeparatorIncrement(
318 	sal_Int16 nIncr)
319 {
320 	nSeparatorIncrement = nIncr;
321 }
322 
323