📄 ptsprimitiveserializer.java
字号:
return buf.toString(); } private void encodePresence(StringBuilder buf, PrimitiveElement p) throws SerializerException { boolean hasQualifier = p.getChild(ImpsTags.Qualifier) != null; String presenceName = p.getTagName(); String presenceNameCode = getPresenceCode(presenceName); if (!mEncodePresenceValue) { encodeNoValuePresence(buf, p); } else { buf.append('('); buf.append(presenceNameCode); buf.append(','); if (hasQualifier) { buf.append(p.getChildContents(ImpsTags.Qualifier)); buf.append(','); } // All the presences with value have this kind of structure: // <name, qualifier, value> // And for the values, there are three different hierarchies: // 1. Simply use PresenceValue to indicate the value, most of the // presences has adapted this way. -> SingleValue // 2. Use special tags for multiple values of this presence, eg. ClientInfo // has adapted this way. -> MultiValue // 3. Has one or more children for the presence, and each child have // multiple values. eg. CommCap has adapted this way. -> ExtMultiValue if (isMultiValuePresence(presenceName)) { // condition 2: multiple value int emptyValueSize = hasQualifier ? 1 : 0; ArrayList<PrimitiveElement> children = p.getChildren(); if (children.size() > emptyValueSize) { buf.append('('); int childCount = children.size(); int j = 0; // used for first value check for (int i = 0; i < childCount; i++, j++) { PrimitiveElement value = children.get(i); if (ImpsTags.Qualifier.equals(value.getTagName())) { j--; continue; } if (j > 0) { buf.append(','); } buf.append('('); buf.append(getPresenceCode(value.getTagName())); buf.append(','); buf.append(PtsCodes.getPAValueCode(value.getContents())); buf.append(')'); } buf.append(')'); } } else if (isExtMultiValuePresence(presenceName)) { // condition 3: extended multiple value // TODO: Implementation } else { // Condition 1: single value if (p.getChild(ImpsTags.PresenceValue) == null) { throw new SerializerException("Can't find presence value for " + presenceName); } buf.append(PtsCodes.getPAValueCode(p.getChildContents(ImpsTags.PresenceValue))); } buf.append(')'); } } private void encodeNoValuePresence(StringBuilder buf, PrimitiveElement p) throws SerializerException { if (p.getChildCount() == 0) { buf.append(getPresenceCode(p.getTagName())); } else { ArrayList<PrimitiveElement> children = p.getChildren(); int childCount = children.size(); buf.append('('); buf.append(getPresenceCode(p.getTagName())); buf.append(",("); for (int i = 0; i < childCount; i++) { if (i > 0) { buf.append(','); } encodeNoValuePresence(buf, children.get(i)); } buf.append("))"); } } private String getPresenceCode(String tagname) throws SerializerException { String code = PtsCodes.getPresenceAttributeCode(tagname); if (code == null) { throw new SerializerException("Unsupport presence attribute: " + tagname); } return code; } private boolean isMultiValuePresence(String presenceName) { if (ImpsTags.ClientInfo.equals(presenceName)) { return true; } // TODO: Add more supported extended multiple presence here return false; } private boolean isExtMultiValuePresence(String presenceName) { // TODO: Add supported extended multiple presence here return false; } } static class ClientIdEncoder extends ElemValueEncoder { @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { String value = elem.getChildContents(ImpsTags.URL); if (value == null) { value = elem.getChildContents(ImpsTags.MSISDN); } return escapeValueString(value); } } static class CapabilityEncoder extends ElemValueEncoder { @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { ArrayList<PrimitiveElement> caps = elem.getChildren(); int i, len; StringBuilder result = new StringBuilder(); result.append('('); for (i = 0, len = caps.size(); i < len; i++) { PrimitiveElement capElem = caps.get(i); String capName = capElem.getTagName(); String capValue = capElem.getContents(); if (i > 0) { result.append(','); } if (!appendNameAndValue(result, capName, capValue, PtsCodes.sCapElementToCode, PtsCodes.sCapValueToCode, ImpsTags.SupportedCIRMethod.equals(capName))) { result.deleteCharAt(result.length() - 1); } } result.append(')'); return result.toString(); } } static class ServiceTreeEncoder extends ElemValueEncoder { @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { StringBuilder buf = new StringBuilder(); buf.append('('); appendFeature(buf, elem.getFirstChild()); buf.append(')'); return buf.toString(); } private void appendFeature(StringBuilder buf, PrimitiveElement elem) throws SerializerException { int childCount = elem.getChildCount(); if (childCount > 0) { ArrayList<PrimitiveElement> children = elem.getChildren(); for (int i = 0; i < childCount; i++) { appendFeature(buf, children.get(i)); } } else { String code = PtsCodes.getServiceTreeCode(elem.getTagName()); if (code == null) { throw new SerializerException("Invalid service tree tag:" + elem.getTagName()); } if (buf.length() > 1) { buf.append(','); } buf.append(code); } } } static class ResultEncoder extends ElemValueEncoder { @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { String code = elem.getChildContents(ImpsTags.Code); String desc = elem.getChildContents(ImpsTags.Description); // Client never sends partial success result, the DetailedResult is // ignored. if (desc == null) { return code; } else { StringBuilder res = new StringBuilder(); appendPairValue(res, code, escapeValueString(desc)); return res.toString(); } } } static class NickListEncoder extends ElemValueEncoder { @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { StringBuilder buf = new StringBuilder(); ArrayList<PrimitiveElement> children = elem.getChildren(); int count = children.size(); buf.append('('); for (int i = 0; i < count; i++) { PrimitiveElement child = children.get(i); String tagName = child.getTagName(); String nickName = null; String userId = null; if (tagName.equals(ImpsTags.NickName)) { nickName = child.getChildContents(ImpsTags.Name); userId = child.getChildContents(ImpsTags.UserID); } else if (tagName.equals(ImpsTags.UserID)) { userId = child.getContents(); } if (i > 0) { buf.append(','); } if (nickName != null) { nickName = escapeValueString(nickName); } appendPairValue(buf, nickName, escapeValueString(userId)); } buf.append(')'); return buf.toString(); } } static class ProperitiesEncoder extends ElemValueEncoder { private HashMap<String, String> mPropNameCodes; public ProperitiesEncoder(HashMap<String, String> propNameCodes) { mPropNameCodes = propNameCodes; } @Override public String encodeValue(Primitive p, PrimitiveElement elem) throws SerializerException { ArrayList<PrimitiveElement> props = elem.getChildren(); StringBuilder result = new StringBuilder(); result.append('('); int count = props.size(); for (int i = 0; i < count; i++) { PrimitiveElement property = props.get(i); String name; String value; if (property.getTagName().equals(ImpsTags.Property)) { name = property.getChildContents(ImpsTags.Name); value = property.getChildContents(ImpsTags.Value); } else { name = property.getTagName(); value = property.getContents(); } if (i > 0) { result.append(','); } appendNameAndValue(result, name, value, mPropNameCodes, null, false); } result.append(')'); return result.toString(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -