2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #ifndef __TULADDRESSSTRINGTOKENIZER_H__
21 #define __TULADDRESSSTRINGTOKENIZER_H__
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.
33 #include <tuladdressstringtokenizer.h>
35 // SFoundItem instance
36 CTulAddressStringTokenizer::SFoundItem item;
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."));
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);
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.
54 // Get count of found items.
55 TInt count(singleSearch->ItemCount());
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));
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));
72 // Get count of found items.
73 TInt count2(multiSearch->ItemCount());
75 // Get currently selected item to the result2 variable.
76 multiSearch->Item(item);
78 // Debug print all items and their type.
79 for( TInt i=0; i<count2; i++)
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);
95 class CTulAddressStringTokenizer : public CBase
99 Enumeration to define the search case.
100 Multiple enumerations can be used as binary mask.
102 enum TTokenizerSearchCase
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
114 // Struct to contain a found item.
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
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();
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 );
137 CTulAddressStringTokenizer();
138 void AddItemL( TInt aStartPos, TInt aLength, TTokenizerSearchCase aType );
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 );
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
151 void ConstructL( const TDesC& aText, TInt aSearchCases, TInt aMinNumbers );
152 void PerformSearchL( const TDesC& aText, TInt aSearchCases );
154 CTulAddressStringTokenizer( const CTulAddressStringTokenizer& ); // Prohibit copy constructor
155 CTulAddressStringTokenizer& operator= ( const CTulAddressStringTokenizer& ); // Prohibit assigment operator
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
163 #endif // __TULADDRESSSTRINGTOKENIZER_H__