aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/serialization/XmlSerializationHelper.java
blob: b2c92e6bb0b361f37180e524b192d00da843d172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.serialization;

import com.yahoo.document.Document;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.BoolFieldValue;
import com.yahoo.document.datatypes.ByteFieldValue;
import com.yahoo.document.datatypes.DoubleFieldValue;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.Float16FieldValue;
import com.yahoo.document.datatypes.FloatFieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.LongFieldValue;
import com.yahoo.document.datatypes.MapFieldValue;
import com.yahoo.document.datatypes.Raw;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.StructuredFieldValue;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.text.Utf8;

import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Date: Apr 17, 2008
 *
 * @author <a href="mailto:humbe@yahoo-inc.com">H&aring;kon Humberset</a>
 */
@Deprecated
public class XmlSerializationHelper {

    private final static Base64.Encoder base64Encoder = Base64.getEncoder();

    public static void printArrayXml(Array array, XmlStream xml) {
        List<FieldValue> lst = array.getValues();
        for (FieldValue value : lst) {
            xml.beginTag("item");
            value.printXml(xml);
            xml.endTag();
        }
    }

    public static <K extends FieldValue, V extends FieldValue> void printMapXml(MapFieldValue<K, V> map, XmlStream xml) {
        for (Map.Entry<K, V> e : map.entrySet()) {
            FieldValue key = e.getKey();
            FieldValue val = e.getValue();
            xml.beginTag("item");
            xml.beginTag("key");
            key.printXml(xml);
            xml.endTag();
            xml.beginTag("value");
            val.printXml(xml);
            xml.endTag();
            xml.endTag();
        }
    }

    public static void printByteXml(ByteFieldValue b, XmlStream xml) {
        xml.addContent(b.toString());
    }

    public static void printDocumentXml(Document doc, XmlStream xml) {
        xml.addAttribute("documenttype", doc.getDataType().getName());
        xml.addAttribute("documentid", doc.getId());
        final java.lang.Long lastModified = doc.getLastModified();
        if (lastModified != null) {
            xml.addAttribute("lastmodifiedtime", lastModified);
        }
        printStructured(doc, xml);
    }

    public static void printDoubleXml(DoubleFieldValue d, XmlStream xml) {
        xml.addContent(d.toString());
    }

    public static void printFloatXml(FloatFieldValue f, XmlStream xml) {
        xml.addContent(f.toString());
    }

    public static void printShortfloatXml(Float16FieldValue f, XmlStream xml) {
        xml.addContent(f.toString());
    }

    public static void printBoolXml(BoolFieldValue f, XmlStream xml) {
        xml.addContent(f.toString());
    }

    public static void printIntegerXml(IntegerFieldValue f, XmlStream xml) {
        xml.addContent(f.toString());
    }

    public static void printLongXml(LongFieldValue l, XmlStream xml) {
        xml.addContent(l.toString());
    }

    public static void printRawXml(Raw r, XmlStream xml) {
        xml.addAttribute("binaryencoding", "base64");
        xml.addContent(base64Encoder.encodeToString(r.getByteBuffer().array()));
    }

    public static void printStringXml(StringFieldValue s, XmlStream xml) {
        String content = s.getString();
        if (containsNonPrintableCharactersString(content)) {
            byte[] bytecontent = Utf8.toBytes(content);
            xml.addAttribute("binaryencoding", "base64");
            xml.addContent(base64Encoder.encodeToString(bytecontent));
        } else {
            xml.addContent(content);
        }
    }

    private static void printStructured(StructuredFieldValue s, XmlStream xml) {
        Iterator<Map.Entry<Field, FieldValue>> it = s.iterator();
        while (it.hasNext()) {
            Map.Entry<Field, FieldValue> val = it.next();
            xml.beginTag(val.getKey().getName());
            val.getValue().printXml(xml);
            xml.endTag();
        }
    }
    public static void printStructXml(Struct s, XmlStream xml) {
        printStructured(s, xml);
    }

    public static void printWeightedSetXml(WeightedSet ws, XmlStream xml) {
        Iterator<FieldValue> it = ws.fieldValueIterator();
        while (it.hasNext()) {
            FieldValue val = it.next();
            xml.beginTag("item");
            xml.addAttribute("weight", ws.get(val));
            val.printXml(xml);
            xml.endTag();
        }
    }

    private static boolean containsNonPrintableCharactersString(final CharSequence buffer) {
        for (int i = 0; i < buffer.length(); i++) {
            char b = buffer.charAt(i);
            if (b < 32 && (b != 9 && b != 10 && b != 13)) return true;
        }
        return false;
    }
}