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