SAXParserException while trying to read the xsd - xsd

My project has a JAXWS class where an XSD is read and finally validated in another method.
Here is the java code where they read the XSD
private static final ThreadLocal <DocumentBuilder> parser =
new ThreadLocal <DocumentBuilder> () {
#Override protected DocumentBuilder initialValue() {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// loading Schema file
//System.out.println("Hi .. am in parser");
DocumentBuilder parserBuilder = null;
if(finalXsd !=null){
logger.info("finalXsd FILE"+finalXsd);
Source schemaFile = new StreamSource(new File(finalXsd));
try{
logger.info ("place-0") ;
Schema schema = factory.newSchema(schemaFile);
logger.info ("place-1") ;
docBuilderFactory.setValidating(false); // true is used for DTD based validation, for schema validation set to false
logger.info ("place-2") ;
docBuilderFactory.setSchema(schema);
logger.info ("place-3") ;
parserBuilder = docBuilderFactory.newDocumentBuilder();
logger.info ("place-4") ;
parserBuilder.setErrorHandler( new JythonResponseErrorHandler() );
logger.info ("place-5") ;
}catch(SAXException s)
{
logger.error("SAXException in ThreadLocal.");
s.printStackTrace() ;
return null;
} catch (ParserConfigurationException e) {
e.printStackTrace();
logger.error("ParserConfigurationException in ThreadLocal.");
return null;
}
}
return parserBuilder;
}
};
XSD FILE (which the above code trying to read) :
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2010 rel. 2 (http://www.altova.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TAG">
<xs:complexType>
<xs:attribute name="name" use="required" type="xs:string" />
<xs:attribute name="value" use="optional" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="STRING">
<xs:complexType>
<xs:sequence>
<xs:element ref="EN" />
<xs:element ref="ES" />
</xs:sequence>
<xs:attribute name="name" use="required" type="xs:string">
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="FRACTION">
<xs:complexType>
<xs:sequence>
<xs:element ref="N" />
<xs:element ref="D" />
</xs:sequence>
<xs:attribute name="name" use="required" >
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="RESULTLIST">
<xs:complexType>
<xs:sequence>
<xs:element ref="RESPONSE" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="RESPONSE">
<xs:complexType mixed="true" >
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="PLURAL">
<xs:complexType>
<xs:sequence>
<xs:element ref="STRING" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NAME">
<xs:complexType>
<xs:sequence>
<xs:element ref="STRING" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="INTEGER">
</xs:element>
<xs:element name="ID" type="xs:int">
</xs:element>
<xs:element name="GENDER">
<xs:complexType>
<xs:sequence>
<xs:element ref="STRING" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="BOOL">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:boolean">
<xs:attribute name="name" use="required" type="xs:string">
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="FILE_ID" type="xs:int">
</xs:element>
<xs:element name="ES" type="xs:string" />
<xs:element name="EN" type="xs:string" />
<xs:element name="N" type="xs:int" />
<xs:element name="D" type="xs:int" />
<xs:element name="DICTIONARY">
<xs:complexType mixed="true">
<xs:choice>
<xs:any minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="name" use="required" type="xs:string">
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="ASSET">
<xs:complexType>
<xs:sequence>
<xs:element ref="NAME" />
<xs:element ref="PLURAL" />
<xs:element ref="ALTTEXT" />
<xs:element ref="GENDER" />
<xs:element ref="ID" />
<xs:element ref="FILE_ID" />
<xs:element ref="DICTIONARY" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="name" use="required" >
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="ARRAY">
<xs:complexType>
<xs:choice>
<xs:element ref="ARRAY" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="STRING" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="ASSET" maxOccurs="unbounded"/>
<xs:element ref="INTEGER" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="FRACTION" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="DICTIONARY" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="name" use="required" type="xs:string">
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="ALTTEXT">
<xs:complexType>
<xs:sequence>
<xs:element ref="STRING" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
while sending a response from SOAP-UI, the above code gets executed and throws below exception
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/D:/softwares/jboss-5.1.0.GA/server/default/deploy/poctest.war/WEB-INF/classes/final.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:594)
at com.hmco.ssms.services.poc.SoarPocJaxWS$1.initialValue(SoarPocJaxWS.java:59)
at com.hmco.ssms.services.poc.SoarPocJaxWS$1.initialValue(SoarPocJaxWS.java:46)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
at java.lang.ThreadLocal.get(ThreadLocal.java:131)
at com.hmco.ssms.services.poc.SoarPocJaxWS.getParametersMultiple(SoarPocJaxWS.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.wsf.container.jboss50.invocation.InvocationHandlerJSE.invoke(InvocationHandlerJSE.java:108)
at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:222)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:474)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:295)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:205)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:131)
at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
I even tried changing XS to XSD in the xsd file. but stil i get this exception.
Please help me on this
Thanks
Arun

