xref: /trunk/main/xmloff/source/transform/FrameOASISTContext.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 #include "FrameOASISTContext.hxx"
31 #include "IgnoreTContext.hxx"
32 #include "MutableAttrList.hxx"
33 #include "xmloff/xmlnmspe.hxx"
34 #include "ActionMapTypesOASIS.hxx"
35 #include "ElemTransformerAction.hxx"
36 #include "TransformerActions.hxx"
37 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
38 #include "TransformerBase.hxx"
39 #endif
40 
41 using ::rtl::OUString;
42 
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::xml::sax;
45 using namespace ::xmloff::token;
46 
47 TYPEINIT1( XMLFrameOASISTransformerContext, XMLTransformerContext );
48 
49 sal_Bool XMLFrameOASISTransformerContext::IsLinkedEmbeddedObject(
50             const OUString& rLocalName,
51             const Reference< XAttributeList >& rAttrList )
52 {
53     if( !(IsXMLToken( rLocalName, XML_OBJECT ) ||
54           IsXMLToken( rLocalName, XML_OBJECT_OLE)  ) )
55         return sal_False;
56 
57     sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
58     for( sal_Int16 i=0; i < nAttrCount; i++ )
59     {
60         OUString aAttrName( rAttrList->getNameByIndex( i ) );
61         OUString aLocalName;
62         sal_uInt16 nPrefix =
63             GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
64                                                                  &aLocalName );
65         if( XML_NAMESPACE_XLINK == nPrefix &&
66             IsXMLToken( aLocalName, XML_HREF ) )
67         {
68             OUString sHRef( rAttrList->getValueByIndex( i ) );
69             if (sHRef.getLength() == 0)
70             {
71                 // When the href is empty then the object is not linked but
72                 // a placeholder.
73                 return sal_False;
74             }
75             GetTransformer().ConvertURIToOOo( sHRef, sal_True );
76             return !(sHRef.getLength() && '#'==sHRef[0]);
77         }
78     }
79 
80     return sal_False;
81 }
82 
83 
84 XMLFrameOASISTransformerContext::XMLFrameOASISTransformerContext(
85         XMLTransformerBase& rImp,
86         const OUString& rQName ) :
87     XMLTransformerContext( rImp, rQName ),
88     m_bIgnoreElement( false )
89 {
90 }
91 
92 XMLFrameOASISTransformerContext::~XMLFrameOASISTransformerContext()
93 {
94 }
95 
96 void XMLFrameOASISTransformerContext::StartElement(
97     const Reference< XAttributeList >& rAttrList )
98 {
99     m_xAttrList = new XMLMutableAttributeList( rAttrList, sal_True );
100 
101     sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
102     for( sal_Int16 i=0; i < nAttrCount; i++ )
103     {
104         const OUString& rAttrName = rAttrList->getNameByIndex( i );
105         OUString aLocalName;
106         sal_uInt16 nPrefix =
107             GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
108 
109         if( (nPrefix == XML_NAMESPACE_PRESENTATION) && IsXMLToken( aLocalName, XML_CLASS ) )
110         {
111             const OUString& rAttrValue = rAttrList->getValueByIndex( i );
112             if( IsXMLToken( rAttrValue, XML_HEADER ) || IsXMLToken( rAttrValue, XML_FOOTER ) ||
113                 IsXMLToken( rAttrValue, XML_PAGE_NUMBER ) || IsXMLToken( rAttrValue, XML_DATE_TIME ) )
114             {
115                 m_bIgnoreElement = true;
116                 break;
117             }
118         }
119     }
120 }
121 
122 XMLTransformerContext *XMLFrameOASISTransformerContext::CreateChildContext(
123         sal_uInt16 nPrefix,
124         const OUString& rLocalName,
125         const OUString& rQName,
126         const Reference< XAttributeList >& rAttrList )
127 {
128     XMLTransformerContext *pContext = 0;
129 
130     if( m_bIgnoreElement )
131     {
132         // do not export the frame element and all of its children
133         pContext = new XMLIgnoreTransformerContext( GetTransformer(),
134                                                                 rQName,
135                                                                 sal_True, sal_True );
136     }
137     else
138     {
139         XMLTransformerActions *pActions =
140             GetTransformer().GetUserDefinedActions( OASIS_FRAME_ELEM_ACTIONS );
141         OSL_ENSURE( pActions, "go no actions" );
142         XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
143         XMLTransformerActions::const_iterator aIter = pActions->find( aKey );
144 
145         if( !(aIter == pActions->end()) )
146         {
147             switch( (*aIter).second.m_nActionType )
148             {
149             case XML_ETACTION_COPY:
150                 if( !m_aElemQName.getLength() &&
151                     !IsLinkedEmbeddedObject( rLocalName, rAttrList ) )
152                 {
153                     pContext = new XMLIgnoreTransformerContext( GetTransformer(),
154                                                                 rQName,
155                                                                 sal_False, sal_False );
156                     m_aElemQName = rQName;
157                     static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
158                         ->AppendAttributeList( rAttrList );
159                     GetTransformer().ProcessAttrList( m_xAttrList,
160                                                       OASIS_SHAPE_ACTIONS,
161                                                       sal_False );
162                     GetTransformer().GetDocHandler()->startElement( m_aElemQName,
163                                                                     m_xAttrList );
164                 }
165                 else
166                 {
167                     pContext = new XMLIgnoreTransformerContext( GetTransformer(),
168                                                                 rQName,
169                                                                 sal_True, sal_True );
170                 }
171                 break;
172             default:
173                 OSL_ENSURE( !this, "unknown action" );
174                 break;
175             }
176         }
177     }
178 
179     // default is copying
180     if( !pContext )
181         pContext = XMLTransformerContext::CreateChildContext( nPrefix,
182                                                               rLocalName,
183                                                               rQName,
184                                                               rAttrList );
185 
186     return pContext;
187 }
188 
189 void XMLFrameOASISTransformerContext::EndElement()
190 {
191     if( !m_bIgnoreElement )
192         GetTransformer().GetDocHandler()->endElement( m_aElemQName );
193 }
194 
195 void XMLFrameOASISTransformerContext::Characters( const OUString& rChars )
196 {
197     // ignore
198     if( m_aElemQName.getLength() && !m_bIgnoreElement )
199         XMLTransformerContext::Characters( rChars );
200 }
201