1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: contentreader.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
33 #include "internal/contentreader.hxx"
34 #include "dummytag.hxx"
35 #include "simpletag.hxx"
36 #include "autostyletag.hxx"
42 CContentReader::CContentReader( const std::string
& DocumentName
, LocaleSet_t
const & DocumentLocale
):
43 CBaseReader( DocumentName
)
47 m_DefaultLocale
= DocumentLocale
;
48 Initialize( DOC_CONTENT_NAME
);
50 catch(xml_parser_exception
&
51 #if OSL_DEBUG_LEVEL > 0
56 ENSURE(false, ex
.what());
60 ENSURE(false, "Unknown error");
64 CContentReader::CContentReader( void* stream
, LocaleSet_t
const & DocumentLocale
, zlib_filefunc_def
* fa
) :
65 CBaseReader( stream
, fa
)
69 m_DefaultLocale
= DocumentLocale
;
70 Initialize( DOC_CONTENT_NAME
);
72 catch(xml_parser_exception
&
73 #if OSL_DEBUG_LEVEL > 0
78 ENSURE(false, ex
.what());
82 ENSURE(false, "Unknown error");
90 CContentReader::~CContentReader( void )
94 /*********************** helper functions ***********************/
96 /** choose an appropriate tag reader
99 ITag
* CContentReader::chooseTagReader( const std::wstring
& tag_name
, const XmlTagAttributes_t
& XmlAttributes
)
101 if (( tag_name
== CONTENT_TEXT_A
)||( tag_name
== CONTENT_TEXT_P
)||
102 ( tag_name
== CONTENT_TEXT_SPAN
) ||( tag_name
== CONTENT_TEXT_H
)||
103 ( tag_name
== CONTENT_TEXT_SEQUENCE
) ||( tag_name
== CONTENT_TEXT_BOOKMARK_REF
)||
104 ( tag_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )
105 return new CSimpleTag(XmlAttributes
);
106 else if ( tag_name
== CONTENT_STYLE_STYLE
)
108 // if style:style | style:name is exist,, fill the style field, otherwise do nothing;
109 if ( XmlAttributes
.find(CONTENT_STYLE_STYLE_NAME
) != XmlAttributes
.end())
110 return new CAutoStyleTag(XmlAttributes
);
112 return new CDummyTag();
114 else if ( ( tag_name
== CONTENT_STYLE_PROPERTIES
) || ( tag_name
== CONTENT_TEXT_STYLE_PROPERTIES
) )
116 assert( !m_TagBuilderStack
.empty() );
118 //here we presume that if CONTENT_STYLE_PROPERTIES tag is present, it just follow CONTENT_STYLE_STYLE;
119 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
120 pTagBuilder
->addAttributes( XmlAttributes
);
122 return new CDummyTag();
125 return new CDummyTag();
128 /** get style of the current content.
130 ::std::wstring
CContentReader::getCurrentContentStyle( void )
132 assert( !m_TagBuilderStack
.empty() );
133 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
135 return ( pTagBuilder
->getTagAttribute(CONTENT_TEXT_STYLENAME
) );
138 /** add chunk into Chunk Buffer.
140 void CContentReader::addChunk( LocaleSet_t
const & Locale
, Content_t
const & Content
)
142 if ( Content
== EMPTY_STRING
)
145 if ( ( ( m_ChunkBuffer
.empty() ) || ( m_ChunkBuffer
.back().first
!= Locale
) ) &&
146 ( ( Content
!= SPACE
) && ( Content
!= LF
) ) )
148 // if met a new locale, add a blank new chunk;
150 Chunk
.first
= Locale
;
151 Chunk
.second
= EMPTY_STRING
;
152 m_ChunkBuffer
.push_back( Chunk
);
155 if ( !m_ChunkBuffer
.empty() )
156 m_ChunkBuffer
.back().second
+= Content
;
159 /** get a style's locale field.
162 LocaleSet_t
const & CContentReader::getLocale( const StyleName_t Style
)
164 if ( m_StyleMap
.empty() )
165 return m_DefaultLocale
;
167 StyleLocaleMap_t :: const_iterator style_Iter
;
169 if ( ( style_Iter
= m_StyleMap
.find( Style
) ) == m_StyleMap
.end( ) )
170 return m_DefaultLocale
;
172 return style_Iter
->second
;
176 /*********************** event handler functions ***********************/
178 //------------------------------
179 // start_element occurs when a tag is start
180 //------------------------------
182 void CContentReader::start_element(
183 const std::wstring
& /*raw_name*/,
184 const std::wstring
& local_name
,
185 const XmlTagAttributes_t
& attributes
)
187 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
188 ITag
* pTagBuilder
= chooseTagReader( local_name
,attributes
);
189 assert( pTagBuilder
!= NULL
);
190 pTagBuilder
->startTag( );
191 m_TagBuilderStack
.push( pTagBuilder
);
195 //------------------------------
196 // end_element occurs when a tag is closed
197 //------------------------------
199 void CContentReader::end_element(const std::wstring
& /*raw_name*/, const std::wstring
& local_name
)
201 assert( !m_TagBuilderStack
.empty() );
202 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
204 if ( local_name
== CONTENT_STYLE_STYLE
)
206 StyleLocalePair_t StyleLocalePair
= static_cast<CAutoStyleTag
* >( pTagBuilder
)->getStyleLocalePair();
207 if ( ( static_cast<CAutoStyleTag
* >( pTagBuilder
)->isFull() ) && ( StyleLocalePair
.second
!= m_DefaultLocale
) )
208 m_StyleMap
.insert( StyleLocalePair
);
210 if (( local_name
== CONTENT_TEXT_A
)||( local_name
== CONTENT_TEXT_SPAN
) ||
211 ( local_name
== CONTENT_TEXT_SEQUENCE
)||( local_name
== CONTENT_TEXT_BOOKMARK_REF
))
212 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( SPACE
) );
213 if ((( local_name
== CONTENT_TEXT_P
)||( local_name
== CONTENT_TEXT_H
) ||
214 ( local_name
== CONTENT_TEXT_INDEX_TITLE_TEMPLATE
) )&&
215 ( EMPTY_STRING
!= pTagBuilder
->getTagContent( ) ) )
216 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( LF
) );
218 m_TagBuilderStack
.pop();
219 pTagBuilder
->endTag();
224 //------------------------------
225 // characters occurs when receiving characters
226 //------------------------------
228 void CContentReader::characters( const std::wstring
& character
)
230 if ( character
.length() > 0 && !HasOnlySpaces( character
) )
232 addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( character
) );
234 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
235 pTagBuilder
->addCharacters( character
);