1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <metainforeader.hxx>
21 #include "dummytag.hxx"
22 #include "simpletag.hxx"
23 #include "keywordstag.hxx"
29 CMetaInfoReader::CMetaInfoReader( const Filepath_t
& DocumentName
):
30 CBaseReader( DocumentName
)
34 m_pKeywords_Builder
= new CKeywordsTag
;
35 m_pSimple_Builder
= new CSimpleTag( );
36 m_pDummy_Builder
= new CDummyTag
;
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
;
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
;
54 Initialize( META_CONTENT_NAME
);
56 catch(xml_parser_exception
&)
58 // OSL_ENSURE(false, ex.what());
62 // OSL_ENSURE(false, "Unknown error");
66 CMetaInfoReader::CMetaInfoReader( StreamInterface
* stream
) :
71 m_pKeywords_Builder
= new CKeywordsTag
;
72 m_pSimple_Builder
= new CSimpleTag( );
73 m_pDummy_Builder
= new CDummyTag
;
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
;
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
;
91 Initialize( META_CONTENT_NAME
);
93 catch(xml_parser_exception
&)
95 // OSL_ENSURE(false, ex.what());
99 // OSL_ENSURE(false, "Unknown error");
107 CMetaInfoReader::~CMetaInfoReader()
109 delete m_pKeywords_Builder
;
110 delete m_pSimple_Builder
;
111 delete m_pDummy_Builder
;
115 /*********************** export functions ***********************/
117 /** check if the Tag is in the target meta.xml file.
120 the name of the tag that will be retrieve.
122 bool CMetaInfoReader::hasTag(const std::wstring
& TagName
) const
124 return ( m_AllMetaInfo
.find(TagName
) != m_AllMetaInfo
.end());
127 /** Get a specific tag content, compound tags will be returned as comma separated list.
130 the name of the tag that will be retrieve.
132 std::wstring
CMetaInfoReader::getTagData( const std::wstring
& TagName
)
134 if( hasTag( TagName
) )
135 return m_AllMetaInfo
[TagName
].first
;
140 /** check if the a tag has the specific attribute.
145 the name of the attribute.
147 bool CMetaInfoReader::hasTagAttribute(const std::wstring
& TagName
, const std::wstring
& AttributeName
)
149 return ( m_AllMetaInfo
[TagName
].second
.find( AttributeName
) != m_AllMetaInfo
[TagName
].second
.end() );
152 /** Get a specific attribute content.
157 the name of the attribute.
159 std::wstring
CMetaInfoReader::getTagAttribute(const std::wstring
& TagName
, const std::wstring
& AttributeName
)
161 if (hasTagAttribute(TagName
, AttributeName
))
162 return m_AllMetaInfo
[ TagName
].second
[AttributeName
];
167 /** helper function for getDefaultLocale().
169 const LocaleSet_t
EN_US_LOCALE( ::std::make_pair( ::std::wstring( L
"en" ), ::std::wstring( L
"US" )));
171 static bool isValidLocale(const ::std::wstring
& Locale
)
173 return ( Locale
.length() == 5 );
176 /** Get the default language of the whole document, if no such field, en-US is returned.
178 LocaleSet_t
CMetaInfoReader::getDefaultLocale()
180 if (hasTag(META_INFO_LANGUAGE
))
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
;
189 /*********************** tag related functions ***********************/
191 /** choose an appropriate tag reader
194 ITag
* CMetaInfoReader::chooseTagReader( const std::wstring
& tag_name
, const XmlTagAttributes_t
& XmlAttributes
)
196 if ( tag_name
== META_INFO_KEYWORD
)
198 m_AllMetaInfo
[META_INFO_KEYWORDS
].second
= XmlAttributes
;
199 return m_pKeywords_Builder
;
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
) )
205 m_AllMetaInfo
[tag_name
].second
= XmlAttributes
;
206 return m_pSimple_Builder
;
209 return m_pDummy_Builder
;
214 // save the received content into structure.
216 void CMetaInfoReader::saveTagContent( const std::wstring
& tag_name
)
219 if ( tag_name
== META_INFO_KEYWORDS
)
220 pTagBuilder
= m_pKeywords_Builder
;
221 else if ( tag_name
== META_INFO_KEYWORD
)
223 // added for support for OASIS xml file format.
224 m_AllMetaInfo
[META_INFO_KEYWORDS
].first
=m_pKeywords_Builder
->getTagContent( );
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
;
232 pTagBuilder
= m_pDummy_Builder
;
234 m_AllMetaInfo
[tag_name
].first
=pTagBuilder
->getTagContent( );
238 /*********************** event handler functions ***********************/
241 // start_element occurs when a tag is start
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
)
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
);
258 // end_element occurs when a tag is closed
261 void CMetaInfoReader::end_element(const string_t
& /*raw_name*/, const string_t
& local_name
)
263 assert( !m_TagBuilderStack
.empty() );
264 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
265 m_TagBuilderStack
.pop();
266 pTagBuilder
->endTag();
268 saveTagContent( local_name
);
273 // characters occurs when receiving characters
276 void CMetaInfoReader::characters( const string_t
& character
)
278 if ( character
.length() > 0 && !HasOnlySpaces( character
) )
280 ITag
* pTagBuilder
= m_TagBuilderStack
.top();
281 pTagBuilder
->addCharacters( character
);
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */