001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: X12SplitterUtil.java 7952 2007-06-16 11:56:26Z zjin $
023: *
024: */
025: package com.bostech.corporation.component.x12splitter.processors;
026:
027: import java.util.LinkedList;
028: import java.util.List;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: *
035: */
036: public class X12SplitterUtil {
037:
038: protected static final transient Log logger = LogFactory
039: .getLog(X12SplitterUtil.class);
040:
041: public static final String ISA = "ISA";
042: public static final String IEA = "IEA";
043: public static final String GS = "GS";
044: public static final String GE = "GE";
045: public static final String ST = "ST";
046: public static final String SE = "SE";
047:
048: public static List<String> splitX12(String x12) {
049: List<String> list = new LinkedList<String>();
050: if (!x12.startsWith(ISA)) {
051: logger.error("Input source is invalid.");
052: }
053: String segmentTerminator = x12.substring(105, 106);
054: logger.debug("segmentTerminator is: " + segmentTerminator);
055: String[] segmentArray = x12.split(segmentTerminator);
056: logger.debug("length of segmentArray is: "
057: + segmentArray.length);
058: String isa = segmentArray[0] + segmentTerminator;
059: String iea = segmentArray[segmentArray.length - 1]
060: + segmentTerminator;
061: for (int j = 1; j < segmentArray.length - 1; j++) {
062: if (segmentArray[j].startsWith(ST)) {
063: logger.debug("find ST!");
064: String gs = searchGSSegment(segmentArray, j)
065: + segmentTerminator;
066: StringBuffer buffer = new StringBuffer();
067: buffer.append(isa);
068: buffer.append(gs);
069: for (int k = j; k < segmentArray.length - 1; k++) {
070: buffer.append(segmentArray[k] + segmentTerminator);
071: if (segmentArray[k].startsWith(SE)) {
072: break;
073: }
074: }
075: String ge = searchGESegment(segmentArray, j,
076: segmentArray.length - 1)
077: + segmentTerminator;
078: buffer.append(ge);
079: buffer.append(iea);
080: list.add(buffer.toString());
081: }
082: }
083: logger.debug("list.size() is: " + list.size());
084: return list;
085: }
086:
087: private static String searchGSSegment(String[] segmentArray,
088: int start) {
089: for (int i = start; i >= 0; i--) {
090: if (segmentArray[i].startsWith(GS)) {
091: return segmentArray[i];
092: }
093: }
094: return "";
095: }
096:
097: private static String searchGESegment(String[] segmentArray,
098: int start, int end) {
099: for (int i = start; i < end; i++) {
100: if (segmentArray[i].startsWith(GE)) {
101: return segmentArray[i];
102: }
103: }
104: return "";
105: }
106:
107: public static void main(String[] args) {
108: String x12 = "ISA*00* *00* *ZZ*A1STORES *ZZ*LEXINGTON *020115*0900*U*00400*000000005*0*T*>~"
109: + "GS*11111111~"
110: + "ST*aaaaaaaa~"
111: + "!!!!!!!!!!!~"
112: + "!!!!!!!!!!!~"
113: + "!!!!!!!!!!!~"
114: + "!!!!!!!!!!!~"
115: + "SE*aaaaaaaa~"
116: + "ST*bbbbbbbb~"
117: + "@@@@@@@@@@@~"
118: + "@@@@@@@@@@@~"
119: + "SE*bbbbbbbb~"
120: + "GE*11111111~"
121: + "GS*22222222~"
122: + "ST*cccccccc~"
123: + "###########~"
124: + "###########~"
125: + "###########~"
126: + "SE*cccccccc~"
127: + "ST*dddddddd~"
128: + "$$$$$$$$$$$~"
129: + "$$$$$$$$$$$~"
130: + "$$$$$$$$$$$~"
131: + "$$$$$$$$$$$~"
132: + "SE*dddddddd~"
133: + "GE*22222222~" + "IEA*1*000000005~";
134: List list = splitX12(x12);
135: for (int i = 0; i < list.size(); i++) {
136: System.out.println(list.size());
137: System.out.println(list.get(i));
138: System.out.println();
139: System.out.println();
140: }
141: }
142: }
|