(file) Return to LanguageParser.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 martin 1.22 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.23 //
  3 martin 1.22 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.23 //
 10 martin 1.22 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.23 //
 17 martin 1.22 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.23 //
 20 martin 1.22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.23 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.23 //
 28 martin 1.22 //////////////////////////////////////////////////////////////////////////
 29 alagaraja 1.6  //
 30                //%/////////////////////////////////////////////////////////////////////////////
 31                
 32                #ifndef Pegasus_LanguageParser_h
 33                #define Pegasus_LanguageParser_h
 34                
 35                #include <cstdlib>
 36                #include <cctype>
 37                #include <Pegasus/Common/Linkage.h>
 38                #include <Pegasus/Common/Config.h>
 39                #include <Pegasus/Common/String.h>
 40                #include <Pegasus/Common/Array.h>
 41 kumpf     1.18 #include <Pegasus/Common/AcceptLanguageList.h>
 42                #include <Pegasus/Common/ContentLanguageList.h>
 43 alagaraja 1.6  
 44                PEGASUS_NAMESPACE_BEGIN
 45                
 46 kumpf     1.17 /**
 47                    This class parses and validates Accept-Language and Content-Language values
 48                    from the respective HTTP headers, and provides utility functions to parse
 49                    and manipulate language information
 50 alagaraja 1.6   */
 51 kumpf     1.17 class PEGASUS_COMMON_LINKAGE LanguageParser
 52                {
 53 alagaraja 1.6  public:
 54                
 55 kumpf     1.17     /**
 56 kumpf     1.18         Builds an AcceptLanguageList object from an HTTP Accept-Language
 57 kumpf     1.17         header value.
 58                        @param acceptLanguageHeader A String containing an HTTP
 59 kumpf     1.18         Accept-Language header value from which to build the AcceptLanguageList
 60 kumpf     1.17         object.
 61                        @exception InvalidAcceptLanguageHeader if a syntax error is
 62                        encountered.
 63                     */
 64 kumpf     1.18     static AcceptLanguageList parseAcceptLanguageHeader(
 65 kumpf     1.17         const String& acceptLanguageHeader);
 66                
 67                    /**
 68 kumpf     1.18         Builds a ContentLanguageList object from an HTTP Content-Language
 69 kumpf     1.17         header value.
 70                        @param contentLanguageHeader A String containing an HTTP
 71 kumpf     1.18         Content-Language header value from which to build the
 72                        ContentLanguageList object.
 73 kumpf     1.17         @exception InvalidContentLanguageHeader if a syntax error is
 74                        encountered.
 75                     */
 76 kumpf     1.18     static ContentLanguageList parseContentLanguageHeader(
 77 kumpf     1.17         const String& contentLanguageHeader);
 78                
 79                    /**
 80                        Validates the syntax of a language tag according to RFC 3066.
 81                        Parses the language tag into language, country, and variant, if
 82                        the language tag is formed using ISO codes (ISO 639 and ISO 3166).
 83                        @param languageTagString The language tag string to parse and
 84                        validate.
 85                        @param language The returned language value parsed from the language
 86                        tag, if applicable.
 87                        @param country The returned country value parsed from the language
 88                        tag, if applicable.
 89                        @param variant The returned language variant value parsed from the
 90                        language tag, if applicable.
 91                        @exception Exception if the language tag is syntactically invalid.
 92                     */
 93                    static void parseLanguageTag(
 94                        const String& languageTagString,
 95                        String& language,
 96                        String& country,
 97                        String& variant);
 98 kumpf     1.17 
 99                    /**
100                        Validates the syntax of an Accept-Language quality value.
101                        @param quality A Real32 quality value to validate.
102                        @exception Exception if the quality value is invalid.
103                     */
104                    static void validateQualityValue(Real32 quality);
105                
106                    /**
107 kumpf     1.18         Builds an HTTP Accept-Language header value from an AcceptLanguageList
108 kumpf     1.17         object.
109 kumpf     1.18         @param acceptLanguages The AcceptLanguageList object to be encoded in
110                        an HTTP Accept-Language header.
111 kumpf     1.17      */
112                    static String buildAcceptLanguageHeader(
113 kumpf     1.18         const AcceptLanguageList& acceptLanguages);
114 kumpf     1.17 
115                    /**
116 kumpf     1.18         Builds an HTTP Content-Language header value from a ContentLanguageList
117 kumpf     1.17         object.
118 kumpf     1.18         @param contentLanguages The ContentLanguageList object to be encoded
119                        in an HTTP Content-Language header.
120 kumpf     1.17      */
121                    static String buildContentLanguageHeader(
122 kumpf     1.18         const ContentLanguageList& contentLanguages);
123 kumpf     1.17 
124 kumpf     1.20 #ifdef PEGASUS_HAS_ICU
125                    /**
126                        Converts a locale ID to language tag syntax by replacing the '_'
127                        separators with '-' separators.
128                        @param localeId The locale ID string to convert to language tag
129                        syntax.  The conversion is performed on the localeId object
130                        directly.
131                        @return The converted String with language tag syntax.
132                     */
133                    static String& convertLocaleIdToLanguageTag(String& localeId);
134                #endif
135                
136 kumpf     1.17     /**
137                        Returns the default accept languages based on environment settings.
138                        ATTN: What is the behavior?  This is only implemented if MESSAGES and
139                        ICU are used.
140                     */
141 kumpf     1.18     static AcceptLanguageList getDefaultAcceptLanguages();
142 alagaraja 1.6  
143                private:
144                
145 kumpf     1.17     /**
146                        Parses the value portion of an HTTP Accept-Language or Content-Language
147                        header into individual language elements.  Comments and whitespace are
148                        removed.
149                        @param headerElements An array of Strings into which the language
150                        elements are stored.
151                        @param languageHeaderValue A String containing the value portion of
152                        an HTTP Accept-Language or Content-Language header.
153                        @exception Exception if a syntax error is encountered.
154                     */
155                    static void _parseLanguageHeader(
156                        const String& languageHeaderValue,
157                        Array<String>& languageElements);
158                
159                    /**
160                        Parses an AcceptLanguage or ContentLanguage value from a header.
161                        If a valid AcceptLanguage value is determined to contain a quality
162                        value, then this quality is validated and returned. Otherwise -1 is
163                        returned.  It is up to the caller to know in what context this call
164                        is being made, that is: it is the callers responsibility to know
165                        whether an AcceptLanguage or a ContentLanguage value is being parsed.
166 kumpf     1.17         @param acceptLanguageHeaderElement String to be parsed into a
167                        language tag and quality value.  The String must not contain comments
168                        or whitespace.
169                        @param languageTag String into which the parsed language tag is
170                        stored.
171                        @param quality Real32 into which the parsed quality value is stored.
172                        @exception Exception if a syntax error is encountered.
173                     */
174                    static void _parseAcceptLanguageElement(
175                        const String& acceptLanguageElement,
176                        String& languageTag,
177                        Real32& quality);
178                
179                    /**
180                        Fills the first position in the array with the most general part of
181                        the language String, each subsequent postion is the array gets more
182                        and more detailed.  For example: en-us-fooblah would be returned as
183                        ["en"]["us"]["fooblah"]
184                        @param subtags Array<String>& array filled in with the root and subtags
185                        @param languageTag String the language to parse
186                        @return String the root of the language tag
187 kumpf     1.17         @exception Exception if a syntax error is encountered.
188                     */
189                    static void _parseLanguageSubtags(
190                        Array<String>& subtags,
191                        const String& languageTagString);
192                
193                    /**
194                        Checks if a String represents a valid language tag primary subtag.
195                        A valid primary subtag contains 1 to 8 alphabetic characters.
196                        @param subtag The subtag String to validate.
197                        @return True if the String represents a valid language tag primary
198                        subtag; false otherwise.
199                     */
200                    static Boolean _isValidPrimarySubtagSyntax(const String& subtag);
201                
202                    /**
203                        Checks if a String represents a valid language tag subtag.
204                        A valid primary subtag contains 1 to 8 alphanumeric characters.
205                        @param subtag The subtag String to validate.
206                        @return True if the String represents a valid language tag subtag;
207                        false otherwise.
208 kumpf     1.17      */
209                    static Boolean _isValidSubtagSyntax(const String& subtag);
210                };
211 alagaraja 1.6  
212                PEGASUS_NAMESPACE_END
213 chuck     1.4  
214 alagaraja 1.6  #endif

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2