001/* 002 * Copyright 2002-2017 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 * https://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 017package org.springframework.beans.factory.parsing; 018 019import org.springframework.core.io.Resource; 020import org.springframework.lang.Nullable; 021import org.springframework.util.Assert; 022 023/** 024 * Class that models an arbitrary location in a {@link Resource resource}. 025 * 026 * <p>Typically used to track the location of problematic or erroneous 027 * metadata in XML configuration files. For example, a 028 * {@link #getSource() source} location might be 'The bean defined on 029 * line 76 of beans.properties has an invalid Class'; another source might 030 * be the actual DOM Element from a parsed XML {@link org.w3c.dom.Document}; 031 * or the source object might simply be {@code null}. 032 * 033 * @author Rob Harrop 034 * @since 2.0 035 */ 036public class Location { 037 038 private final Resource resource; 039 040 @Nullable 041 private final Object source; 042 043 044 /** 045 * Create a new instance of the {@link Location} class. 046 * @param resource the resource with which this location is associated 047 */ 048 public Location(Resource resource) { 049 this(resource, null); 050 } 051 052 /** 053 * Create a new instance of the {@link Location} class. 054 * @param resource the resource with which this location is associated 055 * @param source the actual location within the associated resource 056 * (may be {@code null}) 057 */ 058 public Location(Resource resource, @Nullable Object source) { 059 Assert.notNull(resource, "Resource must not be null"); 060 this.resource = resource; 061 this.source = source; 062 } 063 064 065 /** 066 * Get the resource with which this location is associated. 067 */ 068 public Resource getResource() { 069 return this.resource; 070 } 071 072 /** 073 * Get the actual location within the associated {@link #getResource() resource} 074 * (may be {@code null}). 075 * <p>See the {@link Location class level javadoc for this class} for examples 076 * of what the actual type of the returned object may be. 077 */ 078 @Nullable 079 public Object getSource() { 080 return this.source; 081 } 082 083}