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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2