1. 程式人生 > 實用技巧 >《spring原始碼解讀》 - ioc之驗證模型獲取

《spring原始碼解讀》 - ioc之驗證模型獲取


我們上一篇文章最後呼叫到 `org.springframework.beans.factory.xml. XmlBeanDefinitionReader#doLoadDocument(...)

` 方法,該方法主要程式碼如下:

protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
		//getValidationModeForResource是資料驗證模型
		return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
				getValidationModeForResource(resource), isNamespaceAware());
	}

我們這篇文章主要以 #getValidationModeForResource(...) 方法作為切入,來分析一下驗證模型的主要方法,關於spring中的資料驗證、繫結等內容我們在後面文章一點點的來挖掘

1.getValidationModeForResource

/**
	 * 禁止只用驗證模型
	 * Indicates that the validation should be disabled.
	 */
	public static final int VALIDATION_NONE = XmlValidationModeDetector.VALIDATION_NONE;
	/**
	 * 使用自動檢測驗證模型
	 * Indicates that the validation mode should be detected automatically.
	 */
	public static final int VALIDATION_AUTO = XmlValidationModeDetector.VALIDATION_AUTO;
	/**
	 * 使用DTD驗證模型
	 * Indicates that DTD validation should be used.
	 */
	public static final int VALIDATION_DTD = XmlValidationModeDetector.VALIDATION_DTD;
	/**
	 * 使用XSD驗證模型
	 * Indicates that XSD validation should be used.
	 */
	public static final int VALIDATION_XSD = XmlValidationModeDetector.VALIDATION_XSD;
	/**
	 * 確定當前 resource資源需要的驗證模型 預設自動模式
	 * 如果顯式的配置了驗證模型,則用配置的,如果沒有則從Resource中來獲取
	 * Determine the validation mode for the specified {@link Resource}.
	 * If no explicit validation mode has been configured, then the validation
	 * mode gets {@link #detectValidationMode detected} from the given resource.
	 * <p>Override this method if you would like full control over the validation
	 * mode, even when something other than {@link #VALIDATION_AUTO} was set.
	 * @see #detectValidationMode
	 */
	protected int getValidationModeForResource(Resource resource) {
		// <1> 獲取指定的驗證模式
		int validationModeToUse = getValidationMode();
		// <2> 如果有指定的模式 則直接返回,
		if (validationModeToUse != VALIDATION_AUTO) {
			return validationModeToUse;
		}
		//<3> 沒有指定的 那麼通resource中去獲取
		int detectedMode = detectValidationMode(resource);
		if (detectedMode != VALIDATION_AUTO) {
			return detectedMode;
		}
		// <4> 若以上都不滿足則使用XSD模式
		// Hmm, we didn't get a clear indication... Let's assume XSD,
		// since apparently no DTD declaration has been found up until
		// detection stopped (before finding the document's root tag).
		return VALIDATION_XSD;
	}

對上述程式碼我們做簡單分析

  • <1>處呼叫 #getValidationMode() 方法來獲取指定的驗證模式 ,這裡的 validationMode 如果是外部手動設定的,那麼就直接返回,
/**
	 * Set the validation mode to use. Defaults to {@link #VALIDATION_AUTO}.
	 * <p>Note that this only activates or deactivates validation itself.
	 * If you are switching validation off for schema files, you might need to
	 * activate schema namespace support explicitly: see {@link #setNamespaceAware}.
	 */
	public void setValidationMode(int validationMode) {
		this.validationMode = validationMode;
	}
	/**
	 * Return the validation mode to use.
	 */
	public int getValidationMode() {
		return this.validationMode;
	}
  • <3>處呼叫 #detectValidationMode() 方法來自動獲取驗證模型,程式碼如下:
/**
	 * 進行自動檢測指定的XML用那種檢測模型
	 * Detect which kind of validation to perform on the XML file identified
	 * by the supplied {@link Resource}. If the file has a {@code DOCTYPE}
	 * definition then DTD validation is used otherwise XSD validation is assumed.
	 * <p>Override this method if you would like to customize resolution
	 * of the {@link #VALIDATION_AUTO} mode.
	 */
	protected int detectValidationMode(Resource resource) {
		// <1> 資源是否開啟
		if (resource.isOpen()) {
			throw new BeanDefinitionStoreException(
					"Passed-in Resource [" + resource + "] contains an open stream: " +
					"cannot determine validation mode automatically. Either pass in a Resource " +
					"that is able to create fresh streams, or explicitly specify the validationMode " +
					"on your XmlBeanDefinitionReader instance.");
		}
		InputStream inputStream;
		try {
			inputStream = resource.getInputStream();
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException(
					"Unable to determine validation mode for [" + resource + "]: cannot open InputStream. " +
					"Did you attempt to load directly from a SAX InputSource without specifying the " +
					"validationMode on your XmlBeanDefinitionReader instance?", ex);
		}
		try {
            ////<2> 呼叫XmlValidationModeDetector 中的真實實現 傳入 inputStream
			return this.validationModeDetector.detectValidationMode(inputStream);
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException("Unable to determine validation mode for [" +
					resource + "]: an error occurred whilst reading from the InputStream.", ex);
		}
	}

2.detectValidationMode

上面 <3>處呼叫了 #detectValidationMode() 方法,這裡 validationModeDetector 物件是類中的一個不可變成員變數 定義如下:

private final XmlValidationModeDetector validationModeDetector = new XmlValidationModeDetector();

#detectValidationMode() 方法的具體實現如下:

/**
	 * 通過指定的 inputStream 來檢測 XML 中的驗證模型
	 * Detect the validation mode for the XML document in the supplied {@link InputStream}.
	 * Note that the supplied {@link InputStream} is closed by this method before returning.
	 * @param inputStream the InputStream to parse
	 * @throws IOException in case of I/O failure
	 * @see #VALIDATION_DTD
	 * @see #VALIDATION_XSD
	 */
	public int detectValidationMode(InputStream inputStream) throws IOException {
		//<1> 通過 BufferedReader 讀取inputStream
		// Peek into the file to look for DOCTYPE.
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		try {
			//<2> 是否是DTD驗證模式 ,預設否
			boolean isDtdValidated = false;
			String content;
			// <3> 逐行讀取內容
			while ((content = reader.readLine()) != null) {
				content = consumeCommentTokens(content);
				// <4> 如果是註釋或者為空這跳過
				if (this.inComment || !StringUtils.hasText(content)) {
					continue;
				}
				// <5> 校驗是否包含 DOCTYPE 若包含則為DTD模式
				if (hasDoctype(content)) {
					isDtdValidated = true;
					break;
				}
				// <6> 校驗是否是開啟的標籤, 如果一行有 <  並且後面跟著是字母,那麼返回
				if (hasOpeningTag(content)) {
					// End of meaningful data...
					break;
				}
			}
			// 確定解析方式
			return (isDtdValidated ? VALIDATION_DTD : VALIDATION_XSD);
		}
		catch (CharConversionException ex) {
			// Choked on some character encoding...
			// Leave the decision up to the caller.
			return VALIDATION_AUTO;
		}
		finally {
			//關閉流
			reader.close();
		}
	}
  • 上述程式碼中<5>處校驗是否包含了 DOCTYPE 字元,具體程式碼如下:
/**
	 * The token in a XML document that declares the DTD to use for validation
	 * and thus that DTD validation is being used.
	 */
	private static final String DOCTYPE = "DOCTYPE";
	/**
	 * 判斷是否包含 DOCTYPE
	 * Does the content contain the DTD DOCTYPE declaration?
	 */
	private boolean hasDoctype(String content) {
		return content.contains(DOCTYPE);
	}
  • <6>處進行了標籤的開啟判斷,程式碼如下:
/**
	 * Does the supplied content contain an XML opening tag. If the parse state is currently
	 * in an XML comment then this method always returns false. It is expected that all comment
	 * tokens will have consumed for the supplied content before passing the remainder to this method.
	 */
	private boolean hasOpeningTag(String content) {
		if (this.inComment) {
			return false;
		}
		int openTagIndex = content.indexOf('<'); // 獲取字元中 < 的索引
		return (openTagIndex > -1  // 存在 <
				&& (content.length() > openTagIndex + 1) // 並且後面還有內容
				&&Character.isLetter(content.charAt(openTagIndex + 1)));//後面必須是字母
	}
  • 如果丟擲異常時 CharConversionException 則預設設定為自動 VALIDATION_AUTO

本文由AnonyStar 釋出,可轉載但需宣告原文出處。
仰慕「優雅編碼的藝術」 堅信熟能生巧,努力改變人生
歡迎關注微信公賬號 :雲棲簡碼 獲取更多優質文章
更多文章關注筆者部落格 :雲棲簡碼