1 package net.sf.saxon.functions; 2 import net.sf.saxon.charcode.UnicodeCharacterSet; 3 import net.sf.saxon.expr.XPathContext; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.om.FastStringBuffer; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.AtomicValue; 8 import net.sf.saxon.value.BooleanValue; 9 import net.sf.saxon.value.StringValue; 10 11 15 16 public class EscapeURI extends SystemFunction { 17 18 public static final int ESCAPE = 0; 19 public static final int ENCODE_FOR_URI = 1; 20 public static final int IRI_TO_URI = 2; 21 public static final int HTML_URI = 3; 23 26 27 public Item evaluateItem(XPathContext c) throws XPathException { 28 Item item = argument[0].evaluateItem(c); 29 if (item == null) { 30 return StringValue.EMPTY_STRING; 31 } 32 CharSequence s = item.getStringValueCS(); 33 boolean escapeReserved = false; 34 switch (operation) { 35 case ESCAPE: 36 AtomicValue av2 = (AtomicValue)argument[1].evaluateItem(c); 37 escapeReserved = ((BooleanValue)av2.getPrimitiveValue()).getBooleanValue(); 38 break; 39 case ENCODE_FOR_URI: 40 escapeReserved = true; 41 break; 42 case IRI_TO_URI: 43 escapeReserved = false; 44 break; 45 } 46 return StringValue.makeStringValue(escape(s, escapeReserved)); 47 } 48 49 50 52 public static CharSequence escape(CharSequence s, boolean escapeReserved) { 53 FastStringBuffer sb = new FastStringBuffer(s.length()); 54 for (int i=0; i<s.length(); i++) { 55 char c = s.charAt(i); 56 if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')) { 57 sb.append(c); 58 } else if (c<=0x20 || c>=0x7f) { 59 escapeChar(c, ((i+1)<s.length() ? s.charAt(i+1) : ' '), sb); 60 } else if (escapeReserved) { 61 if ("-_.!~*'()%".indexOf(c)>=0) { 62 sb.append(c); 63 } else { 64 escapeChar(c, ' ', sb); 65 } 66 } else { 67 if ("-_.!~*'()%;/?:@&=+$,#[]".indexOf(c)>=0) { 68 sb.append(c); 69 } else { 70 escapeChar(c, ' ', sb); 71 } 72 } 73 } 74 return sb; 75 } 76 77 private static final String hex = "0123456789ABCDEF"; 78 79 private static void escapeChar(char c, char c2, FastStringBuffer sb) { 80 byte[] array = new byte[4]; 81 int used = UnicodeCharacterSet.getUTF8Encoding(c, c2, array); 82 for (int b=0; b<used; b++) { 83 int v = (array[b]>=0 ? array[b] : 256 + array[b]); 84 sb.append('%'); 85 sb.append(hex.charAt(v/16)); 86 sb.append(hex.charAt(v%16)); 87 } 88 } 89 90 } 91 92 93 94 | Popular Tags |