xref: /trunk/main/xmloff/source/text/XMLIndexSimpleEntryContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "XMLIndexSimpleEntryContext.hxx"
33 #include "XMLIndexTemplateContext.hxx"
34 #include <xmloff/xmlictxt.hxx>
35 #include <xmloff/xmlimp.hxx>
36 #include <xmloff/txtimp.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include "xmloff/xmlnmspe.hxx"
39 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/xmluconv.hxx>
41 
42 
43 //using namespace ::com::sun::star::text;
44 
45 using ::rtl::OUString;
46 using ::com::sun::star::beans::PropertyValue;
47 using ::com::sun::star::beans::PropertyValues;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::Any;
51 using ::com::sun::star::xml::sax::XAttributeList;
52 using ::xmloff::token::IsXMLToken;
53 using ::xmloff::token::XML_STYLE_NAME;
54 
55 const sal_Char sAPI_TokenType[] = "TokenType";
56 const sal_Char sAPI_CharacterStyleName[] = "CharacterStyleName";
57 
58 TYPEINIT1( XMLIndexSimpleEntryContext, SvXMLImportContext);
59 
60 XMLIndexSimpleEntryContext::XMLIndexSimpleEntryContext(
61     SvXMLImport& rImport,
62     const OUString& rEntry,
63     XMLIndexTemplateContext& rTemplate,
64     sal_uInt16 nPrfx,
65     const OUString& rLocalName )
66 :   SvXMLImportContext(rImport, nPrfx, rLocalName)
67 ,   rEntryType(rEntry)
68 ,   bCharStyleNameOK(sal_False)
69 ,   rTemplateContext(rTemplate)
70 ,   nValues(1)
71 {
72 }
73 
74 XMLIndexSimpleEntryContext::~XMLIndexSimpleEntryContext()
75 {
76 }
77 
78 void XMLIndexSimpleEntryContext::StartElement(
79     const Reference<XAttributeList> & xAttrList)
80 {
81     // we know only one attribute: style-name
82     sal_Int16 nLength = xAttrList->getLength();
83     for(sal_Int16 nAttr = 0; nAttr < nLength; 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_STYLE_NAME) )
91         {
92             sCharStyleName = xAttrList->getValueByIndex(nAttr);
93             OUString sDisplayStyleName = GetImport().GetStyleDisplayName(
94                 XML_STYLE_FAMILY_TEXT_TEXT, sCharStyleName );
95             // #142494#: Check if style exists
96             const Reference < ::com::sun::star::container::XNameContainer > & rStyles =
97                 GetImport().GetTextImport()->GetTextStyles();
98             if( rStyles.is() && rStyles->hasByName( sDisplayStyleName ) )
99                 bCharStyleNameOK = sal_True;
100             else
101                 bCharStyleNameOK = sal_False;
102         }
103     }
104 
105     // if we have a style name, set it!
106     if (bCharStyleNameOK)
107     {
108         nValues++;
109     }
110 
111 }
112 
113 void XMLIndexSimpleEntryContext::EndElement()
114 {
115     Sequence<PropertyValue> aValues(nValues);
116 
117     FillPropertyValues(aValues);
118     rTemplateContext.addTemplateEntry(aValues);
119 }
120 
121 void XMLIndexSimpleEntryContext::FillPropertyValues(
122     ::com::sun::star::uno::Sequence<
123         ::com::sun::star::beans::PropertyValue> & rValues)
124 {
125     // due to the limited number of subclasses, we fill the values
126     // directly into the slots. Subclasses will have to know they can
127     // only use slot so-and-so.
128 
129     Any aAny;
130 
131     // token type
132     rValues[0].Name = rTemplateContext.sTokenType;
133     aAny <<= rEntryType;
134     rValues[0].Value = aAny;
135 
136     // char style
137     if (bCharStyleNameOK)
138     {
139         rValues[1].Name = rTemplateContext.sCharacterStyleName;
140         aAny <<= GetImport().GetStyleDisplayName(
141                                     XML_STYLE_FAMILY_TEXT_TEXT,
142                                     sCharStyleName );
143         rValues[1].Value = aAny;
144     }
145 
146 }
147