Issue is fixed,
This is because, in jboss-5 the files inside the WAR file cannot be read using getResource() and hence there were no proper file-binding SchemaFactory
I changed the path of the file and now it is working cool !!

Related

Nodejs SOAP library. Cannot read 'elements' of undefined

I am using soap v0.43.0 to make a soap request to a SOAP service.
I get an error "Cannot read 'elements' of undefined" at node_modules/soap/lib/wsdl/elements.js, when making a soap call.
This WSDL has two operations, SubmitNote & SubmitPromiseToPayNote.
When I comment out everything that pertains to SubmitPromiseToPayNote, my code for SubmitNote works well and I get a response. However, I want both functionalities to work.
//create the client
const clientOptions = {
disableCache: false,
endpoint: notesEndpoint,
escapeXML: false,
preserveWhitespace: true,
httpClient: myX509Client,
request: requestProxy,
};
const client = await soap.createClientAsync(
'src/v1/resources/NoteServiceWSDL.wsdl',
clientOptions,
);
client.addHttpHeader('Content-Type', 'application/soap+xml; charset=utf-8');
//call the operation
const result = await client.SubmitNoteAsync({
_xml: xmlRequest,
});
return result;
This is the WSDL
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="BizTalkServiceInstance" targetNamespace="http://VCI.ESB.NoteServiceOnRamp"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"
xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://VCI.ESB.NoteServiceOnRamp"
xmlns:wsa10="http://www.w3.org/2005/08/addressing"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<wsdl:documentation>
<CreationInfo Created="2014-10-14 09:28:00Z"/>
</wsdl:documentation>
<wsp:Policy wsu:Id="WSHttpBinding_ITwoWayAsyncVoid_policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:TransportBinding
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:TransportToken>
<wsp:Policy>
<sp:HttpsToken RequireClientCertificate="false"/>
</wsp:Policy>
</sp:TransportToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:Basic256/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Strict/>
</wsp:Policy>
</sp:Layout>
</wsp:Policy>
</sp:TransportBinding>
<wsaw:UsingAddressing/>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/Message"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://schemas.microsoft.com/Message">
<xs:complexType name="MessageBody">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##any"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema targetNamespace="http://VCI.ESB.NoteServiceOnRamp/Imports"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://VCI.ESB.NoteServiceOnRamp/Imports">
<xs:import namespace="http://VCI.Enterprise.Auditing.GenericNote"/>
<xs:import/>
</xs:schema>
<xs:schema targetNamespace="http://VCI.Enterprise.Auditing.GenericNote"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
xmlns:ns0="http://VCI.EAI.EventBus.Framework.Schemas.ContextProperties.EnterpriseEventProperties"
xmlns="http://VCI.Enterprise.Auditing.GenericNote">
<xs:annotation>
<xs:appinfo/>
</xs:annotation>
<xs:element name="GenericNote" type="GenericNote">
<xs:annotation>
<xs:appinfo/>
</xs:annotation>
</xs:element>
<xs:complexType name="GenericNote">
<xs:sequence>
<xs:element name="AccountNumber" type="xs:string"/>
<xs:element name="Source" type="xs:string"/>
<xs:element name="NoteType" type="xs:string"/>
<xs:element name="TextField" type="xs:string"/>
<xs:element name="Country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003">
<xs:element name="PromiseToPayNoteDetails" nillable="true" type="NotificationDetails"/>
<xs:complexType name="NotificationDetails">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="ApplicationThreadId" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="AccountNumber" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="CountryCode" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="NotificationPaymentDetails" type="NotificationPaymentDetails"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="NotificationPaymentDetails">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="PaymentSource" type="PaymentSource"/>
<xs:element minOccurs="0" maxOccurs="1" name="ContactName" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="NoteText" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="PayeeName" type="xs:string"/>
<xs:element minOccurs="1" maxOccurs="1" name="CustomerType" type="AccountPartyType"/>
<xs:element minOccurs="0" maxOccurs="1" name="ConfirmationNumber" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="BankAccountNumber" type="xs:string"/>
<xs:element minOccurs="1" maxOccurs="1" name="WithdrawalDate" type="xs:dateTime"/>
<xs:element minOccurs="1" maxOccurs="1" name="WithdrawalAmount" type="xs:decimal"/>
<xs:element minOccurs="1" maxOccurs="1" name="PaymentType" type="PaymentType"/>
<xs:element minOccurs="1" maxOccurs="1" name="FollowUpDate" type="xs:dateTime"/>
<xs:element minOccurs="1" maxOccurs="1" name="EntryDate" type="xs:dateTime"/>
<xs:element minOccurs="1" maxOccurs="1" name="EmailAddress" type="xs:string"/>
<xs:element minOccurs="1" maxOccurs="1" name="CampaignId" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="PaymentSource">
<xs:restriction base="xs:string">
<xs:enumeration value="None"/>
<xs:enumeration value="CSR"/>
<xs:enumeration value="WEB"/>
<xs:enumeration value="PBP"/>
<xs:enumeration value="IVR"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AccountPartyType">
<xs:restriction base="xs:string">
<xs:enumeration value="PrimaryApplicant"/>
<xs:enumeration value="CoApplicant"/>
<xs:enumeration value="Guarantor"/>
<xs:enumeration value="ThirdParty"/>
<xs:enumeration value="None"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PaymentType">
<xs:restriction base="xs:string">
<xs:enumeration value="OneTimePayment"/>
<xs:enumeration value="PayoffPayment"/>
<xs:enumeration value="RecurringACH"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="NotificationPaymentDetails" nillable="true" type="NotificationPaymentDetails"/>
<xs:element name="PaymentSource" type="PaymentSource"/>
<xs:element name="AccountPartyType" type="AccountPartyType"/>
<xs:element name="PaymentType" type="PaymentType"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="NoteService_SubmitNote_InputMessage">
<wsdl:part name="part" element="q1:GenericNote"
xmlns:q1="http://VCI.Enterprise.Auditing.GenericNote"/>
</wsdl:message>
<wsdl:message name="NoteService_SubmitNote_OutputMessage"/>
<wsdl:message name="NoteService_SubmitPromiseToPayNote_InputMessage">
<wsdl:part name="part" element="PromiseToPayNoteDetails"/>
</wsdl:message>
<wsdl:message name="NoteService_SubmitPromiseToPayNote_OutputMessage"/>
<wsdl:portType name="NoteService">
<wsdl:documentation/>
<wsdl:operation name="SubmitNote">
<wsdl:documentation/>
<wsdl:input message="tns:NoteService_SubmitNote_InputMessage"/>
<wsdl:output message="tns:NoteService_SubmitNote_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="SubmitPromiseToPayNote">
<wsdl:documentation/>
<wsdl:input message="tns:NoteService_SubmitPromiseToPayNote_InputMessage"/>
<wsdl:output message="tns:NoteService_SubmitPromiseToPayNote_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WSHttpBinding_ITwoWayAsyncVoid" type="tns:NoteService">
<wsp:PolicyReference URI="#WSHttpBinding_ITwoWayAsyncVoid_policy"/>
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SubmitNote">
<wsdl:documentation/>
<soap12:operation soapAction="SubmitNote" style="document"/>
<wsdl:input>
<soap12:body use="literal" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SubmitPromiseToPayNote">
<wsdl:documentation/>
<soap12:operation soapAction="SubmitPromiseToPayNote" style="document"/>
<wsdl:input>
<soap12:body use="literal" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BizTalkServiceInstance">
<wsdl:port name="WSHttpBinding_ITwoWayAsyncVoid" binding="tns:WSHttpBinding_ITwoWayAsyncVoid">
<soap12:address location="https://vcipa030c-710dv.usvci001.vci.na.vwg:444/VCI.ESB.NoteServiceOnRamp/NoteService.svc"/>
<wsa10:EndpointReference>
<wsa10:Address>https://vcipa030c-710dv.usvci001.vci.na.vwg:444/VCI.ESB.NoteServiceOnRamp/NoteService.svc</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Problems creating DynamicEntities - DynamicHelper.createQuery: Dynamic type not found:

