Line data Source code
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 "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>
26 :
27 : #include <tools/debug.hxx>
28 : #include <osl/diagnose.h>
29 :
30 : #include <unordered_map>
31 :
32 : namespace xmloff { namespace metadata
33 : {
34 :
35 : using namespace ::xmloff::token;
36 :
37 : #define FORM_SINGLE_PROPERTY( id, att ) \
38 : PropertyDescription( PROPERTY_##id, XML_NAMESPACE_FORM, att, &FormHandlerFactory::getFormPropertyHandler, PID_##id, NO_GROUP )
39 :
40 : //= property meta data
41 : namespace
42 : {
43 13 : const PropertyDescription* lcl_getPropertyMetaData()
44 : {
45 : static const PropertyDescription s_propertyMetaData[] =
46 : {
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 ),
55 :
56 : PropertyDescription()
57 23 : };
58 13 : return s_propertyMetaData;
59 : }
60 : }
61 :
62 : namespace
63 : {
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.
70 :
71 : typedef std::unordered_map< OUString, const PropertyDescription*, OUStringHash > DescriptionsByName;
72 :
73 120 : const DescriptionsByName& lcl_getPropertyDescriptions()
74 : {
75 : DBG_TESTSOLARMUTEX();
76 120 : static DescriptionsByName s_propertyDescriptionsByName;
77 120 : if ( s_propertyDescriptionsByName.empty() )
78 : {
79 1 : const PropertyDescription* desc = lcl_getPropertyMetaData();
80 10 : while ( !desc->propertyName.isEmpty() )
81 : {
82 8 : s_propertyDescriptionsByName[ desc->propertyName ] = desc;
83 8 : ++desc;
84 : }
85 : }
86 120 : return s_propertyDescriptionsByName;
87 : }
88 :
89 : typedef ::std::map< PropertyGroup, PropertyDescriptionList > IndexedPropertyGroups;
90 :
91 0 : const IndexedPropertyGroups& lcl_getIndexedPropertyGroups()
92 : {
93 : DBG_TESTSOLARMUTEX();
94 0 : static IndexedPropertyGroups s_indexedPropertyGroups;
95 0 : if ( s_indexedPropertyGroups.empty() )
96 : {
97 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
98 0 : while ( !desc->propertyName.isEmpty() )
99 : {
100 0 : if ( desc->propertyGroup != NO_GROUP )
101 0 : s_indexedPropertyGroups[ desc->propertyGroup ].push_back( desc );
102 0 : ++desc;
103 : }
104 : }
105 0 : return s_indexedPropertyGroups;
106 : }
107 :
108 : typedef std::unordered_map< OUString, XMLTokenEnum, OUStringHash > ReverseTokenLookup;
109 :
110 261 : const ReverseTokenLookup& getReverseTokenLookup()
111 : {
112 : DBG_TESTSOLARMUTEX();
113 261 : static ReverseTokenLookup s_reverseTokenLookup;
114 261 : if ( s_reverseTokenLookup.empty() )
115 : {
116 10 : const PropertyDescription* desc = lcl_getPropertyMetaData();
117 100 : while ( !desc->propertyName.isEmpty() )
118 : {
119 80 : s_reverseTokenLookup[ token::GetXMLToken( desc->attribute.attributeToken ) ] = desc->attribute.attributeToken;
120 80 : ++desc;
121 : }
122 : }
123 261 : return s_reverseTokenLookup;
124 : }
125 :
126 : struct AttributeHash : public ::std::unary_function< AttributeDescription, size_t >
127 : {
128 10 : size_t operator()( const AttributeDescription& i_attribute ) const
129 : {
130 10 : return size_t( i_attribute.attributeToken * 100 ) + size_t( i_attribute.namespacePrefix );
131 : }
132 : };
133 :
134 : typedef std::unordered_multimap< AttributeDescription, PropertyGroup, AttributeHash > AttributeGroups;
135 :
136 1 : const AttributeGroups& lcl_getAttributeGroups()
137 : {
138 : DBG_TESTSOLARMUTEX();
139 1 : static AttributeGroups s_attributeGroups;
140 1 : if ( s_attributeGroups.empty() )
141 : {
142 1 : const PropertyDescription* desc = lcl_getPropertyMetaData();
143 10 : while ( !desc->propertyName.isEmpty() )
144 : {
145 8 : if ( desc->propertyGroup != NO_GROUP )
146 0 : s_attributeGroups.insert( AttributeGroups::value_type( desc->attribute, desc->propertyGroup ) );
147 8 : ++desc;
148 : }
149 : }
150 1 : return s_attributeGroups;
151 : }
152 :
153 : typedef std::unordered_map< AttributeDescription, PropertyGroups, AttributeHash > AttributesWithoutGroup;
154 :
155 1 : const AttributesWithoutGroup& lcl_getAttributesWithoutGroups()
156 : {
157 : DBG_TESTSOLARMUTEX();
158 1 : static AttributesWithoutGroup s_attributesWithoutGroup;
159 1 : if ( s_attributesWithoutGroup.empty() )
160 : {
161 1 : const PropertyDescription* desc = lcl_getPropertyMetaData();
162 10 : while ( !desc->propertyName.isEmpty() )
163 : {
164 8 : if ( desc->propertyGroup == NO_GROUP )
165 : {
166 8 : PropertyDescriptionList singleElementList;
167 8 : singleElementList.push_back( desc );
168 :
169 8 : s_attributesWithoutGroup[ desc->attribute ].push_back( singleElementList );
170 : }
171 8 : ++desc;
172 : }
173 : }
174 1 : return s_attributesWithoutGroup;
175 : }
176 : }
177 :
178 120 : const PropertyDescription* getPropertyDescription( const OUString& i_propertyName )
179 : {
180 120 : const DescriptionsByName& rAllDescriptions( lcl_getPropertyDescriptions() );
181 120 : DescriptionsByName::const_iterator pos = rAllDescriptions.find( i_propertyName );
182 120 : if ( pos != rAllDescriptions.end() )
183 0 : return pos->second;
184 120 : return NULL;
185 : }
186 :
187 0 : void getPropertyGroup( const PropertyGroup i_propertyGroup, PropertyDescriptionList& o_propertyDescriptions )
188 : {
189 : OSL_ENSURE( i_propertyGroup != NO_GROUP, "xmloff::metadata::getPropertyGroup: illegal group!" );
190 :
191 0 : const IndexedPropertyGroups& rPropertyGroups( lcl_getIndexedPropertyGroups() );
192 0 : const IndexedPropertyGroups::const_iterator pos = rPropertyGroups.find( i_propertyGroup );
193 0 : if ( pos != rPropertyGroups.end() )
194 0 : o_propertyDescriptions = pos->second;
195 0 : }
196 :
197 1 : void getPropertyGroupList( const AttributeDescription& i_attribute, PropertyGroups& o_propertyGroups )
198 : {
199 1 : const AttributeGroups& rAttributeGroups = lcl_getAttributeGroups();
200 :
201 : ::std::pair< AttributeGroups::const_iterator, AttributeGroups::const_iterator >
202 1 : range = rAttributeGroups.equal_range( i_attribute );
203 :
204 1 : if ( range.first == range.second )
205 : {
206 : // the attribute is not used for any non-trivial group, which means it is mapped directly to
207 : // a single property
208 1 : const AttributesWithoutGroup& attributesWithoutGroups( lcl_getAttributesWithoutGroups() );
209 1 : const AttributesWithoutGroup::const_iterator pos = attributesWithoutGroups.find( i_attribute );
210 1 : if ( pos != attributesWithoutGroups.end() )
211 1 : o_propertyGroups = pos->second;
212 : }
213 : else
214 : {
215 0 : const IndexedPropertyGroups& rPropertyGroups = lcl_getIndexedPropertyGroups();
216 0 : for ( AttributeGroups::const_iterator group = range.first; group != range.second; ++group )
217 : {
218 0 : const PropertyGroup propGroup = group->second;
219 0 : const IndexedPropertyGroups::const_iterator groupPos = rPropertyGroups.find( propGroup );
220 0 : if( groupPos == rPropertyGroups.end() )
221 : {
222 : SAL_WARN( "xmloff.forms", "getPropertyGroupList: inconsistency!" );
223 0 : continue;
224 : }
225 :
226 0 : o_propertyGroups.push_back( groupPos->second );
227 : }
228 : }
229 1 : }
230 :
231 261 : AttributeDescription getAttributeDescription( const sal_uInt16 i_namespacePrefix, const OUString& i_attributeName )
232 : {
233 261 : AttributeDescription attribute;
234 261 : const ReverseTokenLookup& rTokenLookup( getReverseTokenLookup() );
235 261 : const ReverseTokenLookup::const_iterator pos = rTokenLookup.find( i_attributeName );
236 261 : if ( pos != rTokenLookup.end() )
237 : {
238 1 : attribute.namespacePrefix = i_namespacePrefix;
239 1 : attribute.attributeToken = pos->second;
240 : }
241 261 : return attribute;
242 : }
243 :
244 : } } // namespace xmloff::metadata
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|