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 :
29 : #include <boost/unordered_map.hpp>
30 :
31 : namespace xmloff { namespace metadata
32 : {
33 :
34 : using namespace ::xmloff::token;
35 :
36 : #define FORM_SINGLE_PROPERTY( id, att ) \
37 : PropertyDescription( PROPERTY_##id, XML_NAMESPACE_FORM, att, &FormHandlerFactory::getFormPropertyHandler, PID_##id, NO_GROUP )
38 :
39 : //= property meta data
40 : namespace
41 : {
42 0 : const PropertyDescription* lcl_getPropertyMetaData()
43 : {
44 : static const PropertyDescription s_propertyMetaData[] =
45 : {
46 : FORM_SINGLE_PROPERTY( DATE_MIN, XML_MIN_VALUE ),
47 : FORM_SINGLE_PROPERTY( DATE_MAX, XML_MAX_VALUE ),
48 : FORM_SINGLE_PROPERTY( DEFAULT_DATE, XML_VALUE ),
49 : FORM_SINGLE_PROPERTY( DATE, XML_CURRENT_VALUE ),
50 : FORM_SINGLE_PROPERTY( TIME_MIN, XML_MIN_VALUE ),
51 : FORM_SINGLE_PROPERTY( TIME_MAX, XML_MAX_VALUE ),
52 : FORM_SINGLE_PROPERTY( DEFAULT_TIME, XML_VALUE ),
53 : FORM_SINGLE_PROPERTY( TIME, XML_CURRENT_VALUE ),
54 :
55 : PropertyDescription()
56 0 : };
57 0 : return s_propertyMetaData;
58 : }
59 : }
60 :
61 : namespace
62 : {
63 : // TODO: instead of having all of the below static, it should be some per-instance data. This way, the
64 : // approach used here would scale much better.
65 : // That is, if you have multiple "meta data instances", which manage a small, but closed set of properties,
66 : // then looking looking through those multiple instances would probably be faster than searching within
67 : // one big instance, since in this case, every instance can quickly decide whether it is responsible
68 : // for some attribute or property, and otherwise delegate to the next instance.
69 :
70 : typedef ::boost::unordered_map< OUString, const PropertyDescription*, OUStringHash > DescriptionsByName;
71 :
72 0 : const DescriptionsByName& lcl_getPropertyDescriptions()
73 : {
74 : DBG_TESTSOLARMUTEX();
75 0 : static DescriptionsByName s_propertyDescriptionsByName;
76 0 : if ( s_propertyDescriptionsByName.empty() )
77 : {
78 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
79 0 : while ( !desc->propertyName.isEmpty() )
80 : {
81 0 : s_propertyDescriptionsByName[ desc->propertyName ] = desc;
82 0 : ++desc;
83 : }
84 : }
85 0 : return s_propertyDescriptionsByName;
86 : }
87 :
88 : typedef ::std::map< PropertyGroup, PropertyDescriptionList > IndexedPropertyGroups;
89 :
90 0 : const IndexedPropertyGroups& lcl_getIndexedPropertyGroups()
91 : {
92 : DBG_TESTSOLARMUTEX();
93 0 : static IndexedPropertyGroups s_indexedPropertyGroups;
94 0 : if ( s_indexedPropertyGroups.empty() )
95 : {
96 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
97 0 : while ( !desc->propertyName.isEmpty() )
98 : {
99 0 : if ( desc->propertyGroup != NO_GROUP )
100 0 : s_indexedPropertyGroups[ desc->propertyGroup ].push_back( desc );
101 0 : ++desc;
102 : }
103 : }
104 0 : return s_indexedPropertyGroups;
105 : }
106 :
107 : typedef ::boost::unordered_map< OUString, XMLTokenEnum, OUStringHash > ReverseTokenLookup;
108 :
109 0 : const ReverseTokenLookup& getReverseTokenLookup()
110 : {
111 : DBG_TESTSOLARMUTEX();
112 0 : static ReverseTokenLookup s_reverseTokenLookup;
113 0 : if ( s_reverseTokenLookup.empty() )
114 : {
115 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
116 0 : while ( !desc->propertyName.isEmpty() )
117 : {
118 0 : s_reverseTokenLookup[ token::GetXMLToken( desc->attribute.attributeToken ) ] = desc->attribute.attributeToken;
119 0 : ++desc;
120 : }
121 : }
122 0 : return s_reverseTokenLookup;
123 : }
124 :
125 : struct AttributeHash : public ::std::unary_function< AttributeDescription, size_t >
126 : {
127 0 : size_t operator()( const AttributeDescription& i_attribute ) const
128 : {
129 0 : return size_t( i_attribute.attributeToken * 100 ) + size_t( i_attribute.namespacePrefix );
130 : }
131 : };
132 :
133 : typedef ::boost::unordered_multimap< AttributeDescription, PropertyGroup, AttributeHash > AttributeGroups;
134 :
135 0 : const AttributeGroups& lcl_getAttributeGroups()
136 : {
137 : DBG_TESTSOLARMUTEX();
138 0 : static AttributeGroups s_attributeGroups;
139 0 : if ( s_attributeGroups.empty() )
140 : {
141 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
142 0 : while ( !desc->propertyName.isEmpty() )
143 : {
144 0 : if ( desc->propertyGroup != NO_GROUP )
145 0 : s_attributeGroups.insert( AttributeGroups::value_type( desc->attribute, desc->propertyGroup ) );
146 0 : ++desc;
147 : }
148 : }
149 0 : return s_attributeGroups;
150 : }
151 :
152 : typedef ::boost::unordered_map< AttributeDescription, PropertyGroups, AttributeHash > AttributesWithoutGroup;
153 :
154 0 : const AttributesWithoutGroup& lcl_getAttributesWithoutGroups()
155 : {
156 : DBG_TESTSOLARMUTEX();
157 0 : static AttributesWithoutGroup s_attributesWithoutGroup;
158 0 : if ( s_attributesWithoutGroup.empty() )
159 : {
160 0 : const PropertyDescription* desc = lcl_getPropertyMetaData();
161 0 : while ( !desc->propertyName.isEmpty() )
162 : {
163 0 : if ( desc->propertyGroup == NO_GROUP )
164 : {
165 0 : PropertyDescriptionList singleElementList;
166 0 : singleElementList.push_back( desc );
167 :
168 0 : s_attributesWithoutGroup[ desc->attribute ].push_back( singleElementList );
169 : }
170 0 : ++desc;
171 : }
172 : }
173 0 : return s_attributesWithoutGroup;
174 : }
175 : }
176 :
177 0 : const PropertyDescription* getPropertyDescription( const OUString& i_propertyName )
178 : {
179 0 : const DescriptionsByName& rAllDescriptions( lcl_getPropertyDescriptions() );
180 0 : DescriptionsByName::const_iterator pos = rAllDescriptions.find( i_propertyName );
181 0 : if ( pos != rAllDescriptions.end() )
182 0 : return pos->second;
183 0 : return NULL;
184 : }
185 :
186 0 : void getPropertyGroup( const PropertyGroup i_propertyGroup, PropertyDescriptionList& o_propertyDescriptions )
187 : {
188 : OSL_ENSURE( i_propertyGroup != NO_GROUP, "xmloff::metadata::getPropertyGroup: illegal group!" );
189 :
190 0 : const IndexedPropertyGroups& rPropertyGroups( lcl_getIndexedPropertyGroups() );
191 0 : const IndexedPropertyGroups::const_iterator pos = rPropertyGroups.find( i_propertyGroup );
192 0 : if ( pos != rPropertyGroups.end() )
193 0 : o_propertyDescriptions = pos->second;
194 0 : }
195 :
196 0 : void getPropertyGroupList( const AttributeDescription& i_attribute, PropertyGroups& o_propertyGroups )
197 : {
198 0 : const AttributeGroups& rAttributeGroups = lcl_getAttributeGroups();
199 :
200 : ::std::pair< AttributeGroups::const_iterator, AttributeGroups::const_iterator >
201 0 : range = rAttributeGroups.equal_range( i_attribute );
202 :
203 0 : if ( range.first == range.second )
204 : {
205 : // the attribute is not used for any non-trivial group, which means it is mapped directly to
206 : // a single property
207 0 : const AttributesWithoutGroup& attributesWithoutGroups( lcl_getAttributesWithoutGroups() );
208 0 : const AttributesWithoutGroup::const_iterator pos = attributesWithoutGroups.find( i_attribute );
209 0 : if ( pos != attributesWithoutGroups.end() )
210 0 : o_propertyGroups = pos->second;
211 : }
212 : else
213 : {
214 0 : const IndexedPropertyGroups& rPropertyGroups = lcl_getIndexedPropertyGroups();
215 0 : for ( AttributeGroups::const_iterator group = range.first; group != range.second; ++group )
216 : {
217 0 : const PropertyGroup propGroup = group->second;
218 0 : const IndexedPropertyGroups::const_iterator groupPos = rPropertyGroups.find( propGroup );
219 0 : if( groupPos == rPropertyGroups.end() )
220 : {
221 : SAL_WARN( "xmloff.forms", "getPropertyGroupList: inconsistency!" );
222 0 : continue;
223 : }
224 :
225 0 : o_propertyGroups.push_back( groupPos->second );
226 : }
227 : }
228 0 : }
229 :
230 0 : AttributeDescription getAttributeDescription( const sal_uInt16 i_namespacePrefix, const OUString& i_attributeName )
231 : {
232 0 : AttributeDescription attribute;
233 0 : const ReverseTokenLookup& rTokenLookup( getReverseTokenLookup() );
234 0 : const ReverseTokenLookup::const_iterator pos = rTokenLookup.find( i_attributeName );
235 0 : if ( pos != rTokenLookup.end() )
236 : {
237 0 : attribute.namespacePrefix = i_namespacePrefix;
238 0 : attribute.attributeToken = pos->second;
239 : }
240 0 : return attribute;
241 : }
242 :
243 : } } // namespace xmloff::metadata
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|