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 #undef OSL_DEBUG_LEVEL
23 #include <osl/diagnose.h>
25 #include "internal/contentreader.hxx"
26 #include "dummytag.hxx"
27 #include "simpletag.hxx"
28 #include "autostyletag.hxx"
34 CContentReader::CContentReader( const std::string
& DocumentName
, LocaleSet_t
const & DocumentLocale
):
35 CBaseReader( DocumentName
)
39 m_DefaultLocale
= DocumentLocale
;
40 Initialize( DOC_CONTENT_NAME
);
42 catch(xml_parser_exception
&
43 #if OSL_DEBUG_LEVEL > 0
48 OSL_ENSURE(false, ex
.what());
52 OSL_ENSURE(false, "Unknown error");
56 CContentReader::CContentReader( StreamInterface
* stream
, LocaleSet_t
const & DocumentLocale
) :
61 m_DefaultLocale
= DocumentLocale
;
62 Initialize( DOC_CONTENT_NAME
);
64 catch(xml_parser_exception
&
65 #if OSL_DEBUG_LEVEL > 0
70 OSL_ENSURE(false, ex
.what());
74 OSL_ENSURE(false, "Unknown error");
82 CContentReader::~CContentReader()
86 /*********************** helper functions ***********************/
88 /** choose an appropriate tag reader
91 ITag
* CContentReader::chooseTagReader( const std::wstring
& tag_name
, const XmlTagAttributes_t
& XmlAttributes
)
93 if (( tag_name
== CONTENT_TEXT_A
)||( tag_name
== CONTENT_TEXT_P
)||
94 ( tag_name
== CONTENT_TEXT_SPAN
) ||( tag_name
== CONTENT_TEXT_H
)||
95 ( tag_name
== CONTENT_TEXT_SEQUENCE
) ||( tag_name
== CONTENT_TEXT_BOOKMARK_REF
)||
96 ( tag_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )
97 return new CSimpleTag(XmlAttributes
);
98 else if ( tag_name
== CONTENT_STYLE_STYLE
)
100 // if style:style | style:name is exist,, fill the style field, otherwise do nothing;
101 if ( XmlAttributes
.find(CONTENT_STYLE_STYLE_NAME
) != XmlAttributes
.end())
102 return new CAutoStyleTag(XmlAttributes
);
104 return new CDummyTag();
106 else if ( ( tag_name
== CONTENT_STYLE_PROPERTIES
) || ( tag_name
== CONTENT_TEXT_STYLE_PROPERTIES
) )
108 assert( !m_TagBuilderStack
.empty() );
110 //here we presume that if CONTENT_STYLE_PROPERTIES tag is present, it just follow CONTENT_STYLE_STYLE;
111 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
112 pTagBuilder
->addAttributes( XmlAttributes
);
114 return new CDummyTag();
117 return new CDummyTag();
120 /** get style of the current content.
122 ::std::wstring
CContentReader::getCurrentContentStyle()
124 assert( !m_TagBuilderStack
.empty() );
125 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
127 return ( pTagBuilder
->getTagAttribute(CONTENT_TEXT_STYLENAME
) );
130 /** add chunk into Chunk Buffer.
132 void CContentReader::addChunk( LocaleSet_t
const & Locale
, Content_t
const & Content
)
134 if ( Content
== EMPTY_STRING
)
137 if ( ( ( m_ChunkBuffer
.empty() ) || ( m_ChunkBuffer
.back().first
!= Locale
) ) &&
138 ( ( Content
!= SPACE
) && ( Content
!= LF
) ) )
140 // if met a new locale, add a blank new chunk;
142 Chunk
.first
= Locale
;
143 Chunk
.second
= EMPTY_STRING
;
144 m_ChunkBuffer
.push_back( Chunk
);
147 if ( !m_ChunkBuffer
.empty() )
148 m_ChunkBuffer
.back().second
+= Content
;
151 /** get a style's locale field.
154 LocaleSet_t
const & CContentReader::getLocale( const StyleName_t
& Style
)
156 if ( m_StyleMap
.empty() )
157 return m_DefaultLocale
;
159 StyleLocaleMap_t :: const_iterator style_Iter
;
161 if ( ( style_Iter
= m_StyleMap
.find( Style
) ) == m_StyleMap
.end( ) )
162 return m_DefaultLocale
;
164 return style_Iter
->second
;
168 /*********************** event handler functions ***********************/
171 // start_element occurs when a tag is start
174 void CContentReader::start_element(
175 const std::wstring
& /*raw_name*/,
176 const std::wstring
& local_name
,
177 const XmlTagAttributes_t
& attributes
)
179 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
180 ITag
* pTagBuilder
= chooseTagReader( local_name
,attributes
);
181 assert( pTagBuilder
!= NULL
);
182 pTagBuilder
->startTag( );
183 m_TagBuilderStack
.push( pTagBuilder
);
188 // end_element occurs when a tag is closed
191 void CContentReader::end_element(const std::wstring
& /*raw_name*/, const std::wstring
& local_name
)
193 assert( !m_TagBuilderStack
.empty() );
194 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
196 if ( local_name
== CONTENT_STYLE_STYLE
)
198 StyleLocalePair_t StyleLocalePair
= static_cast<CAutoStyleTag
* >( pTagBuilder
)->getStyleLocalePair();
199 if ( ( static_cast<CAutoStyleTag
* >( pTagBuilder
)->isFull() ) && ( StyleLocalePair
.second
!= m_DefaultLocale
) )
200 m_StyleMap
.insert( StyleLocalePair
);
202 if (( local_name
== CONTENT_TEXT_A
)||( local_name
== CONTENT_TEXT_SPAN
) ||
203 ( local_name
== CONTENT_TEXT_SEQUENCE
)||( local_name
== CONTENT_TEXT_BOOKMARK_REF
))
204 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( SPACE
) );
205 if ((( local_name
== CONTENT_TEXT_P
)||( local_name
== CONTENT_TEXT_H
) ||
206 ( local_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )&&
207 ( EMPTY_STRING
!= pTagBuilder
->getTagContent( ) ) )
208 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( LF
) );
210 m_TagBuilderStack
.pop();
211 pTagBuilder
->endTag();
217 // characters occurs when receiving characters
220 void CContentReader::characters( const std::wstring
& character
)
222 if ( character
.length() > 0 && !HasOnlySpaces( character
) )
224 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( character
) );
226 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
227 pTagBuilder
->addCharacters( character
);
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */