KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > dq > DQMessageUtils


1 /*
2  * $Id: DQMessageUtils.java 3982 2006-11-22 14:28:01Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.dq;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.dom4j.Document;
19 import org.dom4j.Element;
20 import org.dom4j.io.SAXReader;
21 import org.mule.util.IOUtils;
22
23 import com.ibm.as400.access.AS400;
24 import com.ibm.as400.access.AS400Text;
25 import com.ibm.as400.access.CharacterFieldDescription;
26 import com.ibm.as400.access.Record;
27 import com.ibm.as400.access.RecordFormat;
28
29 /**
30  * DQMessageUtils utility class for DQMessage
31  */

32 public final class DQMessageUtils
33 {
34     /**
35      * Logger for this class
36      */

37     private static Log log = LogFactory.getLog(DQMessageUtils.class);
38
39     public static final String JavaDoc RECORD_DESCRIPTOR_KEY = "recordDescriptor";
40
41     /**
42      * The constructor
43      */

44     private DQMessageUtils()
45     {
46         super();
47     }
48
49     /**
50      * Returns a DQMessage corresponding to the byte array and the record format
51      *
52      * @param data The data
53      * @param format The record format
54      * @return The DQMessage
55      * @throws Exception Error during the data processing
56      */

57     public static synchronized DQMessage getDQMessage(final byte[] data, final RecordFormat format)
58         throws Exception JavaDoc
59     {
60         Record record = format.getNewRecord(data);
61
62         DQMessage message = new DQMessage();
63
64         String JavaDoc[] names = format.getFieldNames();
65         String JavaDoc name;
66         for (int i = 0; i < names.length; i++)
67         {
68             name = names[i];
69             message.addEntry(name, record.getField(name));
70         }
71
72         return message;
73     }
74
75     /**
76      * Returns a Record corresponding to the DQMessage and the record format
77      *
78      * @param msg The DQMessage
79      * @param format The record format
80      * @return The Record
81      */

82
83     public static synchronized Record getRecord(final DQMessage msg, final RecordFormat format)
84     {
85         Record rec = format.getNewRecord();
86
87         String JavaDoc name;
88         Object JavaDoc field;
89         String JavaDoc[] tab = format.getFieldNames();
90
91         for (int i = 0; i < tab.length; i++)
92         {
93             name = tab[i];
94             field = msg.getEntry(name);
95             if (field == null)
96             {
97                 field = "";
98             }
99             rec.setField(name, field);
100         }
101
102         return rec;
103     }
104
105     /**
106      * Returns the record format described in the record descriptor file.
107      *
108      * @param recordDescriptor The record descriptor filename
109      * @param as400 The as400
110      * @return The RecordFormat
111      * @throws Exception Error during the record format parsing
112      */

113     public static synchronized RecordFormat getRecordFormat(final String JavaDoc recordDescriptor, final AS400 as400)
114         throws Exception JavaDoc
115     {
116         log.debug("Record descriptor :" + recordDescriptor);
117
118         if (recordDescriptor == null)
119         {
120             throw new Exception JavaDoc("Failed to read record descriptor : recordDescriptor property is not set ");
121         }
122
123         InputStream JavaDoc input = null;
124         RecordFormat recordFormat = null;
125         try
126         {
127             input = IOUtils.getResourceAsStream(recordDescriptor, DQMessageUtils.class);
128             if (input == null)
129             {
130                 throw new Exception JavaDoc("Failed to read record descriptor (" + recordDescriptor
131                                 + ") cannot be found ");
132             }
133             recordFormat = parse(input, as400);
134         }
135         finally
136         {
137             if (input != null)
138             {
139                 input.close();
140             }
141         }
142
143         return recordFormat;
144     }
145
146     /**
147      * Returns the record format.
148      *
149      * @param stream The record descriptor inputstream
150      * @param as400 The as400
151      * @return The RecordFormat
152      * @throws Exception Error during the record format parsing
153      */

154     private static RecordFormat parse(final InputStream JavaDoc stream, final AS400 as400) throws Exception JavaDoc
155     {
156         SAXReader reader = new SAXReader();
157         Document document = reader.read(stream);
158         RecordFormat rec = new RecordFormat();
159
160         for (Iterator JavaDoc i = document.getRootElement().elementIterator(); i.hasNext();)
161         {
162             Element element = (Element)i.next();
163             String JavaDoc name = element.attributeValue("name");
164             int length = Integer.decode(element.attributeValue("length")).intValue();
165             addCharacterField(rec, length, name, as400);
166         }
167
168         return rec;
169     }
170
171     /**
172      * Add a charachter field in the record format.
173      *
174      * @param format The record format
175      * @param length The field length
176      * @param as400 The as400
177      * @param name The field name
178      */

179     private static void addCharacterField(final RecordFormat format,
180                                           final int length,
181                                           final String JavaDoc name,
182                                           final AS400 as400)
183     {
184         format.addFieldDescription(new CharacterFieldDescription(new AS400Text(length, as400), name));
185     }
186
187 }
188
Popular Tags