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 <com/sun/star/xml/sax/SAXParseException.hpp>
31 #include <com/sun/star/xml/sax/SAXException.hpp>
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/xml/sax/XAttributeList.hpp>
34 #include <xmloff/nmspmap.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include "xmloff/xmlnmspe.hxx"
37 
38 #ifndef _XMLOFF_TRANSFOERMERBASE_HXX
39 #include "TransformerBase.hxx"
40 #endif
41 #include "TransformerActions.hxx"
42 #include "AttrTransformerAction.hxx"
43 #include "ActionMapTypesOASIS.hxx"
44 #include "MutableAttrList.hxx"
45 #include "RenameElemTContext.hxx"
46 #include "FlatTContext.hxx"
47 
48 #ifndef _XMLOFF_NOTESCONTEXT_HXX
49 #include "NotesTContext.hxx"
50 #endif
51 
52 using ::rtl::OUString;
53 using namespace ::xmloff::token;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::xml::sax;
56 
57 TYPEINIT1( XMLNotesTransformerContext, XMLPersElemContentTContext );
58 
59 XMLNotesTransformerContext::XMLNotesTransformerContext(
60 		XMLTransformerBase& rImp,
61 	    const OUString& rQName,
62 	    XMLTokenEnum eToken, sal_Bool bPersistent ) :
63 	XMLPersElemContentTContext( rImp, rQName ),
64 	m_bEndNote( sal_False ),
65 	m_bPersistent( bPersistent ),
66 	m_eTypeToken( eToken )
67 {
68 }
69 
70 XMLNotesTransformerContext::~XMLNotesTransformerContext()
71 {
72 }
73 
74 void XMLNotesTransformerContext::StartElement(
75 		const Reference< XAttributeList >& rAttrList )
76 {
77 	XMLTransformerActions *pActions =
78 		GetTransformer().GetUserDefinedActions( OASIS_NOTES_ACTIONS );
79 	OSL_ENSURE( pActions, "go no actions" );
80 
81 	Reference< XAttributeList > xAttrList( rAttrList );
82 	XMLMutableAttributeList *pMutableAttrList = 0;
83 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
84 	for( sal_Int16 i=0; i < nAttrCount; i++ )
85 	{
86 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
87 		OUString aLocalName;
88 		sal_uInt16 nPrefix =
89 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
90 																 &aLocalName );
91 		XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
92 		XMLTransformerActions::const_iterator aIter =
93 			pActions->find( aKey );
94 		if( !(aIter == pActions->end() ) )
95 		{
96 			const OUString& rAttrValue = xAttrList->getValueByIndex( i );
97 
98 			if( !pMutableAttrList )
99 			{
100 				pMutableAttrList =
101 					new XMLMutableAttributeList( xAttrList );
102 				xAttrList = pMutableAttrList;
103 			}
104 			switch( (*aIter).second.m_nActionType )
105 			{
106 			case XML_ATACTION_STYLE_FAMILY:
107 				{
108 					if( IsXMLToken( rAttrValue, XML_FOOTNOTE ) )
109 					{
110 					}
111 					else if( IsXMLToken( rAttrValue, XML_ENDNOTE ) )
112 					{
113 						m_bEndNote = sal_True;
114 					}
115 					pMutableAttrList->RemoveAttributeByIndex( i );
116 					--i;
117 					--nAttrCount;
118 				}
119 				break;
120 			case XML_ATACTION_DECODE_STYLE_NAME:
121 			case XML_ATACTION_DECODE_STYLE_NAME_REF:
122 				{
123 					OUString aAttrValue( rAttrValue );
124 					if( GetTransformer().DecodeStyleName(aAttrValue) )
125 						pMutableAttrList->SetValueByIndex( i, aAttrValue );
126 				}
127 				break;
128 			}
129 		}
130 	}
131 
132 	XMLTokenEnum eToken = XML_FOOTNOTE;
133 	switch( m_eTypeToken )
134 	{
135 	case XML_NOTE:
136 		eToken = (m_bEndNote ? XML_ENDNOTE : XML_FOOTNOTE);
137 		break;
138 	case XML_NOTES_CONFIGURATION:
139 		eToken = (m_bEndNote ? XML_ENDNOTES_CONFIGURATION
140 							 : XML_FOOTNOTES_CONFIGURATION);
141 		break;
142 	case XML_NOTE_REF:
143 		eToken = (m_bEndNote ? XML_ENDNOTE_REF : XML_FOOTNOTE_REF);
144 		break;
145 	default:
146 		OSL_ENSURE( XML_NOTE==m_eTypeToken, "invalid note type" );
147 		break;
148 	}
149 
150 	SetExportQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
151 							XML_NAMESPACE_TEXT,
152 							::xmloff::token::GetXMLToken( eToken ) ) );
153 	if( m_bPersistent )
154 		XMLPersElemContentTContext::StartElement( xAttrList );
155 	else
156 		GetTransformer().GetDocHandler()->startElement( GetExportQName(),
157 														xAttrList );
158 }
159 
160 void XMLNotesTransformerContext::EndElement()
161 {
162 	if( m_bPersistent )
163 	{
164 		XMLPersElemContentTContext::EndElement();
165 	}
166 	else
167 	{
168 		GetTransformer().GetDocHandler()->endElement( GetExportQName() );
169 	}
170 }
171 
172 XMLTransformerContext *XMLNotesTransformerContext::CreateChildContext(
173 		sal_uInt16 nPrefix,
174 		const OUString& rLocalName,
175 		const OUString& rQName,
176 		const Reference< XAttributeList >& rAttrList )
177 {
178 	XMLTransformerContext *pContext = 0;
179 	if( XML_NOTE == m_eTypeToken )
180 	{
181 		if( XML_NAMESPACE_TEXT == nPrefix )
182 		{
183 			XMLTokenEnum eToken ( XML_TOKEN_INVALID );
184 			if( IsXMLToken( rLocalName, XML_NOTE_CITATION ) )
185 			{
186 				eToken = m_bEndNote ? XML_ENDNOTE_CITATION
187 								  : XML_FOOTNOTE_CITATION;
188 			}
189 			else if( IsXMLToken( rLocalName, XML_NOTE_BODY ) )
190 			{
191 				eToken = m_bEndNote ? XML_ENDNOTE_BODY
192 								  : XML_FOOTNOTE_BODY;
193 			}
194 
195 			if( XML_TOKEN_INVALID != eToken )
196 			{
197 				if( m_bPersistent  )
198 				{
199 					pContext = new XMLPersTextContentTContext(
200 									GetTransformer(), rQName,
201 									XML_NAMESPACE_TEXT,
202 									eToken );
203 					AddContent( pContext );
204 
205 				}
206 				else
207 				{
208 					pContext = new XMLRenameElemTransformerContext(
209 									GetTransformer(), rQName,
210 									XML_NAMESPACE_TEXT,
211 									eToken );
212 				}
213 			}
214 		}
215 	}
216 
217 	if( !pContext )
218 	{
219 		pContext = m_bPersistent
220 						? XMLPersElemContentTContext::CreateChildContext(
221 								nPrefix, rLocalName, rQName, rAttrList )
222 						: XMLTransformerContext::CreateChildContext(
223 								nPrefix, rLocalName, rQName, rAttrList );
224 	}
225 
226 	return pContext;
227 }
228 
229 sal_Bool XMLNotesTransformerContext::IsPersistent() const
230 {
231 	return m_bPersistent;
232 }
233