OMAConstructed.java revision 05d2f4e6f26834a94b53187e6121379a16749088
1package com.android.server.wifi.hotspot2.omadm;
2
3import java.io.IOException;
4import java.io.OutputStream;
5import java.nio.charset.StandardCharsets;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.Iterator;
10import java.util.Map;
11
12public class OMAConstructed extends OMANode {
13    private final Map<String, OMANode> mChildren;
14
15    public OMAConstructed(OMANode parent, String name, String context) {
16        super(parent, name, context);
17        mChildren = new HashMap<>();
18    }
19
20    @Override
21    public OMANode addChild(String name, String context, String value, String pathString) throws IOException {
22        if (pathString == null) {
23            OMANode child = value != null ?
24                    new OMAScalar(this, name, context, value) :
25                    new OMAConstructed(this, name, context);
26            mChildren.put(name, child);
27            return child;
28        } else {
29            OMANode target = this;
30            while (target.getParent() != null)
31                target = target.getParent();
32
33            for (String element : pathString.split("/")) {
34                target = target.getChild(element);
35                if (target == null)
36                    throw new IOException("No child node '" + element + "' in " + getPathString());
37                else if (target.isLeaf())
38                    throw new IOException("Cannot add child to leaf node: " + getPathString());
39            }
40            return target.addChild(name, context, value, null);
41        }
42    }
43
44    public String getScalarValue(Iterator<String> path) throws OMAException {
45        if (!path.hasNext()) {
46            throw new OMAException("Path too short for " + getPathString());
47        }
48        String tag = path.next();
49        OMANode child = mChildren.get(tag);
50        if (child != null) {
51            return child.getScalarValue(path);
52        } else {
53            return null;
54        }
55    }
56
57    @Override
58    public OMAConstructed getListValue(Iterator<String> path) throws OMAException {
59        if (!path.hasNext()) {
60            return this;
61        }
62        String tag = path.next();
63        OMANode child = mChildren.get(tag);
64        if (child != null) {
65            return child.getListValue(path);
66        } else {
67            return null;
68        }
69    }
70
71    @Override
72    public boolean isLeaf() {
73        return false;
74    }
75
76    @Override
77    public Collection<OMANode> getChildren() {
78        return Collections.unmodifiableCollection(mChildren.values());
79    }
80
81    public OMANode getChild(String name) {
82        return mChildren.get(name);
83    }
84
85    @Override
86    public String getValue() {
87        throw new UnsupportedOperationException();
88    }
89
90    @Override
91    public void toString(StringBuilder sb, int level) {
92        sb.append(getPathString());
93        if (getContext() != null) {
94            sb.append(" (").append(getContext()).append(')');
95        }
96        sb.append('\n');
97
98        for (OMANode node : mChildren.values()) {
99            node.toString(sb, level + 1);
100        }
101    }
102
103    @Override
104    public void marshal(OutputStream out, int level) throws IOException {
105        OMAConstants.indent(level, out);
106        OMAConstants.serializeString(getName(), out);
107        if (getContext() != null) {
108            out.write(String.format("(%s)", getContext()).getBytes(StandardCharsets.UTF_8));
109        }
110        out.write(new byte[] { '+', '\n' });
111
112        for (OMANode child : mChildren.values()) {
113            child.marshal(out, level + 1);
114        }
115        OMAConstants.indent(level, out);
116        out.write(".\n".getBytes(StandardCharsets.UTF_8));
117    }
118}
119