View Javadoc

1   /*
2    * EDI-Knight Integration and Transformation Platform
3    * Copyright (C) 2006-2007 Holger Joest <hjoest@users.sourceforge.net>
4    *
5    * This program is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package net.sf.ediknight.edifact.setup.scanner;
21  
22  import net.sf.ediknight.common.edi.directory.CompositeElementNodeImpl;
23  import net.sf.ediknight.common.edi.directory.ElementNodeImpl;
24  import net.sf.ediknight.common.edi.directory.SegmentImpl;
25  import net.sf.ediknight.common.edi.directory.SimpleElementNodeImpl;
26  import net.sf.ediknight.edifact.setup.LineScanner;
27  
28  
29  /***
30   * @author Holger Joest
31   */
32  public final class SegmentScanner
33  extends LineScanner {
34  
35      /*** */
36      private SegmentImpl currentSegment;
37  
38      /*** */
39      private ElementNodeImpl elementNode;
40  
41  
42      /***
43       * Constructor.
44       */
45      public SegmentScanner() {
46          setVerbose(false);
47      }
48  
49  
50      /***
51       * {@inheritDoc}
52       * @see net.sf.ediknight.edifact.setup.LineScanner#take(
53       *      int,
54       *      java.lang.String[])
55       */
56      @Override
57      public void take(int n, String[] groups) {
58          switch (n) {
59          case 0:
60              SegmentImpl segment = new SegmentImpl();
61              segment.setId(groups[1]);
62              segment.setDescription(groups[2]);
63              segment.setMode(getContext().getValue("mode"));
64              segment.setDeprecated("X".equals(groups[0]));
65              getDirectory().addSegment(segment);
66              currentSegment = segment;
67              break;
68          case 1:
69              if (groups[0].startsWith("C")
70                      || groups[0].startsWith("E")) {
71                  elementNode = new CompositeElementNodeImpl();
72                  String occurs = groups[4];
73                  if (occurs.length() == 0) {
74                      occurs = "1";
75                  }
76                  elementNode.setMaxOccurs(Integer.parseInt(occurs));
77              } else if (groups[0].startsWith("S")) {
78                  elementNode = new CompositeElementNodeImpl();
79                  String occurs = groups[3];
80                  if (occurs.length() == 0) {
81                      occurs = "1";
82                  }
83                  elementNode.setMaxOccurs(Integer.parseInt(occurs));
84              } else {
85                  elementNode = new SimpleElementNodeImpl();
86                  String occurs = groups[3];
87                  if (occurs.length() == 0) {
88                      occurs = "1";
89                  }
90                  elementNode.setMaxOccurs(Integer.parseInt(occurs));
91              }
92              elementNode.setMinOccurs("M".equals(groups[2]) ? 1 : 0);
93              elementNode.setId(groups[0]);
94              if (getDirectory().getElementById(
95                      elementNode.getId()) == null) {
96                  throw new RuntimeException(
97                          "Element "
98                          + elementNode.getId()
99                          + " not yet defined");
100             }
101             currentSegment.addElement(elementNode);
102             elementNode = null;
103             break;
104         case 2:
105             if (groups[0].startsWith("C")
106                     || groups[0].startsWith("E")) {
107                 elementNode = new CompositeElementNodeImpl();
108             } else if (groups[0].startsWith("S")) {
109                 elementNode = new CompositeElementNodeImpl();
110             } else {
111                 elementNode = new SimpleElementNodeImpl();
112             }
113             elementNode.setId(groups[0]);
114             if (getDirectory().getElementById(
115                     elementNode.getId()) == null) {
116                 throw new RuntimeException(
117                         "Element "
118                         + elementNode.getId()
119                         + " not yet defined");
120             }
121             break;
122         case 3:
123             if (elementNode == null) {
124                 break;
125             }
126             String occurs = groups[2];
127             elementNode.setMaxOccurs(Integer.parseInt(occurs));
128             elementNode.setMinOccurs("M".equals(groups[1]) ? 1 : 0);
129             currentSegment.addElement(elementNode);
130             elementNode = null;
131             break;
132         default:
133             throw new RuntimeException();
134         }
135     }
136 
137 }
138