001/* 002 * Copyright 2002-2020 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; 021 022/** 023 * Context that gets passed along a bean definition reading process, 024 * encapsulating all relevant configuration as well as state. 025 * 026 * @author Rob Harrop 027 * @author Juergen Hoeller 028 * @since 2.0 029 */ 030public class ReaderContext { 031 032 private final Resource resource; 033 034 private final ProblemReporter problemReporter; 035 036 private final ReaderEventListener eventListener; 037 038 private final SourceExtractor sourceExtractor; 039 040 041 /** 042 * Construct a new {@code ReaderContext}. 043 * @param resource the XML bean definition resource 044 * @param problemReporter the problem reporter in use 045 * @param eventListener the event listener in use 046 * @param sourceExtractor the source extractor in use 047 */ 048 public ReaderContext(Resource resource, ProblemReporter problemReporter, 049 ReaderEventListener eventListener, SourceExtractor sourceExtractor) { 050 051 this.resource = resource; 052 this.problemReporter = problemReporter; 053 this.eventListener = eventListener; 054 this.sourceExtractor = sourceExtractor; 055 } 056 057 public final Resource getResource() { 058 return this.resource; 059 } 060 061 062 // Errors and warnings 063 064 /** 065 * Raise a fatal error. 066 */ 067 public void fatal(String message, @Nullable Object source) { 068 fatal(message, source, null, null); 069 } 070 071 /** 072 * Raise a fatal error. 073 */ 074 public void fatal(String message, @Nullable Object source, @Nullable Throwable cause) { 075 fatal(message, source, null, cause); 076 } 077 078 /** 079 * Raise a fatal error. 080 */ 081 public void fatal(String message, @Nullable Object source, @Nullable ParseState parseState) { 082 fatal(message, source, parseState, null); 083 } 084 085 /** 086 * Raise a fatal error. 087 */ 088 public void fatal(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) { 089 Location location = new Location(getResource(), source); 090 this.problemReporter.fatal(new Problem(message, location, parseState, cause)); 091 } 092 093 /** 094 * Raise a regular error. 095 */ 096 public void error(String message, @Nullable Object source) { 097 error(message, source, null, null); 098 } 099 100 /** 101 * Raise a regular error. 102 */ 103 public void error(String message, @Nullable Object source, @Nullable Throwable cause) { 104 error(message, source, null, cause); 105 } 106 107 /** 108 * Raise a regular error. 109 */ 110 public void error(String message, @Nullable Object source, @Nullable ParseState parseState) { 111 error(message, source, parseState, null); 112 } 113 114 /** 115 * Raise a regular error. 116 */ 117 public void error(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) { 118 Location location = new Location(getResource(), source); 119 this.problemReporter.error(new Problem(message, location, parseState, cause)); 120 } 121 122 /** 123 * Raise a non-critical warning. 124 */ 125 public void warning(String message, @Nullable Object source) { 126 warning(message, source, null, null); 127 } 128 129 /** 130 * Raise a non-critical warning. 131 */ 132 public void warning(String message, @Nullable Object source, @Nullable Throwable cause) { 133 warning(message, source, null, cause); 134 } 135 136 /** 137 * Raise a non-critical warning. 138 */ 139 public void warning(String message, @Nullable Object source, @Nullable ParseState parseState) { 140 warning(message, source, parseState, null); 141 } 142 143 /** 144 * Raise a non-critical warning. 145 */ 146 public void warning(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) { 147 Location location = new Location(getResource(), source); 148 this.problemReporter.warning(new Problem(message, location, parseState, cause)); 149 } 150 151 152 // Explicit parse events 153 154 /** 155 * Fire a defaults-registered event. 156 */ 157 public void fireDefaultsRegistered(DefaultsDefinition defaultsDefinition) { 158 this.eventListener.defaultsRegistered(defaultsDefinition); 159 } 160 161 /** 162 * Fire a component-registered event. 163 */ 164 public void fireComponentRegistered(ComponentDefinition componentDefinition) { 165 this.eventListener.componentRegistered(componentDefinition); 166 } 167 168 /** 169 * Fire an alias-registered event. 170 */ 171 public void fireAliasRegistered(String beanName, String alias, @Nullable Object source) { 172 this.eventListener.aliasRegistered(new AliasDefinition(beanName, alias, source)); 173 } 174 175 /** 176 * Fire an import-processed event. 177 */ 178 public void fireImportProcessed(String importedResource, @Nullable Object source) { 179 this.eventListener.importProcessed(new ImportDefinition(importedResource, source)); 180 } 181 182 /** 183 * Fire an import-processed event. 184 */ 185 public void fireImportProcessed(String importedResource, Resource[] actualResources, @Nullable Object source) { 186 this.eventListener.importProcessed(new ImportDefinition(importedResource, actualResources, source)); 187 } 188 189 190 // Source extraction 191 192 /** 193 * Return the source extractor in use. 194 */ 195 public SourceExtractor getSourceExtractor() { 196 return this.sourceExtractor; 197 } 198 199 /** 200 * Call the source extractor for the given source object. 201 * @param sourceCandidate the original source object 202 * @return the source object to store, or {@code null} for none. 203 * @see #getSourceExtractor() 204 * @see SourceExtractor#extractSource 205 */ 206 @Nullable 207 public Object extractSource(Object sourceCandidate) { 208 return this.sourceExtractor.extractSource(sourceCandidate, this.resource); 209 } 210 211}