XmlPrimitiveSerializerpublic class XmlPrimitiveSerializer extends Object implements PrimitiveSerializer
Fields Summary |
---|
private String | mVersionNs | private String | mTransacNs |
Constructors Summary |
---|
public XmlPrimitiveSerializer(String versionNs, String transacNs)
mVersionNs = versionNs;
mTransacNs = transacNs;
|
Methods Summary |
---|
public void | serialize(Primitive primitive, java.io.OutputStream out)
try {
Writer writer =
new BufferedWriter(new OutputStreamWriter(out, "UTF-8"), 8192);
PrimitiveElement elem = primitive.createMessage(mVersionNs,
mTransacNs);
writeElement(writer, elem);
writer.flush();
} catch (UnsupportedEncodingException e) {
// Impossible. UTF-8 is always supported
ImpsLog.logError(e);
}
| private void | writeElement(java.io.Writer writer, PrimitiveElement element)
writer.write('<");
writer.write(element.getTagName());
Map<String, String> attrMap = element.getAttributes();
if(attrMap != null && attrMap.size() > 0) {
for (Map.Entry<String, String> entry : attrMap.entrySet()) {
writer.write(' ");
writer.write(entry.getKey());
writer.write("=\"");
writeEncoded(writer, entry.getValue());
writer.write('"");
}
}
if (element.getContents() != null) {
writer.write('>");
writeEncoded(writer, element.getContents());
writer.write("</");
writer.write(element.getTagName());
writer.write('>");
} else if (element.getChildCount() > 0) {
writer.write('>");
for (PrimitiveElement child : element.getChildren()) {
writeElement(writer, child);
}
writer.write("</");
writer.write(element.getTagName());
writer.write('>");
} else {
writer.write("/>");
}
| private void | writeEncoded(java.io.Writer writer, java.lang.String str)
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
switch (ch) {
case '<":
writer.write("<");
break;
case '>":
writer.write(">");
break;
case '&":
writer.write("&");
break;
case '"":
writer.write(""");
break;
case '\'":
writer.write("'");
break;
default:
writer.write(ch);
}
}
|
|