乐趣区

关于java-ee: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 公布, 可转载但需申明原文出处。
企慕「优雅编码的艺术」深信游刃有余,致力扭转人生
欢送关注微信公账号:云栖简码 获取更多优质文章
更多文章关注笔者博客:云栖简码

退出移动版