Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341801

Summary: Error on marshal when DOMHandler is used with a non-DOM property
Product: z_Archived Reporter: Blaise Doughan <blaise.doughan>
Component: EclipselinkAssignee: Blaise Doughan <blaise.doughan>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipselink.oxm-inbox
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on:    
Bug Blocks: 332770    
Attachments:
Description Flags
MOXy - Test Cases
none
MOXy - Fix
none
MOXy - Fix none

Description Blaise Doughan CLA 2011-04-04 11:04:23 EDT
An error is occurring during the marshal operation when a DOMHandler is used with a non-DOM field/property:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="main")
@XmlAccessorType(XmlAccessType.FIELD)
public class Main {

    private String name;

    private Integer maxInstances;

    @XmlAnyElement(value=ParameterHandler.class)
    private String parameters;

}

and 

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.DomHandler;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class ParameterHandler implements DomHandler<String, StreamResult> {

    private static final String PARAMETERS_START_TAG = "<parameters>";
    private static final String PARAMETERS_END_TAG = "</parameters>";

    private StringWriter xmlWriter = new StringWriter(); 

    public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
        return new StreamResult(xmlWriter);
    }

    public String getElement(StreamResult rt) {
        String xml = rt.getWriter().toString();
        int beginIndex = xml.indexOf(PARAMETERS_START_TAG) + PARAMETERS_START_TAG.length();
        int endIndex = xml.indexOf(PARAMETERS_END_TAG);
        return xml.substring(beginIndex, endIndex);
    }

    public Source marshal(String n, ValidationEventHandler errorHandler) {
        String xml = PARAMETERS_START_TAG + n.trim() + PARAMETERS_END_TAG;
        StringReader xmlReader = new StringReader(xml);
        return new StreamSource(xmlReader);
    }

}

With XML:

<main>
  <name>JET</name>
  <maxInstances>5</maxInstances>
  <parameters>
    <a>1</a>
    <b>
      <b1>test1</b1>
      <b2>test2</b2>
    </b>
  </parameters>
</main>
Comment 1 Blaise Doughan CLA 2011-04-04 13:46:33 EDT
Created attachment 192488 [details]
MOXy - Test Cases
Comment 2 Blaise Doughan CLA 2011-04-04 13:47:00 EDT
Created attachment 192489 [details]
MOXy - Fix
Comment 3 Blaise Doughan CLA 2011-04-05 10:24:39 EDT
Created attachment 192562 [details]
MOXy - Fix
Comment 4 Blaise Doughan CLA 2011-04-05 10:35:22 EDT
Fix checked into trunk at rev:  9206

Code reviewed by:  Matt MacIvor

Fix Description:
The DOMHandler is now leveraged during the marshal operation when necessary.
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:03:19 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink