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