try {
InputSource is = new InputSource(new StringReader(rule));
CSSOMParser parser = new CSSOMParser();
parser.setParentStyleSheet(this);
CSSRule r = parser.parseRule(is);
if (getCssRules().getLength() > 0) {
// We need to check that this type of rule can legally go into
// the requested position.
int msg = -1;
if (r.getType() == CSSRule.CHARSET_RULE) {
// Index must be 0, and there can be only one charset rule
if (index != 0) {
msg = DOMExceptionImpl.CHARSET_NOT_FIRST;
} else if (getCssRules().item(0).getType()
== CSSRule.CHARSET_RULE) {
msg = DOMExceptionImpl.CHARSET_NOT_UNIQUE;
}
} else if (r.getType() == CSSRule.IMPORT_RULE) {
// Import rules must preceed all other rules (except
// charset rules)
if (index <= getCssRules().getLength()) {
for (int i = 0; i < index; i++) {