Announcement Announcement Module
Collapse
No announcement yet.
XML entities in application context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML entities in application context

    I have an application context defined using XML config files with the Spring 2.5 XSD, and would like to use XML entities to provide an extension point for customizing a client's installation.

    I started with an XML config file defined in the customary fashion:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:sec="http://www.springframework.org/schema/security" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xsi:schemaLocation="
           	http://www.springframework.org/schema/beans 
           	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
           	http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
      <!-- Define beans here... -->
    </beans>
    Now, I would like to define some entities that will be resolved using a filesystem resource. A couple of Google searches suggest adding the following inline DTD snippet:

    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "springframework.org/dtd/spring-beans-2.0.dtd"
    [
    <!ENTITY % entities SYSTEM "my/path/entities.ent" >
    %entities;
    ]>
    The problem I'm running into is that the Spring 2.5 XSD referenced in my document root seems to be disregarded in favor of the more restrictive DTD, and the SAX parser no longer accepts my config file as valid XML. I get exceptions such as:

    org.xml.sax.SAXParseException: Attribute "xmlns" must be declared for element type "beans".

    I need the more flexible XSD-based validation because I'm using elements from the Spring Security namespace that are not allowed by the simple DTD. Any suggestions on how to get DTD-based XML entity declarations to co-exist with the Spring 2.5 XSD?

  • #2
    OK, upon closer inspection it's a JAXP DOM parser and not a SAX parser. It turns out that XmlBeanDefinitionReader attempts to guess the validation mode by peeking at the config file contents. If it sees a DTD DOCTYPE declaration prior to the first opening tag, it configures the DOM parser to use DTD-based validation; otherwise, it uses XSD-based validation. You can change this behavior using the XmlBeanDefinitionReader.setValidationMode(int) method.

    In order to set the validation behavior when initializing my web application context, I found it necessary to extend the default XmlWebApplicationContext class:

    Code:
    package com.example;
    
    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    
    public class MyApplicationContext extends XmlWebApplicationContext {
    
      public MyApplicationContext() {
        super();
      }
    
      @Override
      protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
        beanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
      }
    }
    And then add a context parameter to my web.xml to override the default application context with the new custom context class:

    Code:
      <context-param>
        <param-name>contextClass</param-name>
        <param-value>com.example.MyApplicationContext</param-value>
      </context-param>

    Comment

    Working...
    X