1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  ******************************************************************************/
14 package org.eclipse.equinox.bidi.internal.consumable;
15 
16 import org.eclipse.equinox.bidi.advanced.IStructuredTextExpert;
17 import org.eclipse.equinox.bidi.custom.StructuredTextCharTypes;
18 import org.eclipse.equinox.bidi.internal.StructuredTextDelimsEsc;
19 
20 /**
21  *  Handler adapted to processing e-mail addresses.
22  */
23 public class StructuredTextEmail extends StructuredTextDelimsEsc {
24 	static final byte L = Character.DIRECTIONALITY_LEFT_TO_RIGHT;
25 	static final byte R = Character.DIRECTIONALITY_RIGHT_TO_LEFT;
26 	static final byte AL = Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
27 
StructuredTextEmail()28 	public StructuredTextEmail() {
29 		super("<>.:,;@"); //$NON-NLS-1$
30 	}
31 
32 	@Override
getDirection(IStructuredTextExpert expert, String text)33 	public int getDirection(IStructuredTextExpert expert, String text) {
34 		return getDirection(expert, text, new StructuredTextCharTypes(expert, text));
35 	}
36 
37 	/**
38 	 *  @return {@link IStructuredTextExpert#DIR_RTL DIR_RTL} if the following
39 	 *          conditions are satisfied:
40 	 *          <ul>
41 	 *            <li>The current locale (as expressed by the environment
42 	 *                language) is Arabic.</li>
43 	 *            <li>The domain part of the email address contains
44 	 *                at least one RTL character.</li>
45 	 *          </ul>
46 	 *          Otherwise, returns {@link IStructuredTextExpert#DIR_LTR DIR_LTR}.
47 	 */
48 	@Override
getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes)49 	public int getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes) {
50 		String language = expert.getEnvironment().getLanguage();
51 		if (!language.equals("ar")) //$NON-NLS-1$
52 			return IStructuredTextExpert.DIR_LTR;
53 		int domainStart;
54 		domainStart = text.indexOf('@');
55 		if (domainStart < 0)
56 			domainStart = 0;
57 		for (int i = domainStart; i < text.length(); i++) {
58 			byte charType = charTypes.getBidiTypeAt(i);
59 			if (charType == AL || charType == R)
60 				return IStructuredTextExpert.DIR_RTL;
61 		}
62 		return IStructuredTextExpert.DIR_LTR;
63 	}
64 
65 	/**
66 	 *  @return 2 as number of special cases handled by this handler.
67 	 */
68 	@Override
getSpecialsCount(IStructuredTextExpert expert)69 	public int getSpecialsCount(IStructuredTextExpert expert) {
70 		return 2;
71 	}
72 
73 	/**
74 	 *  @return parentheses and quotation marks as delimiters.
75 	 */
76 	@Override
getDelimiters()77 	protected String getDelimiters() {
78 		return "()\"\""; //$NON-NLS-1$
79 	}
80 
81 }
82