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