1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <com/sun/star/xml/sax/SAXException.hpp>
21 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22 #include <com/sun/star/xml/sax/XAttributeList.hpp>
23 #include <osl/diagnose.h>
24 #include <xmloff/nmspmap.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <xmloff/xmlnmspe.hxx>
27 
28 #include "TransformerBase.hxx"
29 #include "TransformerActions.hxx"
30 #include "AttrTransformerAction.hxx"
31 #include "ActionMapTypesOASIS.hxx"
32 #include "MutableAttrList.hxx"
33 #include "RenameElemTContext.hxx"
34 #include "FlatTContext.hxx"
35 
36 #include "NotesTContext.hxx"
37 
38 using namespace ::xmloff::token;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
41 
XMLNotesTransformerContext(XMLTransformerBase & rImp,const OUString & rQName,XMLTokenEnum eToken,bool bPersistent)42 XMLNotesTransformerContext::XMLNotesTransformerContext(
43         XMLTransformerBase& rImp,
44         const OUString& rQName,
45         XMLTokenEnum eToken, bool bPersistent ) :
46     XMLPersElemContentTContext( rImp, rQName ),
47     m_bEndNote( false ),
48     m_bPersistent( bPersistent ),
49     m_eTypeToken( eToken )
50 {
51 }
52 
~XMLNotesTransformerContext()53 XMLNotesTransformerContext::~XMLNotesTransformerContext()
54 {
55 }
56 
StartElement(const Reference<XAttributeList> & rAttrList)57 void XMLNotesTransformerContext::StartElement(
58         const Reference< XAttributeList >& rAttrList )
59 {
60     XMLTransformerActions *pActions =
61         GetTransformer().GetUserDefinedActions( OASIS_NOTES_ACTIONS );
62     OSL_ENSURE( pActions, "go no actions" );
63 
64     Reference< XAttributeList > xAttrList( rAttrList );
65     XMLMutableAttributeList *pMutableAttrList = nullptr;
66     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
67     for( sal_Int16 i=0; i < nAttrCount; i++ )
68     {
69         const OUString& rAttrName = xAttrList->getNameByIndex( i );
70         OUString aLocalName;
71         sal_uInt16 nPrefix =
72             GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
73                                                                  &aLocalName );
74         XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
75         XMLTransformerActions::const_iterator aIter =
76             pActions->find( aKey );
77         if( aIter != pActions->end() )
78         {
79             const OUString& rAttrValue = xAttrList->getValueByIndex( i );
80 
81             if( !pMutableAttrList )
82             {
83                 pMutableAttrList =
84                     new XMLMutableAttributeList( xAttrList );
85                 xAttrList = pMutableAttrList;
86             }
87             switch( (*aIter).second.m_nActionType )
88             {
89             case XML_ATACTION_STYLE_FAMILY:
90                 {
91                     if( IsXMLToken( rAttrValue, XML_FOOTNOTE ) )
92                     {
93                     }
94                     else if( IsXMLToken( rAttrValue, XML_ENDNOTE ) )
95                     {
96                         m_bEndNote = true;
97                     }
98                     pMutableAttrList->RemoveAttributeByIndex( i );
99                     --i;
100                     --nAttrCount;
101                 }
102                 break;
103             case XML_ATACTION_DECODE_STYLE_NAME:
104             case XML_ATACTION_DECODE_STYLE_NAME_REF:
105                 {
106                     OUString aAttrValue( rAttrValue );
107                     if( XMLTransformerBase::DecodeStyleName(aAttrValue) )
108                         pMutableAttrList->SetValueByIndex( i, aAttrValue );
109                 }
110                 break;
111             }
112         }
113     }
114 
115     XMLTokenEnum eToken = XML_FOOTNOTE;
116     switch( m_eTypeToken )
117     {
118     case XML_NOTE:
119         eToken = (m_bEndNote ? XML_ENDNOTE : XML_FOOTNOTE);
120         break;
121     case XML_NOTES_CONFIGURATION:
122         eToken = (m_bEndNote ? XML_ENDNOTES_CONFIGURATION
123                              : XML_FOOTNOTES_CONFIGURATION);
124         break;
125     case XML_NOTE_REF:
126         eToken = (m_bEndNote ? XML_ENDNOTE_REF : XML_FOOTNOTE_REF);
127         break;
128     default:
129         OSL_ENSURE( XML_NOTE==m_eTypeToken, "invalid note type" );
130         break;
131     }
132 
133     SetExportQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
134                             XML_NAMESPACE_TEXT,
135                             ::xmloff::token::GetXMLToken( eToken ) ) );
136     if( m_bPersistent )
137         XMLPersElemContentTContext::StartElement( xAttrList );
138     else
139         GetTransformer().GetDocHandler()->startElement( GetExportQName(),
140                                                         xAttrList );
141 }
142 
EndElement()143 void XMLNotesTransformerContext::EndElement()
144 {
145     if( m_bPersistent )
146     {
147         XMLPersElemContentTContext::EndElement();
148     }
149     else
150     {
151         GetTransformer().GetDocHandler()->endElement( GetExportQName() );
152     }
153 }
154 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const OUString & rQName,const Reference<XAttributeList> & rAttrList)155 rtl::Reference<XMLTransformerContext> XMLNotesTransformerContext::CreateChildContext(
156         sal_uInt16 nPrefix,
157         const OUString& rLocalName,
158         const OUString& rQName,
159         const Reference< XAttributeList >& rAttrList )
160 {
161     rtl::Reference<XMLTransformerContext> pContext;
162     if( XML_NOTE == m_eTypeToken )
163     {
164         if( XML_NAMESPACE_TEXT == nPrefix )
165         {
166             XMLTokenEnum eToken ( XML_TOKEN_INVALID );
167             if( IsXMLToken( rLocalName, XML_NOTE_CITATION ) )
168             {
169                 eToken = m_bEndNote ? XML_ENDNOTE_CITATION
170                                   : XML_FOOTNOTE_CITATION;
171             }
172             else if( IsXMLToken( rLocalName, XML_NOTE_BODY ) )
173             {
174                 eToken = m_bEndNote ? XML_ENDNOTE_BODY
175                                   : XML_FOOTNOTE_BODY;
176             }
177 
178             if( XML_TOKEN_INVALID != eToken )
179             {
180                 if( m_bPersistent  )
181                 {
182                     pContext.set(new XMLPersTextContentTContext(
183                                     GetTransformer(), rQName,
184                                     XML_NAMESPACE_TEXT,
185                                     eToken ));
186                     AddContent( pContext );
187 
188                 }
189                 else
190                 {
191                     pContext.set(new XMLRenameElemTransformerContext(
192                                     GetTransformer(), rQName,
193                                     XML_NAMESPACE_TEXT,
194                                     eToken ));
195                 }
196             }
197         }
198     }
199 
200     if( !pContext.is() )
201     {
202         pContext = m_bPersistent
203                         ? XMLPersElemContentTContext::CreateChildContext(
204                                 nPrefix, rLocalName, rQName, rAttrList )
205                         : XMLTransformerContext::CreateChildContext(
206                                 nPrefix, rLocalName, rQName, rAttrList );
207     }
208 
209     return pContext;
210 }
211 
IsPersistent() const212 bool XMLNotesTransformerContext::IsPersistent() const
213 {
214     return m_bPersistent;
215 }
216 
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
218