bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / forms / property_meta_data.cxx
blobe5898f38a04bc2631b85ee7a9509be9b06db6bf9
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 #include "property_description.hxx"
21 #include "property_meta_data.hxx"
22 #include "forms/form_handler_factory.hxx"
23 #include "strings.hxx"
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlnmspe.hxx>
27 #include <tools/debug.hxx>
28 #include <osl/diagnose.h>
30 #include <unordered_map>
32 namespace xmloff { namespace metadata
35 using namespace ::xmloff::token;
37 #define FORM_SINGLE_PROPERTY( id, att ) \
38 PropertyDescription( PROPERTY_##id, XML_NAMESPACE_FORM, att, &FormHandlerFactory::getFormPropertyHandler, PID_##id, NO_GROUP )
40 //= property meta data
41 namespace
43 const PropertyDescription* lcl_getPropertyMetaData()
45 static const PropertyDescription s_propertyMetaData[] =
47 FORM_SINGLE_PROPERTY( DATE_MIN, XML_MIN_VALUE ),
48 FORM_SINGLE_PROPERTY( DATE_MAX, XML_MAX_VALUE ),
49 FORM_SINGLE_PROPERTY( DEFAULT_DATE, XML_VALUE ),
50 FORM_SINGLE_PROPERTY( DATE, XML_CURRENT_VALUE ),
51 FORM_SINGLE_PROPERTY( TIME_MIN, XML_MIN_VALUE ),
52 FORM_SINGLE_PROPERTY( TIME_MAX, XML_MAX_VALUE ),
53 FORM_SINGLE_PROPERTY( DEFAULT_TIME, XML_VALUE ),
54 FORM_SINGLE_PROPERTY( TIME, XML_CURRENT_VALUE ),
56 PropertyDescription()
58 return s_propertyMetaData;
62 namespace
64 // TODO: instead of having all of the below static, it should be some per-instance data. This way, the
65 // approach used here would scale much better.
66 // That is, if you have multiple "meta data instances", which manage a small, but closed set of properties,
67 // then looking looking through those multiple instances would probably be faster than searching within
68 // one big instance, since in this case, every instance can quickly decide whether it is responsible
69 // for some attribute or property, and otherwise delegate to the next instance.
71 typedef std::unordered_map< OUString, const PropertyDescription*, OUStringHash > DescriptionsByName;
73 const DescriptionsByName& lcl_getPropertyDescriptions()
75 DBG_TESTSOLARMUTEX();
76 static DescriptionsByName s_propertyDescriptionsByName;
77 if ( s_propertyDescriptionsByName.empty() )
79 const PropertyDescription* desc = lcl_getPropertyMetaData();
80 while ( !desc->propertyName.isEmpty() )
82 s_propertyDescriptionsByName[ desc->propertyName ] = desc;
83 ++desc;
86 return s_propertyDescriptionsByName;
89 typedef ::std::map< PropertyGroup, PropertyDescriptionList > IndexedPropertyGroups;
91 const IndexedPropertyGroups& lcl_getIndexedPropertyGroups()
93 DBG_TESTSOLARMUTEX();
94 static IndexedPropertyGroups s_indexedPropertyGroups;
95 if ( s_indexedPropertyGroups.empty() )
97 const PropertyDescription* desc = lcl_getPropertyMetaData();
98 while ( !desc->propertyName.isEmpty() )
100 if ( desc->propertyGroup != NO_GROUP )
101 s_indexedPropertyGroups[ desc->propertyGroup ].push_back( desc );
102 ++desc;
105 return s_indexedPropertyGroups;
108 typedef std::unordered_map< OUString, XMLTokenEnum, OUStringHash > ReverseTokenLookup;
110 const ReverseTokenLookup& getReverseTokenLookup()
112 DBG_TESTSOLARMUTEX();
113 static ReverseTokenLookup s_reverseTokenLookup;
114 if ( s_reverseTokenLookup.empty() )
116 const PropertyDescription* desc = lcl_getPropertyMetaData();
117 while ( !desc->propertyName.isEmpty() )
119 s_reverseTokenLookup[ token::GetXMLToken( desc->attribute.attributeToken ) ] = desc->attribute.attributeToken;
120 ++desc;
123 return s_reverseTokenLookup;
126 struct AttributeHash : public ::std::unary_function< AttributeDescription, size_t >
128 size_t operator()( const AttributeDescription& i_attribute ) const
130 return size_t( i_attribute.attributeToken * 100 ) + size_t( i_attribute.namespacePrefix );
134 typedef std::unordered_multimap< AttributeDescription, PropertyGroup, AttributeHash > AttributeGroups;
136 const AttributeGroups& lcl_getAttributeGroups()
138 DBG_TESTSOLARMUTEX();
139 static AttributeGroups s_attributeGroups;
140 if ( s_attributeGroups.empty() )
142 const PropertyDescription* desc = lcl_getPropertyMetaData();
143 while ( !desc->propertyName.isEmpty() )
145 if ( desc->propertyGroup != NO_GROUP )
146 s_attributeGroups.insert( AttributeGroups::value_type( desc->attribute, desc->propertyGroup ) );
147 ++desc;
150 return s_attributeGroups;
153 typedef std::unordered_map< AttributeDescription, PropertyGroups, AttributeHash > AttributesWithoutGroup;
155 const AttributesWithoutGroup& lcl_getAttributesWithoutGroups()
157 DBG_TESTSOLARMUTEX();
158 static AttributesWithoutGroup s_attributesWithoutGroup;
159 if ( s_attributesWithoutGroup.empty() )
161 const PropertyDescription* desc = lcl_getPropertyMetaData();
162 while ( !desc->propertyName.isEmpty() )
164 if ( desc->propertyGroup == NO_GROUP )
166 PropertyDescriptionList singleElementList;
167 singleElementList.push_back( desc );
169 s_attributesWithoutGroup[ desc->attribute ].push_back( singleElementList );
171 ++desc;
174 return s_attributesWithoutGroup;
178 const PropertyDescription* getPropertyDescription( const OUString& i_propertyName )
180 const DescriptionsByName& rAllDescriptions( lcl_getPropertyDescriptions() );
181 DescriptionsByName::const_iterator pos = rAllDescriptions.find( i_propertyName );
182 if ( pos != rAllDescriptions.end() )
183 return pos->second;
184 return NULL;
187 void getPropertyGroup( const PropertyGroup i_propertyGroup, PropertyDescriptionList& o_propertyDescriptions )
189 OSL_ENSURE( i_propertyGroup != NO_GROUP, "xmloff::metadata::getPropertyGroup: illegal group!" );
191 const IndexedPropertyGroups& rPropertyGroups( lcl_getIndexedPropertyGroups() );
192 const IndexedPropertyGroups::const_iterator pos = rPropertyGroups.find( i_propertyGroup );
193 if ( pos != rPropertyGroups.end() )
194 o_propertyDescriptions = pos->second;
197 void getPropertyGroupList( const AttributeDescription& i_attribute, PropertyGroups& o_propertyGroups )
199 const AttributeGroups& rAttributeGroups = lcl_getAttributeGroups();
201 ::std::pair< AttributeGroups::const_iterator, AttributeGroups::const_iterator >
202 range = rAttributeGroups.equal_range( i_attribute );
204 if ( range.first == range.second )
206 // the attribute is not used for any non-trivial group, which means it is mapped directly to
207 // a single property
208 const AttributesWithoutGroup& attributesWithoutGroups( lcl_getAttributesWithoutGroups() );
209 const AttributesWithoutGroup::const_iterator pos = attributesWithoutGroups.find( i_attribute );
210 if ( pos != attributesWithoutGroups.end() )
211 o_propertyGroups = pos->second;
213 else
215 const IndexedPropertyGroups& rPropertyGroups = lcl_getIndexedPropertyGroups();
216 for ( AttributeGroups::const_iterator group = range.first; group != range.second; ++group )
218 const PropertyGroup propGroup = group->second;
219 const IndexedPropertyGroups::const_iterator groupPos = rPropertyGroups.find( propGroup );
220 if( groupPos == rPropertyGroups.end() )
222 SAL_WARN( "xmloff.forms", "getPropertyGroupList: inconsistency!" );
223 continue;
226 o_propertyGroups.push_back( groupPos->second );
231 AttributeDescription getAttributeDescription( const sal_uInt16 i_namespacePrefix, const OUString& i_attributeName )
233 AttributeDescription attribute;
234 const ReverseTokenLookup& rTokenLookup( getReverseTokenLookup() );
235 const ReverseTokenLookup::const_iterator pos = rTokenLookup.find( i_attributeName );
236 if ( pos != rTokenLookup.end() )
238 attribute.namespacePrefix = i_namespacePrefix;
239 attribute.attributeToken = pos->second;
241 return attribute;
244 } } // namespace xmloff::metadata
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */