Update ooo320-m1
[ooovba.git] / shell / source / all / ooofilereader / metainforeader.cxx
blobe179d9ab14acfb95779bce57b47afe3eb8498f25
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: metainforeader.cxx,v $
10 * $Revision: 1.7 $
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/metainforeader.hxx"
34 #include "dummytag.hxx"
35 #include "simpletag.hxx"
36 #include "keywordstag.hxx"
38 #include "assert.h"
40 /** constructor.
42 CMetaInfoReader::CMetaInfoReader( const std::string& DocumentName ):
43 CBaseReader( DocumentName )
45 try
47 m_pKeywords_Builder = new CKeywordsTag( );
48 m_pSimple_Builder = new CSimpleTag( );
49 m_pDummy_Builder = new CDummyTag( );
51 //retrieve all infomation that is useful
52 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
53 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
54 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
55 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
56 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
57 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
59 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
60 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
61 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
62 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
63 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
64 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
65 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
67 Initialize( META_CONTENT_NAME );
69 catch(xml_parser_exception&
70 #if OSL_DEBUG_LEVEL > 0
72 #endif
75 ENSURE(false, ex.what());
77 catch(...)
79 ENSURE(false, "Unknown error");
83 CMetaInfoReader::CMetaInfoReader( void* stream, zlib_filefunc_def* fa) :
84 CBaseReader( stream, fa)
86 try
88 m_pKeywords_Builder = new CKeywordsTag( );
89 m_pSimple_Builder = new CSimpleTag( );
90 m_pDummy_Builder = new CDummyTag( );
92 //retrieve all infomation that is useful
93 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
94 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
95 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
96 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
97 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
98 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
100 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
101 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
102 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
103 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
104 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
105 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
106 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
108 Initialize( META_CONTENT_NAME );
110 catch(xml_parser_exception&
111 #if OSL_DEBUG_LEVEL > 0
113 #endif
116 ENSURE(false, ex.what());
118 catch(...)
120 ENSURE(false, "Unknown error");
125 /** destructor.
128 CMetaInfoReader::~CMetaInfoReader( void )
130 delete m_pKeywords_Builder;
131 delete m_pSimple_Builder;
132 delete m_pDummy_Builder;
136 /*********************** export functions ***********************/
138 /** check if the Tag is in the target meta.xml file.
140 @param TagName
141 the name of the tag that will be retrive.
143 bool CMetaInfoReader::hasTag( std::wstring TagName ) const
145 return ( m_AllMetaInfo.find(TagName) != m_AllMetaInfo.end());
148 /** Get a specific tag content, compound tags will be returned as comma separated list.
150 @param TagName
151 the name of the tag that will be retrive.
153 std::wstring CMetaInfoReader::getTagData( const std::wstring& TagName)
155 if( hasTag( TagName ) )
156 return m_AllMetaInfo[TagName].first;
157 else
158 return EMPTY_STRING;
161 /** check if the a tag has the specific attribute.
163 @param TagName
164 the name of the tag.
165 @param AttributeName
166 the name of the attribute.
168 bool CMetaInfoReader::hasTagAttribute( const std::wstring TagName, std::wstring AttributeName)
170 return ( m_AllMetaInfo[TagName].second.find( AttributeName) != m_AllMetaInfo[TagName].second.end() );
173 /** Get a specific attribute content.
175 @param TagName
176 the name of the tag.
177 @param AttributeName
178 the name of the attribute.
180 std::wstring CMetaInfoReader::getTagAttribute( const std::wstring TagName, std::wstring AttributeName)
182 if ( hasTagAttribute( TagName, AttributeName ) )
183 return m_AllMetaInfo[ TagName ].second[AttributeName];
184 else
185 return EMPTY_STRING;
188 /** helper function for getDefaultLocale().
190 const LocaleSet_t EN_US_LOCALE( ::std::make_pair( ::std::wstring( L"en" ), ::std::wstring( L"US" )));
192 bool isValidLocale ( ::std::wstring Locale )
194 return ( Locale.length() == 5 );
197 /** Get the default language of the whole document, if no such field, en-US is returned.
199 LocaleSet_t CMetaInfoReader::getDefaultLocale()
201 if (hasTag(META_INFO_LANGUAGE))
203 ::std::wstring locale = m_AllMetaInfo[META_INFO_LANGUAGE].first;
204 return isValidLocale(locale) ? ::std::make_pair(locale.substr( 0,2), locale.substr( 3,2)) : EN_US_LOCALE;
206 else
207 return EN_US_LOCALE;
210 /*********************** tag related functions ***********************/
212 /** choose an appropriate tag reader
215 ITag* CMetaInfoReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
217 if ( tag_name == META_INFO_KEYWORD )
219 m_AllMetaInfo[META_INFO_KEYWORDS].second = XmlAttributes;
220 return m_pKeywords_Builder;
222 if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
223 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
224 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
226 m_AllMetaInfo[tag_name].second = XmlAttributes;
227 return m_pSimple_Builder;
229 else
230 return m_pDummy_Builder;
234 //------------------------------
235 // save the received content into structure.
236 //------------------------------
237 void CMetaInfoReader::saveTagContent( const std::wstring& tag_name )
239 ITag* pTagBuilder;
240 if ( tag_name == META_INFO_KEYWORDS )
241 pTagBuilder = m_pKeywords_Builder;
242 else if ( tag_name == META_INFO_KEYWORD )
244 // added for support for OASIS xml file format.
245 m_AllMetaInfo[META_INFO_KEYWORDS].first =m_pKeywords_Builder->getTagContent( );
246 return;
248 else if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
249 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
250 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
251 pTagBuilder = m_pSimple_Builder;
252 else
253 pTagBuilder = m_pDummy_Builder;
255 m_AllMetaInfo[tag_name].first =pTagBuilder->getTagContent( );
259 /*********************** event handler functions ***********************/
261 //------------------------------
262 // start_element occurs when a tag is start
263 //------------------------------
265 void CMetaInfoReader::start_element(
266 const std::wstring& /*raw_name*/,
267 const std::wstring& local_name,
268 const XmlTagAttributes_t& attributes)
270 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
271 ITag* pTagBuilder = chooseTagReader( local_name,attributes );
272 assert( pTagBuilder!= NULL );
273 pTagBuilder->startTag( );
274 m_TagBuilderStack.push( pTagBuilder );
278 //------------------------------
279 // end_element occurs when a tag is closed
280 //------------------------------
282 void CMetaInfoReader::end_element(const std::wstring& /*raw_name*/, const std::wstring& local_name)
284 assert( !m_TagBuilderStack.empty() );
285 ITag* pTagBuilder = m_TagBuilderStack.top();
286 m_TagBuilderStack.pop();
287 pTagBuilder->endTag();
289 saveTagContent( local_name );
293 //------------------------------
294 // characters occurs when receiving characters
295 //------------------------------
297 void CMetaInfoReader::characters( const std::wstring& character )
299 if ( character.length() > 0 && !HasOnlySpaces( character ) )
301 ITag* pTagBuilder = m_TagBuilderStack.top();
302 pTagBuilder->addCharacters( character );