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 
31 
32 #include "XMLIndexTOCStylesContext.hxx"
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/container/XIndexReplace.hpp>
35 #include <xmloff/xmlictxt.hxx>
36 #include <xmloff/xmlimp.hxx>
37 #include <xmloff/txtimp.hxx>
38 #include "xmloff/xmlnmspe.hxx"
39 #include <xmloff/nmspmap.hxx>
40 #include <xmloff/xmltoken.hxx>
41 #include <xmloff/xmluconv.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <tools/debug.hxx>
44 #include <rtl/ustring.hxx>
45 
46 
47 using namespace ::xmloff::token;
48 
49 using ::rtl::OUString;
50 using ::com::sun::star::beans::XPropertySet;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::uno::Any;
54 using ::com::sun::star::container::XIndexReplace;
55 using ::com::sun::star::xml::sax::XAttributeList;
56 
57 
58 const sal_Char sAPI_LevelParagraphStyles[] = "LevelParagraphStyles";
59 
60 TYPEINIT1( XMLIndexTOCStylesContext, SvXMLImportContext );
61 
62 
63 XMLIndexTOCStylesContext::XMLIndexTOCStylesContext(
64 	SvXMLImport& rImport,
65 	Reference<XPropertySet> & rPropSet,
66 	sal_uInt16 nPrfx,
67 	const OUString& rLocalName )
68 :	SvXMLImportContext(rImport, nPrfx, rLocalName)
69 ,	sLevelParagraphStyles(RTL_CONSTASCII_USTRINGPARAM(sAPI_LevelParagraphStyles))
70 ,	rTOCPropertySet(rPropSet)
71 {
72 }
73 
74 XMLIndexTOCStylesContext::~XMLIndexTOCStylesContext()
75 {
76 }
77 
78 void XMLIndexTOCStylesContext::StartElement(
79 	const Reference<XAttributeList> & xAttrList )
80 {
81 	// find text:outline-level attribute
82 	sal_Int16 nCount = xAttrList->getLength();
83 	for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
84 	{
85 		OUString sLocalName;
86 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
87 			GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
88 							  &sLocalName );
89 		if ( (XML_NAMESPACE_TEXT == nPrefix) &&
90 			 (IsXMLToken(sLocalName, XML_OUTLINE_LEVEL)) )
91 		{
92 			sal_Int32 nTmp;
93 			if (SvXMLUnitConverter::convertNumber(
94 					nTmp, xAttrList->getValueByIndex(nAttr), 1,
95 					GetImport().GetTextImport()->GetChapterNumbering()->
96 																getCount()))
97 			{
98 				// API numbers 0..9, we number 1..10
99 				nOutlineLevel = nTmp-1;
100 			}
101 		}
102 	}
103 }
104 
105 void XMLIndexTOCStylesContext::EndElement()
106 {
107 	// if valid...
108 	if (nOutlineLevel >= 0)
109 	{
110 		// copy vector into sequence
111 		const sal_Int32 nCount = aStyleNames.size();
112 		Sequence<OUString> aStyleNamesSequence(nCount);
113 		for(sal_Int32 i = 0; i < nCount; i++)
114 		{
115 			aStyleNamesSequence[i] = GetImport().GetStyleDisplayName(
116 							XML_STYLE_FAMILY_TEXT_PARAGRAPH,
117 						   	aStyleNames[i] );
118 		}
119 
120 		// get index replace
121 		Any aAny = rTOCPropertySet->getPropertyValue(sLevelParagraphStyles);
122 		Reference<XIndexReplace> xIndexReplace;
123 		aAny >>= xIndexReplace;
124 
125 		// set style names
126 		aAny <<= aStyleNamesSequence;
127 		xIndexReplace->replaceByIndex(nOutlineLevel, aAny);
128 	}
129 }
130 
131 SvXMLImportContext *XMLIndexTOCStylesContext::CreateChildContext(
132 	sal_uInt16 p_nPrefix,
133 	const OUString& rLocalName,
134 	const Reference<XAttributeList> & xAttrList )
135 {
136 	// check for index-source-style
137 	if ( (XML_NAMESPACE_TEXT == p_nPrefix) &&
138 		 IsXMLToken( rLocalName, XML_INDEX_SOURCE_STYLE ) )
139 	{
140 		// find text:style-name attribute and record in aStyleNames
141 		sal_Int16 nCount = xAttrList->getLength();
142 		for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
143 		{
144 			OUString sLocalName;
145 			sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
146 				GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
147 								  &sLocalName );
148 			if ( (XML_NAMESPACE_TEXT == nPrefix) &&
149 				 IsXMLToken( sLocalName, XML_STYLE_NAME ) )
150 			{
151 				aStyleNames.push_back(xAttrList->getValueByIndex(nAttr));
152 			}
153 		}
154 	}
155 
156 	// always return default context; we already got the interesting info
157 	return SvXMLImportContext::CreateChildContext(p_nPrefix, rLocalName,
158 												  xAttrList);
159 }
160