LibreOffice Module shell (master) 1
contentreader.cxx
Go to the documentation of this file.
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
29CContentReader::CContentReader( const Filepath_t& DocumentName, LocaleSet_t const & DocumentLocale ):
30CBaseReader( DocumentName )
31{
32 try
33 {
34 m_DefaultLocale = DocumentLocale;
36 }
38 {
39 // OSL_ENSURE(false, ex.what());
40 }
41 catch(...)
42 {
43 // OSL_ENSURE(false, "Unknown error");
44 }
45}
46
49{
50try
51 {
52 m_DefaultLocale = DocumentLocale;
54 }
56 {
57 // OSL_ENSURE(false, ex.what());
58 }
59 catch(...)
60 {
61 // OSL_ENSURE(false, "Unknown error");
62 }
63}
64
65
70{
71}
72
73/*********************** helper functions ***********************/
74
78ITag* 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
110{
111 assert( !m_TagBuilderStack.empty() );
112 ITag* pTagBuilder = m_TagBuilderStack.top();
113
114 return pTagBuilder->getTagAttribute(CONTENT_TEXT_STYLENAME);
115}
116
119void 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
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
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
178void 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
207void CContentReader::characters( const string_t& character )
208{
209 if ( character.length() > 0 && !HasOnlySpaces( character ) )
210 {
212
213 ITag* pTagBuilder = m_TagBuilderStack.top();
214 pTagBuilder->addCharacters( character );
215 }
216}
217
218/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Implements the ITag interface for building a Style-Locale list.
void Initialize(const std::string &)
Read interested tag content into respective structure then start parsing process.
Definition: basereader.cxx:59
virtual void end_element(const string_t &raw_name, const string_t &local_name) override
end_element occurs when a tag is closed
std::stack< ITag * > m_TagBuilderStack
ChunkBuffer_t m_ChunkBuffer
virtual ~CContentReader() override
destructor.
virtual void start_element(const string_t &raw_name, const string_t &local_name, const xml_tag_attribute_container_t &attributes) override
start_element occurs when a tag is start.
void addChunk(LocaleSet_t const &Locale, Content_t const &Content)
add chunk into Chunk Buffer.
StyleLocaleMap_t m_StyleMap
CContentReader(const Filepath_t &DocumentName, LocaleSet_t const &DocumentLocale)
constructor.
LocaleSet_t const & getLocale(const StyleName_t &Style)
get a style's locale field.
ITag * chooseTagReader(const std::wstring &tag_name, const XmlTagAttributes_t &XmlAttributes)
choose an appropriate tag reader to handle the tag.
LocaleSet_t m_DefaultLocale
::std::wstring getCurrentContentStyle()
get style of the current content.
virtual void characters(const string_t &character) override
characters occurs when receiving characters
Implements the ITag interface but does nothing (Null object pattern), may be used for tags we are not...
Definition: dummytag.hxx:34
Implements the ITag interface for building a general info that is not a compound tag.
Definition: simpletag.hxx:32
Interface for a xml tag character builder.
Definition: itag.hxx:32
virtual ::std::wstring getTagAttribute(::std::wstring const &attrname)=0
virtual void startTag()=0
virtual void addCharacters(const std::wstring &characters)=0
virtual ::std::wstring getTagContent()=0
virtual void addAttributes(const XmlTagAttributes_t &attributes)=0
virtual void endTag()=0
#define DOC_CONTENT_NAME
Definition: config.hxx:29
#define CONTENT_STYLE_PROPERTIES
Definition: config.hxx:71
#define LF
Definition: config.hxx:33
#define CONTENT_TEXT_BOOKMARK_REF
Definition: config.hxx:65
#define EMPTY_STRING
Definition: config.hxx:31
#define CONTENT_TEXT_A
Definition: config.hxx:60
#define CONTENT_TEXT_P
Definition: config.hxx:61
#define CONTENT_TEXT_INDEX_TITLE_TEMPLATE
Definition: config.hxx:66
#define CONTENT_TEXT_STYLENAME
Definition: config.hxx:67
#define CONTENT_TEXT_H
Definition: config.hxx:62
#define CONTENT_TEXT_SPAN
Definition: config.hxx:63
#define CONTENT_TEXT_SEQUENCE
Definition: config.hxx:64
#define CONTENT_STYLE_STYLE_NAME
Definition: config.hxx:70
#define CONTENT_STYLE_STYLE
Definition: config.hxx:69
#define CONTENT_TEXT_STYLE_PROPERTIES
Definition: config.hxx:72
Reference< XOutputStream > stream
std::string Filepath_t
Definition: filepath.hxx:22
std::string string_t
std::map< string_t, string_t > xml_tag_attribute_container_t
#define SPACE
Definition: macbackend.mm:39
@ character
::std::pair< StyleName_t, LocaleSet_t > StyleLocalePair_t
Definition: types.hxx:63
::std::wstring StyleName_t
Definition: types.hxx:62
::std::pair< Language_t, Country_t > LocaleSet_t
Definition: types.hxx:48
::std::wstring Content_t
Definition: types.hxx:50
std::map< std::wstring, std::wstring > XmlTagAttributes_t
Definition: types.hxx:34
::std::pair< LocaleSet_t, Content_t > Chunk_t
Definition: types.hxx:51
bool HasOnlySpaces(const std::wstring &String)
helper function to judge if the string is only has spaces.
Definition: utilities.cxx:100