File indexing completed on 2024-12-15 04:19:54

0001 package mso.generator.utils;
0002 
0003 import java.io.IOException;
0004 import java.util.ArrayList;
0005 import java.util.List;
0006 
0007 import org.w3c.dom.Element;
0008 import org.w3c.dom.Node;
0009 import org.w3c.dom.NodeList;
0010 
0011 public class Struct extends Type {
0012 
0013     public final List<Member> members = new ArrayList<Member>();
0014     final String sizeExpression;
0015     public final boolean containsArrayMember;
0016     public final boolean containsOptionalMember;
0017     public final boolean containsUnknownLengthArrayMember;
0018     public final boolean containsKnownLengthArrayMember;
0019     final boolean containsUnsureChoice;
0020     final boolean containsSureChoice;
0021     public final boolean containsChoice;
0022 
0023     private static int getSize(TypeRegistry registry, Element e)
0024             throws IOException {
0025         int size = 0;
0026         NodeList l = e.getChildNodes();
0027         for (int i = 0; i < l.getLength(); ++i) {
0028             Node n = l.item(i);
0029             if (!(n instanceof Element)) {
0030                 continue;
0031             }
0032             Element me = (Element) n;
0033             if (me.getNodeName().equals("limitation")) {
0034                 continue;
0035             }
0036             Member m = new Member(registry, me);
0037             if (m.isOptional || m.condition != null || m.type() == null
0038                     || m.type().size == -1) {
0039                 return -1;
0040             }
0041             if (m.isArray) {
0042                 Integer asize = null;
0043                 try {
0044                     if (m.size != null)
0045                         asize = Integer.decode(m.size);
0046                 } catch (NumberFormatException e1) {
0047                 }
0048                 Integer count = null;
0049                 try {
0050                     if (m.count != null)
0051                         count = Integer.decode(m.count);
0052                 } catch (NumberFormatException e2) {
0053                 }
0054                 if (asize != null) {
0055                     size += asize * 8;
0056                 } else if (count != null && m.type().size != -1) {
0057                     size += count * m.type().size;
0058                 } else {
0059                     return -1;
0060                 }
0061             } else {
0062                 size += m.type().size;
0063             }
0064         }
0065         if (size == 0) {
0066             throw new Error("size is 0 for " + e.getAttribute("name"));
0067         }
0068         if (size % 8 != 0)
0069             throw new Error("sizes do not add up to multiple of 8: " + size
0070                     + " for " + e.getAttribute("name"));
0071         return size;
0072     }
0073 
0074     Struct(TypeRegistry registry, Element e) throws IOException {
0075         super(registry, e.getAttribute("name"), getSize(registry, e));
0076         sizeExpression = e.getAttribute("size");
0077 
0078         boolean _containsArrayMember = false;
0079         boolean _containsOptionalMember = false;
0080         boolean _containsUnknownLengthArrayMember = false;
0081         boolean _containsKnownLengthArrayMember = false;
0082         boolean _containsSureChoice = false;
0083         boolean _containsUnsureChoice = false;
0084         NodeList l = e.getChildNodes();
0085         for (int i = 0; i < l.getLength(); ++i) {
0086             Node n = l.item(i);
0087             if (!(n instanceof Element)) {
0088                 continue;
0089             }
0090             Element me = (Element) n;
0091             if (me.getNodeName().equals("limitation")) {
0092                 break;
0093             }
0094             Member m = new Member(registry, me);
0095             members.add(m);
0096             _containsArrayMember = _containsArrayMember || m.isArray;
0097             _containsOptionalMember = _containsOptionalMember || m.isOptional;
0098             _containsUnknownLengthArrayMember = _containsUnknownLengthArrayMember
0099                     || (m.isArray && m.count == null);
0100             _containsKnownLengthArrayMember = _containsKnownLengthArrayMember
0101                     || m.count != null;
0102             if (m.type() instanceof Choice) {
0103                 _containsSureChoice = _containsSureChoice
0104                         || ((Choice) m.type()).commonType != null;
0105                 _containsUnsureChoice = _containsSureChoice
0106                         || ((Choice) m.type()).commonType == null;
0107             }
0108         }
0109         containsArrayMember = _containsArrayMember;
0110         containsOptionalMember = _containsOptionalMember;
0111         containsUnknownLengthArrayMember = _containsUnknownLengthArrayMember;
0112         containsKnownLengthArrayMember = _containsKnownLengthArrayMember;
0113         containsUnsureChoice = _containsUnsureChoice;
0114         containsSureChoice = _containsSureChoice;
0115         containsChoice = containsUnsureChoice || containsSureChoice;
0116     }
0117 }