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
27 /** constructor.
28 */
CMetaInfoReader(const Filepath_t & DocumentName)29 CMetaInfoReader::CMetaInfoReader( const Filepath_t& DocumentName ):
30 CBaseReader( DocumentName )
31 {
32 try
33 {
34 m_pKeywords_Builder = new CKeywordsTag;
35 m_pSimple_Builder = new CSimpleTag( );
36 m_pDummy_Builder = new CDummyTag;
37
38 //retrieve all information that is useful
39 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
40 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
41 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
42 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
43 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
44 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
45
46 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
47 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
48 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
49 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
50 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
51 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
52 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
53
54 Initialize( META_CONTENT_NAME );
55 }
56 catch(xml_parser_exception&)
57 {
58 // OSL_ENSURE(false, ex.what());
59 }
60 catch(...)
61 {
62 // OSL_ENSURE(false, "Unknown error");
63 }
64 }
65
CMetaInfoReader(StreamInterface * stream)66 CMetaInfoReader::CMetaInfoReader( StreamInterface* stream ) :
67 CBaseReader( stream )
68 {
69 try
70 {
71 m_pKeywords_Builder = new CKeywordsTag;
72 m_pSimple_Builder = new CSimpleTag( );
73 m_pDummy_Builder = new CDummyTag;
74
75 //retrieve all information that is useful
76 m_AllMetaInfo[META_INFO_AUTHOR] = EMPTY_XML_TAG;
77 m_AllMetaInfo[META_INFO_TITLE] = EMPTY_XML_TAG;
78 m_AllMetaInfo[META_INFO_SUBJECT] = EMPTY_XML_TAG;
79 m_AllMetaInfo[META_INFO_KEYWORDS] = EMPTY_XML_TAG;
80 m_AllMetaInfo[META_INFO_DESCRIPTION] = EMPTY_XML_TAG;
81 m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC] = EMPTY_XML_TAG;
82
83 m_AllMetaInfo[META_INFO_GENERATOR] = EMPTY_XML_TAG;
84 m_AllMetaInfo[META_INFO_CREATION] = EMPTY_XML_TAG;
85 m_AllMetaInfo[META_INFO_CREATOR] = EMPTY_XML_TAG;
86 m_AllMetaInfo[META_INFO_MODIFIED] = EMPTY_XML_TAG;
87 m_AllMetaInfo[META_INFO_LANGUAGE] = EMPTY_XML_TAG;
88 m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER] = EMPTY_XML_TAG;
89 m_AllMetaInfo[META_INFO_EDITING_TIME] = EMPTY_XML_TAG;
90
91 Initialize( META_CONTENT_NAME );
92 }
93 catch(xml_parser_exception&)
94 {
95 // OSL_ENSURE(false, ex.what());
96 }
97 catch(...)
98 {
99 // OSL_ENSURE(false, "Unknown error");
100 }
101
102 }
103
104 /** destructor.
105 */
106
~CMetaInfoReader()107 CMetaInfoReader::~CMetaInfoReader()
108 {
109 delete m_pKeywords_Builder;
110 delete m_pSimple_Builder;
111 delete m_pDummy_Builder;
112 }
113
114
115 /*********************** export functions ***********************/
116
117 /** check if the Tag is in the target meta.xml file.
118
119 @param TagName
120 the name of the tag that will be retrieve.
121 */
hasTag(const std::wstring & TagName) const122 bool CMetaInfoReader::hasTag(const std::wstring& TagName) const
123 {
124 return ( m_AllMetaInfo.find(TagName) != m_AllMetaInfo.end());
125 }
126
127 /** Get a specific tag content, compound tags will be returned as comma separated list.
128
129 @param TagName
130 the name of the tag that will be retrieve.
131 */
getTagData(const std::wstring & TagName)132 std::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
140 /** check if the a tag has the specific attribute.
141
142 @param TagName
143 the name of the tag.
144 @param AttributeName
145 the name of the attribute.
146 */
hasTagAttribute(const std::wstring & TagName,const std::wstring & AttributeName)147 bool 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
152 /** Get a specific attribute content.
153
154 @param TagName
155 the name of the tag.
156 @param AttributeName
157 the name of the attribute.
158 */
getTagAttribute(const std::wstring & TagName,const std::wstring & AttributeName)159 std::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
167 /** helper function for getDefaultLocale().
168 */
169 const LocaleSet_t EN_US_LOCALE( ::std::make_pair( ::std::wstring( L"en" ), ::std::wstring( L"US" )));
170
isValidLocale(const::std::wstring & Locale)171 static bool isValidLocale(const ::std::wstring& Locale)
172 {
173 return ( Locale.length() == 5 );
174 }
175
176 /** Get the default language of the whole document, if no such field, en-US is returned.
177 */
getDefaultLocale()178 LocaleSet_t CMetaInfoReader::getDefaultLocale()
179 {
180 if (hasTag(META_INFO_LANGUAGE))
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
191 /** choose an appropriate tag reader
192 */
193
chooseTagReader(const std::wstring & tag_name,const XmlTagAttributes_t & XmlAttributes)194 ITag* 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
saveTagContent(const std::wstring & tag_name)216 void 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.
224 m_AllMetaInfo[META_INFO_KEYWORDS].first =m_pKeywords_Builder->getTagContent( );
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
start_element(const string_t &,const string_t & local_name,const xml_tag_attribute_container_t & attributes)244 void CMetaInfoReader::start_element(
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
end_element(const string_t &,const string_t & local_name)261 void 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
characters(const string_t & character)276 void 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: */
286