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 <contentreader.hxx>
21 #include "dummytag.hxx"
22 #include "simpletag.hxx"
23 #include "autostyletag.hxx"
24 
25 #include <assert.h>
26 
27 /** constructor.
28 */
CContentReader(const Filepath_t & DocumentName,LocaleSet_t const & DocumentLocale)29 CContentReader::CContentReader( const Filepath_t& DocumentName, LocaleSet_t const & DocumentLocale ):
30 CBaseReader( DocumentName )
31 {
32     try
33     {
34         m_DefaultLocale = DocumentLocale;
35         Initialize( DOC_CONTENT_NAME );
36     }
37     catch(xml_parser_exception&)
38     {
39         // OSL_ENSURE(false, ex.what());
40     }
41     catch(...)
42     {
43         // OSL_ENSURE(false, "Unknown error");
44     }
45 }
46 
CContentReader(StreamInterface * stream,LocaleSet_t const & DocumentLocale)47 CContentReader::CContentReader( StreamInterface* stream, LocaleSet_t const & DocumentLocale ) :
48 CBaseReader( stream )
49 {
50 try
51     {
52         m_DefaultLocale = DocumentLocale;
53         Initialize( DOC_CONTENT_NAME );
54     }
55     catch(xml_parser_exception&)
56     {
57         // OSL_ENSURE(false, ex.what());
58     }
59     catch(...)
60     {
61         // OSL_ENSURE(false, "Unknown error");
62     }
63 }
64 
65 
66 /** destructor.
67 */
68 
~CContentReader()69 CContentReader::~CContentReader()
70 {
71 }
72 
73 /***********************   helper functions  ***********************/
74 
75 /** choose an appropriate tag reader
76 */
77 
chooseTagReader(const std::wstring & tag_name,const XmlTagAttributes_t & XmlAttributes)78 ITag* CContentReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
79 {
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 )
86     {
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);
90        else
91             return new CDummyTag;
92     }
93     else if ( ( tag_name == CONTENT_STYLE_PROPERTIES ) || ( tag_name == CONTENT_TEXT_STYLE_PROPERTIES ) )
94     {
95         assert( !m_TagBuilderStack.empty() );
96 
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 );
100 
101         return new CDummyTag;
102     }
103     else
104         return new CDummyTag;
105 }
106 
107 /** get style of the current content.
108 */
getCurrentContentStyle()109 ::std::wstring CContentReader::getCurrentContentStyle()
110 {
111     assert( !m_TagBuilderStack.empty() );
112     ITag* pTagBuilder = m_TagBuilderStack.top();
113 
114     return pTagBuilder->getTagAttribute(CONTENT_TEXT_STYLENAME);
115 }
116 
117 /** add chunk into Chunk Buffer.
118 */
addChunk(LocaleSet_t const & Locale,Content_t const & Content)119 void CContentReader::addChunk( LocaleSet_t const & Locale, Content_t const & Content )
120 {
121     if ( Content == EMPTY_STRING )
122         return;
123 
124     if ( ( ( m_ChunkBuffer.empty() ) || ( m_ChunkBuffer.back().first != Locale ) ) &&
125          ( ( Content != SPACE )  && ( Content != LF ) ) )
126     {
127         // if met a new locale, add a blank new chunk;
128         Chunk_t Chunk;
129         Chunk.first = Locale;
130         Chunk.second = EMPTY_STRING;
131         m_ChunkBuffer.push_back( Chunk );
132     }
133 
134     if ( !m_ChunkBuffer.empty() )
135         m_ChunkBuffer.back().second += Content;
136 }
137 
138 /** get a style's locale field.
139 */
140 
getLocale(const StyleName_t & Style)141 LocaleSet_t const & CContentReader::getLocale( const StyleName_t& Style )
142 {
143     if ( m_StyleMap.empty() )
144         return m_DefaultLocale;
145 
146     StyleLocaleMap_t::const_iterator style_Iter;
147 
148     if ( ( style_Iter = m_StyleMap.find( Style ) ) == m_StyleMap.end( ) )
149         return m_DefaultLocale;
150     else
151         return style_Iter->second;
152 
153 }
154 
155 /***********************   event handler functions  ***********************/
156 
157 
158 // start_element occurs when a tag is start
159 
160 
start_element(const string_t &,const string_t & local_name,const xml_tag_attribute_container_t & attributes)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)
165 {
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 );
171 
172 }
173 
174 
175 // end_element occurs when a tag is closed
176 
177 
end_element(const string_t &,const string_t & local_name)178 void CContentReader::end_element(const string_t& /*raw_name*/, const string_t& local_name)
179 {
180     assert( !m_TagBuilderStack.empty() );
181     ITag* pTagBuilder = m_TagBuilderStack.top();
182 
183     if ( local_name == CONTENT_STYLE_STYLE )
184     {
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 );
188     }
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 ) );
196 
197     m_TagBuilderStack.pop();
198     pTagBuilder->endTag();
199     delete pTagBuilder;
200 
201 }
202 
203 
204 // characters occurs when receiving characters
205 
206 
characters(const string_t & character)207 void CContentReader::characters( const string_t& character )
208 {
209     if ( character.length() > 0 && !HasOnlySpaces( character ) )
210     {
211         addChunk( getLocale( getCurrentContentStyle() ), character );
212 
213         ITag* pTagBuilder = m_TagBuilderStack.top();
214         pTagBuilder->addCharacters( character );
215     }
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
219