I seem to be having an issue that for the life of me I can not see why. I suspect it is a name space problem.
The following code always results in the following error: DynamicHelper.createQuery: Dynamic type not found:
I have tried numerous namespaces
"mmckenzie.stockcontrol/supplier"
"supplier"
"supplierDAO"
"mmckenzie.stockcontrol/supplierDAO"
I fully expect that this is going to be user error. Any guidence is most welcome.
JAVA CODE:
package model;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.JAXBException;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Test
{
public static void main(String[] args)
{
try {
URL schema = new URL("http://localhost:8080/stockcontrol/schema/stockcontrol.xsd");
InputStream xsd = schema.openStream();
EntityResolver er = new EntityResolver()
{
#Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
{
URL schema = new URL("http://localhost:8080/stockcontrol/schema/"+(new File(systemId)).getName());
InputStream xsd = schema.openStream();
InputSource is = new InputSource(xsd);
is.setSystemId(schema.getPath());
return is;
}
};
//create the context from the XSD
DynamicJAXBContext xmlContext = DynamicJAXBContextFactory.createContextFromXSD(xsd,er, null, null);
//create a DYNAMICEntity for the supplier object.
DynamicEntity de = xmlContext.newDynamicEntity("mmckenzie.stockcontrol/supplier");
}
catch (IOException | JAXBException e)
{
e.printStackTrace();
}
}
}
XSD
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="mmckenzie.stockcontrol" xmlns:tns="mmckenzie.stockcontrol" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="stockcontrol2.xsd"/>
<xs:element name="batch" type="tns:batchDAO"/>
<xs:element name="batches" type="tns:batchesDAO"/>
<xs:element name="component" type="tns:componentDAO"/>
<xs:element name="components" type="tns:componentsDAO"/>
<xs:element name="prefixes" type="tns:componentPrefixesDAO"/>
<xs:element name="product" type="tns:productDAO"/>
<xs:element name="product_batches" type="tns:productBatchesDAO"/>
<xs:element name="productbatch" type="tns:productBatchDAO"/>
<xs:element name="products" type="tns:productsDAO"/>
<xs:element name="supplier" type="tns:supplierDAO"/>
<xs:element name="suppliers" type="tns:suppliersDAO"/>
<xs:complexType name="batchesDAO">
<xs:sequence>
<xs:element name="batch" type="tns:batchDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="batchDAO">
<xs:sequence>
<xs:element name="idbatch" type="xs:int"/>
<xs:element name="dateReceived" type="xs:long"/>
<xs:element name="dateConsumed" type="xs:long"/>
<xs:element name="invoice" type="xs:string" minOccurs="0"/>
<xs:element name="qty" type="xs:int"/>
<xs:element name="qtyInStock" type="xs:int"/>
<xs:element name="unitCost" type="xs:decimal" minOccurs="0"/>
<xs:element name="componentId" type="xs:int"/>
<xs:element name="supplierId" type="xs:int"/>
<xs:element name="unitType" type="unitType" minOccurs="0"/>
<xs:element name="isActive" type="xs:boolean"/>
<xs:element ref="tns:products" minOccurs="0"/>
<xs:element name="batchCost" type="xs:decimal" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productsDAO">
<xs:sequence>
<xs:element name="product" type="tns:productDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productDAO">
<xs:sequence>
<xs:element name="idProduct" type="xs:int"/>
<xs:element name="cost" type="xs:decimal" minOccurs="0"/>
<xs:element name="description" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:int"/>
<xs:element name="sellPrice" type="xs:decimal" minOccurs="0"/>
<xs:element name="productBatches" type="tns:productBatchesDAO" minOccurs="0"/>
<xs:element name="date_sold" type="timestamp" minOccurs="0"/>
<xs:element name="date_produced" type="timestamp" minOccurs="0"/>
<xs:element ref="tns:components" minOccurs="0"/>
<xs:element name="hours_worked" type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productBatchesDAO">
<xs:sequence>
<xs:element name="productbatches" type="tns:productBatchDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productBatchDAO">
<xs:sequence>
<xs:element ref="tns:component" minOccurs="0"/>
<xs:element ref="tns:batch" minOccurs="0"/>
<xs:element name="quantyUsed" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentDAO">
<xs:sequence>
<xs:element name="idcomponent" type="xs:int"/>
<xs:element name="componentName" type="xs:string" minOccurs="0"/>
<xs:element ref="tns:batches" minOccurs="0"/>
<xs:element name="componentPrefix" type="componentPrefix" minOccurs="0"/>
<xs:element name="qtyInStock" type="xs:int"/>
<xs:element ref="tns:products" minOccurs="0"/>
<xs:element name="quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentsDAO">
<xs:sequence>
<xs:element name="component" type="tns:componentDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="suppliersDAO">
<xs:sequence>
<xs:element name="supplier" type="tns:supplierDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="supplierDAO">
<xs:sequence>
<xs:element name="idsupplier" type="xs:int"/>
<xs:element name="modified" type="timestamp" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element ref="tns:batches" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentPrefixesDAO">
<xs:sequence>
<xs:element name="prefix" type="componentPrefix" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
All comments suggestions most welcome.
The way that it creates the namespaces is not what I was expecting. The correct answer for this example would be
DynamicEntity de = xmlContext.newDynamicEntity("stockcontrol.mmckenzie.SupplierDAO");
This article was useful in finding the resolution.
Appendix D: Binding XML Names to Java Identifiers" of the Java Architecture for XML Binding (JAXB) 2.2 Specification (http://jcp.org/en/jsr/detail?id=222)
However using the eclipse debugger I was able for find the Helper names spaces. To use this method follow these steps in eclipse (Mars in mycase).
start your app in debugger with a break point just after your DynamicJAXBContext code.
in the eclipse varibles tab, find your DynamicJAXBContext object.
(your context)->ContextState->Helpers->ElementData->(non null object)->fqClassNameToDescripters->table->(non null entry)->key
The key value will give you an idea of what namespace it is expecting.

