📄 ptsprimitiveparser.java
字号:
PrimitiveElement presence = new PrimitiveElement(ImpsTags.Presence); if (valueGroup.get(0).mStrValue == null) { throw new ParserException("UserID must have string value!"); } presence.addChild(ImpsTags.UserID, valueGroup.get(0).mStrValue); if (valueGroup.size() > 1) { // has presence sub list presence.addChild(translatePresenceSubList(valueGroup.get(1))); } return presence; } private static PrimitiveElement translatePresenceSubList(ParamValue value) throws ParserException { checkGroupValue(value.mValueGroup, UNCERTAIN_GROUP_SIZE); PrimitiveElement presenceSubList = new PrimitiveElement(ImpsTags.PresenceSubList); int groupSize = value.mValueGroup.size(); for (int i = 0; i < groupSize; i++) { ParamValue v = value.mValueGroup.get(i); if (v.mStrValue != null) { throw new ParserException("Unexpected string value for presence attribute"); } presenceSubList.addChild(translatePresenceAttribute(v.mValueGroup)); } return presenceSubList; } // <attribute>[,<qualifier>][,<value>] // <attribute>[,<qualifier>,<sub-attribute>] private static PrimitiveElement translatePresenceAttribute( ArrayList<ParamValue> valueGroup) throws ParserException { String type = valueGroup.get(0).mStrValue; if (type == null) { return null; } String tag = PtsCodes.getPresenceAttributeElement(type); if (tag == null) { return null; } PrimitiveElement paElem = new PrimitiveElement(tag); if (valueGroup.size() == 2) { // no qualifier translateAttributeValue(paElem, valueGroup.get(1), false); }else if (valueGroup.size() == 3) { // has qualifier, and it should has no group value ParamValue qualifierValue = valueGroup.get(1); if (qualifierValue.mStrValue == null) { throw new ParserException("Qualifier value can't be group value!"); } if (!"".equals(qualifierValue.mStrValue)) { paElem.addChild(ImpsTags.Qualifier, qualifierValue.mStrValue); } translateAttributeValue(paElem, valueGroup.get(2), true); } else { return null; } return paElem; } private static void translateAttributeValue(PrimitiveElement paElem, ParamValue v, boolean hasQualifier) throws ParserException { if (v.mStrValue == null) { // sub-attribute as value checkGroupValue(v.mValueGroup, UNCERTAIN_GROUP_SIZE); if (v.mValueGroup.get(0).mStrValue != null) { paElem.addChild(translatePresenceAttribute(v.mValueGroup)); } else { int groupSize = v.mValueGroup.size(); for (int i = 0; i < groupSize; i++) { ParamValue value = v.mValueGroup.get(i); if (value.mStrValue != null) { throw new ParserException("Presence Attribute value error!"); } checkGroupValue(value.mValueGroup, UNCERTAIN_GROUP_SIZE); paElem.addChild(translatePresenceAttribute(value.mValueGroup)); } } } else { // single simple value if (hasQualifier) { paElem.addChild(ImpsTags.PresenceValue, PtsCodes.getPAValue(v.mStrValue)); } else { paElem.setContents(PtsCodes.getPAValue(v.mStrValue)); } } } private static void checkGroupValue(ArrayList<ParamValue> valueGroup, int expectedGroupSize) throws ParserException { if (valueGroup == null || (expectedGroupSize != UNCERTAIN_GROUP_SIZE && valueGroup.size() != expectedGroupSize)) { throw new ParserException("Invalid group value!"); } int groupSize = valueGroup.size(); for (int i = 0; i < groupSize; i++) { if (valueGroup.get(i) == null) { throw new ParserException("Invalid group value!"); } } } private static PrimitiveElement translateCapabilityList(ParamValue elemValue) throws ParserException { PrimitiveElement elem = new PrimitiveElement(ImpsTags.AgreedCapabilityList); ArrayList<ParamValue> params = elemValue.mValueGroup; if (params != null) { checkGroupValue(params, UNCERTAIN_GROUP_SIZE); int paramsSize = params.size(); for (int i = 0; i < paramsSize; i++) { ArrayList<ParamValue> capElemGroup = params.get(i).mValueGroup; checkGroupValue(capElemGroup, 2); String capElemCode = capElemGroup.get(0).mStrValue; String capElemName; if (capElemCode == null || (capElemName = PtsCodes.getCapElement(capElemCode)) == null) { throw new ParserException("Unknown capability element " + capElemCode); } String capElemValue = capElemGroup.get(1).mStrValue; if (capElemValue == null) { throw new ParserException("Illegal capability value for " + capElemCode); } capElemValue = PtsCodes.getCapValue(capElemValue); elem.addChild(capElemName, capElemValue); } } return elem; } private static PrimitiveElement translateServiceTree(String elemCode, ParamValue elemValue) throws ParserException { String elemName = PtsCodes.getElement(elemCode); PrimitiveElement elem = new PrimitiveElement(elemName); // TODO: translate the service tree. return elem; } private static PrimitiveElement translateSimpleElem(String elemCode, ParamValue value) throws ParserException { String elemName = PtsCodes.getElement(elemCode); if (elemName == null) { throw new ParserException("Unrecognized parameter " + elemCode); } PrimitiveElement elem = new PrimitiveElement(elemName); if (value.mStrValue != null) { elem.setContents(value.mStrValue); } else { throw new ParserException("Don't know how to handle parameters for " + elemName); } return elem; } private HashMap<String, ParamValue> parseParams() throws ParserException { int pos = mPos; StringBuilder buf = mStringBuf; int len = buf.length(); HashMap<String, ParamValue> ret = new HashMap<String, ParamValue>(); String paramName; ParamValue paramValue; while (pos < len) { int nameStart = pos; while (pos < len) { char ch = buf.charAt(pos); if (ch == ' ' || ch == '=') { break; } pos++; } if (nameStart == pos) { throw new ParserException("Missing parameter name near " + pos); } paramName = buf.substring(nameStart, pos); if (pos < len && buf.charAt(pos) == '=') { pos++; mPos = pos; paramValue = parseParamValue(); pos = mPos; } else { paramValue = null; } ret.put(paramName, paramValue); if (pos < len) { // more parameters ahead match(' '); pos = mPos; } } return ret; } private ParamValue parseParamValue() throws ParserException { int pos = mPos; StringBuilder buf = mStringBuf; int len = buf.length(); if (pos == len) { throw new ParserException("Missing parameter value near " + pos); } ParamValue value = new ParamValue(); char ch = buf.charAt(pos); if (ch == '(') { // value list pos++; ArrayList<ParamValue> valueGroup = new ArrayList<ParamValue>(); while (pos < len) { mPos = pos; valueGroup.add(parseParamValue()); pos = mPos; if (pos == len) { throw new ParserException("Unexpected parameter end"); } if (buf.charAt(pos) != ',') { break; } pos++; } mPos = pos; match(')'); if (valueGroup.isEmpty()) { throw new ParserException("Empty value group near " + mPos); } value.mValueGroup = valueGroup; } else { // single value if (ch == '"') { // quoted value pos++; StringBuilder escapedValue = new StringBuilder(); boolean quotedEnd = false; while (pos < len) { ch = buf.charAt(pos); pos++; if (ch == '"') { if (pos < len && buf.charAt(pos) == '"') { // "doubled" quote pos++; } else { quotedEnd = true; break; } } escapedValue.append(ch); } if (!quotedEnd) { throw new ParserException("Unexpected quoted parameter end"); } value.mStrValue = escapedValue.toString(); } else { int valueStart = pos; while (pos < len) { ch = buf.charAt(pos); if (ch == ',' || ch == ')' || ch == ' ') { break; } if ("\"(=&".indexOf(ch) != -1) { throw new ParserException("Special character " + ch + " must be quoted"); } pos++; } value.mStrValue = buf.substring(valueStart, pos); } mPos = pos; } return value; } private void match(char c) throws ParserException { if (mStringBuf.charAt(mPos) != c) { throw new ParserException("Expected " + c + " at pos " + mPos); } mPos++; } /** * Detect if this short message is a PTS encoded WV-primitive. */ public static boolean isPtsPrimitive(CharSequence msg) { if (msg == null) { return false; } Matcher m = sPreamplePattern.matcher(msg); return m.matches(); } static final class ParamValue { public String mStrValue; public ArrayList<ParamValue> mValueGroup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -