Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD generate a MAP<String, Boolean> properties

I am trying to generate java class from XSD file, which contains Map<String, Boolean. I've read tutorial (http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html) which says that I have to use Adapter and Binding in order to achieve desired result.

But for some reason after generation property is a List instead of the Map. Could you please help me to figure out my mistake?

Thanks!

My XSD schema:

<xs:element name="AutoCompleteReq">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="queryString" type="xs:string"/>
    <xs:element name="boostingFactors" type="test:StringBooleanMapModeller" minOccurs="0"/>
   </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:complexType name="StringBooleanMapModeller">
  <xs:sequence>
    <xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
     <xs:complexType>
      <xs:sequence>
        <xs:element name="key" type="xs:string"/>
        <xs:element name="value" type="xs:boolean"/>
      </xs:sequence>
     </xs:complexType>
   </xs:element>
  </xs:sequence>
</xs:complexType>

My Binding file:

<jaxb:bindings schemaLocation="AutoCompleteReq.xsd">

  <jaxb:bindings node="//xs:element[@name='AutoCompleteReq']//xs:element[@name='boostingFactors']">
   <jaxb:property>
    <jaxb:baseType name="com.company.support.jaxb.StringBooleanMap&lt;String,Boolean&gt;" />
   </jaxb:property>
  </jaxb:bindings>

</jaxb:bindings>

</jaxb:bindings>

StringBooleanMap.java

package com.company.support.jaxb;

import java.util.HashMap;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap<String, Boolean> extends HashMap<String, Boolean> {

}

StringBooleanMapAdapter.java

package com.company.support.jaxb;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.company.xml.representation.StringBooleanMapModeller;

public class StringBooleanMapAdapter extends XmlAdapter<StringBooleanMapModeller, HashMap<String, Boolean>> {

    @Override
    public HashMap<String, Boolean> unmarshal(StringBooleanMapModeller v)
            throws Exception {

        HashMap<String, Boolean> map = new HashMap<String, Boolean>();
        for(StringBooleanMapModeller.Entry e : v.getEntry())
        {
            map.put(e.getKey(), e.isValue());
        }

        return map;
    }

    @Override
    public StringBooleanMapModeller marshal(HashMap<String, Boolean> v)
            throws Exception {

        StringBooleanMapModeller modeller = new StringBooleanMapModeller();
        for(Map.Entry<String, Boolean> entry : v.entrySet())
        {
            StringBooleanMapModeller.Entry e = new StringBooleanMapModeller.Entry();
            e.setKey(entry.getKey());
            e.setValue(entry.getValue());
            modeller.getEntry().add(e);
        }
        return modeller;
    }
}

pom.xml (piece for XSD generation)

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>Representations</id>
                        <configuration>
                            <schemaDirectory>XSD/Representation</schemaDirectory>
                            <packageName>com.company.xml.representation</packageName>
                            <bindingDirectory>XSD/Representation</bindingDirectory>
                            <outputDirectory>src/main/generated-sources</outputDirectory>
                            <staleFile>${project.build.directory}/generated-sources/jaxb/.representation</staleFile>
                            <clearOutputDir>false</clearOutputDir>
                        </configuration>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>Events</id>
                        <configuration>
                            <schemaDirectory>XSD/Representation/Event</schemaDirectory>
                            <packageName>com.company.xml.representation.event</packageName>
                            <bindingDirectory>XSD/Representation/Event</bindingDirectory>
                            <outputDirectory>src/main/generated-sources</outputDirectory>
                            <staleFile>${project.build.directory}/generated-sources/jaxb/.event</staleFile>
                            <clearOutputDir>false</clearOutputDir>
                        </configuration>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
like image 835
Wild Goat Avatar asked Oct 22 '13 17:10

Wild Goat


1 Answers

Remove <String, Boolean> from behind StringBooleanMap, it doesn't do what you think it's doing. String and Boolean are interpreted as generic parameters, not as java.lang.String and java.lang.Boolean.

Correct:

package com.company.support.jaxb;

import java.util.HashMap;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(StringBooleanMapAdapter.class)
public class StringBooleanMap extends HashMap<String, Boolean> {

}

Your code was interpreted as:

public class StringBooleanMap<S,B> extends HashMap<S,B> {

Also, change the jaxb:baseType reference to:

<jaxb:baseType name="com.company.support.jaxb.StringBooleanMap" />
like image 127
Erwin Bolwidt Avatar answered Nov 06 '22 15:11

Erwin Bolwidt