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