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