Error: Content is not allowed in prolog in Groovy script

I tried to run following xsd validation in SoapUI using Groovy
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.*
// setup validator
Validator validator;
def url = "file:///D:/SoapUI/XSDFile.xsd"
log.info url
URI uri = new URI(url);
InputStream inp = uri.toURL().openStream();
try
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource entitySs = new StreamSource(inp);
Schema schema = factory.newSchema(entitySs);
assert(schema != null);
validator = schema.newValidator();
}
finally
{
inp.close();
inp = null;
}
def response = context.expand( "file:///D:/SoapUI/response.xml" )
log.info response
validator.validate(new StreamSource(new StringReader(response)))
But I am getting below error in the groovy step in the SoapUI test Case
:ERROR:org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at javax.xml.validation.Validator$validate.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:33)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.runTestStep(AbstractTestCaseRunner.java:239)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runCurrentTestStep(WsdlTestCaseRunner.java:48)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:148)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:43)
at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
As per solution given on the web I removed unwanted characters though I cannot see as such by converting file from UTF8 file to ANSI
But still giving the same error
Is this caused some other issue?
Response
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Rio de Janeiro, Brazil</formatted_address>
<address_component>
<long_name>Rio de Janeiro</long_name>
<short_name>Rio de Janeiro</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Rio de Janeiro</long_name>
<short_name>RJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brazil</long_name>
<short_name>BR</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>-22.9035393</lat>
<lng>-43.2095869</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>-23.0830199</lat>
<lng>-43.7954493</lng>
</southwest>
<northeast>
<lat>-22.7490450</lat>
<lng>-43.0993817</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>-23.0830199</lat>
<lng>-43.7954493</lng>
</southwest>
<northeast>
<lat>-22.7490450</lat>
<lng>-43.0993817</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
Schema file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://maps.googleapis.com" elementFormDefault="qualified" xmlns="http://maps.googleapis.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GeocodeResponse" type="_GeocodeResponse"/>
<xs:complexType name="result_geometry">
<xs:sequence>
<xs:element name="location" type="geometry_location"/>
<xs:element name="location_type" type="xs:string"/>
<xs:element name="viewport" type="geometry_viewport"/>
<xs:element name="bounds" type="geometry_bounds"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="viewport_southwest">
<xs:sequence>
<xs:element name="lathjj" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bounds_northeast">
<xs:sequence>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="geometry_viewport">
<xs:sequence>
<xs:element name="southwest" type="viewport_southwest"/>
<xs:element name="northeast" type="viewport_northeast"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="geometry_bounds">
<xs:sequence>
<xs:element name="southwest" type="bounds_southwest"/>
<xs:element name="northeast" type="bounds_northeast"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bounds_southwest">
<xs:sequence>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="viewport_northeast">
<xs:sequence>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="_GeocodeResponse">
<xs:sequence>
<xs:element name="status" type="xs:string"/>
<xs:element name="result" type="GeocodeResponse_result"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="address_component">
<xs:sequence>
<xs:element name="long_name" type="xs:string"/>
<xs:element name="short_name" type="xs:string"/>
<xs:element name="type" type="xs:string" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="GeocodeResponse_result">
<xs:sequence>
<xs:element name="type" type="xs:string" maxOccurs="2"/>
<xs:element name="formatted_address" type="xs:string"/>
<xs:element name="address_component" type="address_component" maxOccurs="3"/>
<xs:element name="geometry" type="result_geometry"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="geometry_location">
<xs:sequence>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Appreciate your response
Cheers

