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 java.util.Iterator;
23  
24  import net.sf.ediknight.common.edi.directory.MessageImpl;
25  import net.sf.ediknight.common.edi.directory.SegmentNodeImpl;
26  import net.sf.ediknight.edifact.setup.LineScanner;
27  
28  
29  /***
30   * @author Holger Joest
31   */
32  public final class MessageScanner
33  extends LineScanner {
34  
35      /*** */
36      private String currentMessageId;
37  
38  
39      /***
40       * Constructor.
41       */
42      public MessageScanner() {
43          setVerbose(false);
44      }
45  
46  
47      /***
48       * {@inheritDoc}
49       * @see net.sf.ediknight.edifact.setup.LineScanner
50       *      #getIterator(java.lang.String)
51       */
52      @Override
53      public Iterator<String> getIterator(String name) {
54          if (!"message".equals(name)) {
55              return null;
56          }
57          final MessageImpl[] messages =
58              getDirectory().getMessages();
59  
60          return new Iterator<String>() {
61  
62              private int p = 0;
63  
64              public boolean hasNext() {
65                  while (p < messages.length
66                          && !messages[p].getMode().equals(
67                                  getContext().getValue("mode"))) {
68                      ++p;
69                  }
70                  return p < messages.length;
71              }
72  
73              public String next() {
74                  currentMessageId = messages[p].getId();
75                  ++p;
76                  return currentMessageId;
77              }
78  
79              public void remove() {
80                  throw new UnsupportedOperationException();
81              }
82  
83          };
84      }
85  
86  
87      /***
88       * {@inheritDoc}
89       * @see net.sf.ediknight.edifact.setup.LineScanner#take(
90       *      int,
91       *      java.lang.String[])
92       */
93      @Override
94      public void take(int n, String[] groups) {
95          SegmentNodeImpl segmentNode = new SegmentNodeImpl();
96          int depth;
97          switch (n) {
98          case 0:
99          case 1:
100             segmentNode.setId(groups[1]);
101             segmentNode.setMaxOccurs(Integer.parseInt(groups[4]));
102             segmentNode.setMinOccurs("M".equals(groups[3]) ? 1 : 0);
103             depth = groups[5].length();
104             segmentNode.setDepth(depth);
105             if (!segmentNode.getId().startsWith("U")
106                     && getDirectory().getSegmentById(
107                             segmentNode.getId()) == null) {
108                 throw new RuntimeException(
109                         "Segment "
110                         + segmentNode.getId()
111                         + " not yet defined");
112             }
113             break;
114         case 2:
115         case 3:
116             segmentNode.setId("SG" + groups[1]);
117             segmentNode.setMaxOccurs(Integer.parseInt(groups[3]));
118             segmentNode.setMinOccurs("M".equals(groups[2]) ? 1 : 0);
119             depth = groups[4].length();
120             if (n == 3) {
121                 --depth;
122             }
123             segmentNode.setDepth(depth);
124             break;
125         default:
126             throw new RuntimeException();
127         }
128         MessageImpl message =
129             (MessageImpl) getDirectory().getMessageById(
130                     currentMessageId);
131         message.addSegment(segmentNode);
132     }
133 
134 }
135