* @param key the attribute key
* @return the value corresponding to the attribute key.
*/
public static String getPIAttribute(String pi, String key)
{
CharBuffer nameBuf = new CharBuffer();
CharBuffer valueBuf = new CharBuffer();
int i = 0;
int length = pi.length();;
while (i < length) {
int ch = 0;
for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) {
}
nameBuf.clear();
for (; i < length && XmlChar.isNameChar(ch = pi.charAt(i)); i++)
nameBuf.append((char) ch);
for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) {
}
if (i < length && ch != '=') {
if (nameBuf.length() == 0)
return null;
else if (nameBuf.toString().equals(key))
return nameBuf.toString();
else
continue;
}
i++;
for (; i < length && XmlChar.isWhitespace(ch = pi.charAt(i)); i++) {
}
// Parse the attribute value: '.*' or ".*" or \w+
valueBuf.clear();
if (ch == '\'') {
i++;
for (; i < length && (ch = pi.charAt(i)) != '\''; i++)
valueBuf.append((char) ch);
i++;
}
else if (ch == '\"') {
i++;
for (; i < length && (ch = pi.charAt(i)) != '\"'; i++)
valueBuf.append((char) ch);
i++;
}
else if (XmlChar.isNameChar(ch)) {
for (; i < length && XmlChar.isNameChar(ch = pi.charAt(i)); i++)
valueBuf.append((char) ch);
}
else
return null; // XXX: should throw an exception?
String name = nameBuf.toString();
if (name.equals(key))
return valueBuf.toString();
}
return null;
}