1 18 19 package org.netbeans.modules.xml.text.syntax.dom; 20 21 import org.netbeans.editor.TokenItem; 22 23 29 public class Util { 30 31 public static String [] knownEntityStrings = {"<", ">", "'", """, "&"}; 32 33 public static char[] knownEntityChars = {'<', '>', '\'', '"', '&'}; 34 35 39 public static TokenItem skipAttributeValue(TokenItem attribute, char delim) { 40 TokenItem next = attribute; 41 for (; next != null; next = next.getNext()) { 42 String image = next.getImage(); 43 if (image.endsWith("" + delim)) { 44 return next.getNext(); 45 } 46 } 47 return null; 48 } 49 50 58 public static String actualAttributeValue(String attributeValue) { 59 int ltIndex = attributeValue.indexOf('<'); 60 int gtIndex = attributeValue.indexOf('>'); 61 int firstUnwantedIndex = -1; 62 if (gtIndex != -1) { 63 if (ltIndex != -1 && ltIndex < gtIndex) { 64 firstUnwantedIndex = ltIndex; 65 } else { 66 firstUnwantedIndex = gtIndex; 67 } 68 } else { 69 firstUnwantedIndex = ltIndex; 70 } 71 72 if (firstUnwantedIndex != -1) { 73 char charAtIndex = attributeValue.charAt(firstUnwantedIndex); 74 while (charAtIndex == ' ' || charAtIndex == '\t' || charAtIndex == '\n' || 75 charAtIndex == '\r' || charAtIndex == '<' || charAtIndex == '>') { 76 firstUnwantedIndex--; 77 if (firstUnwantedIndex < 0) { 78 break; 79 } 80 charAtIndex = attributeValue.charAt(firstUnwantedIndex); 81 } 82 83 return attributeValue.substring(0, firstUnwantedIndex + 1); 84 } else { 85 return attributeValue; 86 } 87 } 88 89 95 public static String replaceEntityStringsWithChars(String value) { 96 StringBuffer buf = new StringBuffer (value); 97 for (int entity = 0; entity < knownEntityStrings.length; entity++) { 98 String curEntityString = knownEntityStrings[entity]; 99 int indexOfEntity = buf.toString().indexOf(curEntityString); 100 while (indexOfEntity != -1) { 101 buf.replace(indexOfEntity, indexOfEntity + curEntityString.length(), 102 new String (new char[]{knownEntityChars[entity]})); 103 indexOfEntity = buf.toString().indexOf(curEntityString); 104 } 105 } 106 107 return buf.toString(); 108 } 109 110 116 public static String replaceCharsWithEntityStrings(String value) { 117 if (value == null) { 118 return null; 119 } 120 StringBuffer replBuf = new StringBuffer (value.length()); 121 for (int ind = 0; ind < value.length(); ind++) { 122 boolean charReplaced = false; 123 char curChar = value.charAt(ind); 124 for (int entity = 0; entity < knownEntityChars.length; entity++) { 125 if (curChar == knownEntityChars[entity]) { 126 replBuf.append(knownEntityStrings[entity]); 127 charReplaced = true; 128 break; 129 } 130 } 131 132 if (!charReplaced) { 133 replBuf.append(curChar); 134 } 135 } 136 137 return replBuf.toString(); 138 } 139 140 } 141 | Popular Tags |