1 chuck 1.1 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 chuck 1.1 //==============================================================================
23 //
24 // Author: Humberto Rivero (hurivero@us.ibm.com)
25 //
26 // Modified By:
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #ifndef Pegasus_LanguageParser_h
31 #define Pegasus_LanguageParser_h
32
33 #include <cstdlib>
34 #include <cctype>
35 #include <Pegasus/Common/Linkage.h>
36 #include <Pegasus/Common/Config.h>
37 #include <Pegasus/Common/String.h>
38 #include <Pegasus/Common/Array.h>
39
40
41 PEGASUS_NAMESPACE_BEGIN
42
43 chuck 1.1
44 /** This class parses and validates AcceptLanguage OR ContentLanguage values
45 * from the respective HTTP headers, and provides various utility functions
46 * to parse and manipulate language information
47 */
48 class PEGASUS_COMMON_LINKAGE LanguageParser{
49
50 public:
51
52 /** This member is used to represent an empty LanguageParser. Using this
53 member avoids construction of an empty LanguageParser
54 (e.g., LanguageParser()).
55 */
56 static const LanguageParser EMPTY;
57
58 /** Parses the value portion of either an AcceptLanguage HTTP header or a
59 * ContentLanguage HTTP header, extracts the individual fields and stores
60 * them in an array.
61 * @param values Array<String>& where the individual fields are stored
62 * @param hdr String the value portion of the HTTP Accept/ContentLanguage header
63 */
64 chuck 1.1 void parseHdr(Array<String> &values, String & hdr);
65
66 /** Parses an AcceptLanguage or ContentLanguage value from a header.
67 * If a valid AcceptLanguage value is determined to contain a quality
68 * value, then this quality once validated is returned. Otherwise -1 is returned.
69 * It is up to the caller to know in what context this call is being made,
70 * that is: it is the callers responsibility to know whether
71 * an AcceptLanguage or a ContentLanguage value is being parsed.
72 * @param language String& where the valid language is stored
73 * @param hdr String the value being parsed and validated
74 * @return Real32 if a valid quality found then 1 <=> 0, otherwise 1
75 * @exception throws ???
76 */
77 Real32 parseAcceptLanguageValue(String &language, String & hdr);
78
79 String parseContentLanguageValue(String & hdr);
80
81 /**
82 * Given a language ex: en-US-xx,
83 * returns the base of the language "en".
84 * @param language String
85 chuck 1.1 * @param separator Char16 the character that separates the various subtags
86 * @return String base of the language parameter
87 */
88 String getLanguage(String & language_tag);
89
90 String getCountry(String & language_tag);
91
92 String getVariant(String & language_tag);
93
94 private:
95
96 /**
97 * Fills the first position in the array with the most general part of
98 * the language String, each subsequent postion is the array gets more
99 * and more detailed. For example: en-us-fooblah would be returned as
100 * ["en"]["us"]["fooblah"]
101 * @param subtags Array<String>& array filled in with the root and subtags
102 * @param language String the language to parse
103 * @param separator Char16 the character that separates the various subtags
104 * @return String the root of the language tag
105 */
106 chuck 1.1 void parseLanguageSubtags(Array<String> &subtags, String language_tag);
107
108 /** Breaks the language into its parts(subtags), then validates each part
109 * according to the RFCs.
110 * @param s String language to be validated
111 * @return Boolean
112 */
113 Boolean isValid(String language_tag, Boolean length_check = true);
114
115 /** Checks if the String has all characters in A-Z OR a-z range.
116 * @param s String
117 * @return Boolean
118 */
119 Boolean checkAlpha(CString _str);
120
121 CString replaceSeparator(CString _s, char new_sep = '-');
122
123 char findSeparator(CString _str);
124
125 String convertPrivateLanguageTag(String language_tag);
126
127 chuck 1.1 }; //end LanguageParser
128
129 PEGASUS_NAMESPACE_END
130
131 #endif
|