Browse Source

avoid potential NPE (SPR-5930)

conversation
Juergen Hoeller 16 years ago
parent
commit
68363f17a7
  1. 21
      org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/FileEditor.java

21
org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/FileEditor.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,9 +79,14 @@ public class FileEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
if (!StringUtils.hasText(text)) {
setValue(null);
return;
}
// Check whether we got an absolute file path without "file:" prefix. // Check whether we got an absolute file path without "file:" prefix.
// For backwards compatibility, we'll consider those as straight file path. // For backwards compatibility, we'll consider those as straight file path.
if (StringUtils.hasText(text) && !ResourceUtils.isUrl(text)) { if (!ResourceUtils.isUrl(text)) {
File file = new File(text); File file = new File(text);
if (file.isAbsolute()) { if (file.isAbsolute()) {
setValue(file); setValue(file);
@ -92,10 +97,11 @@ public class FileEditor extends PropertyEditorSupport {
// Proceed with standard resource location parsing. // Proceed with standard resource location parsing.
this.resourceEditor.setAsText(text); this.resourceEditor.setAsText(text);
Resource resource = (Resource) this.resourceEditor.getValue(); Resource resource = (Resource) this.resourceEditor.getValue();
// Non URLs will be treated as relative paths if the resource was not found
if(ResourceUtils.isUrl(text) || resource.exists()) { // If it's a URL or a path pointing to an existing resource, use it as-is.
if (ResourceUtils.isUrl(text) || resource.exists()) {
try { try {
setValue(resource != null ? resource.getFile() : null); setValue(resource.getFile());
} }
catch (IOException ex) { catch (IOException ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -103,9 +109,8 @@ public class FileEditor extends PropertyEditorSupport {
} }
} }
else { else {
// Create a relative File reference and hope for the best // Create a relative File reference and hope for the best.
File file = new File(text); setValue(new File(text));
setValue(file);
} }
} }

Loading…
Cancel
Save