Reusable complexType, and xml-registry, xml-element-decl usage

I've the below TimePeriodType
<xs:simpleType name="UnitOfTimePeriodType">
<xs:restriction base="xs:token">
<xs:enumeration value="Months"/>
<xs:enumeration value="Days"/>
<xs:enumeration value="Years"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="TimePeriodType">
<xs:sequence>
<xs:element name="Length" type="digit1-3"/>
</xs:sequence>
<xs:attribute name="unitOfLength" type="UnitOfTimePeriodType" use="optional" default="Months"/>
</xs:complexType>
which gets used in multiple types as shown below
<xs:complexType name="USAddressType">
<xs:complexContent>
<xs:extension base="AddressType">
<xs:sequence>
<xs:element name="LengthAtAddress" type="TimePeriodType" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="CanadaAddressType">
<xs:complexContent>
<xs:extension base="AddressType">
<xs:sequence>
<xs:element name="LengthAtAddress" type="TimePeriodType" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
So, the below instance xml
<LengthAtAddress unitOfLength="Months">
<Length>36</Length>
</LengthAtAddress>
should be mapped to the following pojo
public abstract class AbstractAddress
{
protected int lengthAtAddress;
protected UnitOfLengthType unitOfLengthAtAddress;
public int getLengthAtAddress()
{
return lengthAtAddress;
}
public UnitOfLengthType getUnitOfLengthAtAddress()
{
return unitOfLengthAtAddress;
}
//setters
}
I think I should be using a combination of xml-registry and xml-element-decl while using external mapping. Not able to figure our how. Any help is appreciated.
FIX
<xml-element java-attribute="lengthAtAddress" xml-path="LengthAtAddress/Length/text()"/>
<xml-element java-attribute="unitOfLengthAtAddress" xml-path="LengthAtAddress/#unitOfLength">
<xml-java-type-adapter value="com.equifax.ic.platform.sts.domain.transformation.response.CommonsEnumAdapter"/>
</xml-element>
You could use MOXy's #XmlPath extension or the equivalent in MOXy's external mapping document.
#XmlPath("LengthAtAddress/Length/text()")
public int getLengthAtAddress()
{
return lengthAtAddress;
}

