1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_shell.hxx"
30*cdf0e10cSrcweir #include "internal/contentreader.hxx"
31*cdf0e10cSrcweir #include "dummytag.hxx"
32*cdf0e10cSrcweir #include "simpletag.hxx"
33*cdf0e10cSrcweir #include "autostyletag.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "assert.h"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /** constructor.
38*cdf0e10cSrcweir */
39*cdf0e10cSrcweir CContentReader::CContentReader( const std::string& DocumentName, LocaleSet_t const & DocumentLocale ):
40*cdf0e10cSrcweir CBaseReader( DocumentName )
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir     try
43*cdf0e10cSrcweir     {
44*cdf0e10cSrcweir 		m_DefaultLocale = DocumentLocale;
45*cdf0e10cSrcweir         Initialize( DOC_CONTENT_NAME );
46*cdf0e10cSrcweir     }
47*cdf0e10cSrcweir     catch(xml_parser_exception&
48*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
49*cdf0e10cSrcweir     ex
50*cdf0e10cSrcweir     #endif
51*cdf0e10cSrcweir     )
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         ENSURE(false, ex.what());
54*cdf0e10cSrcweir     }
55*cdf0e10cSrcweir     catch(...)
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         ENSURE(false, "Unknown error");
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir CContentReader::CContentReader( void* stream, LocaleSet_t const & DocumentLocale, zlib_filefunc_def* fa ) :
62*cdf0e10cSrcweir CBaseReader( stream, fa )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir try
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir 		m_DefaultLocale = DocumentLocale;
67*cdf0e10cSrcweir         Initialize( DOC_CONTENT_NAME );
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir     catch(xml_parser_exception&
70*cdf0e10cSrcweir     #if OSL_DEBUG_LEVEL > 0
71*cdf0e10cSrcweir     ex
72*cdf0e10cSrcweir     #endif
73*cdf0e10cSrcweir     )
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         ENSURE(false, ex.what());
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir     catch(...)
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         ENSURE(false, "Unknown error");
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir /** destructor.
85*cdf0e10cSrcweir */
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir CContentReader::~CContentReader( void )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir /***********************   helper functions  ***********************/
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir /** choose an appropriate tag reader
94*cdf0e10cSrcweir */
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir ITag* CContentReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     if (( tag_name == CONTENT_TEXT_A )||( tag_name == CONTENT_TEXT_P )||
99*cdf0e10cSrcweir 		( tag_name == CONTENT_TEXT_SPAN ) ||( tag_name == CONTENT_TEXT_H )||
100*cdf0e10cSrcweir 		( tag_name == CONTENT_TEXT_SEQUENCE ) ||( tag_name == CONTENT_TEXT_BOOKMARK_REF )||
101*cdf0e10cSrcweir 		( tag_name == CONTENT_TEXT_INDEX_TITLE_TEMPLATE ) )
102*cdf0e10cSrcweir         return new CSimpleTag(XmlAttributes);
103*cdf0e10cSrcweir     else if ( tag_name == CONTENT_STYLE_STYLE )
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         // if style:style | style:name is exist,, fill the style field, otherwise do nothing;
106*cdf0e10cSrcweir         if  ( XmlAttributes.find(CONTENT_STYLE_STYLE_NAME) != XmlAttributes.end())
107*cdf0e10cSrcweir 	        return new CAutoStyleTag(XmlAttributes);
108*cdf0e10cSrcweir        else
109*cdf0e10cSrcweir 	        return new CDummyTag();
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir     else if ( ( tag_name == CONTENT_STYLE_PROPERTIES ) || ( tag_name == CONTENT_TEXT_STYLE_PROPERTIES ) )
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir 		assert( !m_TagBuilderStack.empty() );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		//here we presume that if CONTENT_STYLE_PROPERTIES tag is present, it just follow CONTENT_STYLE_STYLE;
116*cdf0e10cSrcweir 		ITag* pTagBuilder = m_TagBuilderStack.top();
117*cdf0e10cSrcweir 		pTagBuilder->addAttributes( XmlAttributes );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	    return new CDummyTag();
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir     else
122*cdf0e10cSrcweir 	    return new CDummyTag();
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir /** get style of the current content.
126*cdf0e10cSrcweir */
127*cdf0e10cSrcweir ::std::wstring CContentReader::getCurrentContentStyle( void )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir 	assert( !m_TagBuilderStack.empty() );
130*cdf0e10cSrcweir 	ITag* pTagBuilder = m_TagBuilderStack.top();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	return ( pTagBuilder->getTagAttribute(CONTENT_TEXT_STYLENAME) );
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir /** add chunk into Chunk Buffer.
136*cdf0e10cSrcweir */
137*cdf0e10cSrcweir void CContentReader::addChunk( LocaleSet_t const & Locale, Content_t const & Content )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	if ( Content == EMPTY_STRING )
140*cdf0e10cSrcweir 		return;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	if ( ( ( m_ChunkBuffer.empty() ) || ( m_ChunkBuffer.back().first != Locale ) ) &&
143*cdf0e10cSrcweir 		 ( ( Content != SPACE )  && ( Content != LF ) ) )
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir 		// if met a new locale, add a blank new chunk;
146*cdf0e10cSrcweir 		Chunk_t Chunk;
147*cdf0e10cSrcweir 		Chunk.first = Locale;
148*cdf0e10cSrcweir 		Chunk.second = EMPTY_STRING;
149*cdf0e10cSrcweir 		m_ChunkBuffer.push_back( Chunk );
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	if ( !m_ChunkBuffer.empty() )
153*cdf0e10cSrcweir 		m_ChunkBuffer.back().second += Content;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir /** get a style's locale field.
157*cdf0e10cSrcweir */
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir LocaleSet_t const & CContentReader::getLocale( const StyleName_t Style )
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	if ( m_StyleMap.empty() )
162*cdf0e10cSrcweir 		return m_DefaultLocale;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	StyleLocaleMap_t :: const_iterator style_Iter;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	if ( ( style_Iter = m_StyleMap.find( Style ) ) == m_StyleMap.end( ) )
167*cdf0e10cSrcweir 		return m_DefaultLocale;
168*cdf0e10cSrcweir 	else
169*cdf0e10cSrcweir 		return style_Iter->second;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir /***********************   event handler functions  ***********************/
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir //------------------------------
176*cdf0e10cSrcweir // start_element occurs when a tag is start
177*cdf0e10cSrcweir //------------------------------
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir void CContentReader::start_element(
180*cdf0e10cSrcweir     const std::wstring& /*raw_name*/,
181*cdf0e10cSrcweir     const std::wstring& local_name,
182*cdf0e10cSrcweir     const XmlTagAttributes_t& attributes)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir     //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
185*cdf0e10cSrcweir     ITag* pTagBuilder = chooseTagReader( local_name,attributes );
186*cdf0e10cSrcweir     assert( pTagBuilder != NULL );
187*cdf0e10cSrcweir     pTagBuilder->startTag( );
188*cdf0e10cSrcweir     m_TagBuilderStack.push( pTagBuilder );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir //------------------------------
193*cdf0e10cSrcweir // end_element occurs when a tag is closed
194*cdf0e10cSrcweir //------------------------------
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir void CContentReader::end_element(const std::wstring& /*raw_name*/, const std::wstring& local_name)
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir     assert( !m_TagBuilderStack.empty() );
199*cdf0e10cSrcweir     ITag* pTagBuilder = m_TagBuilderStack.top();
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     if ( local_name == CONTENT_STYLE_STYLE )
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir 		StyleLocalePair_t StyleLocalePair = static_cast<CAutoStyleTag * >( pTagBuilder)->getStyleLocalePair();
204*cdf0e10cSrcweir 		if ( ( static_cast<CAutoStyleTag * >( pTagBuilder)->isFull() ) && ( StyleLocalePair.second != m_DefaultLocale ) )
205*cdf0e10cSrcweir 				m_StyleMap.insert( StyleLocalePair );
206*cdf0e10cSrcweir 	}
207*cdf0e10cSrcweir     if (( local_name == CONTENT_TEXT_A )||( local_name == CONTENT_TEXT_SPAN ) ||
208*cdf0e10cSrcweir 		( local_name == CONTENT_TEXT_SEQUENCE )||( local_name == CONTENT_TEXT_BOOKMARK_REF ))
209*cdf0e10cSrcweir 		addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( SPACE ) );
210*cdf0e10cSrcweir     if ((( local_name == CONTENT_TEXT_P )||( local_name == CONTENT_TEXT_H ) ||
211*cdf0e10cSrcweir 		 ( local_name == CONTENT_TEXT_INDEX_TITLE_TEMPLATE ) )&&
212*cdf0e10cSrcweir 		( EMPTY_STRING != pTagBuilder->getTagContent( ) ) )
213*cdf0e10cSrcweir 		addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( LF ) );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     m_TagBuilderStack.pop();
216*cdf0e10cSrcweir     pTagBuilder->endTag();
217*cdf0e10cSrcweir     delete pTagBuilder;
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir //------------------------------
222*cdf0e10cSrcweir // characters occurs when receiving characters
223*cdf0e10cSrcweir //------------------------------
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir void CContentReader::characters( const std::wstring& character )
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir     if ( character.length() > 0 && !HasOnlySpaces( character ) )
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir 		addChunk( getLocale( getCurrentContentStyle() ), ::std::wstring( character ) );
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         ITag* pTagBuilder = m_TagBuilderStack.top();
232*cdf0e10cSrcweir         pTagBuilder->addCharacters( character );
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236