1 /***
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.xbean.spring.example;
18
19 import java.beans.PropertyEditorSupport;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23
24 /***
25 *
26 * Used to verify that per property PropertyEditors work correctly.
27 *
28 * @author chirino
29 */
30 public class MilliLittersPropertyEditor extends PropertyEditorSupport {
31
32 public void setAsText(String text) throws IllegalArgumentException {
33
34 Pattern p = Pattern.compile("^(//d+)//s*(l(iter)?)?$", Pattern.CASE_INSENSITIVE);
35 Matcher m = p.matcher(text);
36 if( m.matches() ) {
37 setValue(new Long(Long.parseLong(m.group(1))*1000));
38 return;
39 }
40
41 p = Pattern.compile("^(//d+)//s*(ml)?$", Pattern.CASE_INSENSITIVE);
42 m = p.matcher(text);
43 if( m.matches() ) {
44 setValue(new Long(Long.parseLong(m.group(1))));
45 return;
46 }
47
48 p = Pattern.compile("^(//d+)//s*pints?$", Pattern.CASE_INSENSITIVE);
49 m = p.matcher(text);
50 if( m.matches() ) {
51 long pints = Long.parseLong(m.group(1));
52 setValue(new Long( (long)(pints * 1750) ));
53 return;
54 }
55
56 throw new IllegalArgumentException("Could convert not to long (in ml) for "+ text);
57 }
58
59 public String getAsText() {
60 Long value = (Long) getValue();
61 return (value != null ? value.toString() : "");
62 }
63 }
64