📄 xmlexporter.cs
字号:
exportCompoundKey(body, offs, size, type);
}
else
{
writer.Write(" key=\"");
exportKey(body, offs, size, type);
writer.Write("\"");
}
writer.Write("/>\n");
}
internal void indentation(int indent)
{
while (--indent >= 0)
{
writer.Write(' ');
}
}
internal void exportChar(char ch)
{
switch (ch)
{
case '<':
writer.Write("<");
break;
case '>':
writer.Write(">");
break;
case '&':
writer.Write("&");
break;
case '"':
writer.Write(""");
break;
default:
writer.Write(ch);
break;
}
}
internal int exportString(byte[] body, int offs)
{
int len = Bytes.unpack4(body, offs);
offs += 4;
if (len >= 0)
{
writer.Write("\"");
while (--len >= 0)
{
exportChar((char) Bytes.unpack2(body, offs));
offs += 2;
}
writer.Write("\"");
}
else if (len < -1)
{
writer.Write("\"");
string s;
if (storage.encoding != null)
{
s = storage.encoding.GetString(body, offs, -len-2);
}
else
{
s = Encoding.Default.GetString(body, offs, -len-2);
}
offs -= len+2;
for (int i = 0, n = s.Length; i < n; i++)
{
exportChar(s[i]);
}
writer.Write("\"");
}
else
{
writer.Write("null");
}
return offs;
}
internal static char[] hexDigit = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
internal void exportRef(int oid)
{
writer.Write("<ref id=\"" + oid + "\"/>");
if (oid != 0 && (exportedBitmap[oid >> 5] & (1 << (oid & 31))) == 0)
{
markedBitmap[oid >> 5] |= 1 << (oid & 31);
}
}
internal int exportBinary(byte[] body, int offs)
{
int len = Bytes.unpack4(body, offs);
offs += 4;
if (len < 0)
{
if (len == -2-(int)ClassDescriptor.FieldType.tpObject)
{
exportRef(Bytes.unpack4(body, offs));
offs += 4;
}
else if (len < -1)
{
writer.Write("\"#");
writer.Write(hexDigit[-2-len]);
len = ClassDescriptor.Sizeof[-2-len];
while (--len >= 0)
{
byte b = body[offs++];
writer.Write(hexDigit[(b >> 4) & 0xF]);
writer.Write(hexDigit[b & 0xF]);
}
writer.Write('\"');
}
else
{
writer.Write("null");
}
}
else
{
writer.Write('\"');
while (--len >= 0)
{
byte b = body[offs++];
writer.Write(hexDigit[(b >> 4) & 0xF]);
writer.Write(hexDigit[b & 0xF]);
}
writer.Write('\"');
}
return offs;
}
internal int exportObject(ClassDescriptor desc, byte[] body, int offs, int indent)
{
ClassDescriptor.FieldDescriptor[] all = desc.allFields;
for (int i = 0, n = all.Length; i < n; i++)
{
ClassDescriptor.FieldDescriptor fd = all[i];
FieldInfo f = fd.field;
indentation(indent);
String fieldName = exportIdentifier(fd.fieldName);
writer.Write("<" + fieldName + ">");
switch (fd.type)
{
case ClassDescriptor.FieldType.tpBoolean:
writer.Write(body[offs++] != 0?"1":"0");
break;
case ClassDescriptor.FieldType.tpByte:
writer.Write(System.Convert.ToString((byte) body[offs++]));
break;
case ClassDescriptor.FieldType.tpSByte:
writer.Write(System.Convert.ToString((sbyte) body[offs++]));
break;
case ClassDescriptor.FieldType.tpChar:
writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
offs += 2;
break;
case ClassDescriptor.FieldType.tpShort:
writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
offs += 2;
break;
case ClassDescriptor.FieldType.tpUShort:
writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
offs += 2;
break;
case ClassDescriptor.FieldType.tpInt:
writer.Write(System.Convert.ToString(Bytes.unpack4(body, offs)));
offs += 4;
break;
case ClassDescriptor.FieldType.tpEnum:
writer.Write(Enum.ToObject(f.FieldType, Bytes.unpack4(body, offs)));
offs += 4;
break;
case ClassDescriptor.FieldType.tpUInt:
writer.Write(System.Convert.ToString((uint)Bytes.unpack4(body, offs)));
offs += 4;
break;
case ClassDescriptor.FieldType.tpLong:
writer.Write(System.Convert.ToString(Bytes.unpack8(body, offs)));
offs += 8;
break;
case ClassDescriptor.FieldType.tpULong:
writer.Write(System.Convert.ToString((ulong)Bytes.unpack8(body, offs)));
offs += 8;
break;
case ClassDescriptor.FieldType.tpFloat:
writer.Write(System.Convert.ToString(Bytes.unpackF4(body, offs)));
offs += 4;
break;
case ClassDescriptor.FieldType.tpDouble:
writer.Write(System.Convert.ToString(Bytes.unpackF8(body, offs)));
offs += 8;
break;
case ClassDescriptor.FieldType.tpGuid:
writer.Write("\"" + Bytes.unpackGuid(body, offs) + "\"");
offs += 16;
break;
case ClassDescriptor.FieldType.tpDecimal:
writer.Write("\"" + Bytes.unpackDecimal(body, offs) + "\"");
offs += 16;
break;
case ClassDescriptor.FieldType.tpString:
offs = exportString(body, offs);
break;
case ClassDescriptor.FieldType.tpDate:
{
long msec = Bytes.unpack8(body, offs);
offs += 8;
if (msec >= 0)
{
writer.Write("\"" + new System.DateTime(msec) + "\"");
}
else
{
writer.Write("null");
}
break;
}
case ClassDescriptor.FieldType.tpObject:
case ClassDescriptor.FieldType.tpOid:
exportRef(Bytes.unpack4(body, offs));
offs += 4;
break;
case ClassDescriptor.FieldType.tpValue:
writer.Write('\n');
offs = exportObject(fd.valueDesc, body, offs, indent + 1);
indentation(indent);
break;
#if SUPPORT_RAW_TYPE
case ClassDescriptor.FieldType.tpRaw:
#endif
case ClassDescriptor.FieldType.tpArrayOfByte:
case ClassDescriptor.FieldType.tpArrayOfSByte:
offs = exportBinary(body, offs);
break;
case ClassDescriptor.FieldType.tpCustom:
{
MemoryStream s = new MemoryStream(body, offs, body.Length - offs);
CustomSerializable obj = storage.serializer.Unpack(s);
String str = obj.ToString();
writer.Write("\"");
for (int j = 0; j < str.Length; j++)
{
exportChar(str[j]);
}
writer.Write("\"");
offs += (int)s.Position;
break;
}
case ClassDescriptor.FieldType.tpArrayOfBoolean:
{
int len = Bytes.unpack4(body, offs);
offs += 4;
if (len < 0)
{
writer.Write("null");
}
else
{
writer.Write('\n');
while (--len >= 0)
{
indentation(indent + 1);
writer.Write("<element>" + (body[offs++] != 0?"1":"0") + "</element>\n");
}
indentation(indent);
}
break;
}
case ClassDescriptor.FieldType.tpArrayOfChar:
{
int len = Bytes.unpack4(body, offs);
offs += 4;
if (len < 0)
{
writer.Write("null");
}
else
{
writer.Write('\n');
while (--len >= 0)
{
indentation(indent + 1);
writer.Write("<element>" + (Bytes.unpack2(body, offs) & 0xFFFF) + "</element>\n");
offs += 2;
}
indentation(indent);
}
break;
}
case ClassDescriptor.FieldType.tpArrayOfShort:
{
int len = Bytes.unpack4(body, offs);
offs += 4;
if (len < 0)
{
writer.Write("null");
}
else
{
writer.Write('\n');
while (--len >= 0)
{
indentation(indent + 1);
writer.Write("<element>" + Bytes.unpack2(body, offs) + "</element>\n");
offs += 2;
}
indentation(indent);
}
break;
}
case ClassDescriptor.FieldType.tpArrayOfUShort:
{
int len = Bytes.unpack4(body, offs);
offs += 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -