// ---- 标签属性处理 ----
List<String> directiveNames = new ArrayList<String>();
List<String> directiveValues = new ArrayList<String>();
List<Attribute> directiveAttributes = new ArrayList<Attribute>();
// 迭代标签属性,查找指令属性
Attributes attributes = element.getAttributes();
if (attributes != null) {
for (Attribute attribute : attributes) {
if (attribute != null) {
String name = attribute.getName();
if (name != null && (isDirective(name) || (attributeNamespace != null && name.startsWith(attributeNamespace)) && isDirective(name.substring(attributeNamespace.length())))) { // 识别名称空间
String directiveName = attributeNamespace != null ? name.substring(attributeNamespace.length()) : name;
String value = attribute.getValue();
directiveNames.add(directiveName);
directiveValues.add(value);
directiveAttributes.add(attribute);
}
}
}
}
// ---- 指令处理 ----
if (directiveNames.size() > 0) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < directiveNames.size(); i++) { // 按顺序添加块指令
String directiveName = (String) directiveNames.get(i);
String directiveValue = (String) directiveValues.get(i);
buf.append("#");
buf.append(directiveName);
buf.append("(");
buf.append(directiveValue);
buf.append(")");
}
document.insert(element.getBegin(), buf.toString()); // 插入块指令
}
// ---- 指令属性处理 ----
for (int i = 0; i < directiveAttributes.size(); i++) {
Attribute attribute = (Attribute) directiveAttributes.get(i);
document.remove(new Segment(source, attribute.getBegin() - 1, attribute.getEnd())); // 移除属性
}
if (attributes != null) {
//检查扩展的ifattr指令
for (Attribute attribute : attributes) {
if (attribute != null) {
String name = attribute.getName();
if (ifattr.equals(name)) {
String val = attribute.getValue();
String[] arr = val.split(",");
String attrName = arr[0].trim();
String expression = arr[1].trim();
//修改原attribute
Attribute oriattr = attributes.get(attrName);
if (oriattr != null) {
String buf = String.format("#if(%s)%s=\"%s\"#end()", expression, oriattr.getName(), oriattr.getValue());
document.replace(new Segment(source, oriattr.getBegin(), oriattr.getEnd()), buf);
document.remove(new Segment(source, attribute.getBegin(), attribute.getEnd())); // 移除ifattr控制属性
}
}
}
}
//检查扩展的setattr指令
for (Attribute attribute : attributes) {
if (attribute != null) {
String name = attribute.getName();
if (setattr.equals(name)) {
String val = attribute.getValue();
String[] arr = val.split(",");
String attrName = arr[0].trim();
String expression = arr[1].trim();
//将控制指令直接替换为动态属性赋值
Attribute oriattr = attributes.get(attrName);
String buf = String.format("%s=\"%s\"", attrName, expression);
document.replace(new Segment(source, attribute.getBegin(), attribute.getEnd()), buf);
//如果有已经存在的静态属性,直接删去即可
if (oriattr != null) {