1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <contentreader.hxx>
21 #include "dummytag.hxx"
22 #include "simpletag.hxx"
23 #include "autostyletag.hxx"
29 CContentReader::CContentReader( const Filepath_t
& DocumentName
, LocaleSet_t
const & DocumentLocale
):
30 CBaseReader( DocumentName
)
34 m_DefaultLocale
= DocumentLocale
;
35 Initialize( DOC_CONTENT_NAME
);
37 catch(xml_parser_exception
&)
39 // OSL_ENSURE(false, ex.what());
43 // OSL_ENSURE(false, "Unknown error");
47 CContentReader::CContentReader( StreamInterface
* stream
, LocaleSet_t
const & DocumentLocale
) :
52 m_DefaultLocale
= DocumentLocale
;
53 Initialize( DOC_CONTENT_NAME
);
55 catch(xml_parser_exception
&)
57 // OSL_ENSURE(false, ex.what());
61 // OSL_ENSURE(false, "Unknown error");
69 CContentReader::~CContentReader()
73 /*********************** helper functions ***********************/
75 /** choose an appropriate tag reader
78 ITag
* CContentReader::chooseTagReader( const std::wstring
& tag_name
, const XmlTagAttributes_t
& XmlAttributes
)
80 if (( tag_name
== CONTENT_TEXT_A
)||( tag_name
== CONTENT_TEXT_P
)||
81 ( tag_name
== CONTENT_TEXT_SPAN
) ||( tag_name
== CONTENT_TEXT_H
)||
82 ( tag_name
== CONTENT_TEXT_SEQUENCE
) ||( tag_name
== CONTENT_TEXT_BOOKMARK_REF
)||
83 ( tag_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )
84 return new CSimpleTag(XmlAttributes
);
85 else if ( tag_name
== CONTENT_STYLE_STYLE
)
87 // if style:style | style:name is exist,, fill the style field, otherwise do nothing;
88 if ( XmlAttributes
.find(CONTENT_STYLE_STYLE_NAME
) != XmlAttributes
.end())
89 return new CAutoStyleTag(XmlAttributes
);
93 else if ( ( tag_name
== CONTENT_STYLE_PROPERTIES
) || ( tag_name
== CONTENT_TEXT_STYLE_PROPERTIES
) )
95 assert( !m_TagBuilderStack
.empty() );
97 //here we presume that if CONTENT_STYLE_PROPERTIES tag is present, it just follow CONTENT_STYLE_STYLE;
98 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
99 pTagBuilder
->addAttributes( XmlAttributes
);
101 return new CDummyTag
;
104 return new CDummyTag
;
107 /** get style of the current content.
109 ::std::wstring
CContentReader::getCurrentContentStyle()
111 assert( !m_TagBuilderStack
.empty() );
112 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
114 return pTagBuilder
->getTagAttribute(CONTENT_TEXT_STYLENAME
);
117 /** add chunk into Chunk Buffer.
119 void CContentReader::addChunk( LocaleSet_t
const & Locale
, Content_t
const & Content
)
121 if ( Content
== EMPTY_STRING
)
124 if ( ( ( m_ChunkBuffer
.empty() ) || ( m_ChunkBuffer
.back().first
!= Locale
) ) &&
125 ( ( Content
!= SPACE
) && ( Content
!= LF
) ) )
127 // if met a new locale, add a blank new chunk;
129 Chunk
.first
= Locale
;
130 Chunk
.second
= EMPTY_STRING
;
131 m_ChunkBuffer
.push_back( Chunk
);
134 if ( !m_ChunkBuffer
.empty() )
135 m_ChunkBuffer
.back().second
+= Content
;
138 /** get a style's locale field.
141 LocaleSet_t
const & CContentReader::getLocale( const StyleName_t
& Style
)
143 if ( m_StyleMap
.empty() )
144 return m_DefaultLocale
;
146 StyleLocaleMap_t::const_iterator style_Iter
;
148 if ( ( style_Iter
= m_StyleMap
.find( Style
) ) == m_StyleMap
.end( ) )
149 return m_DefaultLocale
;
151 return style_Iter
->second
;
155 /*********************** event handler functions ***********************/
158 // start_element occurs when a tag is start
161 void CContentReader::start_element(
162 const string_t
& /*raw_name*/,
163 const string_t
& local_name
,
164 const xml_tag_attribute_container_t
& attributes
)
166 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
167 ITag
* pTagBuilder
= chooseTagReader( local_name
,attributes
);
168 assert( pTagBuilder
!= nullptr );
169 pTagBuilder
->startTag( );
170 m_TagBuilderStack
.push( pTagBuilder
);
175 // end_element occurs when a tag is closed
178 void CContentReader::end_element(const string_t
& /*raw_name*/, const string_t
& local_name
)
180 assert( !m_TagBuilderStack
.empty() );
181 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
183 if ( local_name
== CONTENT_STYLE_STYLE
)
185 StyleLocalePair_t StyleLocalePair
= static_cast<CAutoStyleTag
* >( pTagBuilder
)->getStyleLocalePair();
186 if ( ( static_cast<CAutoStyleTag
* >( pTagBuilder
)->isFull() ) && ( StyleLocalePair
.second
!= m_DefaultLocale
) )
187 m_StyleMap
.insert( StyleLocalePair
);
189 if (( local_name
== CONTENT_TEXT_A
)||( local_name
== CONTENT_TEXT_SPAN
) ||
190 ( local_name
== CONTENT_TEXT_SEQUENCE
)||( local_name
== CONTENT_TEXT_BOOKMARK_REF
))
191 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( SPACE
) );
192 if ((( local_name
== CONTENT_TEXT_P
)||( local_name
== CONTENT_TEXT_H
) ||
193 ( local_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )&&
194 ( EMPTY_STRING
!= pTagBuilder
->getTagContent( ) ) )
195 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( LF
) );
197 m_TagBuilderStack
.pop();
198 pTagBuilder
->endTag();
204 // characters occurs when receiving characters
207 void CContentReader::characters( const string_t
& character
)
209 if ( character
.length() > 0 && !HasOnlySpaces( character
) )
211 addChunk( getLocale( getCurrentContentStyle() ), character
);
213 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
214 pTagBuilder
->addCharacters( character
);
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */