summaryrefslogtreecommitdiff
path: root/src/com/google/wireless/gdata/spreadsheets/serializer/xml/XmlCellEntryGDataSerializer.java
blob: 995af2c5d735b7aa5e00ed81f5c99a42ef1da599 (plain)
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
package com.google.wireless.gdata.spreadsheets.serializer.xml;

import com.google.wireless.gdata.data.Entry;
import com.google.wireless.gdata.data.StringUtils;
import com.google.wireless.gdata.parser.ParseException;
import com.google.wireless.gdata.parser.xml.XmlParserFactory;
import com.google.wireless.gdata.serializer.xml.XmlEntryGDataSerializer;
import com.google.wireless.gdata.spreadsheets.data.CellEntry;

import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;

/**
 * A serializer for handling GData Spreadsheets Cell entries.
 */
public class XmlCellEntryGDataSerializer extends XmlEntryGDataSerializer {
    /** The namespace to use for the GData Cell attributes */
    public static final String NAMESPACE_GS = "gs";

    /** The URI of the GData Cell namespace */
    public static final String NAMESPACE_GS_URI =
            "http://schemas.google.com/spreadsheets/2006";

    /**
     * Creates a new XmlCellEntryGDataSerializer.
     * 
     * @param entry the entry to be serialized
     */
    public XmlCellEntryGDataSerializer(XmlParserFactory xmlFactory,
            Entry entry) {
        super(xmlFactory, entry);
    }

    /**
     * Sets up the GData Cell namespace.
     * 
     * @param serializer the serializer to use
     */
    protected void declareExtraEntryNamespaces(XmlSerializer serializer)
            throws IOException {
        serializer.setPrefix(NAMESPACE_GS, NAMESPACE_GS_URI);
    }

    /*
     * Handles the non-Atom data belonging to the GData Spreadsheets Cell
     * namespace.
     * 
     * @param serializer the XML serializer to use
     * @param format unused
     * @throws ParseException if the data could not be serialized
     * @throws IOException on network error
     */
    protected void serializeExtraEntryContents(XmlSerializer serializer,
            int format) throws ParseException, IOException {
        CellEntry entry = (CellEntry) getEntry();
        int row = entry.getRow();
        int col = entry.getCol();
        String value = entry.getValue();
        String inputValue = entry.getInputValue();
        if (row < 0 || col < 0) {
            throw new ParseException("Negative row or column value");
        }

        // cells require row & col attrs, and allow inputValue and
        // numericValue
        serializer.startTag(NAMESPACE_GS_URI, "cell");
        serializer.attribute(null /* ns */, "row", "" + row);
        serializer.attribute(null /* ns */, "col", "" + col);
        if (inputValue != null) {
            serializer.attribute(null /* ns */, "inputValue", inputValue);
        }
        if (entry.hasNumericValue()) {
            serializer.attribute(null /* ns */, "numericValue", entry
                    .getNumericValue());
        }

        // set the child text...
        value = StringUtils.isEmpty(value) ? "" : value;
        serializer.text(value);
        serializer.endTag(NAMESPACE_GS_URI, "cell");
    }
}