com.dianping.cat.alarm.rule.transform.DefaultXmlBuilder Maven / Gradle / Ivy
The newest version!
package com.dianping.cat.alarm.rule.transform;
import static com.dianping.cat.alarm.rule.Constants.ATTR_ALERTTYPE;
import static com.dianping.cat.alarm.rule.Constants.ATTR_AVAILABLE;
import static com.dianping.cat.alarm.rule.Constants.ATTR_ENDTIME;
import static com.dianping.cat.alarm.rule.Constants.ATTR_ID;
import static com.dianping.cat.alarm.rule.Constants.ATTR_METRICITEMTEXT;
import static com.dianping.cat.alarm.rule.Constants.ATTR_MINUTE;
import static com.dianping.cat.alarm.rule.Constants.ATTR_MONITORAVG;
import static com.dianping.cat.alarm.rule.Constants.ATTR_MONITORCOUNT;
import static com.dianping.cat.alarm.rule.Constants.ATTR_MONITORSUM;
import static com.dianping.cat.alarm.rule.Constants.ATTR_PRODUCTTEXT;
import static com.dianping.cat.alarm.rule.Constants.ATTR_STARTTIME;
import static com.dianping.cat.alarm.rule.Constants.ATTR_TEXT;
import static com.dianping.cat.alarm.rule.Constants.ATTR_TITLE;
import static com.dianping.cat.alarm.rule.Constants.ATTR_TYPE;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_CONDITION;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_CONFIG;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_METRIC_ITEM;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_MONITOR_RULES;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_RULE;
import static com.dianping.cat.alarm.rule.Constants.ENTITY_SUB_CONDITION;
import java.lang.reflect.Array;
import java.util.Collection;
import com.dianping.cat.alarm.rule.IEntity;
import com.dianping.cat.alarm.rule.IVisitor;
import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
public class DefaultXmlBuilder implements IVisitor {
private IVisitor m_visitor = this;
private int m_level;
private StringBuilder m_sb;
private boolean m_compact;
public DefaultXmlBuilder() {
this(false);
}
public DefaultXmlBuilder(boolean compact) {
this(compact, new StringBuilder(4096));
}
public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
m_compact = compact;
m_sb = sb;
m_sb.append("\r\n");
}
public String buildXml(IEntity> entity) {
entity.accept(m_visitor);
return m_sb.toString();
}
protected void endTag(String name) {
m_level--;
indent();
m_sb.append("").append(name).append(">\r\n");
}
protected String escape(Object value) {
return escape(value, false);
}
protected String escape(Object value, boolean text) {
if (value == null) {
return null;
}
String str = toString(value);
int len = str.length();
StringBuilder sb = new StringBuilder(len + 16);
for (int i = 0; i < len; i++) {
final char ch = str.charAt(i);
switch (ch) {
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
case '"':
if (!text) {
sb.append(""");
break;
}
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
protected void indent() {
if (!m_compact) {
for (int i = m_level - 1; i >= 0; i--) {
m_sb.append(" ");
}
}
}
protected void startTag(String name) {
startTag(name, false, null);
}
protected void startTag(String name, boolean closed, java.util.Map dynamicAttributes, Object... nameValues) {
startTag(name, null, closed, dynamicAttributes, nameValues);
}
protected void startTag(String name, java.util.Map dynamicAttributes, Object... nameValues) {
startTag(name, null, false, dynamicAttributes, nameValues);
}
protected void startTag(String name, Object text, boolean closed, java.util.Map dynamicAttributes, Object... nameValues) {
indent();
m_sb.append('<').append(name);
int len = nameValues.length;
for (int i = 0; i + 1 < len; i += 2) {
Object attrName = nameValues[i];
Object attrValue = nameValues[i + 1];
if (attrValue != null) {
m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
}
}
if (dynamicAttributes != null) {
for (java.util.Map.Entry e : dynamicAttributes.entrySet()) {
m_sb.append(' ').append(e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
}
}
if (text != null && closed) {
m_sb.append('>');
m_sb.append(escape(text, true));
m_sb.append("").append(name).append(">\r\n");
} else {
if (closed) {
m_sb.append('/');
} else {
m_level++;
}
m_sb.append(">\r\n");
}
}
@SuppressWarnings("unchecked")
protected String toString(Object value) {
if (value instanceof String) {
return (String) value;
} else if (value instanceof Collection) {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy