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 "PageMasterImportContext.hxx"
29 #include "xmloff/xmlnmspe.hxx"
30 #include <xmloff/xmltoken.hxx>
31 #include "PageMasterPropHdl.hxx"
32 #include "PagePropertySetContext.hxx"
33 #ifndef _XMLOFF_PAGEPHEADERFOOTERCONTEXT_HXX
34 #include "PageHeaderFooterContext.hxx"
35 #endif
36 #ifndef _XMLOFF_PAGEMASTERPROPMAPPER_HXX
37 #include "PageMasterPropMapper.hxx"
38 #endif
39 #include "PageMasterImportPropMapper.hxx"
40 #ifndef _XMLOFF_PAGEMASTERSTYLEMAP_HXX
41 #include <xmloff/PageMasterStyleMap.hxx>
42 #endif
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 
45 using namespace ::com::sun::star;
46 using namespace ::xmloff::token;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 
50 void PageStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
51                                         const rtl::OUString& rLocalName,
52                                         const rtl::OUString& rValue )
53 {
54     // TODO: use a map here
55     if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_PAGE_USAGE ) )
56     {
57         sPageUsage = rValue;
58     }
59     else
60     {
61         XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
62     }
63 }
64 
65 TYPEINIT1( PageStyleContext, XMLPropStyleContext );
66 
67 PageStyleContext::PageStyleContext( SvXMLImport& rImport,
68         sal_uInt16 nPrfx, const rtl::OUString& rLName,
69         const uno::Reference< xml::sax::XAttributeList > & xAttrList,
70         SvXMLStylesContext& rStyles,
71         sal_Bool bDefaultStyle) :
72     XMLPropStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, XML_STYLE_FAMILY_PAGE_MASTER, bDefaultStyle),
73     sPageUsage()
74 {
75 }
76 
77 PageStyleContext::~PageStyleContext()
78 {
79 }
80 
81 SvXMLImportContext *PageStyleContext::CreateChildContext(
82         sal_uInt16 nPrefix,
83         const rtl::OUString& rLocalName,
84         const uno::Reference< xml::sax::XAttributeList > & xAttrList )
85 {
86     SvXMLImportContext *pContext = NULL;
87 
88     if( XML_NAMESPACE_STYLE == nPrefix &&
89         ((IsXMLToken(rLocalName, XML_HEADER_STYLE )) ||
90          (IsXMLToken(rLocalName, XML_FOOTER_STYLE )) ) )
91     {
92         sal_Bool bHeader = IsXMLToken(rLocalName, XML_HEADER_STYLE);
93         UniReference < SvXMLImportPropertyMapper > xImpPrMap =
94             GetStyles()->GetImportPropertyMapper( GetFamily() );
95         if( xImpPrMap.is() )
96         {
97             const UniReference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
98             sal_Int32 nFlag;
99             if (bHeader)
100                 nFlag = CTF_PM_HEADERFLAG;
101             else
102                 nFlag = CTF_PM_FOOTERFLAG;
103             sal_Int32 nStartIndex (-1);
104             sal_Int32 nEndIndex (-1);
105             sal_Bool bFirst(sal_False);
106             sal_Bool bEnd(sal_False);
107             sal_Int32 nIndex = 0;
108             while ( nIndex < rMapper->GetEntryCount() && !bEnd)
109             {
110                 if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
111                 {
112                     if (!bFirst)
113                     {
114                         bFirst = sal_True;
115                         nStartIndex = nIndex;
116                     }
117                 }
118                 else if (bFirst)
119                 {
120                     bEnd = sal_True;
121                     nEndIndex = nIndex;
122                 }
123                 nIndex++;
124             }
125             if (!bEnd)
126                 nEndIndex = nIndex;
127             pContext = new PageHeaderFooterContext(GetImport(), nPrefix, rLocalName,
128                             xAttrList, GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
129         }
130     }
131     if( XML_NAMESPACE_STYLE == nPrefix &&
132         IsXMLToken(rLocalName, XML_PAGE_LAYOUT_PROPERTIES) )
133     {
134         UniReference < SvXMLImportPropertyMapper > xImpPrMap =
135             GetStyles()->GetImportPropertyMapper( GetFamily() );
136         if( xImpPrMap.is() )
137         {
138             const UniReference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
139             sal_Int32 nEndIndex (-1);
140             sal_Bool bEnd(sal_False);
141             sal_Int32 nIndex = 0;
142             sal_Int16 nContextID;
143             while ( nIndex < rMapper->GetEntryCount() && !bEnd)
144             {
145                 nContextID = rMapper->GetEntryContextId( nIndex );
146                 if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
147                 {
148                     nEndIndex = nIndex;
149                     bEnd = sal_True;
150                 }
151                 nIndex++;
152             }
153             if (!bEnd)
154                 nEndIndex = nIndex;
155             PageContextType aType = Page;
156             pContext = new PagePropertySetContext( GetImport(), nPrefix,
157                                                     rLocalName, xAttrList,
158 													XML_TYPE_PROP_PAGE_LAYOUT,
159                                                     GetProperties(),
160                                                     xImpPrMap, 0, nEndIndex, aType);
161         }
162     }
163 
164     if (!pContext)
165         pContext = XMLPropStyleContext::CreateChildContext( nPrefix, rLocalName,
166                                                            xAttrList );
167     return pContext;
168 }
169 
170 void PageStyleContext::FillPropertySet(
171             const uno::Reference<beans::XPropertySet > & rPropSet )
172 {
173     XMLPropStyleContext::FillPropertySet(rPropSet);
174     if (sPageUsage.getLength())
175     {
176         uno::Any aPageUsage;
177         XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
178         if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
179             rPropSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("PageStyleLayout")), aPageUsage);
180     }
181 }
182 
183 // text grid enhancement for better CJK support
184 //set default page layout style
185 void PageStyleContext::SetDefaults( )
186 {
187 	Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
188 	if (xFactory.is())
189 	{
190 		Reference < XInterface > xInt = xFactory->createInstance (
191 	    rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.text.Defaults" ) ) );
192 	    Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
193 	    if ( xProperties.is() )
194 	    	FillPropertySet ( xProperties );
195 	}
196 }
197 
198