PtsPrimitiveSerializerpublic class PtsPrimitiveSerializer extends Object implements PrimitiveSerializer
Fields Summary |
---|
private final String | mPreampleHead | private static final Pattern | sTxIdPattern | private static final Pattern | sCharsToBeQuoted |
Constructors Summary |
---|
public PtsPrimitiveSerializer(com.android.im.imps.ImpsConstants.ImpsVersion impsVersion)
if (impsVersion == ImpsVersion.IMPS_VERSION_11) {
mPreampleHead = "WV11";
}else if (impsVersion == ImpsVersion.IMPS_VERSION_12) {
mPreampleHead = "WV12";
} else if (impsVersion == ImpsVersion.IMPS_VERSION_13) {
mPreampleHead = "WV13";
} else {
throw new SerializerException("Unsupported IMPS version");
}
|
Methods Summary |
---|
static boolean | appendNameAndValue(java.lang.StringBuilder buf, java.lang.String name, java.lang.String value, java.util.HashMap nameCodes, java.util.HashMap valueCodes, boolean ignoreUnsupportedValue)Appends a name and value pair like "(,)".
String nameCode = nameCodes.get(name);
if (nameCode == null) {
ImpsLog.log("PTS: Ignoring value " + name);
return false;
}
String valueCode = null;
if (valueCodes != null) {
valueCode = valueCodes.get(value);
}
if (valueCode != null) {
value = valueCode;
} else {
if (ignoreUnsupportedValue) {
return false;
}
value = escapeValueString(value);
}
appendPairValue(buf, nameCode, value);
return true;
| static void | appendPairValue(java.lang.StringBuilder buf, java.lang.String first, java.lang.String second)
buf.append('(");
if (first != null) {
buf.append(first);
}
buf.append(',");
buf.append(second);
buf.append(')");
| static java.lang.String | escapeValueString(java.lang.String contents)
Matcher m = sCharsToBeQuoted.matcher(contents);
if (m.find()) {
if (contents.indexOf('"") != -1) {
contents = contents.replace("\"", "\"\"");
}
return "\"" + contents + "\"";
}
return contents;
| public void | serialize(Primitive p, java.io.OutputStream out)
String txId = p.getTransactionID();
if (txId == null) {
if (!ImpsTags.Polling_Request.equals(p.getType())) {
throw new SerializerException("null Transaction-ID for non polling request");
}
// FIXME: what should this be? Temporarily use 0
txId = "0";
} else {
Matcher m = sTxIdPattern.matcher(txId);
if (!m.matches()) {
throw new SerializerException(
"Transaction-ID must be in range 0-999 without preceding zero");
}
}
// TODO: use buffered writer?
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.write(mPreampleHead);
String code = PtsCodes.getTxCode(p.getType());
if (code == null) {
throw new SerializerException("Unsupported transaction type "
+ p.getType());
}
writer.write(code);
writer.write(txId);
if (p.getSessionId() != null) {
writer.write(" SI=");
writer.write(p.getSessionId());
}
PrimitiveElement content = p.getContentElement();
if (content != null && content.getChildCount() > 0) {
ArrayList<PrimitiveElement> infoElems = content.getChildren();
ArrayList<String> users = new ArrayList<String>();
ArrayList<String> lists = new ArrayList<String>();
int len = infoElems.size();
for (int i = 0; i < len; i++) {
PrimitiveElement elem = infoElems.get(i);
String elemName = elem.getTagName();
// workaround for multiple elements
if (ImpsTags.User.equals(elemName)) {
users.add(elem.getChildContents(ImpsTags.UserID));
continue;
} else if (ImpsTags.UserID.equals(elemName)) {
users.add(elem.getContents());
continue;
} else if (ImpsTags.ContactList.equals(elemName)) {
lists.add(elem.getContents());
continue;
}
String elemCode = PtsCodes.getElementCode(elemName, p.getType());
if (elemCode == null) {
throw new SerializerException("Don't know how to encode element "
+ elemName);
}
writer.write(' ");
writer.write(elemCode);
// so far all top level information elements have values.
writer.write('=");
String value;
ElemValueEncoder encoder = ElemValueEncoder.getEncoder(elemName);
if (encoder == null) {
// default simple value
value = escapeValueString(elem.getContents());
} else {
value = encoder.encodeValue(p, elem);
}
if (value == null) {
throw new SerializerException("Empty value for element "
+ elemName);
}
writer.write(value);
}
writeMultiValue(writer, PtsCodes.getElementCode(ImpsTags.UserID, p.getType()), users);
writeMultiValue(writer, PtsCodes.getElementCode(ImpsTags.ContactList, p.getType()), lists);
}
writer.close();
| private void | writeMultiValue(java.io.Writer writer, java.lang.String code, java.util.ArrayList values)
if (values.size() == 0) {
return;
}
writer.write(' ");
writer.write(code);
writer.write('=");
if (values.size() == 1) {
writer.write(escapeValueString(values.get(0)));
} else {
writer.write('(");
int valueCount = values.size();
for (int i = 0; i < valueCount; i++) {
if (i > 0) {
writer.write(',");
}
writer.write(escapeValueString(values.get(i)));
}
writer.write(')");
}
|
|