Author: sdzmitrovich
Date: 2009-03-28 09:39:21 -0400 (Sat, 28 Mar 2009)
New Revision: 14364
Added:
trunk/vpe/plugins/org.jboss.tools.vpe/resources/htmlCodes.properties
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/TextUtil.java
Log:
JBIDE-3810, fixed with help of yzhishko
Added: trunk/vpe/plugins/org.jboss.tools.vpe/resources/htmlCodes.properties
===================================================================
--- trunk/vpe/plugins/org.jboss.tools.vpe/resources/htmlCodes.properties
(rev 0)
+++ trunk/vpe/plugins/org.jboss.tools.vpe/resources/htmlCodes.properties 2009-03-28
13:39:21 UTC (rev 14364)
@@ -0,0 +1,252 @@
+34="
+38=&
+60=<
+62=>
+160=
+161=¡
+162=¢
+163=£
+164=¤
+165=¥
+166=¦
+167=§
+168=¨
+169=©
+170=ª
+171=«
+172=¬
+173=­
+174=®
+175=¯
+176=°
+177=±
+178=²
+179=³
+180=´
+181=µ
+182=¶
+183=·
+184=¸
+185=¹
+186=º
+187=»
+188=¼
+189=½
+190=¾
+191=¿
+192=À
+193=Á
+194=Â
+195=Ã
+196=Ä
+197=Å
+198=Æ
+199=Ç
+200=È
+201=É
+202=Ê
+203=Ë
+204=Ì
+205=Í
+206=Î
+207=Ï
+208=Ð
+209=Ñ
+210=Ò
+211=Ó
+212=Ô
+213=Õ
+214=Ö
+215=×
+216=Ø
+217=Ù
+218=Ú
+219=Û
+220=Ü
+221=Ý
+222=Þ
+223=ß
+224=à
+225=á
+226=â
+227=ã
+228=ä
+229=å
+230=æ
+231=ç
+232=è
+233=é
+234=ê
+235=ë
+236=ì
+237=í
+238=î
+239=ï
+240=ð
+241=ñ
+242=ò
+243=ó
+244=ô
+245=õ
+246=ö
+247=÷
+248=ø
+249=ù
+250=ú
+251=û
+252=ü
+253=ý
+254=þ
+255=ÿ
+338=Œ
+339=œ
+352=Š
+353=š
+376=Ÿ
+710=ˆ
+732=˜
+8194= 
+8195= 
+8201= 
+8204=‌
+8205=‍
+8206=‎
+8207=‏
+8211=–
+8212=—
+8216=‘
+8217=’
+8218=‚
+8220=“
+8221=”
+8222=„
+8224=†
+8225=‡
+8240=‰
+8249=‹
+8250=›
+8364=€
+402=ƒ
+913=Α
+914=Β
+915=Γ
+916=Δ
+917=Ε
+918=Ζ
+919=Η
+920=Θ
+921=Ι
+922=Κ
+923=Λ
+924=Μ
+925=Ν
+926=Ξ
+927=Ο
+928=Π
+929=Ρ
+931=Σ
+932=Τ
+933=Υ
+934=Φ
+935=Χ
+936=Ψ
+937=Ω
+945=α
+946=β
+947=γ
+948=δ
+949=ε
+950=ζ
+951=η
+952=θ
+953=ι
+954=κ
+955=λ
+956=μ
+957=ν
+958=ξ
+959=ο
+960=π
+961=ρ
+962=ς
+963=σ
+964=τ
+965=υ
+966=φ
+967=χ
+968=ψ
+969=ω
+977=ϑ
+978=ϒ
+982=ϖ
+8226=•
+8230=…
+8242=′
+8243=″
+8254=‾
+8260=⁄
+8472=℘
+8465=ℑ
+8476=ℜ
+8482=™
+8501=ℵ
+8592=←
+8593=↑
+8594=→
+8595=↓
+8596=↔
+8629=↵
+8656=⇐
+8657=⇑
+8658=⇒
+8659=⇓
+8660=⇔
+8704=∀
+8706=∂
+8707=∃
+8709=∅
+8711=∇
+8712=∈
+8713=∉
+8715=∋
+8719=∏
+8721=∑
+8722=−
+8727=∗
+8730=√
+8733=∝
+8734=∞
+8736=∠
+8743=∧
+8744=∨
+8745=∩
+8746=∪
+8747=∫
+8756=∴
+8764=∼
+8773=≅
+8776=≈
+8800=≠
+8801=≡
+8804=≤
+8805=≥
+8834=⊂
+8835=⊃
+8836=⊄
+8838=⊆
+8839=⊇
+8853=⊕
+8855=⊗
+8869=⊥
+8901=⋅
+8968=⌈
+8969=⌉
+8970=⌊
+8971=⌋
+9001=⟨
+9002=⟩
+9674=◊
+9824=♠
+9827=♣
+9829=♥
+9830=♦
\ No newline at end of file
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/TextUtil.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/TextUtil.java 2009-03-27
21:33:55 UTC (rev 14363)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/TextUtil.java 2009-03-28
13:39:21 UTC (rev 14364)
@@ -1,383 +1,335 @@
-/*******************************************************************************
- * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
- * Distributed under license by Red Hat, Inc. All rights reserved.
- * This program is made available under the terms of the
- * Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at
http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
- ******************************************************************************/
-package org.jboss.tools.vpe.editor.util;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.mozilla.interfaces.nsIDOMKeyEvent;
-
-public class TextUtil {
- private final static String SOURCE_BREAK = "\r\n"; //$NON-NLS-1$
- private final static String VISUAL_BREAK = "\n"; //$NON-NLS-1$
- private final static char SOURCE_SPACE = ' ';
- private final static char VISUAL_SPACE = 160;
- private final static int CHR_ESC_START = '&';
- private final static int CHR_ESC_STOP = ';';
- private final static String SPCHARS = "\f\n\r\t\u0020\u2028\u2029";
//$NON-NLS-1$
- private final static Map<Character,String> textSet = new
HashMap<Character,String>();
- static{
- textSet.put(new Character('"'), """); //$NON-NLS-1$
- textSet.put(new Character('&'),"&"); //$NON-NLS-1$
- textSet.put(new Character('<'),"<"); //$NON-NLS-1$
- textSet.put(new Character('>'),">"); //$NON-NLS-1$
- textSet.put(new Character('\u00A0')," "); //$NON-NLS-1$
- textSet.put(new Character('\u00A1'),"¡"); //$NON-NLS-1$
- textSet.put(new Character('\u00A2'),"¢"); //$NON-NLS-1$
- textSet.put(new Character('\u00A3'),"£"); //$NON-NLS-1$
- textSet.put(new Character('\u00A4'),"¤"); //$NON-NLS-1$
- textSet.put(new Character('\u00A5'),"¥"); //$NON-NLS-1$
- textSet.put(new Character('\u00A6'),"¦"); //$NON-NLS-1$
- textSet.put(new Character('\u00A7'),"§"); //$NON-NLS-1$
- textSet.put(new Character('\u00A8'),"¨"); //$NON-NLS-1$
- textSet.put(new Character('\u00A9'),"©"); //$NON-NLS-1$
- textSet.put(new Character('\u00AA'),"ª"); //$NON-NLS-1$
- textSet.put(new Character('\u00AB'),"«"); //$NON-NLS-1$
- textSet.put(new Character('\u00AC'),"¬"); //$NON-NLS-1$
- //textSet.put(new Character('\u00AD'),"­");
- textSet.put(new Character('\u2022'), "•"); //$NON-NLS-1$
- textSet.put(new Character('\u00AE'),"®"); //$NON-NLS-1$
- textSet.put(new Character('\u00AF'),"¯"); //$NON-NLS-1$
- textSet.put(new Character('\u00B0'),"°"); //$NON-NLS-1$
- textSet.put(new Character('\u00B1'),"±"); //$NON-NLS-1$
- textSet.put(new Character('\u00B2'),"²"); //$NON-NLS-1$
- textSet.put(new Character('\u00B3'),"³"); //$NON-NLS-1$
- textSet.put(new Character('\u00B4'),"´"); //$NON-NLS-1$
- textSet.put(new Character('\u00B5'),"µ"); //$NON-NLS-1$
- textSet.put(new Character('\u00B6'),"¶"); //$NON-NLS-1$
- textSet.put(new Character('\u00B7'),"·"); //$NON-NLS-1$
- textSet.put(new Character('\u00B8'),"¸"); //$NON-NLS-1$
- textSet.put(new Character('\u00B9'),"¹"); //$NON-NLS-1$
- textSet.put(new Character('\u00BA'),"º"); //$NON-NLS-1$
- textSet.put(new Character('\u00BB'),"»"); //$NON-NLS-1$
- textSet.put(new Character('\u00BC'),"¼"); //$NON-NLS-1$
- textSet.put(new Character('\u00BD'),"½"); //$NON-NLS-1$
- textSet.put(new Character('\u00BE'),"¾"); //$NON-NLS-1$
- textSet.put(new Character('\u00BF'),"¿"); //$NON-NLS-1$
- textSet.put(new Character('\u00C0'),"À"); //$NON-NLS-1$
- textSet.put(new Character('\u00E0'),"à"); //$NON-NLS-1$
- textSet.put(new Character('\u00C1'),"Á"); //$NON-NLS-1$
- textSet.put(new Character('\u00E1'),"á"); //$NON-NLS-1$
- textSet.put(new Character('\u00C2'),"Â"); //$NON-NLS-1$
- textSet.put(new Character('\u00E2'),"â"); //$NON-NLS-1$
- textSet.put(new Character('\u00C3'),"Ã"); //$NON-NLS-1$
- textSet.put(new Character('\u00E3'),"ã"); //$NON-NLS-1$
- textSet.put(new Character('\u00C4'),"Ä"); //$NON-NLS-1$
- textSet.put(new Character('\u00E4'),"ä"); //$NON-NLS-1$
- textSet.put(new Character('\u00C5'),"Å"); //$NON-NLS-1$
- textSet.put(new Character('\u00E5'),"å"); //$NON-NLS-1$
- textSet.put(new Character('\u00C6'),"Æ"); //$NON-NLS-1$
- textSet.put(new Character('\u00E6'),"æ"); //$NON-NLS-1$
- textSet.put(new Character('\u00C7'),"Ç"); //$NON-NLS-1$
- textSet.put(new Character('\u00E7'),"ç"); //$NON-NLS-1$
- textSet.put(new Character('\u00C8'),"È"); //$NON-NLS-1$
- textSet.put(new Character('\u00E8'),"è"); //$NON-NLS-1$
- textSet.put(new Character('\u00C9'),"É"); //$NON-NLS-1$
- textSet.put(new Character('\u00E9'),"é"); //$NON-NLS-1$
- textSet.put(new Character('\u00CA'),"Ê"); //$NON-NLS-1$
- textSet.put(new Character('\u00EA'),"ê"); //$NON-NLS-1$
- textSet.put(new Character('\u00CB'),"Ë"); //$NON-NLS-1$
- textSet.put(new Character('\u00EB'),"ë"); //$NON-NLS-1$
- textSet.put(new Character('\u00CC'),"Ì"); //$NON-NLS-1$
- textSet.put(new Character('\u00EC'),"ì"); //$NON-NLS-1$
- textSet.put(new Character('\u00CD'),"Í"); //$NON-NLS-1$
- textSet.put(new Character('\u00ED'),"í"); //$NON-NLS-1$
- textSet.put(new Character('\u00CE'),"Î"); //$NON-NLS-1$
- textSet.put(new Character('\u00EE'),"î"); //$NON-NLS-1$
- textSet.put(new Character('\u00CF'),"Ï"); //$NON-NLS-1$
- textSet.put(new Character('\u00EF'),"ï"); //$NON-NLS-1$
- textSet.put(new Character('\u00D0'),"Ð"); //$NON-NLS-1$
- textSet.put(new Character('\u00F0'),"ð"); //$NON-NLS-1$
- textSet.put(new Character('\u00D1'),"Ñ"); //$NON-NLS-1$
- textSet.put(new Character('\u00F1'),"ñ"); //$NON-NLS-1$
- textSet.put(new Character('\u00D2'),"Ò"); //$NON-NLS-1$
- textSet.put(new Character('\u00F2'),"ò"); //$NON-NLS-1$
- textSet.put(new Character('\u00D3'),"Ó"); //$NON-NLS-1$
- textSet.put(new Character('\u00F3'),"ó"); //$NON-NLS-1$
- textSet.put(new Character('\u00D4'),"Ô"); //$NON-NLS-1$
- textSet.put(new Character('\u00F4'),"ô"); //$NON-NLS-1$
- textSet.put(new Character('\u00D5'),"Õ"); //$NON-NLS-1$
- textSet.put(new Character('\u00F5'),"õ"); //$NON-NLS-1$
- textSet.put(new Character('\u00D6'),"Ö"); //$NON-NLS-1$
- textSet.put(new Character('\u00F6'),"ö"); //$NON-NLS-1$
- textSet.put(new Character('\u00D7'),"×"); //$NON-NLS-1$
- textSet.put(new Character('\u00F7'),"÷"); //$NON-NLS-1$
- textSet.put(new Character('\u00D8'),"Ø"); //$NON-NLS-1$
- textSet.put(new Character('\u00F8'),"ø"); //$NON-NLS-1$
- textSet.put(new Character('\u00D9'),"Ù"); //$NON-NLS-1$
- textSet.put(new Character('\u00F9'),"ù"); //$NON-NLS-1$
- textSet.put(new Character('\u00DA'),"Ú"); //$NON-NLS-1$
- textSet.put(new Character('\u00FA'),"ú"); //$NON-NLS-1$
- textSet.put(new Character('\u00DB'),"Û"); //$NON-NLS-1$
- textSet.put(new Character('\u00FB'),"û"); //$NON-NLS-1$
- textSet.put(new Character('\u00DC'),"Ü"); //$NON-NLS-1$
- textSet.put(new Character('\u00FC'),"ü"); //$NON-NLS-1$
- textSet.put(new Character('\u00DD'),"Ý"); //$NON-NLS-1$
- textSet.put(new Character('\u00FD'),"ý"); //$NON-NLS-1$
- textSet.put(new Character('\u00DE'),"Þ"); //$NON-NLS-1$
- textSet.put(new Character('\u00FE'),"þ"); //$NON-NLS-1$
- textSet.put(new Character('\u00DF'),"ß"); //$NON-NLS-1$
- textSet.put(new Character('\u00FF'),"ÿ"); //$NON-NLS-1$
- textSet.put(new Character('\u2013'),"–"); //$NON-NLS-1$
- textSet.put(new Character('\u2014'),"—"); //$NON-NLS-1$
- textSet.put(new Character('\u2018'),"‘"); //$NON-NLS-1$
- textSet.put(new Character('\u2019'),"’"); //$NON-NLS-1$
- textSet.put(new Character('\u201C'),"“"); //$NON-NLS-1$
- textSet.put(new Character('\u201D'),"”"); //$NON-NLS-1$
- textSet.put(new Character('\u20AC'),"€"); //$NON-NLS-1$
- }
-
- public static boolean containsKey(char key){
-
- return textSet.containsKey(new Character(key));
- }
-
- public static String getValue(char key){
- return (String)textSet.get(new Character(key));
- }
-
- public static String isEcsToLeft(String sourceText, int sourcePosition) {
- String s1 = sourceText;
-
- String s2 = s1.substring(0, Math.min(sourcePosition, s1.length()));
- int startIndex = s2.lastIndexOf(CHR_ESC_START);
- int endIndex = s2.lastIndexOf(CHR_ESC_STOP);
- if(startIndex >= 0 && endIndex >= 0 && endIndex ==
s2.length()-1){
- String value = s2.substring(startIndex, endIndex+1);
- if(textSet.containsValue(value)) return value;
- }
-
- return null;
- }
-
- public static String isEcsToRight(String sourceText, int sourcePosition) {
- String s1 = sourceText;
-
- String s2 = s1.substring(sourcePosition, s1.length());
- int startIndex = s2.indexOf(CHR_ESC_START);
- int endIndex = s2.indexOf(CHR_ESC_STOP);
- if(startIndex >= 0 && endIndex >= 0 && startIndex == 0){
- String value = s2.substring(startIndex, endIndex+1);
- if(textSet.containsValue(value)) return value;
- }
-
- return null;
- }
-
- public static boolean isEcs(String sourceText, String visualText, int visualPosition) {
- String s1 = visualText;
-
- String s3 = s1.substring(0, Math.min(visualPosition+1, s1.length()));
- s3 = sourceText(s3);
-
- Character ch;
- int sourceIndex = 0;
- int visualIndex = 0;
- for(visualIndex=0;visualIndex<s3.length();visualIndex++){
- ch = new Character(s3.charAt(visualIndex));
-
- String value = (String)textSet.get(ch);
- char sourceChar = sourceText.charAt(sourceIndex);
- if(value != null){
- if(sourceChar != ch.charValue() || (ch.charValue() == '&' &&
sourceText.indexOf("&",sourceIndex) >= 0)){ //$NON-NLS-1$
- if(visualIndex == visualPosition) return true;
- sourceIndex += value.length()-1;
- }
- }else if(s3.charAt(visualIndex) == 160){
- if(sourceChar != ' ' && sourceChar != 160){
- if(visualIndex == visualPosition) return true;
- sourceIndex += 5;
- }
- }
- sourceIndex++;
- }
- return false;
- }
-
-// public static int sourcePosition(String sourceText, String visualText, int
visualPosition) {
-// String s1 = visualText;
-//
-// String s3 = s1.substring(0, Math.min(visualPosition, s1.length()));
-// s3 = sourceText(s3);
-//
-// Character ch;
-// int sourceIndex = 0;
-// int visualIndex = 0;
-// for(visualIndex=0;visualIndex<s3.length();visualIndex++){
-// ch = new Character(s3.charAt(visualIndex));
-//
-// String value = (String)textSet.get(ch);
-// char sourceChar = sourceText.charAt(sourceIndex);
-// if(value != null){
-// if(sourceChar != ch.charValue() || (ch.charValue() == '&' &&
sourceText.indexOf("&",sourceIndex) >= 0)){
-// sourceIndex += value.length()-1;
-// }
-// }else if(s3.charAt(visualIndex) == 160){
-// if(sourceChar != ' ' && sourceChar != 160){
-// sourceIndex += 5;
-// }
-// }
-// sourceIndex++;
-// }
-// return s3.length()+(sourceIndex-visualIndex);
-// }
- public static int sourcePosition(String sourceText, String visualText, int
visualPosition) {
- int sourceIndex = 0;
- int visualIndex = 0;
- while (sourceIndex < sourceText.length() && visualIndex < visualPosition)
{
- char sourceChar = sourceText.charAt(sourceIndex);
- if (sourceChar == '\r') {
- if (visualText.charAt(visualIndex) == '\r') {
- visualIndex++;
- }
- sourceIndex++;
- } else if (sourceChar == CHR_ESC_START) {
- int end = sourceText.indexOf(CHR_ESC_STOP, sourceIndex + 1);
- if (end != -1 && textSet.containsValue(sourceText.substring(sourceIndex, end
+ 1))) {
- sourceIndex += end - sourceIndex;
- }
- sourceIndex++;
- visualIndex++;
- } else {
- sourceIndex++;
- visualIndex++;
- }
- }
- return sourceIndex;
- }
-
- public static int sourceInnerPosition(String visualText, long visualPosition) {
- visualText = visualText.substring(0, (int)Math.min(visualPosition,
visualText.length()));
- String sourceText = visualText.replaceAll(VISUAL_BREAK, SOURCE_BREAK);
- return sourceText.length();
- }
-
- public static int visualPosition(String sourceText, int sourcePosition) {
- int calcPosition = sourcePosition;
- if(sourceText==null) {
- return 0;
- }
- int start = sourceText.indexOf(CHR_ESC_START);
- while (start != -1 && start < sourcePosition && start + 1 <
sourceText.length()) {
- int stop = sourceText.indexOf(CHR_ESC_STOP, start + 1);
- if (stop == -1) {
- break;
- } else if (stop < sourcePosition) {
- if (textSet.containsValue(sourceText.substring(start, stop + 1))) {
- calcPosition -= stop - start;
- }
- if (stop + 1 < sourceText.length()) {
- start = sourceText.indexOf(CHR_ESC_START, stop + 1);
- } else {
- break;
- }
- } else {
- calcPosition -= sourcePosition - start;
- break;
- }
- }
- String s1 = sourceText.substring(0, sourcePosition);
- String s2 = s1.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
- return calcPosition - (s1.length() - s2.length());
- }
-
- public static int visualInnerPosition(String sourceText, int sourcePosition) {
- sourceText = sourceText.substring(0, Math.min(sourcePosition, sourceText.length()));
- String visualText = sourceText.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
- return visualText.length();
- }
-
- public static int _visualPosition(String sourceText, int sourcePosition) {
- int delta = 7;
- if(sourcePosition+delta > sourceText.length()) delta =
sourceText.length()-sourcePosition;
- int position = Math.min(sourcePosition+delta, sourceText.length());
-
- String s1 = sourceText.substring(0, position);
- int start1 = s1.length()-delta;
- if(position < sourceText.length()){
- while(position > sourcePosition){
- int ampersandPosition = s1.lastIndexOf("&"); //$NON-NLS-1$
- int semicolonPosition = s1.lastIndexOf(";"); //$NON-NLS-1$
- if(ampersandPosition > 0 && semicolonPosition > 0 &&
ampersandPosition < semicolonPosition && (semicolonPosition+1) >
sourcePosition){
- String value = s1.substring(ampersandPosition, semicolonPosition+1);
- if(textSet.containsValue(value))
- position = ampersandPosition;
- else
- position--;
- }else if(ampersandPosition > 0 && semicolonPosition > 0 &&
semicolonPosition > sourcePosition){
- position = semicolonPosition+1;
- }else{
- position--;
- }
- s1 = s1.substring(0, position);
- }
- }else{
- position -= delta;
- s1 = s1.substring(0, position);
- }
- int start2 = s1.length();
- int startLength = s1.length();
-
- String[] strings = (String[]) textSet.values().toArray(new String[]{});
- for(int i=0;i<strings.length;i++){
- s1 = s1.replaceAll(strings[i]," "); //$NON-NLS-1$
- }
- s1 = visualText(s1);
- return sourcePosition-(startLength - s1.length())-(start1-start2);
- }
-
- public static String visualText(String sourceText) {
- return sourceText.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
- }
-
- public static String sourceText(String visualText) {
- return visualText.replaceAll(VISUAL_BREAK, SOURCE_BREAK).replace(VISUAL_SPACE,
SOURCE_SPACE);
- }
-
- public static boolean isWhitespace(char sourceString) {
- String visualString = visualText((new Character(sourceString)).toString());
- return SPCHARS.indexOf(sourceString) != -1;
- }
-
- public static boolean isWhitespaceText(String sourceString) {
- if (sourceString != null && sourceString.length() > 0) {
- char[] chars = sourceString.toCharArray();
- for (int i = 0; i < sourceString.length(); i++) {
- if (!TextUtil.isWhitespace(sourceString.charAt(i)))
- return false;
- }
- }
- return true;
- }
-
- public static String getChar(nsIDOMKeyEvent keyEvent) {
- // get inserted string
- long charCode = keyEvent.getCharCode();
- char[] s = new char[1];
- s[0] = (char) charCode;
- String str = new String(s);
- if (TextUtil.containsKey(s[0])) {
- str = TextUtil.getValue(s[0]);
- }
-
- return str;
- }
-
-
- /**
- * @param value
- * @return
- */
- public static boolean isContainsEl(final String value) {
- return (value.contains("#{") || value.contains("${"));
//$NON-NLS-1$//$NON-NLS-2$
- }
-
-}
+/*******************************************************************************
+ * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.editor.util;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Map.Entry;
+
+import org.jboss.tools.vpe.VpePlugin;
+import org.mozilla.interfaces.nsIDOMKeyEvent;
+
+public class TextUtil {
+
+
+ // ISO codes are contained in interval [0,2^31 -1]
+ private final static int MIN_ISO_CODE_RANGE = 0;
+ private final static int MAX_ISO_CODE_RANGE = Integer.MAX_VALUE;
+
+ private final static String CODES_FILE = "htmlCodes.properties";
//$NON-NLS-1$
+
+ private final static String SOURCE_BREAK = "\r\n"; //$NON-NLS-1$
+ private final static String VISUAL_BREAK = "\n"; //$NON-NLS-1$
+ private final static char SOURCE_SPACE = ' ';
+ private final static char VISUAL_SPACE = 160;
+ private final static char CHR_ESC_START = '&';
+ private final static char CHR_SHARP = '#';
+ private final static char CHR_ESC_STOP = ';';
+ private final static char CHR_HEX_FLAG = 'x';
+ private final static String SPCHARS = "\f\n\r\t\u0020\u2028\u2029";
//$NON-NLS-1$
+ private final static Map<Character,String> textSet = new
HashMap<Character,String>();
+ static{
+ try {
+ InputStream is =
VpePlugin.getDefault().getBundle().getResource(CODES_FILE).openStream();
+ Properties prop = new Properties();
+ prop.load(is);
+ for (Entry<Object, Object> e : prop.entrySet()) {
+ textSet.put((char)Integer.parseInt((String) e.getKey()), (String) e.getValue());
+ }
+ is.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static boolean containsKey(char key){
+
+ return textSet.containsKey(new Character(key));
+ }
+
+ public static String getValue(char key){
+ return (String)textSet.get(new Character(key));
+ }
+
+ public static String isEcsToLeft(String sourceText, int sourcePosition) {
+ String s1 = sourceText;
+
+ String s2 = s1.substring(0, Math.min(sourcePosition, s1.length()));
+ int startIndex = s2.lastIndexOf(CHR_ESC_START);
+ int endIndex = s2.lastIndexOf(CHR_ESC_STOP);
+ if(startIndex >= 0 && endIndex >= 0 && endIndex ==
s2.length()-1){
+ String value = s2.substring(startIndex, endIndex+1);
+ if(textSet.containsValue(value)) return value;
+ }
+
+ return null;
+ }
+
+ public static String isEcsToRight(String sourceText, int sourcePosition) {
+ String s1 = sourceText;
+
+ String s2 = s1.substring(sourcePosition, s1.length());
+ int startIndex = s2.indexOf(CHR_ESC_START);
+ int endIndex = s2.indexOf(CHR_ESC_STOP);
+ if(startIndex >= 0 && endIndex >= 0 && startIndex == 0){
+ String value = s2.substring(startIndex, endIndex+1);
+ if(textSet.containsValue(value)) return value;
+ }
+
+ return null;
+ }
+
+ public static boolean isEcs(String sourceText, String visualText, int visualPosition) {
+ String s1 = visualText;
+
+ String s3 = s1.substring(0, Math.min(visualPosition+1, s1.length()));
+ s3 = sourceText(s3);
+
+ Character ch;
+ int sourceIndex = 0;
+ int visualIndex = 0;
+ for(visualIndex=0;visualIndex<s3.length();visualIndex++){
+ ch = new Character(s3.charAt(visualIndex));
+
+ String value = (String)textSet.get(ch);
+ char sourceChar = sourceText.charAt(sourceIndex);
+ if(value != null){
+ if(sourceChar != ch.charValue() || (ch.charValue() == '&' &&
sourceText.indexOf("&",sourceIndex) >= 0)){ //$NON-NLS-1$
+ if(visualIndex == visualPosition) return true;
+ sourceIndex += value.length()-1;
+ }
+ }else if(s3.charAt(visualIndex) == 160){
+ if(sourceChar != ' ' && sourceChar != 160){
+ if(visualIndex == visualPosition) return true;
+ sourceIndex += 5;
+ }
+ }
+ sourceIndex++;
+ }
+ return false;
+ }
+
+// public static int sourcePosition(String sourceText, String visualText, int
visualPosition) {
+// String s1 = visualText;
+//
+// String s3 = s1.substring(0, Math.min(visualPosition, s1.length()));
+// s3 = sourceText(s3);
+//
+// Character ch;
+// int sourceIndex = 0;
+// int visualIndex = 0;
+// for(visualIndex=0;visualIndex<s3.length();visualIndex++){
+// ch = new Character(s3.charAt(visualIndex));
+//
+// String value = (String)textSet.get(ch);
+// char sourceChar = sourceText.charAt(sourceIndex);
+// if(value != null){
+// if(sourceChar != ch.charValue() || (ch.charValue() == '&' &&
sourceText.indexOf("&",sourceIndex) >= 0)){
+// sourceIndex += value.length()-1;
+// }
+// }else if(s3.charAt(visualIndex) == 160){
+// if(sourceChar != ' ' && sourceChar != 160){
+// sourceIndex += 5;
+// }
+// }
+// sourceIndex++;
+// }
+// return s3.length()+(sourceIndex-visualIndex);
+// }
+ public static int sourcePosition(String sourceText, String visualText, int
visualPosition) {
+ int sourceIndex = 0;
+ int visualIndex = 0;
+ while (sourceIndex < sourceText.length() && visualIndex < visualPosition)
{
+ char sourceChar = sourceText.charAt(sourceIndex);
+ if (sourceChar == '\r') {
+ if (visualText.charAt(visualIndex) == '\r') {
+ visualIndex++;
+ }
+ sourceIndex++;
+ } else if (sourceChar == CHR_ESC_START) {
+ // find index of the escape sequence's end
+ int end = sourceText.indexOf(CHR_ESC_STOP, sourceIndex + 1);
+
+ // if next symbol is '#' then escape sequence may be iso code
+ if (end != -1 && (sourceIndex + 2 < sourceText.length())
+ && sourceText.charAt(sourceIndex + 1) == CHR_SHARP) {
+
+ //convert string to number
+
+ int isoCode = MIN_ISO_CODE_RANGE - 1;
+
+ // if number has prefix 'x' means that it is hex number
+ int radix = sourceText.charAt(sourceIndex + 2) == CHR_HEX_FLAG ? 16
+ : 10;
+ int offcet = sourceText.charAt(sourceIndex + 2) == CHR_HEX_FLAG ? 3
+ : 2;
+
+ try {
+ isoCode = Integer.parseInt(sourceText.substring(
+ sourceIndex + offcet, end), radix);
+ } catch (NumberFormatException exception) {
+ }
+
+ // if escape sequence is iso code
+ if (isoCode >= MIN_ISO_CODE_RANGE
+ && isoCode <= MAX_ISO_CODE_RANGE) {
+ sourceIndex += end - sourceIndex;
+ }
+
+ }
+ // check is escape sequence is contained in the list
+ else if (end != -1
+ && textSet.containsValue(sourceText.substring(
+ sourceIndex, end + 1))) {
+ sourceIndex += end - sourceIndex;
+ }
+
+ sourceIndex++;
+ visualIndex++;
+ }
+ else {
+ sourceIndex++;
+ visualIndex++;
+ }
+ }
+ return sourceIndex;
+ }
+
+ public static int sourceInnerPosition(String visualText, long visualPosition) {
+ visualText = visualText.substring(0, (int)Math.min(visualPosition,
visualText.length()));
+ String sourceText = visualText.replaceAll(VISUAL_BREAK, SOURCE_BREAK);
+ return sourceText.length();
+ }
+
+ public static int visualPosition(String sourceText, int sourcePosition) {
+ int calcPosition = sourcePosition;
+ if(sourceText==null) {
+ return 0;
+ }
+ int start = sourceText.indexOf(CHR_ESC_START);
+ while (start != -1 && start < sourcePosition && start + 1 <
sourceText.length()) {
+ int stop = sourceText.indexOf(CHR_ESC_STOP, start + 1);
+ if (stop == -1) {
+ break;
+ } else if (stop < sourcePosition) {
+ if (textSet.containsValue(sourceText.substring(start, stop + 1))) {
+ calcPosition -= stop - start;
+ }
+ if (stop + 1 < sourceText.length()) {
+ start = sourceText.indexOf(CHR_ESC_START, stop + 1);
+ } else {
+ break;
+ }
+ } else {
+ calcPosition -= sourcePosition - start;
+ break;
+ }
+ }
+ String s1 = sourceText.substring(0, sourcePosition);
+ String s2 = s1.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
+ return calcPosition - (s1.length() - s2.length());
+ }
+
+ public static int visualInnerPosition(String sourceText, int sourcePosition) {
+ sourceText = sourceText.substring(0, Math.min(sourcePosition, sourceText.length()));
+ String visualText = sourceText.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
+ return visualText.length();
+ }
+
+ public static int _visualPosition(String sourceText, int sourcePosition) {
+ int delta = 7;
+ if(sourcePosition+delta > sourceText.length()) delta =
sourceText.length()-sourcePosition;
+ int position = Math.min(sourcePosition+delta, sourceText.length());
+
+ String s1 = sourceText.substring(0, position);
+ int start1 = s1.length()-delta;
+ if(position < sourceText.length()){
+ while(position > sourcePosition){
+ int ampersandPosition = s1.lastIndexOf("&"); //$NON-NLS-1$
+ int semicolonPosition = s1.lastIndexOf(";"); //$NON-NLS-1$
+ if(ampersandPosition > 0 && semicolonPosition > 0 &&
ampersandPosition < semicolonPosition && (semicolonPosition+1) >
sourcePosition){
+ String value = s1.substring(ampersandPosition, semicolonPosition+1);
+ if(textSet.containsValue(value))
+ position = ampersandPosition;
+ else
+ position--;
+ }else if(ampersandPosition > 0 && semicolonPosition > 0 &&
semicolonPosition > sourcePosition){
+ position = semicolonPosition+1;
+ }else{
+ position--;
+ }
+ s1 = s1.substring(0, position);
+ }
+ }else{
+ position -= delta;
+ s1 = s1.substring(0, position);
+ }
+ int start2 = s1.length();
+ int startLength = s1.length();
+
+ String[] strings = (String[]) textSet.values().toArray(new String[]{});
+ for(int i=0;i<strings.length;i++){
+ s1 = s1.replaceAll(strings[i]," "); //$NON-NLS-1$
+ }
+ s1 = visualText(s1);
+ return sourcePosition-(startLength - s1.length())-(start1-start2);
+ }
+
+ public static String visualText(String sourceText) {
+ return sourceText.replaceAll(SOURCE_BREAK, VISUAL_BREAK);
+ }
+
+ public static String sourceText(String visualText) {
+ return visualText.replaceAll(VISUAL_BREAK, SOURCE_BREAK).replace(VISUAL_SPACE,
SOURCE_SPACE);
+ }
+
+ public static boolean isWhitespace(char sourceString) {
+ String visualString = visualText((new Character(sourceString)).toString());
+ return SPCHARS.indexOf(sourceString) != -1;
+ }
+
+ public static boolean isWhitespaceText(String sourceString) {
+ if (sourceString != null && sourceString.length() > 0) {
+ char[] chars = sourceString.toCharArray();
+ for (int i = 0; i < sourceString.length(); i++) {
+ if (!TextUtil.isWhitespace(sourceString.charAt(i)))
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static String getChar(nsIDOMKeyEvent keyEvent) {
+ // get inserted string
+ long charCode = keyEvent.getCharCode();
+ char[] s = new char[1];
+ s[0] = (char) charCode;
+ String str = new String(s);
+ if (TextUtil.containsKey(s[0])) {
+ str = TextUtil.getValue(s[0]);
+ }
+
+ return str;
+ }
+
+
+ /**
+ * @param value
+ * @return
+ */
+ public static boolean isContainsEl(final String value) {
+ return (value.contains("#{") || value.contains("${"));
//$NON-NLS-1$//$NON-NLS-2$
+ }
+
+}