1 17 package org.alfresco.util; 18 19 import java.util.Collection ; 20 21 import org.alfresco.service.namespace.NamespaceException; 22 import org.alfresco.service.namespace.NamespacePrefixResolver; 23 import org.alfresco.service.namespace.NamespaceService; 24 import org.alfresco.service.namespace.QName; 25 26 import com.sun.org.apache.xerces.internal.util.XMLChar; 27 28 33 public class ISO9075 34 { 35 38 private static final int MASK = (1 << 4) - 1; 39 40 43 private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 44 'f' }; 45 46 50 private ISO9075() 51 { 52 super(); 53 } 54 55 61 public static String encode(String toEncode) 62 { 63 if ((toEncode == null) || (toEncode.length() == 0)) 64 { 65 return toEncode; 66 } 67 else if (XMLChar.isValidName(toEncode) && (toEncode.indexOf("_x") == -1) && (toEncode.indexOf(':') == -1)) 68 { 69 return toEncode; 70 } 71 else 72 { 73 StringBuilder builder = new StringBuilder (toEncode.length()); 74 for (int i = 0; i < toEncode.length(); i++) 75 { 76 char c = toEncode.charAt(i); 77 if (i == 0) 79 { 80 if (XMLChar.isNCNameStart(c)) 81 { 82 if (matchesEncodedPattern(toEncode, i)) 85 { 86 encode('_', builder); 88 } 89 else 90 { 91 builder.append(c); 93 } 94 } 95 else 96 { 97 encode(c, builder); 100 } 101 } 102 else if (!XMLChar.isNCName(c)) 103 { 104 encode(c, builder); 105 } 106 else 107 { 108 if (matchesEncodedPattern(toEncode, i)) 109 { 110 encode('_', builder); 112 } 113 else 114 { 115 builder.append(c); 116 } 117 } 118 } 119 return builder.toString(); 120 } 121 122 } 123 124 private static boolean matchesEncodedPattern(String string, int position) 125 { 126 return (string.length() >= position + 6) 127 && (string.charAt(position) == '_') && (string.charAt(position + 1) == 'x') 128 && isHexChar(string.charAt(position + 2)) && isHexChar(string.charAt(position + 3)) 129 && isHexChar(string.charAt(position + 4)) && isHexChar(string.charAt(position + 5)) 130 && (string.charAt(position + 6) == '_'); 131 } 132 133 private static boolean isHexChar(char c) 134 { 135 switch (c) 136 { 137 case '0': 138 case '1': 139 case '2': 140 case '3': 141 case '4': 142 case '5': 143 case '6': 144 case '7': 145 case '8': 146 case '9': 147 case 'a': 148 case 'b': 149 case 'c': 150 case 'd': 151 case 'e': 152 case 'f': 153 case 'A': 154 case 'B': 155 case 'C': 156 case 'D': 157 case 'E': 158 case 'F': 159 return true; 160 default: 161 return false; 162 } 163 } 164 165 public static String decode(String toDecode) 166 { 167 if ((toDecode == null) || (toDecode.length() < 7) || (toDecode.indexOf("_x") < 0)) 168 { 169 return toDecode; 170 } 171 StringBuffer decoded = new StringBuffer (); 172 for (int i = 0, l = toDecode.length(); i < l; i++) 173 { 174 if (matchesEncodedPattern(toDecode, i)) 175 { 176 decoded.append(((char) Integer.parseInt(toDecode.substring(i + 2, i + 6), 16))); 177 i += 6; 178 } 179 else 180 { 181 decoded.append(toDecode.charAt(i)); 182 } 183 } 184 return decoded.toString(); 185 } 186 187 private static void encode(char c, StringBuilder builder) 188 { 189 char[] buf = new char[] { '_', 'x', '0', '0', '0', '0', '_' }; 190 int charPos = 6; 191 do 192 { 193 buf[--charPos] = DIGITS[c & MASK]; 194 c >>>= 4; 195 } 196 while (c != 0); 197 builder.append(buf); 198 } 199 200 public static String getXPathName(QName qName, NamespacePrefixResolver nspr) 201 { 202 203 Collection <String > prefixes = nspr.getPrefixes(qName.getNamespaceURI()); 204 if (prefixes.size() == 0) 205 { 206 throw new NamespaceException("A namespace prefix is not registered for uri " + qName.getNamespaceURI()); 207 } 208 String prefix = prefixes.iterator().next(); 209 if (prefix.equals(NamespaceService.DEFAULT_PREFIX)) 210 { 211 return ISO9075.encode(qName.getLocalName()); 212 } 213 else 214 { 215 return prefix + ":" + ISO9075.encode(qName.getLocalName()); 216 } 217 218 } 219 220 public static String getXPathName(QName qName) 221 { 222 223 return "{" + qName.getNamespaceURI() + "}" + ISO9075.encode(qName.getLocalName()); 224 225 } 226 } 227 | Popular Tags |