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