1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26
27
28 #include "XMLIndexTOCStylesContext.hxx"
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/container/XIndexReplace.hpp>
31 #include <xmloff/xmlictxt.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/txtimp.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include <xmloff/xmluconv.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <tools/debug.hxx>
40 #include <rtl/ustring.hxx>
41
42
43 using namespace ::xmloff::token;
44
45 using ::rtl::OUString;
46 using ::com::sun::star::beans::XPropertySet;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::container::XIndexReplace;
51 using ::com::sun::star::xml::sax::XAttributeList;
52
53
54 const sal_Char sAPI_LevelParagraphStyles[] = "LevelParagraphStyles";
55
56 TYPEINIT1( XMLIndexTOCStylesContext, SvXMLImportContext );
57
58
XMLIndexTOCStylesContext(SvXMLImport & rImport,Reference<XPropertySet> & rPropSet,sal_uInt16 nPrfx,const OUString & rLocalName)59 XMLIndexTOCStylesContext::XMLIndexTOCStylesContext(
60 SvXMLImport& rImport,
61 Reference<XPropertySet> & rPropSet,
62 sal_uInt16 nPrfx,
63 const OUString& rLocalName )
64 : SvXMLImportContext(rImport, nPrfx, rLocalName)
65 , sLevelParagraphStyles(RTL_CONSTASCII_USTRINGPARAM(sAPI_LevelParagraphStyles))
66 , rTOCPropertySet(rPropSet)
67 {
68 }
69
~XMLIndexTOCStylesContext()70 XMLIndexTOCStylesContext::~XMLIndexTOCStylesContext()
71 {
72 }
73
StartElement(const Reference<XAttributeList> & xAttrList)74 void XMLIndexTOCStylesContext::StartElement(
75 const Reference<XAttributeList> & xAttrList )
76 {
77 // find text:outline-level attribute
78 sal_Int16 nCount = xAttrList->getLength();
79 for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
80 {
81 OUString sLocalName;
82 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
83 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
84 &sLocalName );
85 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
86 (IsXMLToken(sLocalName, XML_OUTLINE_LEVEL)) )
87 {
88 sal_Int32 nTmp;
89 if (SvXMLUnitConverter::convertNumber(
90 nTmp, xAttrList->getValueByIndex(nAttr), 1,
91 GetImport().GetTextImport()->GetChapterNumbering()->
92 getCount()))
93 {
94 // API numbers 0..9, we number 1..10
95 nOutlineLevel = nTmp-1;
96 }
97 }
98 }
99 }
100
EndElement()101 void XMLIndexTOCStylesContext::EndElement()
102 {
103 // if valid...
104 if (nOutlineLevel >= 0)
105 {
106 // copy vector into sequence
107 const sal_Int32 nCount = aStyleNames.size();
108 Sequence<OUString> aStyleNamesSequence(nCount);
109 for(sal_Int32 i = 0; i < nCount; i++)
110 {
111 aStyleNamesSequence[i] = GetImport().GetStyleDisplayName(
112 XML_STYLE_FAMILY_TEXT_PARAGRAPH,
113 aStyleNames[i] );
114 }
115
116 // get index replace
117 Any aAny = rTOCPropertySet->getPropertyValue(sLevelParagraphStyles);
118 Reference<XIndexReplace> xIndexReplace;
119 aAny >>= xIndexReplace;
120
121 // set style names
122 aAny <<= aStyleNamesSequence;
123 xIndexReplace->replaceByIndex(nOutlineLevel, aAny);
124 }
125 }
126
CreateChildContext(sal_uInt16 p_nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList)127 SvXMLImportContext *XMLIndexTOCStylesContext::CreateChildContext(
128 sal_uInt16 p_nPrefix,
129 const OUString& rLocalName,
130 const Reference<XAttributeList> & xAttrList )
131 {
132 // check for index-source-style
133 if ( (XML_NAMESPACE_TEXT == p_nPrefix) &&
134 IsXMLToken( rLocalName, XML_INDEX_SOURCE_STYLE ) )
135 {
136 // find text:style-name attribute and record in aStyleNames
137 sal_Int16 nCount = xAttrList->getLength();
138 for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
139 {
140 OUString sLocalName;
141 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
142 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
143 &sLocalName );
144 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
145 IsXMLToken( sLocalName, XML_STYLE_NAME ) )
146 {
147 aStyleNames.push_back(xAttrList->getValueByIndex(nAttr));
148 }
149 }
150 }
151
152 // always return default context; we already got the interesting info
153 return SvXMLImportContext::CreateChildContext(p_nPrefix, rLocalName,
154 xAttrList);
155 }
156