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 <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class SerialAddressEditor extends AbstractPropertyEditor {
39      @Override
40      protected String toText(Object value) {
41          SerialAddress addr = (SerialAddress) value;
42          return addr.getName() + ':' +
43                 addr.getBauds() + ':' +
44                 toText(addr.getDataBits()) + ':' +
45                 toText(addr.getStopBits()) + ':' +
46                 toText(addr.getParity()) + ':' +
47                 toText(addr.getFlowControl());
48      }
49  
50      private String toText(DataBits bits) {
51          switch (bits) {
52          case DATABITS_5:
53              return "5";
54          case DATABITS_6:
55              return "6";
56          case DATABITS_7:
57              return "7";
58          case DATABITS_8:
59              return "8";
60          default:
61              throw new IllegalArgumentException("Unknown dataBits: " + bits);
62          }
63      }
64  
65      private String toText(StopBits bits) {
66          switch (bits) {
67          case BITS_1:
68              return "1";
69          case BITS_1_5:
70              return "1.5";
71          case BITS_2:
72              return "2";
73          default:
74              throw new IllegalArgumentException("Unknown stopBits: " + bits);
75          }
76      }
77  
78      private String toText(Parity parity) {
79          switch (parity) {
80          case EVEN:
81              return "even";
82          case ODD:
83              return "odd";
84          case MARK:
85              return "mark";
86          case NONE:
87              return "none";
88          case SPACE:
89              return "space";
90          default:
91              throw new IllegalArgumentException("Unknown parity: " + parity);
92          }
93      }
94  
95      private String toText(FlowControl flowControl) {
96          switch (flowControl) {
97          case NONE:
98              return "none";
99          case RTSCTS_IN:
100             return "rtscts-in";
101         case RTSCTS_OUT:
102             return "rtscts-out";
103         case XONXOFF_IN:
104             return "xonxoff-in";
105         case XONXOFF_OUT:
106             return "xonxoff-out";
107         default:
108             throw new IllegalArgumentException("Unknown flowControl: " + flowControl);
109         }
110     }
111 
112     @Override
113     protected Object toValue(String text) throws IllegalArgumentException {
114         String[] components = text.split(":");
115         if (components.length != 6) {
116             throw new IllegalArgumentException(
117                     "SerialAddress must have 6 components separated " +
118                     "by colon: " + text);
119         }
120 
121         return new SerialAddress(
122                 components[0].trim(),
123                 toBauds(components[1].trim()),
124                 toDataBits(components[2].trim()),
125                 toStopBits(components[3].trim()),
126                 toParity(components[4].trim()),
127                 toFlowControl(components[5].trim()));
128     }
129 
130     private int toBauds(String text) {
131         try {
132             return Integer.parseInt(text);
133         } catch (NumberFormatException e) {
134             throw new IllegalArgumentException("bauds: " + text);
135         }
136     }
137 
138     private DataBits toDataBits(String text) {
139         try {
140             return DataBits.valueOf("DATABITS_" + Integer.parseInt(text));
141         } catch (IllegalArgumentException e) {
142             throw new IllegalArgumentException("dataBits: " + text);
143         }
144     }
145 
146     private StopBits toStopBits(String text) {
147         try {
148             return StopBits.valueOf("BITS_" + text.replace('.', '_'));
149         } catch (IllegalArgumentException e) {
150             throw new IllegalArgumentException("stopBits: " + text);
151         }
152     }
153 
154     private Parity toParity(String text) {
155         try {
156             return Parity.valueOf(text.toUpperCase());
157         } catch (IllegalArgumentException e) {
158             throw new IllegalArgumentException("parity: " + text);
159         }
160     }
161 
162     private FlowControl toFlowControl(String text) {
163         String normalizedText = text.toUpperCase().replaceAll("(-|_)", "");
164         if (normalizedText.endsWith("IN")) {
165             normalizedText = normalizedText.substring(0, normalizedText.length() - 2) + "_IN";
166         }
167         if (normalizedText.endsWith("OUT")) {
168             normalizedText = normalizedText.substring(0, normalizedText.length() - 3) + "_OUT";
169         }
170 
171         try {
172             return FlowControl.valueOf(normalizedText);
173         } catch (IllegalArgumentException e) {
174             throw new IllegalArgumentException("flowControl: " + text);
175         }
176     }
177 }