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