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