wsimport not generating type class - java

I have the following element in my web service request and used wsimport to generate client. I also have other enum types which get classes generated. As this particular type contains values like 1-Critical I think it fails to generate enum type but treats it as String.
<xsd:element type="s0:UrgencyType" name="Urgency" nillable="true" minOccurs="0" maxOccurs="1"/>
<xsd:simpleType name="UrgencyType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="1-Critical"/>
<xsd:enumeration value="2-High"/>
<xsd:enumeration value="3-Medium"/>
<xsd:enumeration value="4-Low"/>
</xsd:restriction>
</xsd:simpleType>
The generated parameter as below.
#WebParam(name = "Urgency", targetNamespace = "urn:GIL:Create_WS")
String urgency,
I want the variable urgency type to be UrgencyType.

Related

Translate enum value to Apache Axis SimpleType

I have programmed a method that translates a normal java enum value to a value of a class generated from a SimpleType in an xsd file. The enum and the SimpleType are identical in content. Unfortunately my method returns null.
public enum Type {
VALUE_A, VALUE_B;
public static com.name.phi.framework.ws.mediaScanner.Type convertToWSDL(Type type) {
return com.name.phi.framework.ws.mediaScanner.Type.Factory.fromValue(type.toString());
}
}
In the xsd file the SimpleType is defined as follows:
<xsd:simpleType name="Type">
<xsd:annotation>
<xsd:documentation>Type definition<br/>
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="VALUE_A"/>
<xsd:enumeration value="VALUE_B"/>
</xsd:restriction>
</xsd:simpleType>
Using the following plugin to generate the java classes:
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
Does anyone have an idea how I can translate the enum value?. Thanks in advance.

Issue with generating sources from xsd schema

I'm getting the following error when generating sources for my project. I have extracted a few common types to a schema called CommonTypes.xsd and I get the following error:
org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.
The following simple type is defined in my CommonTypes.xsd schema as below:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:unit="http://www.mywebsite.com/unit"
xmlns:types="http://www.mywebsite.com/types"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.mywebsite.com//types">
<!-- import types -->
<xsd:import namespace="http://www.mywebsite.com/unit"/>
<!-- other common types -->
<xsd:simpleType name="nonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:pattern value=".*[^\s].*"/>
</xsd:restriction>
</xsd:simpleType>
And line 241 which causes the error is below:
<xsd:complexType name="Message">
<xsd:simpleContent>
<xsd:extension base="nonEmptyString">
<xsd:attribute type="xsd:string" name="code" use="required"/>
<xsd:attribute name="category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="Error"/>
<xsd:enumeration value="Info"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute type="xsd:string" name="component" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Do you have any idea what may cause the error? I've tried searching through StackOverflow and experimenting with the targetNamespace and xmlns, but with no success.
You are trying to refer to a simple type which has
name 'nonEmptyString'
namespace ""
But the simple type 'nonEmptyString' is defined in this XSD, which has targetNamespace="http://www.mywebsite.com//types". So you should be referring to a simple type which has
name 'nonEmptyString'
namespace "http://www.mywebsite.com//types"
You simply need to change this:
<xsd:extension base="nonEmptyString">
to this:
<xsd:extension base="types:nonEmptyString">
You need to import your nonEmptyString in the corresponding namespace and make this namespace referenceable via the prefix.
To do this, add xmlns:types="http://www.mywebsite.com/types" to xsd:schema of the importing schema.
Also provide the namespace in xsd:import of the importing schema. Should be something like:
<xsd:import
namespace="http://www.mywebsite.com/types"
schemaLocation="calc/lps/CommonTypes.xsd"/>
Then you should be able to reference your nonEmptyString type as types:nonEmptyString.

Element#getAttribute not returning default value when attribute isn't present

I have the following XML file:
<el id="el1">content</el>
The part of my file which defines the element above is as follows:
<xsd:element name="el" type="myns:el" />
<xsd:simpleType name="state">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="enabled" />
<xsd:enumeration value="disabled" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="el">
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="state" type="myns:state"
default="enabled" />
</xsd:complexType>
My Java code which is supposed to obtain the default value for state looks like the following:
Element e = (Element) ns.getElementsByTagName("el").item(0);
System.out.println(e.getAttribute("state"));
The code above always prints an empty string if I don't set the state attribute in the XML file. Why is that the case? According to the Java documentation, getAttribute returns
The Attr value as a string, or the empty string if that attribute does
not have a specified or default value.
My question is: why doesn't getAttribute return the default value when a value is not specified?

element with default enumeration value

Is it possible to set a default value for an element with type of enumeration?
I tried the following:
<xsd:element name="progressType" type="tns:progressType" default="Type A" nillable="false"/>
where tns:progressType is an enumeration:
<xsd:simpleType name="progressType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Type A" />
<xsd:enumeration value="Type B" />
</xsd:restriction>
</xsd:simpleType>
However, when I'm instantiating any object which contains the progressType attribute object.getProgressType() is always null.

Character limit in webservice at wsdl level

I hace deployed a java WebService in an application using WebSphere AST. I need to limit the length of some fields (Strings) and I wondered if I can do that at the wsdl instead of coding some validations at java level.
I mean, if rigth now I have the elements defined like this:
<element name="code" nillable="true" type="xsd:string"/>
Can I set some property that restrict the max length of "code"?
Thank you.
<element name="code" nillable="true" type="xsd:string"/>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="2"/>
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>

Categories