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.common.edi;
21  
22  import net.sf.ediknight.common.edi.directory.CompositeElementImpl;
23  import net.sf.ediknight.edi.DocumentHandler;
24  import net.sf.ediknight.edi.EDIParseException;
25  import net.sf.ediknight.edi.SyntaxHandler;
26  import net.sf.ediknight.edi.UnknownMessageException;
27  import net.sf.ediknight.edi.directory.CompositeElement;
28  import net.sf.ediknight.edi.directory.Directory;
29  import net.sf.ediknight.edi.directory.DirectoryEnumerator;
30  import net.sf.ediknight.edi.directory.Element;
31  
32  
33  /***
34   *
35   */
36  public final class DocumentSyntaxHandler
37  implements SyntaxHandler {
38  
39      /*** */
40      private DirectoryEnumerator enumerator;
41  
42      /*** */
43      private String lastSegmentId;
44  
45      /*** */
46      private CompositeElement lastCompositeElement;
47  
48      /*** */
49      private Element lastElement;
50  
51      /*** */
52      private DocumentHandler documentHandler;
53  
54  
55      /***
56       * Creates a syntax handler from a given document handler.
57       *
58       * @param directory a directory
59       * @param documentHandler the document handler
60       */
61      public DocumentSyntaxHandler(Directory directory,
62              DocumentHandler documentHandler) {
63          this.enumerator = directory.getEnumerator(this);
64          this.documentHandler = documentHandler;
65          documentHandler.startDocument();
66      }
67  
68  
69      /***
70       * Start a segment group.
71       *
72       * @param segmentId the segment identifier
73       */
74      public void startSegmentGroup(String segmentId) {
75          finishSegment();
76  //        System.err.println("startSegmentGroup: <" + segmentId + ">");
77          documentHandler.startSegmentGroup(segmentId);
78      }
79  
80  
81      /***
82       * Closes a segment group.
83       *
84       * @param segmentId the segment identifier
85       */
86      public void endSegmentGroup(String segmentId) {
87          finishSegment();
88  //        System.err.println("endSegmentGroup: <" + segmentId + ">");
89          documentHandler.endSegmentGroup(segmentId);
90      }
91  
92  
93      /***
94       * {@inheritDoc}
95       * @see net.sf.ediknight.edi.SyntaxHandler#nextSegment(
96       *      java.lang.String)
97       */
98      public void nextSegment(String segmentId)
99      throws EDIParseException {
100         finishSegment();
101         enumerator.nextSegment(segmentId);
102 //        System.err.println("nextSegment: <" + segmentId + ">");
103         documentHandler.startSegment(segmentId);
104         lastSegmentId = segmentId;
105         lastElement = null;
106     }
107 
108 
109     /***
110      * {@inheritDoc}
111      * @see net.sf.ediknight.edi.SyntaxHandler
112      *      #nextCompositeElement()
113      */
114     public void nextCompositeElement() {
115         finishComposite();
116         enumerator.nextComposite();
117         if (enumerator.hasMoreElements()) {
118             lastElement = enumerator.nextElement();
119             if (lastElement instanceof CompositeElementImpl) {
120                 documentHandler.startCompositeElement(
121                         lastElement.getId());
122                 lastCompositeElement =
123                     (CompositeElementImpl) lastElement;
124                 lastElement = null;
125                 nextSimpleElement();
126             } else {
127                 String elementId = lastElement.getId();
128 //                System.out.println("simple: " + elementId);
129                 documentHandler.startSimpleElement(elementId);
130                 lastCompositeElement = null;
131             }
132         }
133     }
134 
135 
136     /***
137      * {@inheritDoc}
138      * @see net.sf.ediknight.edi.SyntaxHandler
139      *      #nextSimpleElement()
140      */
141     public void nextSimpleElement() {
142         finishSimple();
143         if (enumerator.hasMoreElements()) {
144             lastElement = enumerator.nextElement();
145             if (lastElement instanceof CompositeElementImpl) {
146                 throw new RuntimeException(
147                         "No composite element allowed here");
148             }
149             String elementId = lastElement.getId();
150 //            System.out.println("simple: " + elementId);
151             documentHandler.startSimpleElement(elementId);
152         } else {
153             throw new RuntimeException("No more elements");
154         }
155     }
156 
157 
158     /***
159      * @see net.sf.ediknight.edi.SyntaxHandler#finish()
160      */
161     public void finish() {
162         finishSegment();
163         documentHandler.endDocument();
164     }
165 
166 
167     /***
168      * {@inheritDoc}
169      * @see net.sf.ediknight.edi.SyntaxHandler#characters(
170      *      java.lang.String)
171      */
172     public void characters(String value)
173     throws EDIParseException {
174         if (lastElement != null) {
175             documentHandler.characters(value);
176             if ("0065".equals(lastElement.getId())) {
177                 enumerator.setMessageId(value);
178             } else if ("D143".equals(lastElement.getId())) {
179                 enumerator.setMessageId("M" + value);
180             }
181         }
182     }
183 
184 
185     public void setMessageId(String messageId)
186     throws UnknownMessageException {
187         enumerator.setMessageId(messageId);
188     }
189 
190 
191     /***
192      *
193      */
194     private void finishSimple() {
195         if (lastElement != null) {
196             documentHandler.endSimpleElement(
197                     lastElement.getId());
198             lastElement = null;
199         }
200     }
201 
202 
203     /***
204      *
205      */
206     private void finishComposite() {
207         finishSimple();
208         if (lastCompositeElement != null) {
209             documentHandler.endCompositeElement(
210                     lastCompositeElement.getId());
211             lastCompositeElement = null;
212         }
213     }
214 
215 
216     /***
217      *
218      */
219     private void finishSegment() {
220         finishComposite();
221         if (lastSegmentId != null) {
222             documentHandler.endSegment(lastSegmentId);
223             lastSegmentId = null;
224         }
225     }
226 
227 }
228 
229