001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.propertyeditors;
018:
019: import java.beans.PropertyEditorSupport;
020: import java.io.IOException;
021: import java.net.URI;
022: import java.net.URISyntaxException;
023:
024: import org.springframework.core.io.ClassPathResource;
025: import org.springframework.util.ClassUtils;
026: import org.springframework.util.ResourceUtils;
027:
028: /**
029: * Editor for <code>java.net.URI</code>, to directly populate a URI property
030: * instead of using a String property as bridge.
031: *
032: * <p>Supports Spring-style URI notation: any fully qualified standard URI
033: * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL,
034: * which will be resolved to a corresponding URI.
035: *
036: * <p>Note: A URI is more relaxed than a URL in that it does not require
037: * a valid protocol to be specified. Any scheme within a valid URI syntax
038: * is allowed, even without a matching protocol handler being registered.
039: *
040: * <p>Since <code>java.net.URI</code> is only available on JDK 1.4 or higher,
041: * this editor is only available on JDK 1.4 or higher as well.
042: *
043: * @author Juergen Hoeller
044: * @since 2.0.2
045: * @see java.net.URI
046: * @see URLEditor
047: */
048: public class URIEditor extends PropertyEditorSupport {
049:
050: private final ClassLoader classLoader;
051:
052: /**
053: * Create a new URIEditor,
054: * using the default ClassLoader for "classpath:" resources.
055: */
056: public URIEditor() {
057: this .classLoader = ClassUtils.getDefaultClassLoader();
058: }
059:
060: /**
061: * Create a new URIEditor,
062: * using the given ClassLoader for "classpath:" resources.
063: * @param classLoader the ClassLoader to use
064: */
065: public URIEditor(ClassLoader classLoader) {
066: this .classLoader = classLoader;
067: }
068:
069: public void setAsText(String text) throws IllegalArgumentException {
070: if (text == null) {
071: setValue(null);
072: } else if (text.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
073: ClassPathResource resource = new ClassPathResource(text
074: .substring(ResourceUtils.CLASSPATH_URL_PREFIX
075: .length()), this .classLoader);
076: try {
077: setValue(new URI(resource.getURL().toString()));
078: } catch (IOException ex) {
079: throw new IllegalArgumentException(
080: "Could not retrieve URI for " + resource + ": "
081: + ex.getMessage());
082: } catch (URISyntaxException ex) {
083: throw new IllegalArgumentException(
084: "Invalid URI syntax: " + ex);
085: }
086: } else {
087: try {
088: setValue(new URI(text));
089: } catch (URISyntaxException ex) {
090: throw new IllegalArgumentException(
091: "Invalid URI syntax: " + ex);
092: }
093: }
094: }
095:
096: public String getAsText() {
097: URI value = (URI) getValue();
098: return (value != null ? value.toString() : "");
099: }
100:
101: }
|