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 */
017 package org.apache.xbean.server.propertyeditor;
018
019 import java.beans.PropertyEditorSupport;
020 import java.net.URI;
021 import java.net.URISyntaxException;
022
023 /**
024 * URIEditor is a java beans property editor that can convert an URI to and from a String.
025 *
026 * @author Dain Sundstrom
027 * @version $Id$
028 * @since 2.0
029 */
030 public class URIEditor extends PropertyEditorSupport {
031 /**
032 * Converts the specified string value into an URI and stores the value in this instance.
033 * @param value the string to convert into an URI
034 * @throws IllegalArgumentException if the specified string value is not a valid URI
035 */
036 public void setAsText(String value) throws IllegalArgumentException {
037 try {
038 setValue(new URI(value));
039 } catch (URISyntaxException e) {
040 throw (IllegalArgumentException) new IllegalArgumentException().initCause(e);
041 }
042 }
043
044 /**
045 * Converts the stored URI value into a String.
046 * @return the string form of the current URI value
047 * @throws NullPointerException if the current URI is null
048 */
049 public String getAsText() {
050 URI uri = (URI) getValue();
051 if (uri == null) {
052 throw new NullPointerException("Current URI value is null");
053 }
054 String text = uri.toString();
055 return text;
056 }
057 }