Polymorphism in XSD schema and JAXB classes

I have an xml like this:
<todo>
<doLaundry cost="1"/>
<washCar cost="10"/>
<tidyBedroom cost="0" experiencePoints="5000"/>
</todo>
And the XSD schema for it is:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="doLaundry" />
<xs:element name="washCar" type="washCar" />
<xs:element name="tidyBedroom" type="tidyBedroom" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="doLaundry">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>
<xs:complexType name="washCar">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>
<xs:complexType name="tidyBedroom">
<xs:attribute name="cost" type="xs:int" />
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:complexType>
</xs:schema>
And when I process this schema through JAXB I get a class with a method like this:
public class Todo {
public List<Object> getDoLaundryOrWashCarOrTidyBedroom() {
...
}
}
Ideally, what I would like is a way to define a generic base type that all the other XSD types extend. The Jaxb classes generated from the XSD schema should have a method to return a list of generic tasks. This would make it very easy to add new tasks to the todo list:
public class Todo {
public List<Task> getTasks() {
...
}
}
public abstract class Task {
public int getCost() {
...
}
}
public class TidyBedroom extends Task {
public int getExperiencePoints() {
...
}
}
What should the XSD schema look like in order to generate the above Java classes?
I found the answer with the help of Blaise Doughan's article here: http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-xsitype.html
This schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="doLaundry" />
<xs:element name="washCar" type="washCar" />
<xs:element name="tidyBedroom" type="tidyBedroom" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType abstract="true" name="Task">
<xs:attribute name="cost" type="xs:int" use="required" />
</xs:complexType>
<xs:complexType name="doLaundry">
<xs:complexContent>
<xs:extension base="Task">
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="washCar">
<xs:complexContent>
<xs:extension base="Task">
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="tidyBedroom">
<xs:complexContent>
<xs:extension base="Task">
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
combined with a binding file:
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings>
<jxb:bindings schemaLocation="todo.xsd" node="/xs:schema/xs:complexType[#name='todo']/xs:sequence/xs:choice">
<jxb:property name="Tasks"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Will give abstract and inherited classes as I described in the question. The binding file will change Jaxb's default method name from getDoLaundryOrWashCarOrTidyBedroom() to getTasks().
xsd:choice corresponds to the #XmlElements annotation. You could apply this annotation directly to your desired object model.
For more information see:
http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
Use xs:extension in your schema and your JAXB classes will be inherited (extended) as you define in your schema.
Maybe I'm not 'getting' the question, but what is wrong with..
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="task" />
<xs:element name="washCar" type="task" />
<xs:element name="tidyBedroom" type="task" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="task">
<xs:attribute name="cost" type="xs:int" />
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:complexType>
</xs:schema>

Resources