Announcement Announcement Module
Collapse
No announcement yet.
Is there any way to store arbitrary XML content in a value? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there any way to store arbitrary XML content in a value?

    Something like:

    Code:
    <bean class=...>
        <property name="xml">
            <value>
                <myxml>
                    <mydata>
                        <![CDATA[
                            my data
                        ]]>
                    </mydata>
                </myxml>
            </value>
         </property>
    </bean>
    I know that the DTD does not allow that for the value element but is there some workaround? (beside wrapping the entire value with CDATA which as you can see I am not able to do since I already have CDATA inside my xml)

    And the second question I have is somewhat related.
    If I reference an external xml file inside the property value:
    Code:
    <bean class=...>
        <property name="xml" value="myfile.xml"/>
    </bean>
    And I put myfile.xml in the same directory where THIS bean context definition file is located.
    How can I resolve then the absolute path of the file for my Java code? Basically, I need the path to be resolved by the Spring framework and not in my Java code which I want to keep independent of Spring.
    I have read about Resource abstraction but this will introduce Spring dependency so it won't work for me.

    Thanks!

  • #2
    You may solve both "issue" by extending Spring. All you need to do is create a new FactoryBean as follows:
    Code:
    pakage org.adam.factory;
    import org.springframework.beans.factory.FactoryBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.binding.support.Assert;
    import org.springframework.core.io.Resource;
    
    /**
     * @author <a href="mailto:[email protected]">Omar Irbouh</a>
     * @since 2005.10.27
     */
    public class ResourceFactoryBean implements FactoryBean, InitializingBean {
    
      private String content = null;
      private Resource location = null;
    
      public Object getObject() throws Exception {
        return content;
      }
    
      public Class getObjectType() {
        return content.getClass();
      }
    
      public boolean isSingleton() {
        return true;
      }
    
      public void afterPropertiesSet() throws Exception {
        Assert.notNull(location);
        //you may provide a better method for reading the inputStream
        byte[] buffer = new byte[location.getInputStream().available()];
        if (location.getInputStream().read(buffer) > 0) {
          content = new String(buffer);
        }
      }
    
      public Resource getLocation() {
        return location;
      }
    
      public void setLocation(Resource location) {
        this.location = location;
      }
    }
    This FactoryBean can be used as follows:
    Code:
    <bean class=...>
        <property name="xml">
          <bean class="org.adam.factory.ResourceFactoryBean">
            <property name="location" value="classpath:myfile.xml"/>
          </bean>
        </property>
    </bean>
    Hope this helps.

    Comment

    Working...
    X