|
1 /* |
|
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #ifndef __TULADDRESSSTRINGTOKENIZER_H__ |
|
21 #define __TULADDRESSSTRINGTOKENIZER_H__ |
|
22 |
|
23 #include <e32base.h> |
|
24 |
|
25 /** |
|
26 Address String Tokenizer API offers methods for parsing phone numbers and e-mail, |
|
27 URL and URI addresses from the given text. The API consists of the |
|
28 CTulAddressStringTokenizer class. |
|
29 |
|
30 Usage: |
|
31 |
|
32 @code |
|
33 #include <tuladdressstringtokenizer.h> |
|
34 |
|
35 // SFoundItem instance |
|
36 CTulAddressStringTokenizer::SFoundItem item; |
|
37 |
|
38 // Some text |
|
39 TBufC<256> strSomeText(_L("Mail to me@someplace.com or call 040 1234567. |
|
40 You can also tune in to audio feed at rtsp://someplace.com/somefeed.ra.")); |
|
41 |
|
42 // First the user has to create an instance of CTulAddressStringTokenizer by using the |
|
43 // factory method NewL(). The method takes two parameters. The first |
|
44 // parameter defines the text to be searched from and the second parameter |
|
45 // tells what exactly is being looked for. |
|
46 CTulAddressStringTokenizer singleSearch = CTulAddressStringTokenizer::NewL(strSomeText, |
|
47 CTulAddressStringTokenizer::EFindItemSearchMailAddressBin); |
|
48 |
|
49 // The passed text is parsed in construction, and found items can be fetched |
|
50 // by using the ItemArray() method. It returns a constant array containing |
|
51 // all the found items. The interface also offers helper functions for |
|
52 // handling the item array by itself. |
|
53 |
|
54 // Get count of found items. |
|
55 TInt count(singleSearch->ItemCount()); |
|
56 |
|
57 // Get currently selected item (me@someplace.com) to the result1 variable. |
|
58 singleSearch->Item(item); |
|
59 TPtrC16 result1(strSomeText.Mid(item.iStartPos, item.iLength)); |
|
60 |
|
61 // Deallocate memory |
|
62 delete singleSearch; |
|
63 |
|
64 // Create an instance of CTulAddressStringTokenizer and look for all possible |
|
65 // things (cases work as binary mask). |
|
66 CTulAddressStringTokenizer* multiSearch = CTulAddressStringTokenizer::NewL(strSomeText, |
|
67 (CTulAddressStringTokenizer::EFindItemSearchPhoneNumberBin | |
|
68 CTulAddressStringTokenizer::EFindItemSearchURLBin | |
|
69 CTulAddressStringTokenizer::EFindItemSearchMailAddressBin | |
|
70 CTulAddressStringTokenizer::EFindItemSearchScheme)); |
|
71 |
|
72 // Get count of found items. |
|
73 TInt count2(multiSearch->ItemCount()); |
|
74 |
|
75 // Get currently selected item to the result2 variable. |
|
76 multiSearch->Item(item); |
|
77 |
|
78 // Debug print all items and their type. |
|
79 for( TInt i=0; i<count2; i++) |
|
80 { |
|
81 TPtrC16 result2(strSomeText.Mid(item.iStartPos, item.iLength)); |
|
82 RDebug::Print(_L("Found type %d item:"), item.iItemType); |
|
83 RDebug::Print(_L("%S"), &result2); |
|
84 multiSearch->NextItem(item); |
|
85 } |
|
86 |
|
87 // Deallocate memory |
|
88 delete multiSearch; |
|
89 @endcode |
|
90 |
|
91 @publishedAll |
|
92 @released |
|
93 */ |
|
94 |
|
95 class CTulAddressStringTokenizer : public CBase |
|
96 { |
|
97 public: |
|
98 /** |
|
99 Enumeration to define the search case. |
|
100 Multiple enumerations can be used as binary mask. |
|
101 */ |
|
102 enum TTokenizerSearchCase |
|
103 { |
|
104 // Searches phone numbers. |
|
105 EFindItemSearchPhoneNumberBin = 4, |
|
106 // Searches mail addresses. |
|
107 EFindItemSearchMailAddressBin = 8, |
|
108 // Searches fixed start URLs ("http://", "https://", "rtsp://"), "www.", "wap." and IPv4 addresses. |
|
109 EFindItemSearchURLBin = 16, |
|
110 // Searches for all URIs containing a scheme. |
|
111 EFindItemSearchScheme = 32 |
|
112 }; |
|
113 |
|
114 // Struct to contain a found item. |
|
115 struct SFoundItem |
|
116 { |
|
117 TInt iStartPos; // Start position of the found item. |
|
118 TInt iLength; // Length of the found item (characters). |
|
119 TTokenizerSearchCase iItemType; // Search case of the found item |
|
120 }; |
|
121 |
|
122 public: // Constructors and destructor |
|
123 IMPORT_C static CTulAddressStringTokenizer* NewL( const TDesC& aText, TInt aSearchCases ); |
|
124 IMPORT_C static CTulAddressStringTokenizer* NewL( const TDesC& aText, TInt aSearchCases, TInt aMinNumbers ); |
|
125 IMPORT_C ~CTulAddressStringTokenizer(); |
|
126 public: |
|
127 IMPORT_C TInt ItemCount() const; |
|
128 IMPORT_C TBool Item( SFoundItem& aItem ) const; |
|
129 IMPORT_C TBool NextItem( SFoundItem& aItem ); |
|
130 IMPORT_C TBool PrevItem( SFoundItem& aItem ); |
|
131 IMPORT_C const CArrayFixFlat<SFoundItem>* ItemArray() const; |
|
132 IMPORT_C TInt Position() const; |
|
133 IMPORT_C void ResetPosition(); |
|
134 IMPORT_C TInt DoNewSearchL( const TDesC& aText, TInt aSearchCases); |
|
135 IMPORT_C TInt DoNewSearchL( const TDesC& aText, TInt aSearchCases, TInt aMinNumbers ); |
|
136 private: |
|
137 CTulAddressStringTokenizer(); |
|
138 void AddItemL( TInt aStartPos, TInt aLength, TTokenizerSearchCase aType ); |
|
139 |
|
140 TBool SearchPhoneNumberL( const TDesC& aText ); |
|
141 TBool SearchMailAddressL( const TDesC& aText ); |
|
142 TBool SearchGenericUriL( const TDesC& aText ); |
|
143 TBool SearchUrlL( const TDesC& aText, TBool aFindFixedSchemas ); |
|
144 TBool ParseUrlL( const TDesC& aType, const TPtrC& aTokenPtr, TInt aTextOffset ); |
|
145 |
|
146 static TBool IsValidEmailChar(const TChar& charac); // Login part of the e-mail address |
|
147 static TBool IsValidEmailHostChar(const TChar& charac); // Host part of the e-mail address |
|
148 static TBool IsValidPhoneNumberChar(const TChar& charac); // Phone number |
|
149 static TBool IsValidUrlChar( const TChar& charac); // URL |
|
150 |
|
151 void ConstructL( const TDesC& aText, TInt aSearchCases, TInt aMinNumbers ); |
|
152 void PerformSearchL( const TDesC& aText, TInt aSearchCases ); |
|
153 |
|
154 CTulAddressStringTokenizer( const CTulAddressStringTokenizer& ); // Prohibit copy constructor |
|
155 CTulAddressStringTokenizer& operator= ( const CTulAddressStringTokenizer& ); // Prohibit assigment operator |
|
156 private: |
|
157 CArrayFixFlat<SFoundItem>* iFoundItems; // Array of all found items. |
|
158 TInt iPosition; // Engine's position in the iFoundItems. |
|
159 TInt iMinNumbers; // Minimum count of numbers in a phone number |
|
160 }; |
|
161 |
|
162 |
|
163 #endif // __TULADDRESSSTRINGTOKENIZER_H__ |
|
164 |