001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.xbean.propertyeditor; 018 019import java.beans.PropertyEditor; 020import java.beans.PropertyEditorManager; 021 022public class PropertyEditorConverter extends AbstractConverter { 023 public PropertyEditorConverter(final Class<?> type) { 024 super(type); 025 } 026 027 public String toStringImpl(final Object value) throws PropertyEditorException { 028 final PropertyEditor editor = PropertyEditorManager.findEditor(getType()); 029 editor.setValue(value); 030 try { 031 return editor.getAsText(); 032 } catch (final Exception e) { 033 throw new PropertyEditorException("Error while converting a \"" + getType().getSimpleName() + "\" to text " + 034 " using the property editor " + editor.getClass().getSimpleName(), e); 035 } 036 } 037 038 public Object toObjectImpl(final String text) throws PropertyEditorException { 039 final PropertyEditor editor = PropertyEditorManager.findEditor(getType()); 040 editor.setAsText(text); 041 try { 042 return editor.getValue(); 043 } catch (final Exception e) { 044 throw new PropertyEditorException("Error while converting \"" + text + "\" to a " + getType().getSimpleName() + 045 " using the property editor " + editor.getClass().getSimpleName(), e); 046 } 047 } 048}