示例#1
0
    private void setVarFields(Map<Integer, ComponentDef> components, List<Element> elements) {
      for (Element e : elements) {

        Integer index = getOptionalInteger(e, ATTR_INDEX);
        if (index == null) {
          index = Integer.valueOf(getMandatoryAttribute(e, ATTR_TAG));
        }
        ComponentDef existingDef = components.get(index);
        ComponentDef newDef = null;
        if (isCompositeVar(e) || ELEMENT_COMPOSITE_TLV.equals(e.getTagName())) {
          if (existingDef != null) {
            newDef = buildCompositeDef(existingDef, e);
          }
        }

        if (newDef == null) {
          if (ELEMENT_COMPOSITE_TLV.equals(((Element) e.getParentNode()).getTagName())) {
            newDef = buildTlvComponent(e, getOrdinality(e));
          } else {
            newDef = buildComponent(e, getOrdinality(e));
          }
        }

        components.put(index, newDef);
      }
    }
示例#2
0
    private Codec<?> buildCustomCodec(Element e) {
      String classAttr = e.getAttribute(ATTR_CODEC);
      Class<?> customCodecClass;
      try {
        customCodecClass = Class.forName(classAttr);
        if (CustomCodec.class.isAssignableFrom(customCodecClass)) {
          CustomCodec customCodec = (CustomCodec) customCodecClass.newInstance();

          Map<String, String> params = new HashMap<>();
          List<Element> paramElements = getSubElements(e);
          for (Element paramElement : paramElements) {
            params.put(paramElement.getAttribute(ATTR_KEY), paramElement.getAttribute(ATTR_VALUE));
          }
          if (customCodec instanceof Configurable) {
            ((Configurable) customCodec).configure(params);
          }

          return new CustomCodecAdapter(customCodec, getOptionalInteger(e, ATTR_LENGTH));
        } else {
          throw new ConfigException(format("Invalid custom class %s", classAttr));
        }
      } catch (ClassNotFoundException
          | SecurityException
          | InstantiationException
          | IllegalAccessException
          | IllegalArgumentException ex) {
        throw new ConfigException(ex.getMessage(), ex);
      }
    }
示例#3
0
    /**
     * @param bitmapCodec
     * @return
     */
    private Map<Integer, CompositeDef> buildFieldsDefs(BitmapCodec bitmapCodec) {
      NodeList messageList = doc.getElementsByTagName(ELEMENT_MESSAGE);
      Map<Integer, CompositeDef> defs = new TreeMap<>();

      for (int i = 0; i < messageList.getLength(); i++) {
        Element messageDef = (Element) messageList.item(i);
        Integer mti = getOptionalInteger(messageDef, ATTR_MTI);

        if (defs.containsKey(mti)) {
          throw new ConfigException(format("Duplicate message config for mti %d", mti));
        }

        SortedMap<Integer, ComponentDef> messageFieldDefs = buildVarComponents(messageDef);

        if (messageFieldDefs.containsKey(1)) {
          throw new ConfigException("Message field with index 1 not allowed");
        }

        defs.put(
            mti,
            new CompositeDef(
                messageFieldDefs, new VarCompositeCodec(bitmapCodec, defaultFailFast), true));
      }

      return defs;
    }
示例#4
0
 private void removeFields(Map<Integer, ComponentDef> componentDefs, List<Element> subElements) {
   for (Element e : subElements) {
     Integer index = Integer.valueOf(e.getAttribute(ATTR_INDEX));
     switch (e.getTagName()) {
       case ELEMENT_FIELD:
         if (componentDefs.remove(index) == null) {
           throw new ConfigException(format("Expected field %d not found", index));
         }
         break;
       case ELEMENT_COMPOSITE:
         ComponentDef def = componentDefs.get(index);
         if (def instanceof CompositeDef) {
           SortedMap<Integer, ComponentDef> subComponentDefs =
               ((CompositeDef) def).getSubComponentDefs();
           removeFields(subComponentDefs, getSubElements(e));
           break;
         }
         throw new ConfigException(format("Expected composite field %d not found", index));
       default:
         throw new ConfigException(
             format("Unknown message remove component instruction %s", e.getTagName()));
     }
   }
 }
示例#5
0
    private void buildFieldsDefsExtension(Map<Integer, CompositeDef> existingCodecs) {
      NodeList messageExtList = doc.getElementsByTagName(ELEMENT_MESSAGE_EXT);
      Map<Integer, CompositeDef> extensions = new TreeMap<>();
      for (int i = 0; i < messageExtList.getLength(); i++) {
        Element messageDef = (Element) messageExtList.item(i);
        Integer mtiExisting = getOptionalInteger(messageDef, ATTR_EXTENDS);
        Integer mti = getOptionalInteger(messageDef, ATTR_MTI);
        if (existingCodecs.containsKey(mti) || extensions.containsKey(mti)) {
          throw new ConfigException(format("Duplicate message config for mti %d", mti));
        }

        CompositeDef existing = existingCodecs.get(mtiExisting);
        if (existing == null) {
          throw new ConfigException(format("Error extending mti %d, no config available", mti));
        }
        SortedMap<Integer, ComponentDef> clonedFieldsDef =
            cloneSubComponentDefs(existing.getSubComponentDefs());

        Element setElement = null;
        Element removeElement = null;
        for (Element e : getSubElements(messageDef)) {
          switch (e.getTagName()) {
            case ELEMENT_SET:
              setElement = e;
              break;
            case ELEMENT_REMOVE:
              removeElement = e;
              break;
            default:
              throw new ConfigException(
                  format("Unknown message extension instruction %s", e.getTagName()));
          }
        }

        if (setElement != null) {
          setVarFields(clonedFieldsDef, getSubElements(setElement));
        }

        if (removeElement != null) {
          removeFields(clonedFieldsDef, getSubElements(removeElement));
        }

        extensions.put(
            mti,
            new CompositeDef(
                clonedFieldsDef,
                existing.getCompositeCodec(),
                existing.isMandatory(),
                existing.getLengthCodec()));
      }
      existingCodecs.putAll(extensions);
    }