1 package net.sf.saxon.om; 2 3 4 9 10 11 public class ProcInstParser { 12 13 16 17 private ProcInstParser() { 18 } 19 20 26 27 public static String getPseudoAttribute(String content, String name) { 28 29 int pos = 0; 30 while (pos <= content.length()-4) { int nextQuote = -1; 32 for (int q=pos; q<content.length(); q++) { 33 if (content.charAt(q)=='"' || content.charAt(q)=='\'') { 34 nextQuote = q; 35 break; 36 } 37 } 38 if (nextQuote < 0) return null; 39 40 int closingQuote = content.indexOf(content.charAt(nextQuote), nextQuote+1); 41 if (closingQuote<0) return null; 42 int nextName = content.indexOf(name, pos); 43 if (nextName < 0) return null; 44 if (nextName < nextQuote) { 45 boolean found = true; 47 for (int s = nextName + name.length(); s < nextQuote; s++) { 48 char c = content.charAt(s); 49 if (!Character.isWhitespace(c) && c!='=') { 50 found=false; 51 break; 52 } 53 } 54 if (found) { 55 String val = content.substring(nextQuote+1, closingQuote); 56 return unescape(val); 57 } 58 } 59 pos = closingQuote + 1; 60 } 61 return null; 62 } 63 64 67 68 private static String unescape(String value) { 69 if (value.indexOf('&')<0) return value; 70 FastStringBuffer sb = new FastStringBuffer(value.length()); 71 for (int i=0; i<value.length(); i++) { 72 char c = value.charAt(i); 73 if (c=='&') { 74 if (i+2 < value.length() && value.charAt(i+1)=='#') { 75 if (value.charAt(i+2)=='x') { 76 int x = i+3; 77 int charval = 0; 78 while (x<value.length() && value.charAt(x)!=';') { 79 int digit = "0123456789abcdef".indexOf(value.charAt(x)); 80 if (digit<0) { 81 digit = "0123456789ABCDEF".indexOf(value.charAt(x)); 82 } 83 if (digit<0) { 84 return null; 85 } 86 charval = charval * 16 + digit; 87 x++; 88 } 89 char hexchar = (char)charval; 90 sb.append(hexchar); 91 i=x; 92 } else { 93 int x = i+2; 94 int charval = 0; 95 while (x<value.length() && value.charAt(x)!=';') { 96 int digit = "0123456789".indexOf(value.charAt(x)); 97 if (digit<0) { 98 return null; 99 } 100 charval = charval * 10 + digit; 101 x++; 102 } 103 char decchar = (char)charval; 104 sb.append(decchar); 105 i=x; 106 } 107 } else if (value.substring(i+1).startsWith("lt;")) { 108 sb.append('<'); 109 i+=3; 110 } else if (value.substring(i+1).startsWith("gt;")) { 111 sb.append('>'); 112 i+=3; 113 } else if (value.substring(i+1).startsWith("amp;")) { 114 sb.append('&'); 115 i+=4; 116 } else if (value.substring(i+1).startsWith("quot;")) { 117 sb.append('"'); 118 i+=5; 119 } else if (value.substring(i+1).startsWith("apos;")) { 120 sb.append('\''); 121 i+=5; 122 } else { 123 return null; 124 } 125 126 } else { 127 sb.append(c); 128 } 129 } 130 return sb.toString(); 131 } 132 133 } 134 135 136 | Popular Tags |