1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 #include "internal/metainforeader.hxx"
27 #include "dummytag.hxx"
28 #include "simpletag.hxx"
29 #include "keywordstag.hxx"
30 
31 #include "assert.h"
32 
33 /** constructor.
34 */
35 CMetaInfoReader::CMetaInfoReader( const std::string& DocumentName ):
36 CBaseReader( DocumentName )
37 {
38     try
39     {
40 		m_pKeywords_Builder = new CKeywordsTag( );
41 		m_pSimple_Builder = new CSimpleTag( );
42 		m_pDummy_Builder   = new CDummyTag( );
43 
44         //retrieve all information that is useful
45         m_AllMetaInfo[META_INFO_AUTHOR]               = EMPTY_XML_TAG;
46         m_AllMetaInfo[META_INFO_TITLE]                = EMPTY_XML_TAG;
47         m_AllMetaInfo[META_INFO_SUBJECT]              = EMPTY_XML_TAG;
48         m_AllMetaInfo[META_INFO_KEYWORDS]             = EMPTY_XML_TAG;
49         m_AllMetaInfo[META_INFO_DESCRIPTION]          = EMPTY_XML_TAG;
50         m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC]   = EMPTY_XML_TAG;
51 
52         m_AllMetaInfo[META_INFO_GENERATOR]            = EMPTY_XML_TAG;
53         m_AllMetaInfo[META_INFO_CREATION]             = EMPTY_XML_TAG;
54         m_AllMetaInfo[META_INFO_CREATOR]              = EMPTY_XML_TAG;
55         m_AllMetaInfo[META_INFO_MODIFIED]             = EMPTY_XML_TAG;
56         m_AllMetaInfo[META_INFO_LANGUAGE]             = EMPTY_XML_TAG;
57         m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER]      = EMPTY_XML_TAG;
58         m_AllMetaInfo[META_INFO_EDITING_TIME]         = EMPTY_XML_TAG;
59 
60 		Initialize( META_CONTENT_NAME );
61     }
62     catch(xml_parser_exception&
63     #if OSL_DEBUG_LEVEL > 0
64     ex
65     #endif
66     )
67     {
68         ENSURE(false, ex.what());
69     }
70     catch(...)
71     {
72         ENSURE(false, "Unknown error");
73     }
74 }
75 
76 CMetaInfoReader::CMetaInfoReader( void* stream, zlib_filefunc_def* fa) :
77 CBaseReader( stream, fa)
78 {
79 try
80     {
81 		m_pKeywords_Builder = new CKeywordsTag( );
82 		m_pSimple_Builder = new CSimpleTag( );
83 		m_pDummy_Builder   = new CDummyTag( );
84 
85         //retrieve all information that is useful
86         m_AllMetaInfo[META_INFO_AUTHOR]               = EMPTY_XML_TAG;
87         m_AllMetaInfo[META_INFO_TITLE]                = EMPTY_XML_TAG;
88         m_AllMetaInfo[META_INFO_SUBJECT]              = EMPTY_XML_TAG;
89         m_AllMetaInfo[META_INFO_KEYWORDS]             = EMPTY_XML_TAG;
90         m_AllMetaInfo[META_INFO_DESCRIPTION]          = EMPTY_XML_TAG;
91         m_AllMetaInfo[META_INFO_DOCUMENT_STATISTIC]   = EMPTY_XML_TAG;
92 
93         m_AllMetaInfo[META_INFO_GENERATOR]            = EMPTY_XML_TAG;
94         m_AllMetaInfo[META_INFO_CREATION]             = EMPTY_XML_TAG;
95         m_AllMetaInfo[META_INFO_CREATOR]              = EMPTY_XML_TAG;
96         m_AllMetaInfo[META_INFO_MODIFIED]             = EMPTY_XML_TAG;
97         m_AllMetaInfo[META_INFO_LANGUAGE]             = EMPTY_XML_TAG;
98         m_AllMetaInfo[META_INFO_DOCUMENT_NUMBER]      = EMPTY_XML_TAG;
99         m_AllMetaInfo[META_INFO_EDITING_TIME]         = EMPTY_XML_TAG;
100 
101 		Initialize( META_CONTENT_NAME );
102     }
103     catch(xml_parser_exception&
104     #if OSL_DEBUG_LEVEL > 0
105     ex
106     #endif
107     )
108     {
109         ENSURE(false, ex.what());
110     }
111     catch(...)
112     {
113         ENSURE(false, "Unknown error");
114     }
115 
116 }
117 
118 /** destructor.
119 */
120 
121 CMetaInfoReader::~CMetaInfoReader( void )
122 {
123     delete m_pKeywords_Builder;
124 	delete m_pSimple_Builder;
125     delete m_pDummy_Builder;
126 }
127 
128 
129 /***********************   export functions  ***********************/
130 
131 /** check if the Tag is in the target meta.xml file.
132 
133     @param TagName
134     the name of the tag that will be retrive.
135 */
136 bool CMetaInfoReader::hasTag( std::wstring TagName ) const
137 {
138     return ( m_AllMetaInfo.find(TagName) != m_AllMetaInfo.end());
139 }
140 
141 /** Get a specific tag content, compound tags will be returned as comma separated list.
142 
143     @param TagName
144     the name of the tag that will be retrive.
145 */
146 std::wstring CMetaInfoReader::getTagData( const std::wstring& TagName)
147 {
148     if( hasTag( TagName ) )
149         return m_AllMetaInfo[TagName].first;
150     else
151         return EMPTY_STRING;
152 }
153 
154 /** check if the a tag has the specific attribute.
155 
156     @param TagName
157     the name of the tag.
158     @param AttributeName
159     the name of the attribute.
160 */
161 bool CMetaInfoReader::hasTagAttribute( const std::wstring TagName,  std::wstring AttributeName)
162 {
163     return ( m_AllMetaInfo[TagName].second.find( AttributeName) != m_AllMetaInfo[TagName].second.end() );
164 }
165 
166 /** Get a specific attribute content.
167 
168     @param TagName
169     the name of the tag.
170     @param AttributeName
171     the name of the attribute.
172 */
173 std::wstring CMetaInfoReader::getTagAttribute( const std::wstring TagName,  std::wstring AttributeName)
174 {
175     if ( hasTagAttribute( TagName, AttributeName ) )
176         return  m_AllMetaInfo[ TagName ].second[AttributeName];
177     else
178         return EMPTY_STRING;
179 }
180 
181 /** helper function for getDefaultLocale().
182 */
183 const LocaleSet_t EN_US_LOCALE( ::std::make_pair( ::std::wstring( L"en" ),  ::std::wstring( L"US" )));
184 
185 bool isValidLocale ( ::std::wstring Locale )
186 {
187 	return ( Locale.length() == 5 );
188 }
189 
190 /** Get the default language of the whole document, if no such field, en-US is returned.
191 */
192 LocaleSet_t CMetaInfoReader::getDefaultLocale()
193 {
194     if (hasTag(META_INFO_LANGUAGE))
195     {
196 		::std::wstring locale = m_AllMetaInfo[META_INFO_LANGUAGE].first;
197 		return isValidLocale(locale) ? ::std::make_pair(locale.substr( 0,2), locale.substr( 3,2)) : EN_US_LOCALE;
198     }
199     else
200         return EN_US_LOCALE;
201 }
202 
203 /***********************   tag related functions  ***********************/
204 
205 /** choose an appropriate tag reader
206 */
207 
208 ITag* CMetaInfoReader::chooseTagReader( const std::wstring& tag_name, const XmlTagAttributes_t& XmlAttributes )
209 {
210 	if ( tag_name == META_INFO_KEYWORD )
211 	{
212 		m_AllMetaInfo[META_INFO_KEYWORDS].second = XmlAttributes;
213 		return m_pKeywords_Builder;
214 	}
215 	if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
216 		( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
217 		( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
218 	{
219 		m_AllMetaInfo[tag_name].second = XmlAttributes;
220 		return m_pSimple_Builder;
221 	}
222 	else
223         return m_pDummy_Builder;
224 
225 }
226 
227 //------------------------------
228 // save the received content into structure.
229 //------------------------------
230 void CMetaInfoReader::saveTagContent( const std::wstring& tag_name )
231 {
232 	ITag* pTagBuilder;
233 	if ( tag_name == META_INFO_KEYWORDS )
234         pTagBuilder = m_pKeywords_Builder;
235 	else if ( tag_name == META_INFO_KEYWORD )
236 	{
237 		// added for support for OASIS xml file format.
238 		m_AllMetaInfo[META_INFO_KEYWORDS].first =m_pKeywords_Builder->getTagContent( );
239 		return;
240 	}
241 	else if (( tag_name == META_INFO_TITLE )||( tag_name == META_INFO_AUTHOR )||( tag_name == META_INFO_SUBJECT )||( tag_name == META_INFO_DESCRIPTION )||
242 		( tag_name == META_INFO_DOCUMENT_STATISTIC )||( tag_name == META_INFO_GENERATOR )||( tag_name == META_INFO_CREATION )||( tag_name == META_INFO_CREATOR )||
243 		( tag_name == META_INFO_MODIFIED )||( tag_name == META_INFO_LANGUAGE )||( tag_name == META_INFO_DOCUMENT_NUMBER )||( tag_name == META_INFO_EDITING_TIME ) )
244         pTagBuilder = m_pSimple_Builder;
245 	else
246         pTagBuilder = m_pDummy_Builder;
247 
248 	m_AllMetaInfo[tag_name].first =pTagBuilder->getTagContent( );
249 }
250 
251 
252 /***********************   event handler functions  ***********************/
253 
254 //------------------------------
255 // start_element occurs when a tag is start
256 //------------------------------
257 
258 void CMetaInfoReader::start_element(
259 	const std::wstring& /*raw_name*/,
260 	const std::wstring& local_name,
261 	const XmlTagAttributes_t& attributes)
262 {
263 	//get appropriate Xml Tag Builder using MetaInfoBuilderFactory;
264 	ITag* pTagBuilder = chooseTagReader( local_name,attributes );
265     assert( pTagBuilder!= NULL );
266 	pTagBuilder->startTag( );
267 	m_TagBuilderStack.push( pTagBuilder );
268 
269 }
270 
271 //------------------------------
272 // end_element occurs when a tag is closed
273 //------------------------------
274 
275 void CMetaInfoReader::end_element(const std::wstring& /*raw_name*/, const std::wstring& local_name)
276 {
277 	assert( !m_TagBuilderStack.empty() );
278 	ITag* pTagBuilder = m_TagBuilderStack.top();
279 	m_TagBuilderStack.pop();
280 	pTagBuilder->endTag();
281 
282 	saveTagContent( local_name );
283 
284 }
285 
286 //------------------------------
287 // characters occurs when receiving characters
288 //------------------------------
289 
290 void CMetaInfoReader::characters( const std::wstring& character )
291 {
292 	if ( character.length() > 0 &&  !HasOnlySpaces( character ) )
293 	{
294 		ITag* pTagBuilder = m_TagBuilderStack.top();
295 		pTagBuilder->addCharacters( character );
296 	}
297 }
298 
299