Bump version to 5.0-14
[LibreOffice.git] / shell / source / win32 / ooofilereader / metainforeader.cxx
blobb4273bdc89b65cc7098f8f20f7a9e2ddda003319
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/metainforeader.hxx"
26 #include "dummytag.hxx"
27 #include "simpletag.hxx"
28 #include "keywordstag.hxx"
30 #include "assert.h"
32 /** constructor.
34 CMetaInfoReader::CMetaInfoReader( const std::string& DocumentName ):
35 CBaseReader( DocumentName )
37 try
39 m_pKeywords_Builder = new CKeywordsTag( );
40 m_pSimple_Builder = new CSimpleTag( );
41 m_pDummy_Builder = new CDummyTag( );
43 //retrieve all information that is useful
44 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
45 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
46 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
47 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
48 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
49 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
51 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
52 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
53 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
54 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
55 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
56 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
57 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
59 Initialize( META_CONTENT_NAME );
61 catch(xml_parser_exception&
62 #if OSL_DEBUG_LEVEL > 0
64 #endif
67 OSL_ENSURE(false, ex.what());
69 catch(...)
71 OSL_ENSURE(false, "Unknown error");
75 CMetaInfoReader::CMetaInfoReader( StreamInterface* stream ) :
76 CBaseReader( stream )
78 try
80 m_pKeywords_Builder = new CKeywordsTag( );
81 m_pSimple_Builder = new CSimpleTag( );
82 m_pDummy_Builder = new CDummyTag( );
84 //retrieve all information that is useful
85 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
86 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
87 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
88 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
89 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
90 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
92 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
93 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
94 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
95 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
96 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
97 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
98 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
100 Initialize( META_CONTENT_NAME );
102 catch(xml_parser_exception&
103 #if OSL_DEBUG_LEVEL > 0
105 #endif
108 OSL_ENSURE(false, ex.what());
110 catch(...)
112 OSL_ENSURE(false, "Unknown error");
117 /** destructor.
120 CMetaInfoReader::~CMetaInfoReader()
122 delete m_pKeywords_Builder;
123 delete m_pSimple_Builder;
124 delete m_pDummy_Builder;
128 /*********************** export functions ***********************/
130 /** check if the Tag is in the target meta.xml file.
132 @param TagName
133 the name of the tag that will be retrieve.
135 bool CMetaInfoReader::hasTag(const std::wstring& TagName) const
137 return ( m_AllMetaInfo.find(TagName) != m_AllMetaInfo.end());
140 /** Get a specific tag content, compound tags will be returned as comma separated list.
142 @param TagName
143 the name of the tag that will be retrieve.
145 std::wstring CMetaInfoReader::getTagData( const std::wstring& TagName)
147 if( hasTag( TagName ) )
148 return m_AllMetaInfo[TagName].first;
149 else
150 return EMPTY_STRING;
153 /** check if the a tag has the specific attribute.
155 @param TagName
156 the name of the tag.
157 @param AttributeName
158 the name of the attribute.
160 bool CMetaInfoReader::hasTagAttribute(const std::wstring& TagName, const std::wstring& AttributeName)
162 return ( m_AllMetaInfo[TagName].second.find( AttributeName) != m_AllMetaInfo[TagName].second.end() );
165 /** Get a specific attribute content.
167 @param TagName
168 the name of the tag.
169 @param AttributeName
170 the name of the attribute.
172 std::wstring CMetaInfoReader::getTagAttribute(const std::wstring& TagName, const std::wstring& AttributeName)
174 if (hasTagAttribute(TagName, AttributeName))
175 return m_AllMetaInfo[ TagName ].second[AttributeName];
176 else
177 return EMPTY_STRING;
180 /** helper function for getDefaultLocale().
182 const LocaleSet_t EN_US_LOCALE( ::std::make_pair( ::std::wstring( L"en" ), ::std::wstring( L"US" )));
184 bool isValidLocale(const ::std::wstring& Locale)
186 return ( Locale.length() == 5 );
189 /** Get the default language of the whole document, if no such field, en-US is returned.
191 LocaleSet_t CMetaInfoReader::getDefaultLocale()
193 if (hasTag(META_INFO_LANGUAGE))
195 ::std::wstring locale = m_AllMetaInfo[META_INFO_LANGUAGE].first;
196 return isValidLocale(locale) ? ::std::make_pair(locale.substr( 0,2), locale.substr( 3,2)) : EN_US_LOCALE;
198 else
199 return EN_US_LOCALE;
202 /*********************** tag related functions ***********************/
204 /** choose an appropriate tag reader
207 ITag* CMetaInfoReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
209 if ( tag_name == META_INFO_KEYWORD )
211 m_AllMetaInfo[META_INFO_KEYWORDS].second = XmlAttributes;
212 return m_pKeywords_Builder;
214 if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
215 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
216 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
218 m_AllMetaInfo[tag_name].second = XmlAttributes;
219 return m_pSimple_Builder;
221 else
222 return m_pDummy_Builder;
227 // save the received content into structure.
229 void CMetaInfoReader::saveTagContent( const std::wstring& tag_name )
231 ITag* pTagBuilder;
232 if ( tag_name == META_INFO_KEYWORDS )
233 pTagBuilder = m_pKeywords_Builder;
234 else if ( tag_name == META_INFO_KEYWORD )
236 // added for support for OASIS xml file format.
237 m_AllMetaInfo[META_INFO_KEYWORDS].first =m_pKeywords_Builder->getTagContent( );
238 return;
240 else if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
241 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
242 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
243 pTagBuilder = m_pSimple_Builder;
244 else
245 pTagBuilder = m_pDummy_Builder;
247 m_AllMetaInfo[tag_name].first =pTagBuilder->getTagContent( );
251 /*********************** event handler functions ***********************/
254 // start_element occurs when a tag is start
257 void CMetaInfoReader::start_element(
258 const std::wstring& /*raw_name*/,
259 const std::wstring& local_name,
260 const XmlTagAttributes_t& attributes)
262 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
263 ITag* pTagBuilder = chooseTagReader( local_name,attributes );
264 assert( pTagBuilder!= NULL );
265 pTagBuilder->startTag( );
266 m_TagBuilderStack.push( pTagBuilder );
271 // end_element occurs when a tag is closed
274 void CMetaInfoReader::end_element(const std::wstring& /*raw_name*/, const std::wstring& local_name)
276 assert( !m_TagBuilderStack.empty() );
277 ITag* pTagBuilder = m_TagBuilderStack.top();
278 m_TagBuilderStack.pop();
279 pTagBuilder->endTag();
281 saveTagContent( local_name );
286 // characters occurs when receiving characters
289 void CMetaInfoReader::characters( const std::wstring& character )
291 if ( character.length() > 0 && !HasOnlySpaces( character ) )
293 ITag* pTagBuilder = m_TagBuilderStack.top();
294 pTagBuilder->addCharacters( character );
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */