View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.serial;
21  
22  import java.beans.PropertyEditor;
23  
24  import org.apache.mina.integration.beans.AbstractPropertyEditor;
25  import org.apache.mina.transport.serial.SerialAddress.DataBits;
26  import org.apache.mina.transport.serial.SerialAddress.FlowControl;
27  import org.apache.mina.transport.serial.SerialAddress.Parity;
28  import org.apache.mina.transport.serial.SerialAddress.StopBits;
29  
30  /**
31   * A {@link PropertyEditor} which converts a {@link String} into a
32   * {@link SerialAddress} and vice versa.  Valid values specify 6 address
33   * components separated by colon (e.g. <tt>COM1:9600:7:1:even:rtscts-in</tt>);
34   * port name, bauds, data bits, stop bits, parity and flow control respectively.
35   *
36   * @author The Apache MINA Project (dev@mina.apache.org)
37   * @version $Revision: 645790 $, $Date: 2008-04-08 10:38:03 +0200 (mar, 08 avr 2008) $
38   */
39  public class SerialAddressEditor extends AbstractPropertyEditor {
40      @Override
41      protected String toText(Object value) {
42          SerialAddress addr = (SerialAddress) value;
43          return addr.getName() + ':' +
44                 addr.getBauds() + ':' +
45                 toText(addr.getDataBits()) + ':' +
46                 toText(addr.getStopBits()) + ':' +
47                 toText(addr.getParity()) + ':' +
48                 toText(addr.getFlowControl());
49      }
50  
51      private String toText(DataBits bits) {
52          switch (bits) {
53          case DATABITS_5:
54              return "5";
55          case DATABITS_6:
56              return "6";
57          case DATABITS_7:
58              return "7";
59          case DATABITS_8:
60              return "8";
61          default:
62              throw new IllegalArgumentException("Unknown dataBits: " + bits);
63          }
64      }
65  
66      private String toText(StopBits bits) {
67          switch (bits) {
68          case BITS_1:
69              return "1";
70          case BITS_1_5:
71              return "1.5";
72          case BITS_2:
73              return "2";
74          default:
75              throw new IllegalArgumentException("Unknown stopBits: " + bits);
76          }
77      }
78  
79      private String toText(Parity parity) {
80          switch (parity) {
81          case EVEN:
82              return "even";
83          case ODD:
84              return "odd";
85          case MARK:
86              return "mark";
87          case NONE:
88              return "none";
89          case SPACE:
90              return "space";
91          default:
92              throw new IllegalArgumentException("Unknown parity: " + parity);
93          }
94      }
95  
96      private String toText(FlowControl flowControl) {
97          switch (flowControl) {
98          case NONE:
99              return "none";
100         case RTSCTS_IN:
101             return "rtscts-in";
102         case RTSCTS_OUT:
103             return "rtscts-out";
104         case XONXOFF_IN:
105             return "xonxoff-in";
106         case XONXOFF_OUT:
107             return "xonxoff-out";
108         default:
109             throw new IllegalArgumentException("Unknown flowControl: " + flowControl);
110         }
111     }
112 
113     @Override
114     protected Object toValue(String text) throws IllegalArgumentException {
115         String[] components = text.split(":");
116         if (components.length != 6) {
117             throw new IllegalArgumentException(
118                     "SerialAddress must have 6 components separated " +
119                     "by colon: " + text);
120         }
121 
122         return new SerialAddress(
123                 components[0].trim(),
124                 toBauds(components[1].trim()),
125                 toDataBits(components[2].trim()),
126                 toStopBits(components[3].trim()),
127                 toParity(components[4].trim()),
128                 toFlowControl(components[5].trim()));
129     }
130 
131     private int toBauds(String text) {
132         try {
133             return Integer.parseInt(text);
134         } catch (NumberFormatException e) {
135             throw new IllegalArgumentException("bauds: " + text);
136         }
137     }
138 
139     private DataBits toDataBits(String text) {
140         try {
141             return DataBits.valueOf("DATABITS_" + Integer.parseInt(text));
142         } catch (IllegalArgumentException e) {
143             throw new IllegalArgumentException("dataBits: " + text);
144         }
145     }
146 
147     private StopBits toStopBits(String text) {
148         try {
149             return StopBits.valueOf("BITS_" + text.replace('.', '_'));
150         } catch (IllegalArgumentException e) {
151             throw new IllegalArgumentException("stopBits: " + text);
152         }
153     }
154 
155     private Parity toParity(String text) {
156         try {
157             return Parity.valueOf(text.toUpperCase());
158         } catch (IllegalArgumentException e) {
159             throw new IllegalArgumentException("parity: " + text);
160         }
161     }
162 
163     private FlowControl toFlowControl(String text) {
164         String normalizedText = text.toUpperCase().replaceAll("(-|_)", "");
165         if (normalizedText.endsWith("IN")) {
166             normalizedText = normalizedText.substring(0, normalizedText.length() - 2) + "_IN";
167         }
168         if (normalizedText.endsWith("OUT")) {
169             normalizedText = normalizedText.substring(0, normalizedText.length() - 3) + "_OUT";
170         }
171 
172         try {
173             return FlowControl.valueOf(normalizedText);
174         } catch (IllegalArgumentException e) {
175             throw new IllegalArgumentException("flowControl: " + text);
176         }
177     }
178 }