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 #if !defined INCLUDED_CPPUHELPER_UNOURL_HXX
25 #define INCLUDED_CPPUHELPER_UNOURL_HXX
26 
27 #include <memory>
28 
29 namespace rtl { class OUString; }
30 
31 namespace cppu {
32 
33 /** A descriptor as part of a UNO URL (connection descriptor or protocol
34     descriptor).
35 
36     Such a descriptor can also be useful outside the context of a full UNO URL.
37     For example, some functions take a string representing a connection or
38     protocol descriptor as input, and can use this class to parse the string.
39  */
40 class UnoUrlDescriptor
41 {
42 public:
43     /** @internal
44      */
45     class Impl;
46 
47     /** Construct a descriptor from a string representation.
48 
49         @param rDescriptor
50         The string representation of a descriptor.
51 
52         @exception rtl::MalformedUriException
53         Thrown when the given string representation is invalid.
54      */
55     explicit UnoUrlDescriptor(rtl::OUString const & rDescriptor);
56 
57     /** @internal
58      */
59     explicit UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl);
60 
61     UnoUrlDescriptor(UnoUrlDescriptor const & rOther);
62 
63     ~UnoUrlDescriptor();
64 
65     UnoUrlDescriptor & operator =(UnoUrlDescriptor const & rOther);
66 
67     /** Return the string representation of the descriptor.
68 
69         @return
70         A reference to the string representation used to construct this
71         descriptor, without any modifications.  The reference is valid for the
72         lifetime of this URL object.
73      */
74     rtl::OUString const & getDescriptor() const;
75 
76     /** Return the name component of the descriptor.
77 
78         @return
79         A reference to the (case insensitive) name, in lower case form.  The
80         reference is valid for the lifetime of this URL object.
81      */
82     rtl::OUString const & getName() const;
83 
84     /** Test whether the parameters contain a key.
85 
86         @param
87         A (case insensitive) key.
88 
89         @return
90         True if the parameters contain a matching key/value pair.
91      */
92     bool hasParameter(rtl::OUString const & rKey) const;
93 
94     /** Return the parameter value for a key.
95 
96         @param
97         A (case insensitive) key.
98 
99         @return
100         The (case sensitive) value associated with the given key, or an empty
101         string if there is no matching key/value pair.
102      */
103     rtl::OUString getParameter(rtl::OUString const & rKey) const;
104 
105 private:
106     std::auto_ptr< Impl > m_xImpl;
107 };
108 
109 /** Parse UNO URLs into their components.
110 
111     The ABNF for UNO URLs is as follows (see RFCs 2234, 2396, also see
112     <http://udk.openoffice.org/common/man/spec/uno-url.html>):
113 
114     uno-url = "UNO:" connection ";" protocol ";" object-name
115     connection = descriptor
116     protocol = descriptor
117     descriptor = name *("," parameter)
118     name = 1*alphanum
119     parameter = key "=" value
120     key = 1*alphanum
121     value = *vchar
122     valchar = unreserved / escaped / "$" / "&" / "+" / "/" / ":" / "?" / "@"
123     object-name = 1*ochar
124     ochar = unreserved / "$" / "&" / "+" / "," / "/" / ":" / "=" / "?" / "@"
125 
126     Within a descriptor, the name and the keys are case insensitive, and within
127     the parameter list all keys must be distinct.
128 
129     Parameter values are encoded using UTF-8.  Note that parsing of parameter
130     values as done by UnoUrl and UnoUrlDescriptor is not strict:  Invalid UTF-16
131     entities in the input, as well as broken escape sequences ("%" not followed
132     by two hex digits) are copied verbatim to the output, invalid bytes in the
133     converted UTF-8 data are considered individual Unicode characters, and
134     invalid UTF-16 entities in the resulting output (e.g., a high surrogate not
135     followed by a low surrogate) are not detected.
136  */
137 class UnoUrl
138 {
139 public:
140     /** Construct a UNO URL from a string representation.
141 
142         @param rUrl
143         The string representation of a UNO URL.
144 
145         @exception rtl::MalformedUriException
146         Thrown when the given string representation is invalid.
147      */
148     explicit UnoUrl(rtl::OUString const & rUrl);
149 
150     UnoUrl(UnoUrl const & rOther);
151 
152     ~UnoUrl();
153 
154     UnoUrl & operator =(UnoUrl const & rOther);
155 
156     /** Return the connection descriptor component of the URL.
157 
158         @return
159         A reference to the connection descriptor.  The reference is valid for
160         the lifetime of this URL object.
161      */
162     UnoUrlDescriptor const & getConnection() const;
163 
164     /** Return the protocol descriptor component of the URL.
165 
166         @return
167         A reference to the protocol descriptor.  The reference is valid for the
168         lifetime of this URL object.
169      */
170     UnoUrlDescriptor const & getProtocol() const;
171 
172     /** Return the object-name component of the URL.
173 
174         @return
175         A reference to the (case sensitive) object-name.  The reference is valid
176         for the lifetime of this URL object.
177      */
178     rtl::OUString const & getObjectName() const;
179 
180 private:
181     class Impl;
182 
183     std::auto_ptr< Impl > m_xImpl;
184 };
185 
186 }
187 
188 #endif // INCLUDED_RTL_UNOURL_HXX
189