/** * Creates an empty instance of {@link Loop} and adds the loop as a child to the current Loop. The * returned instance can be used to add segments to the child loop. * * @param name name of the loop * @return a new child Loop object */ public Loop addChild(String name) { Loop l = new Loop(this.context, name); l.setParent(this); l.depth = this.depth + 1; // debug loops.add(l); return l; }
/** * Creates a new {@link Loop} and replaces the child loop at the specified position. The returned * instance can be used to add segments to the child loop. * * @param name name of the loop * @param index position at which to add the loop. * @return a new child Loop object */ public Loop setChild(int index, String name) { Loop l = new Loop(this.context, name); l.setParent(this); l.depth = this.depth + 1; // debug loops.set(index, l); return l; }
/** * Returns number of segments in Loop and child loops * * @return size */ public int size() { int size; size = this.segments.size(); for (Loop l : this.childList()) { size += l.size(); } return size; }
/** * Returns the Loop in X12 {@link java.lang.String} format. This method is used to convert the X12 * object into a X12 transaction. * * @param bRemoveTrailingEmptyElements a flag for whether or not empty trailing elements should be * removed. * @return String representation of the loop. */ public String toString(boolean bRemoveTrailingEmptyElements) { StringBuilder dump = new StringBuilder(); for (Segment s : this.segments) { dump.append(s.toString(bRemoveTrailingEmptyElements)); dump.append(context.getSegmentSeparator()); } for (Loop l : this.childList()) { dump.append(l.toString(bRemoveTrailingEmptyElements)); } return dump.toString(); }
/** * Checks if the Loop contains the specified child Loop. It will check the complete child * hierarchy. * * @param name name of a child loop * @return boolean */ public boolean hasLoop(String name) { for (Loop l : this.childList()) { if (name.equals(l.getName())) { return true; } if (l.hasLoop(name)) { return true; } } return false; }
/** * Returns the Loop in XML {@link java.lang.String} format. This method is used to convert the X12 * object into a XML string. * * @param bRemoveTrailingEmptyElements a flag for whether or not empty trailing elements should be * removed. * @return String the loop in XML string format. */ public String toXML(boolean bRemoveTrailingEmptyElements) { StringBuilder dump = new StringBuilder(); dump.append("<LOOP NAME=\"").append(this.name).append("\">"); for (Segment s : this.segments) { dump.append(s.toXML(bRemoveTrailingEmptyElements)); } for (Loop l : this.childList()) { dump.append(l.toXML(bRemoveTrailingEmptyElements)); } dump.append("</LOOP>"); return dump.toString(); }
/** * Get the loop in the X12 transaction It will check the complete child hierarchy. * * @param name name of a loop * @return {@link java.util.List}<{@link Loop}> */ public List<Loop> findLoop(String name) { List<Loop> foundLoops = new ArrayList<Loop>(); for (Loop l : this.childList()) { if (name.equals(l.getName())) { foundLoops.add(l); } List<Loop> moreLoops = l.findLoop(name); if (moreLoops.size() > 0) { foundLoops.addAll(moreLoops); } } return foundLoops; }
/** * Get the segment in the X12 transaction It will check the current loop and the complete child * hierarchy. * * @param name name of a segment * @return {@link java.util.List}<{@link Segment}> */ public List<Segment> findSegment(String name) { List<Segment> foundSegments = new ArrayList<Segment>(); for (Segment s : this.segments) { if (name.equals(s.getElement(0))) { foundSegments.add(s); } } for (Loop l : this.childList()) { List<Segment> moreSegments = l.findSegment(name); if (moreSegments.size() > 0) { foundSegments.addAll(moreSegments); } } return foundSegments; }
/** * Inserts {@link Loop} as a child loop at the specified position. * * @param index position at which to add the loop. * @param loop a {@link Loop} object. */ public void addChild(int index, Loop loop) { loop.setParent(this); loop.depth = this.depth + 1; // debug loops.add(index, loop); }