summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/theme/util/FontConfigParser.java
blob: ae963b821642fd3c1ec397c121a7013bbba09a70 (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2014 Cyanogen, Inc.
 */
package com.cyngn.theme.util;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Parses an XML font config. Example:
 *
 *<familyset>
 *
 *  <family>
 *      <nameset>
 *          <name>sans-serif</name>
 *          <name>arial</name>
 *      </nameset>
 *      <fileset>
 *          <file>Roboto-Regular.ttf</file>
 *          <file>Roboto-Bold.ttf</file>
 *          <file>Roboto-Italic.ttf</file>
 *          <file>Roboto-BoldItalic.ttf</file>
 *      </fileset>
 *  </family>
 *  <family>
 *    ...
 *  </family>
 *</familyset>
 */
public class FontConfigParser {

    public static class Family {
        public List<String> nameset = new ArrayList<String>();
        public List<String> fileset = new ArrayList<String>();
    }

    public static List<Family> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(in, null);
            parser.nextTag();
            return readFamilySet(parser);
        } finally {
            in.close();
        }
    }

    private static List<Family> readFamilySet(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<Family> families = new ArrayList<Family>();
        parser.require(XmlPullParser.START_TAG, null, "familyset");

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();

            // Starts by looking for the entry tag
            if (name.equals("family")) {
                Family family = readFamily(parser);
                families.add(family);
            }
        }
        return families;
    }

    private static Family readFamily(XmlPullParser parser) throws XmlPullParserException, IOException {
        Family family = new Family();
        parser.require(XmlPullParser.START_TAG, null, "family");

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("nameset")) {
                List<String> nameset = readNameset(parser);
                family.nameset = nameset;
            } else if (name.equals("fileset")) {
                List<String> fileset = readFileset(parser);
                family.fileset = fileset;
            } else {
                skip(parser);
            }
        }
        return family;
    }

    private static List<String> readNameset(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<String> names = new ArrayList<String>();
        parser.require(XmlPullParser.START_TAG, null, "nameset");

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String tagname = parser.getName();
            if (tagname.equals("name")) {
                String name = readText(parser);
                names.add(name);
            } else {
                skip(parser);
            }
        }
        return names;
    }

    private static List<String> readFileset(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<String> files = new ArrayList<String>();
        parser.require(XmlPullParser.START_TAG, null, "fileset");

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("file")) {
                String file = readText(parser);
                files.add(file);
            } else {
                skip(parser);
            }
        }
        return files;
    }

    // For the tags title and summary, extracts their text values.
    private static String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

    private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
    }
}