Esempio n. 1
0
 private boolean getOrdinality(Element e) {
   String value = getOptionalAttribute(e, ATTR_MANDATORY);
   return value != null ? Boolean.parseBoolean(value) : defaultMandatory;
 }
Esempio n. 2
0
    public ConfigBuilder(InputStream configXml) {
      try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(configXml);
        doc.getDocumentElement().normalize();

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema =
            factory.newSchema(
                new StreamSource(
                    Thread.currentThread()
                        .getContextClassLoader()
                        .getResourceAsStream("jen8583.xsd")));

        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(doc));
      } catch (Exception e) {
        throw new ConfigException(e.getMessage(), e);
      }

      Element defaults = (Element) doc.getElementsByTagName(ELEMENT_DEFAULTS).item(0);

      for (Element e : getSubElements(defaults)) {
        switch (e.getTagName()) {
          case ELEMENT_VAR:
            defaultLengthEncoding =
                Encoding.valueOf(getMandatoryAttribute(e, ATTR_LENGTH_ENCODING));
            LOG.info("Default length encoding: {}", defaultLengthEncoding);
            break;
          case ELEMENT_TLV:
            defaultTlvTagEncoding = Encoding.valueOf(getMandatoryAttribute(e, ATTR_TAG_ENCODING));
            LOG.info("Default tlv tag encoding: {}", defaultTlvTagEncoding);
            defaultTlvLengthEncoding =
                Encoding.valueOf(getMandatoryAttribute(e, ATTR_LENGTH_ENCODING));
            LOG.info("Default tlv length encoding: {}", defaultTlvLengthEncoding);
            break;
          case ELEMENT_ALPHA:
            defaultTrim = Boolean.valueOf(getMandatoryAttribute(e, ATTR_TRIM));
            LOG.info("Default trim: {}", defaultTrim);
            String stringJustify = getMandatoryAttribute(e, ATTR_JUSTIFIED);
            switch (stringJustify) {
              case ATTR_CONST_LEFT:
                defaultLeftJustified = true;
                break;
              case ATTR_CONST_RIGHT:
                defaultLeftJustified = false;
                break;
              default:
                throw new ConfigException(format("Invalid value for justified: %s", stringJustify));
            }
            LOG.info(
                "Default {} justified", defaultLeftJustified ? ATTR_CONST_LEFT : ATTR_CONST_RIGHT);
            break;
          case ELEMENT_NUMERIC:
            defaultNumericEncoding = Encoding.valueOf(getMandatoryAttribute(e, ATTR_ENCODING));
            LOG.info("Default numeric encoding: {}", defaultNumericEncoding);
            break;
          case ELEMENT_DATE:
            defaultDateEncoding = Encoding.valueOf(getMandatoryAttribute(e, ATTR_ENCODING));
            LOG.info("Default date encoding: {}", defaultDateEncoding);
            String tzDefault = getMandatoryAttribute(e, ATTR_TIMEZONE);
            defaultTimeZone =
                ATTR_CONST_SYSTEM.equals(tzDefault)
                    ? TimeZone.getDefault()
                    : ZoneInfo.getTimeZone(tzDefault);
            LOG.info("Default timezone: {}", defaultTimeZone.getID());
            break;
          case ELEMENT_ORDINALITY:
            defaultMandatory = Boolean.valueOf(getMandatoryAttribute(e, ATTR_MANDATORY));
            LOG.info("Default mandatory: {}", defaultMandatory);
            defaultFailFast = Boolean.valueOf(getMandatoryAttribute(e, ATTR_FAIL_FAST));
            LOG.info("Default fail-fast: {}", defaultFailFast);
            break;
        }
      }
    }