Esempio n. 1
0
  private Object traverse(Node node)
      throws FlatwormUnsetFieldValueException, FlatwormConfigurationValueException {
    int type = node.getNodeType();
    if (type == Node.ELEMENT_NODE) {
      String nodeName = node.getNodeName();
      if (nodeName.equals("file-format")) {
        FileFormat f = new FileFormat();
        String encoding = Charset.defaultCharset().name();
        if (hasAttributeValueNamed(node, "encoding")) {
          encoding = getAttributeValueNamed(node, "encoding");
        }
        f.setEncoding(encoding);

        List<Object> children = getChildNodes(node);
        for (int i = 0; i < children.size(); i++) {
          if (children.get(i).getClass().equals(Converter.class)) {
            f.addConverter((Converter) children.get(i));
          }
          if (children.get(i).getClass().equals(Record.class)) {
            f.addRecord((Record) children.get(i));
          }
        }
        return f;
      }
      if (nodeName.equals("converter")) {
        Converter c = new Converter();
        c.setConverterClass(getAttributeValueNamed(node, "class"));
        c.setMethod(getAttributeValueNamed(node, "method"));
        c.setReturnType(getAttributeValueNamed(node, "return-type"));
        c.setName(getAttributeValueNamed(node, "name"));
        return c;
      }
      if (nodeName.equals("record")) {
        Record r = new Record();
        r.setName(getAttributeValueNamed(node, "name"));
        Node identChild = getChildElementNodeOfType("record-ident", node);
        if (identChild != null) {
          Node fieldChild = getChildElementNodeOfType("field-ident", identChild);
          Node lengthChild = getChildElementNodeOfType("length-ident", identChild);
          if (lengthChild != null) {
            r.setLengthIdentMin(Integer.parseInt(getAttributeValueNamed(lengthChild, "minlength")));
            r.setLengthIdentMax(Integer.parseInt(getAttributeValueNamed(lengthChild, "maxlength")));
            r.setIdentTypeFlag('L');
          } else if (fieldChild != null) {
            r.setFieldIdentStart(
                Integer.parseInt(getAttributeValueNamed(fieldChild, "field-start")));
            r.setFieldIdentLength(
                Integer.parseInt(getAttributeValueNamed(fieldChild, "field-length")));
            List<Node> matchNodes = getChildElementNodesOfType("match-string", fieldChild);
            for (int j = 0; j < matchNodes.size(); j++) {
              r.addFieldIdentMatchString(getChildTextNodeValue(matchNodes.get(j)));
            }
            r.setIdentTypeFlag('F');
          }
        }
        Node recordChild = getChildElementNodeOfType("record-definition", node);
        r.setRecordDefinition((RecordDefinition) traverse(recordChild));
        return r;
      }
      if (nodeName.equals("record-definition")) {
        RecordDefinition rd = new RecordDefinition();
        List<Object> children = getChildNodes(node);
        for (int i = 0; i < children.size(); i++) {
          Object o = children.get(i);
          if (o.getClass().equals(Bean.class)) {
            rd.addBeanUsed((Bean) o);
          }
          if (o.getClass().equals(Line.class)) {
            rd.addLine((Line) o);
          }
        }
        return rd;
      }
      if (nodeName.equals("bean")) {
        Bean b = new Bean();
        b.setBeanName(getAttributeValueNamed(node, "name"));
        b.setBeanClass(getAttributeValueNamed(node, "class"));
        try {
          b.setBeanObjectClass(Class.forName(b.getBeanClass()));
        } catch (ClassNotFoundException e) {
          throw new FlatwormConfigurationValueException("Unable to load class " + b.getBeanClass());
        }
        return b;
      }
      if (nodeName.equals("line")) {
        Line li = new Line();

        // JBL - Determine if this line is delimited
        // Determine value of quote character, default = "
        // These field is optional
        Node delimit = getAttributeNamed(node, "delimit");
        Node quote = getAttributeNamed(node, "quote");
        if (delimit != null) {
          li.setDelimeter(getAttributeValueNamed(node, "delimit"));
        }
        if (quote != null) {
          li.setQuoteChar(getAttributeValueNamed(node, "quote"));
        }
        List<Object> v = getChildNodes(node);
        for (int i = 0; i < v.size(); i++) {
          Object o = v.get(i);
          if (o instanceof LineElement) {
            li.addElement((LineElement) o);
          }
        }
        return li;
      }
      if (nodeName.equals("segment-element")) {
        SegmentElement segment = new SegmentElement();
        segment.setCardinalityMode(CardinalityMode.LOOSE);
        segment.setName(getAttributeValueNamed(node, "name"));
        segment.setMinCount(Integer.parseInt(getAttributeValueNamed(node, "minCount")));
        segment.setMaxCount(Integer.parseInt(getAttributeValueNamed(node, "maxCount")));
        segment.setBeanRef(getAttributeValueNamed(node, "beanref"));
        segment.setParentBeanRef(getAttributeValueNamed(node, "parent-beanref"));
        segment.setAddMethod(getAttributeValueNamed(node, "addMethod"));
        String segmentMode = getAttributeValueNamed(node, "cardinality-mode");
        if (!StringUtils.isBlank(segmentMode)) {
          if (segmentMode.toLowerCase().startsWith("strict")) {
            segment.setCardinalityMode(CardinalityMode.STRICT);
          } else if (segmentMode.toLowerCase().startsWith("restrict")) {
            segment.setCardinalityMode(CardinalityMode.RESTRICTED);
          }
        }

        Node fieldChild = getChildElementNodeOfType("field-ident", node);
        if (fieldChild != null) {
          segment.setFieldIdentStart(
              Integer.parseInt(getAttributeValueNamed(fieldChild, "field-start")));
          segment.setFieldIdentLength(
              Integer.parseInt(getAttributeValueNamed(fieldChild, "field-length")));
          List<Node> matchNodes = getChildElementNodesOfType("match-string", fieldChild);
          for (int j = 0; j < matchNodes.size(); j++) {
            segment.addFieldIdentMatchString(getChildTextNodeValue((Node) matchNodes.get(j)));
          }
        }
        validateSegmentConfiguration(segment);
        List<Object> v = getChildNodes(node);
        for (int i = 0; i < v.size(); i++) {
          Object o = v.get(i);
          if (o instanceof LineElement) {
            segment.addElement((LineElement) o);
          }
        }
        return segment;
      }
      if (nodeName.equals("record-element")) {
        RecordElement re = new RecordElement();

        Node start = getAttributeNamed(node, "start");
        Node end = getAttributeNamed(node, "end");
        Node length = getAttributeNamed(node, "length");
        Node beanref = getAttributeNamed(node, "beanref");
        Node beanType = getAttributeNamed(node, "type");
        if ((end == null) && (length == null)) {
          FlatwormConfigurationValueException err =
              new FlatwormConfigurationValueException(
                  "Must set either the 'end' or 'length' properties");
          throw err;
        }
        if ((end != null) && (length != null)) {
          FlatwormConfigurationValueException err =
              new FlatwormConfigurationValueException(
                  "Can't specify both the 'end' or 'length' properties");
          throw err;
        }
        if (start != null) {
          re.setFieldStart(Integer.parseInt(start.getNodeValue()));
        }
        if (end != null) {
          re.setFieldEnd(Integer.parseInt(end.getNodeValue()));
        }
        if (length != null) {
          re.setFieldLength(Integer.parseInt(length.getNodeValue()));
        }
        if (beanref != null) {
          re.setBeanRef(beanref.getNodeValue());
        }
        if (beanType != null) {
          re.setType(beanType.getNodeValue());
        }
        List<Node> children = getChildElementNodesOfType("conversion-option", node);
        for (int i = 0; i < children.size(); i++) {
          Node o = (Node) children.get(i);

          String name = getAttributeValueNamed(o, "name");
          String value = getAttributeValueNamed(o, "value");
          ConversionOption co = new ConversionOption(name, value);

          re.addConversionOption(name, co);
        }
        return re;
      }
    }
    return null;
  }