LibreOffice Module shell (master) 1
metainforeader.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <metainforeader.hxx>
21#include "dummytag.hxx"
22#include "simpletag.hxx"
23#include "keywordstag.hxx"
24
25#include <assert.h>
26
30CBaseReader( DocumentName )
31{
32 try
33 {
37
38 //retrieve all information that is useful
45
53
55 }
57 {
58 // OSL_ENSURE(false, ex.what());
59 }
60 catch(...)
61 {
62 // OSL_ENSURE(false, "Unknown error");
63 }
64}
65
68{
69try
70 {
74
75 //retrieve all information that is useful
82
90
92 }
94 {
95 // OSL_ENSURE(false, ex.what());
96 }
97 catch(...)
98 {
99 // OSL_ENSURE(false, "Unknown error");
100 }
101
102}
103
108{
109 delete m_pKeywords_Builder;
110 delete m_pSimple_Builder;
111 delete m_pDummy_Builder;
112}
113
114
115/*********************** export functions ***********************/
116
122bool CMetaInfoReader::hasTag(const std::wstring& TagName) const
123{
124 return ( m_AllMetaInfo.find(TagName) != m_AllMetaInfo.end());
125}
126
132std::wstring CMetaInfoReader::getTagData( const std::wstring& TagName)
133{
134 if( hasTag( TagName ) )
135 return m_AllMetaInfo[TagName].first;
136 else
137 return EMPTY_STRING;
138}
139
147bool CMetaInfoReader::hasTagAttribute(const std::wstring& TagName, const std::wstring& AttributeName)
148{
149 return ( m_AllMetaInfo[TagName].second.find( AttributeName) != m_AllMetaInfo[TagName].second.end() );
150}
151
159std::wstring CMetaInfoReader::getTagAttribute(const std::wstring& TagName, const std::wstring& AttributeName)
160{
161 if (hasTagAttribute(TagName, AttributeName))
162 return m_AllMetaInfo[ TagName ].second[AttributeName];
163 else
164 return EMPTY_STRING;
165}
166
169const LocaleSet_t EN_US_LOCALE( ::std::make_pair( ::std::wstring( L"en" ), ::std::wstring( L"US" )));
170
171static bool isValidLocale(const ::std::wstring& Locale)
172{
173 return ( Locale.length() == 5 );
174}
175
179{
181 {
182 ::std::wstring locale = m_AllMetaInfo[META_INFO_LANGUAGE].first;
183 return isValidLocale(locale) ? ::std::make_pair(locale.substr( 0,2), locale.substr( 3,2)) : EN_US_LOCALE;
184 }
185 else
186 return EN_US_LOCALE;
187}
188
189/*********************** tag related functions ***********************/
190
194ITag* CMetaInfoReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
195{
196 if ( tag_name == META_INFO_KEYWORD )
197 {
198 m_AllMetaInfo[META_INFO_KEYWORDS].second = XmlAttributes;
199 return m_pKeywords_Builder;
200 }
201 if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
202 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
203 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
204 {
205 m_AllMetaInfo[tag_name].second = XmlAttributes;
206 return m_pSimple_Builder;
207 }
208 else
209 return m_pDummy_Builder;
210
211}
212
213
214// save the received content into structure.
215
216void CMetaInfoReader::saveTagContent( const std::wstring& tag_name )
217{
218 ITag* pTagBuilder;
219 if ( tag_name == META_INFO_KEYWORDS )
220 pTagBuilder = m_pKeywords_Builder;
221 else if ( tag_name == META_INFO_KEYWORD )
222 {
223 // added for support for OASIS xml file format.
225 return;
226 }
227 else if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
228 ( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
229 ( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
230 pTagBuilder = m_pSimple_Builder;
231 else
232 pTagBuilder = m_pDummy_Builder;
233
234 m_AllMetaInfo[tag_name].first =pTagBuilder->getTagContent( );
235}
236
237
238/*********************** event handler functions ***********************/
239
240
241// start_element occurs when a tag is start
242
243
245 const string_t& /*raw_name*/,
246 const string_t& local_name,
247 const xml_tag_attribute_container_t& attributes)
248{
249 //get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
250 ITag* pTagBuilder = chooseTagReader( local_name,attributes );
251 assert( pTagBuilder!= nullptr );
252 pTagBuilder->startTag( );
253 m_TagBuilderStack.push( pTagBuilder );
254
255}
256
257
258// end_element occurs when a tag is closed
259
260
261void CMetaInfoReader::end_element(const string_t& /*raw_name*/, const string_t& local_name)
262{
263 assert( !m_TagBuilderStack.empty() );
264 ITag* pTagBuilder = m_TagBuilderStack.top();
265 m_TagBuilderStack.pop();
266 pTagBuilder->endTag();
267
268 saveTagContent( local_name );
269
270}
271
272
273// characters occurs when receiving characters
274
275
276void CMetaInfoReader::characters( const string_t& character )
277{
278 if ( character.length() > 0 && !HasOnlySpaces( character ) )
279 {
280 ITag* pTagBuilder = m_TagBuilderStack.top();
281 pTagBuilder->addCharacters( character );
282 }
283}
284
285/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Initialize(const std::string &)
Read interested tag content into respective structure then start parsing process.
Definition: basereader.cxx:59
Implements the ITag interface but does nothing (Null object pattern), may be used for tags we are not...
Definition: dummytag.hxx:34
Implements the ITag interface for building a keyword list.
Definition: keywordstag.hxx:32
virtual std::wstring getTagContent() override
Definition: keywordstag.cxx:38
std::wstring getTagData(const std::wstring &TagName)
Get a specific tag content, compound tags will be returned as comma separated list.
virtual void characters(const string_t &character) override
characters occurs when receiving characters
bool hasTag(const std::wstring &TagName) const
check if the Tag is in the target meta.xml file.
void saveTagContent(const std::wstring &tag_name)
save the received content into structure.
std::wstring getTagAttribute(const std::wstring &TagName, const std::wstring &AttributeName)
Get a specific attribute content.
std::stack< ITag * > m_TagBuilderStack
CMetaInfoReader(const Filepath_t &DocumentName)
constructor.
ITag * chooseTagReader(const std::wstring &tag_name, const XmlTagAttributes_t &XmlAttributes)
choose an appropriate tag reader to handle the tag.
virtual void start_element(const string_t &raw_name, const string_t &local_name, const xml_tag_attribute_container_t &attributes) override
start_element occurs when a tag is start.
CDummyTag * m_pDummy_Builder
XmlTags_t m_AllMetaInfo
virtual void end_element(const string_t &raw_name, const string_t &local_name) override
end_element occurs when a tag is closed
CSimpleTag * m_pSimple_Builder
bool hasTagAttribute(const std::wstring &TagName, const std::wstring &AttributeName)
check if the a tag has the specific attribute.
CKeywordsTag * m_pKeywords_Builder
virtual ~CMetaInfoReader() override
destructor.
LocaleSet_t getDefaultLocale()
Get the default language of the whole document.
Implements the ITag interface for building a general info that is not a compound tag.
Definition: simpletag.hxx:32
Interface for a xml tag character builder.
Definition: itag.hxx:32
virtual void startTag()=0
virtual void addCharacters(const std::wstring &characters)=0
virtual ::std::wstring getTagContent()=0
virtual void endTag()=0
#define META_INFO_AUTHOR
Definition: config.hxx:35
#define META_INFO_KEYWORD
Definition: config.hxx:38
#define META_INFO_MODIFIED
Definition: config.hxx:50
#define META_INFO_LANGUAGE
Definition: config.hxx:54
#define EMPTY_STRING
Definition: config.hxx:31
#define META_INFO_DOCUMENT_NUMBER
Definition: config.hxx:51
#define META_INFO_EDITING_TIME
Definition: config.hxx:52
#define META_INFO_DOCUMENT_STATISTIC
Definition: config.hxx:49
#define META_INFO_CREATOR
Definition: config.hxx:55
#define META_INFO_CREATION
Definition: config.hxx:56
#define META_INFO_SUBJECT
Definition: config.hxx:36
#define META_CONTENT_NAME
Definition: config.hxx:28
#define META_INFO_KEYWORDS
Definition: config.hxx:37
#define META_INFO_TITLE
Definition: config.hxx:34
#define META_INFO_DESCRIPTION
Definition: config.hxx:39
#define META_INFO_GENERATOR
Definition: config.hxx:57
Reference< XOutputStream > stream
std::string Filepath_t
Definition: filepath.hxx:22
std::string string_t
std::map< string_t, string_t > xml_tag_attribute_container_t
static bool isValidLocale(const ::std::wstring &Locale)
const LocaleSet_t EN_US_LOCALE(::std::make_pair(::std::wstring(L"en"), ::std::wstring(L"US")))
helper function for getDefaultLocale().
@ character
const XmlTag_t EMPTY_XML_TAG
Definition: types.hxx:38
::std::pair< Language_t, Country_t > LocaleSet_t
Definition: types.hxx:48
std::map< std::wstring, std::wstring > XmlTagAttributes_t
Definition: types.hxx:34
bool HasOnlySpaces(const std::wstring &String)
helper function to judge if the string is only has spaces.
Definition: utilities.cxx:100