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 #ifndef _WEBDAV_UCP_CONTENTPROPERTIES_HXX
25 #define _WEBDAV_UCP_CONTENTPROPERTIES_HXX
26 
27 #include <memory>
28 #include <vector>
29 #include <hash_map>
30 #include <rtl/ustring.hxx>
31 #include <com/sun/star/uno/Any.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 
34 namespace com { namespace sun { namespace star { namespace beans {
35     struct Property;
36 } } } }
37 
38 namespace http_dav_ucp
39 {
40 
41 struct DAVResource;
42 
43 //=========================================================================
44 
45 struct equalString
46 {
operator ()http_dav_ucp::equalString47   bool operator()( const rtl::OUString& s1, const rtl::OUString& s2 ) const
48   {
49       return !!( s1 == s2 );
50   }
51 };
52 
53 struct hashString
54 {
operator ()http_dav_ucp::hashString55     size_t operator()( const rtl::OUString & rName ) const
56     {
57         return rName.hashCode();
58     }
59 };
60 
61 //=========================================================================
62 //
63 // PropertyValueMap.
64 //
65 //=========================================================================
66 
67 class PropertyValue
68 {
69 private:
70     ::com::sun::star::uno::Any m_aValue;
71     bool                       m_bIsCaseSensitive;
72 
73 public:
PropertyValue()74     PropertyValue()
75     : m_bIsCaseSensitive( true ) {}
76 
PropertyValue(const::com::sun::star::uno::Any & rValue,bool bIsCaseSensitive)77     PropertyValue( const ::com::sun::star::uno::Any & rValue,
78                    bool bIsCaseSensitive )
79     : m_aValue( rValue),
80       m_bIsCaseSensitive( bIsCaseSensitive ) {}
81 
isCaseSensitive() const82     bool isCaseSensitive() const { return m_bIsCaseSensitive; }
value() const83     const ::com::sun::star::uno::Any & value() const { return m_aValue; }
84 
85 };
86 
87 typedef std::hash_map
88 <
89     rtl::OUString,
90     PropertyValue,
91     hashString,
92     equalString
93 >
94 PropertyValueMap;
95 
96 struct DAVResource;
97 
98 class ContentProperties
99 {
100 public:
101     ContentProperties();
102 
103     ContentProperties( const DAVResource& rResource );
104 
105     // Mini props for transient contents.
106     ContentProperties( const rtl::OUString & rTitle, sal_Bool bFolder );
107 
108     // Micro props for non-existing contents.
109     ContentProperties( const rtl::OUString & rTitle );
110 
111     ContentProperties( const ContentProperties & rOther );
112 
113     bool contains( const rtl::OUString & rName ) const;
114 
115     const com::sun::star::uno::Any &
116     getValue( const rtl::OUString & rName ) const;
117 
118     // Maps the UCB property names contained in rProps with their DAV property
119     // counterparts, if possible. All unmappable properties will be included
120     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
121     // The vector filles by this method can directly be handed over to
122     // DAVResourceAccess::PROPFIND. The result from PROPFIND
123     // (vector< DAVResource >) can be used to create a ContentProperties
124     // instance which can map DAV properties back to UCB properties.
125     static void UCBNamesToDAVNames( const com::sun::star::uno::Sequence<
126                                         com::sun::star::beans::Property > &
127                                             rProps,
128                                     std::vector< rtl::OUString > & resources,
129                                     bool bIncludeUnmatched = true );
130 
131     // Maps the UCB property names contained in rProps with their HTTP header
132     // counterparts, if possible. All unmappable properties will be included
133     // unchanged in resulting vector unless bIncludeUnmatched is set to false.
134     // The vector filles by this method can directly be handed over to
135     // DAVResourceAccess::HEAD. The result from HEAD (vector< DAVResource >)
136     // can be used to create a ContentProperties instance which can map header
137     // names back to UCB properties.
138     static void UCBNamesToHTTPNames( const com::sun::star::uno::Sequence<
139                                         com::sun::star::beans::Property > &
140                                             rProps,
141                                     std::vector< rtl::OUString > & resources,
142                                     bool bIncludeUnmatched = true );
143 
144     // return true, if all properties contained in rProps are contained in
145     // this ContentProperties instance. Otherwiese, false will be returned.
146     // rNamesNotContained contain the missing names.
147     bool containsAllNames(
148                     const com::sun::star::uno::Sequence<
149                         com::sun::star::beans::Property >& rProps,
150                     std::vector< rtl::OUString > & rNamesNotContained ) const;
151 
152     // adds all properties described by rProps that are actually contained in
153     // rContentProps to this instance. In case of duplicates the value
154     // already contained in this will left unchanged.
155     void addProperties( const std::vector< rtl::OUString > & rProps,
156                         const ContentProperties & rContentProps );
157 
158     // overwrites probably existing entries.
159     void addProperties( const ContentProperties & rProps );
160 
161     // overwrites probably existing entries.
162     void addProperties( const std::vector< DAVPropertyValue > & rProps );
163 
164     // overwrites probably existing entry.
165     void addProperty( const rtl::OUString & rName,
166                      const com::sun::star::uno::Any & rValue,
167                      bool bIsCaseSensitive );
168 
169     // overwrites probably existing entry.
170     void addProperty( const DAVPropertyValue & rProp );
171 
isTrailingSlash() const172     bool isTrailingSlash() const { return m_bTrailingSlash; }
173 
getEscapedTitle() const174     const rtl::OUString & getEscapedTitle() const { return m_aEscapedTitle; }
175 
176     // Not good to expose implementation details, but this is actually an
177     // internal class.
getProperties() const178     const std::auto_ptr< PropertyValueMap > & getProperties() const
179     { return m_xProps; }
180 
181 private:
182     ::rtl::OUString m_aEscapedTitle;
183     std::auto_ptr< PropertyValueMap > m_xProps;
184     bool m_bTrailingSlash;
185 
186     static com::sun::star::uno::Any m_aEmptyAny;
187 
188     ContentProperties & operator=( const ContentProperties & ); // n.i.
189 
190     const PropertyValue * get( const rtl::OUString & rName ) const;
191 };
192 
193 class CachableContentProperties
194 {
195 private:
196     ContentProperties m_aProps;
197 
198     CachableContentProperties & operator=( const CachableContentProperties & ); // n.i.
199     CachableContentProperties( const CachableContentProperties & ); // n.i.
200 
201 public:
202     CachableContentProperties( const ContentProperties & rProps );
203 
204     void addProperties( const ContentProperties & rProps );
205 
206     void addProperties( const std::vector< DAVPropertyValue > & rProps );
207 
containsAllNames(const com::sun::star::uno::Sequence<com::sun::star::beans::Property> & rProps,std::vector<rtl::OUString> & rNamesNotContained) const208     bool containsAllNames(
209                     const com::sun::star::uno::Sequence<
210                         com::sun::star::beans::Property >& rProps,
211                     std::vector< rtl::OUString > & rNamesNotContained ) const
212     { return m_aProps.containsAllNames( rProps, rNamesNotContained ); }
213 
214     const com::sun::star::uno::Any &
getValue(const rtl::OUString & rName) const215     getValue( const rtl::OUString & rName ) const
216     { return m_aProps.getValue( rName ); }
217 
218     operator const ContentProperties & () const { return m_aProps; }
219 };
220 
221 } // namespace http_dav_ucp
222 
223 #endif /* !_WEBDAV_UCP_CONTENTPROPERTIES_HXX */
224