Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » extension base
extension base [message #66641] Fri, 17 March 2006 08:40 Go to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi

I am new to this group and XSD.

I have gone through news group and understand that to resolve extension
base elements we should use GetContentType(). But I was not able to
resolve.

Can any one please share me code snipped to resolve the below schema.

Advance Thanks
Param.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Person" type="xs:string"/>
</xs:complexType>
<xs:complexType name="USAddress">
<xs:complexContent>
<xs:extension base="Address">
<xs:sequence>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Surname" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="NYAddress">
<xs:complexContent>
<xs:extension base="USAddress">
<xs:attribute name="RentControlled" type="xs:boolean"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="address" type="NYAddress"/>
</xs:schema>
Re: extension base [message #66667 is a reply to message #66641] Fri, 17 March 2006 08:44 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Param,

Have a look at XSDMainExample.java for how to load the schema properly.
Then if you are stuck, show exactly what you've tried to do.


Param wrote:
> Hi
>
> I am new to this group and XSD.
>
> I have gone through news group and understand that to resolve
> extension base elements we should use GetContentType(). But I was not
> able to resolve.
>
> Can any one please share me code snipped to resolve the below schema.
>
> Advance Thanks
> Param.
>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:complexType name="Address">
> <xs:sequence>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Person" type="xs:string"/>
> </xs:complexType>
> <xs:complexType name="USAddress">
> <xs:complexContent>
> <xs:extension base="Address">
> <xs:sequence>
> <xs:element name="state" type="xs:string"/>
> <xs:element name="zip" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Surname" type="xs:string"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:complexType name="NYAddress">
> <xs:complexContent>
> <xs:extension base="USAddress">
> <xs:attribute name="RentControlled" type="xs:boolean"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:element name="address" type="NYAddress"/>
> </xs:schema>
>
Re: extension base [message #66730 is a reply to message #66667] Mon, 20 March 2006 11:40 Go to previous messageGo to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,
Thanks for your support.

I have used XSDComplexTypeDefinition.getContentType() to get resolved
elements from imported/included schema files.

I have created standalone java class file which will resolve elements and
prints their names in tree format for your reference.

The problem with current code is it is not able to resolve element
'oa:Parties' defination the xpath for this element is Parties
/ProcessRemittanceAdvice/DataArea/RemittanceAdvice/Header/Pa rties

From this element onwards it is going to infinite loop.

sorry for my english. here are the code and sample files I have used.

Advance Thanks
Param

Java Class (XSDParserStandAlone.java):
/*
* Created on Mar 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.gxs.ai.ngwb.xsdparser;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupContent;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDAttributeUse;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaContent;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;



/**
* @author tatinip
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XSDParserStandAlone
{

private String m_rootElementName;
private boolean m_isRoot = false;
private boolean m_ignoreCpexTypeDefnName = false;

public XSDParserStandAlone(){


}
public boolean parseXSD(String xsdFileName ,
String rootElementName)
throws FileNotFoundException , IOException , Exception
{
m_rootElementName = rootElementName;
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl()); //$NON-NLS-1$

try
{
boolean status = validateAndProcess(xsdFileName);
return status;
}catch(IOException ioe)
{
throw ioe;
}catch(Exception e)
{
throw e;
}


}


private boolean validateAndProcess(String xsdFile) throws Exception
{
/*
* This let's us test whether the string exists as a file.
* It not, we try as a URI.
*/

ArrayList retVal = new ArrayList();

URI uri;

if (xsdFile==null ){
//DMParseError parserError = new DMParseError(4,"","Schema file not
specified",0,0);
//m_error_List.add(parserError);


//this.rootNode = null;
return true;
}

File file = new File(xsdFile);
if (!file.exists()){
//DMParseError parserError = new DMParseError(4,"","File Not
Found",0,0);
//m_error_List.add(parserError);

//this.rootNode = null;
return true;
}

if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

// Create a resource set, create a schema resource, and load the main
schema file into it.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);
XSDResourceImpl xsdMainResource =
(XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
//$NON-NLS-1$
xsdMainResource.setURI(uri);
xsdMainResource.load(resourceSet.getLoadOptions());

// Get the first resource, i.e., the main resource and those that
have been included or imported.
Object resource = resourceSet.getResources().get(0);
if (resource instanceof XSDResourceImpl)
{
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
XSDSchema xsdSchema = xsdResource.getSchema();
xsdSchema.validate();


/*
* Schema file is valid if Diagnostics is empty
*/
if (!xsdSchema.getAllDiagnostics().isEmpty())
{
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
xsdDiagnostic.getLocation();
// print errors here


}

//this.rootNode = null;
xsdResource.unload();
return false;
}

// If the user not selected root element set first element as
root element
if (m_rootElementName == null ||
m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
Iterator i = xsdSchema.getContents().iterator();
while(i.hasNext()){
XSDSchemaContent e = (XSDSchemaContent)i.next(); // probable root
element
if (e instanceof XSDElementDeclaration){
m_rootElementName = e.getElement().getAttribute("name");
//$NON-NLS-1$
break;
}
}
}

processRoot(xsdSchema);
xsdResource.unload();
return true;
}
return false;
}

private void processRoot(XSDSchema schema)
{
//process each element directly under the 'schema' element
for(Iterator i = schema.getElementDeclarations().iterator();
i.hasNext(); )
{
XSDElementDeclaration e = (XSDElementDeclaration)i.next(); // probable
root element
m_isRoot = true;
if (m_rootElementName != null &&
!m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
if (m_rootElementName.equalsIgnoreCase(e.getName())){
dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
break;
}
}else{
dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
}

}
}
/**
* Method dumpContentItem.
* @param item
* @param level
*
* This is a recursive method called for handling (currently)
XSDElementDeclaration,
* XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
XSDAttributeDeclaration,
* XSDModelGroupDefinition, XSDAttributeGroupDefinition
*
*/
private void dumpContentItem(XSDSchemaContent item, int level, String
xPath , String xPathwithTNS)
{
try{

if(item instanceof XSDElementDeclaration)
{

XSDElementDeclarationImpl elem = (XSDElementDeclarationImpl)item;

if(elem.getName() != null)
{
if(xPath.startsWith("/")) //$NON-NLS-1$
{
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();

}
else
{
// For the root element
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
}

System.out.println(xPath);

}

m_isRoot = false; // Need to do this after writing the root header,
//because the processing of child elements starts here
// Get the type definition of the Element
XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();

if(elemTypeDef==null &&
elem.getResolvedElementDeclaration().getValue() == null)
{
// For a reference

dumpContentItem(elem.getResolvedElementDeclaration(), level, xPath ,
xPathwithTNS);

}else if((elemTypeDef instanceof XSDSimpleTypeDefinition) &&
((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
{
// Element doesn't have any children i.e simpleContent but might have
restrictions etc
}
else // The 'type' attribute is another element in the same XSD file
{
/* name of the Complex type must be ignored when an element refers to
* it with the 'type' attribute
*/
if(elemTypeDef instanceof XSDComplexTypeDefinition)
{
if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
m_ignoreCpexTypeDefnName = true;

}

dumpContentItem(elemTypeDef, level, xPath , xPathwithTNS);
}
}
else if (item instanceof XSDComplexTypeDefinition)
{

XSDComplexTypeDefinitionImpl cplex = (XSDComplexTypeDefinitionImpl)item;

if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
{
xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
System.out.println(xPath);
}


// Process the attributes
for(Iterator i = cplex.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeGroupContent att_grp = (XSDAttributeGroupContent) i.next();

// For attribute group
if(att_grp instanceof XSDAttributeGroupDefinition)
{
dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath
, xPathwithTNS);

}else if(att_grp instanceof XSDAttributeUse)
{


dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
xPath , xPathwithTNS);
}
else
{
System.out.println("2. UNHANDLED"); //$NON-NLS-1$
}

}

if (cplex.getContentType() != null){
XSDComplexTypeContent cont = cplex.getContentType();
if (cont instanceof XSDParticle)
dumpParticle((XSDParticle)cont,level, xPath , xPathwithTNS);
}else if(cplex.getContent() != null)
{
if(cplex.getContent() instanceof XSDParticle)
{
dumpParticle((XSDParticle)cplex.getContent(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContent() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}
/*if (cplex.getContentType() != null){
if(cplex.getContentType() instanceof XSDParticle)
{

dumpParticle((XSDParticle)cplex.getContentType(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContentType() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}*/

}
else if (item instanceof XSDSimpleTypeDefinition)
{
XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition)item;

xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;

System.out.println(xPath);

if(simple.getContents() != null )
{
if(!simple.getContents().isEmpty())
{
for(Iterator i = simple.getContents().iterator(); i.hasNext(); )
{
XSDSimpleTypeDefinition child_simple =
(XSDSimpleTypeDefinition)i.next();
dumpContentItem(child_simple, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("4. UNHANDLED"); //$NON-NLS-1$
}
}
else
{
System.out.println("3. UNHANDLED"); //$NON-NLS-1$
}
}
else if (item instanceof XSDAttributeDeclaration)
{
XSDAttributeDeclaration attrib = (XSDAttributeDeclaration)item;

xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();

System.out.println(xPath);

} else if(item instanceof XSDModelGroupDefinition)
{
// For sequence, all, choice
XSDModelGroupDefinition mgdef = (XSDModelGroupDefinition)item;
if(mgdef.getModelGroup() != null)
{
for(Iterator i = mgdef.getModelGroup().getContents().iterator();
i.hasNext(); )
{
XSDParticle pcle = (XSDParticle)i.next();
dumpParticle(pcle, level+1, xPath , xPathwithTNS);
}
} else
{
// for "<xs:group ref=
dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
xPath , xPathwithTNS);
}

} else if(item instanceof XSDAttributeGroupDefinition)
{
XSDAttributeGroupDefinition att_grp = (XSDAttributeGroupDefinition)item;
if(!att_grp.getAttributeUses().isEmpty())
{
for(Iterator i = att_grp.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeUse att_use = (XSDAttributeUse)i.next();

dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
xPathwithTNS);
}
}else // For an attributeGroup reference
{
int k = 0 ;
//dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
level, xPath);
}

}
else
{
// example : XSDAnnotation, XSDNotationDeclaration, XSDImport,
XSDInclude,
if(item != null)
System.out.println("Unhandled Schema Content : "+ item.toString());
//$NON-NLS-1$
}
}catch(Exception e)
{
e.printStackTrace();
}
// Set the class level helper variables to default values
m_ignoreCpexTypeDefnName = false;
m_isRoot = false;

}

//---------------------------------------------------------- -----------------------------------------------------



/**
* Method dumpParticle.
* @param p
* @param level
*/
private void dumpParticle(XSDParticle p, int level,String xPath , String
xPathwithTNS)
{

XSDParticleContent partContent= p.getContent();
//XSDComplexTypeDefinition com =
partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
if(partContent instanceof XSDSimpleTypeDefinition){
dumpContentItem((XSDSimpleTypeDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDElementDeclaration)
{

dumpContentItem((XSDElementDeclaration)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroupDefinition)
{
dumpContentItem((XSDModelGroupDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroup)
{
XSDModelGroup temp = (XSDModelGroup)partContent;
for(Iterator i = temp.getParticles().iterator(); i.hasNext(); )
{
dumpParticle((XSDParticle)i.next(), level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("unhandled particle "+ p.toString()); //$NON-NLS-1$
//XSDTerm
//XSDWildcard
}
}


public static void main(String args[])
{
// System.exit(((Integer)new XSDMainExample().run(args)).intValue());
XSDParserStandAlone xp = new XSDParserStandAlone();
try {
xp.parseXSD("ProcessRemittanceAdvice.xsd","ProcessRemittanceAdvice ");
//
xp.parseXSD("C:\\Trandev501048\\schematest\\Schemas\\NYAddress.xsd ","address");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Sample XSD files :

1) ProcessRemittanceAdvice.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
xmlns:agilent="http://www.agilent.com/oagis"
targetNamespace="http://www.agilent.com/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://www.openapplications.org/oagis"
schemaLocation="RemittanceAdviceTemplate.xsd"/>
<xs:include schemaLocation="RemittanceAdvice.xsd"/>
<xs:element name="ProcessRemittanceAdvice"
type="agilent:ProcessRemittanceAdvice"/>
<xs:complexType name="ProcessRemittanceAdvice">
<xs:complexContent>
<xs:extension base="oa:BusinessObjectDocument">
<xs:sequence>
<xs:element name="DataArea"
type="agilent:ProcessRemittanceAdviceDataArea"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ProcessRemittanceAdviceDataArea">
<xs:sequence>
<xs:element ref="agilent:RemittanceAdvice" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

2) RemittanceAdviceTemplate.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Components_renamed.xsd"/>
</xs:schema>

3) RemittanceAdvice.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:agilent="http://www.agilent.com/oagis"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.agilent.com/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Header" type="agilent:RemittanceHeader"/>
<xs:element name="RemittanceAdvice" type="agilent:RemittanceAdvice"/>
<xs:complexType name="PaymentOrder">
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RemittanceAdvice">
<xs:sequence>
<xs:element ref="agilent:Header"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RemittanceHeader">
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0"/>
<xs:element ref="oa:Parties" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

4) Meta.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="0.02">
<xs:complexType name="BusinessObjectDocument">
<xs:attribute name="revision" use="required">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:element name="BusinessObjectDocument"
type="oa:BusinessObjectDocument" abstract="true"/>
</xs:schema>

5) Fields_renamed.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="0.02">
<xs:include schemaLocation="Meta.xsd"/>
<xs:complexType name="PartyIdType">
<xs:sequence/>
</xs:complexType>
</xs:schema>

6)Components_renamed.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Fields_renamed.xsd"/>
<xs:element name="Parties" type="oa:Parties">
<xs:annotation>
<xs:documentation
source="http://www.openapplications.org/oagis">Grouping of semantically
named Parties.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="Parties" block="extension">
<xs:sequence>
<xs:element ref="oa:PartyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PartyBase" abstract="true">
<xs:attribute name="active" type="xs:boolean" use="optional"
default="false"/>
<xs:attribute name="oneTime" type="xs:boolean" use="optional"
default="false"/>
</xs:complexType>
<xs:element name="PartyType" type="oa:PartyBase" abstract="true"/>
</xs:schema>

End of Schema files

Thankyou very much
Param
Re: extension base [message #66748 is a reply to message #66730] Mon, 20 March 2006 11:42 Go to previous messageGo to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed,

I am using XSD plugin 2.0.2 with Eclipse 3.1

Once again Thank you

Param
Re: extension base [message #66764 is a reply to message #66748] Tue, 21 March 2006 04:36 Go to previous messageGo to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed,

Here is the simplified sample files.

I am running into infinite loop when resolving for Address element

Sample Schema files.

1) NyAddress.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://gxs.com/1" schemaLocation="imp1.xsd"/>
<xs:import namespace="http://gxs.com/2" schemaLocation="imp2.xsd"/>
<xs:complexType name="NYAddress">
<xs:complexContent>
<xs:extension base="imp1:USAddress"/>
</xs:complexContent>
</xs:complexType>
<xs:element name="address">
<xs:complexType>
<xs:complexContent>
<xs:extension base="NYAddress"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>

---- End of sample XSD------------------------------------

2)imp1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
targetNamespace="http://gxs.com/1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="USAddress" block="extension">
<xs:sequence>
<xs:element ref="imp1:PAddress"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Surname" type="xs:string"/>
</xs:complexType>
<xs:element name="USAddress" type="imp1:USAddress"/>
<xs:element name="PAddress" type="imp1:PAddress"/>
<xs:complexType name="PAddress">
<xs:sequence>
<xs:element name="start" type="xs:string"/>
<xs:element ref="imp2:Address"/>
<xs:element name="end" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

---- End of sample XSD------------------------------------
3)imp2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp2="http://gxs.com/2" targetNamespace="http://gxs.com/2"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Address" type="imp2:Address"/>
</xs:schema>

---- End of sample XSD------------------------------------

Here is the java code I am using

/*
* Created on Mar 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.gxs.ai.ngwb.xsdparser;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupContent;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDAttributeUse;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDDiagnosticSeverity;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaContent;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;



/**
* @author tatinip
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XSDParserStandAlone
{

private String m_rootElementName;
private boolean m_isRoot = false;
private boolean m_ignoreCpexTypeDefnName = false;

public XSDParserStandAlone(){


}
public boolean parseXSD(String xsdFileName ,
String rootElementName)
throws FileNotFoundException , IOException , Exception
{
m_rootElementName = rootElementName;
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl()); //$NON-NLS-1$

try
{
boolean status = validateAndProcess(xsdFileName);
return status;
}catch(IOException ioe)
{
throw ioe;
}catch(Exception e)
{
throw e;
}


}


private boolean validateAndProcess(String xsdFile) throws Exception
{
/*
* This let's us test whether the string exists as a file.
* It not, we try as a URI.
*/

ArrayList retVal = new ArrayList();

URI uri;

if (xsdFile==null ){
//DMParseError parserError = new DMParseError(4,"","Schema file not
specified",0,0);
//m_error_List.add(parserError);


//this.rootNode = null;
return true;
}

File file = new File(xsdFile);
if (!file.exists()){
//DMParseError parserError = new DMParseError(4,"","File Not
Found",0,0);
//m_error_List.add(parserError);

//this.rootNode = null;
return true;
}

if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

// Create a resource set, create a schema resource, and load the main
schema file into it.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);
XSDResourceImpl xsdMainResource =
(XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
//$NON-NLS-1$
xsdMainResource.setURI(uri);
xsdMainResource.load(resourceSet.getLoadOptions());

// Get the first resource, i.e., the main resource and those that
have been included or imported.
Object resource = resourceSet.getResources().get(0);
if (resource instanceof XSDResourceImpl)
{
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
XSDSchema xsdSchema = xsdResource.getSchema();
xsdSchema.validate();


/*
* Schema file is valid if Diagnostics is empty
*/
if (!xsdSchema.getAllDiagnostics().isEmpty())
{
boolean errorFound = false;
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
if (xsdDiagnostic.getSeverity().getValue()!=
XSDDiagnosticSeverity.WARNING)
errorFound = true;
xsdDiagnostic.getLocation();
// print errors here


}

if (errorFound){
xsdResource.unload();

return false;
}
}

// If the user not selected root element set first element as
root element
if (m_rootElementName == null ||
m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
Iterator i = xsdSchema.getContents().iterator();
while(i.hasNext()){
XSDSchemaContent e = (XSDSchemaContent)i.next(); // probable root
element
if (e instanceof XSDElementDeclaration){
m_rootElementName = e.getElement().getAttribute("name");
//$NON-NLS-1$
break;
}
}
}

processRoot(xsdSchema);
xsdResource.unload();
return true;
}
return false;
}

private void processRoot(XSDSchema schema)
{
//process each element directly under the 'schema' element
for(Iterator i = schema.getElementDeclarations().iterator();
i.hasNext(); )
{
XSDElementDeclaration e = (XSDElementDeclaration)i.next(); // probable
root element
m_isRoot = true;
if (m_rootElementName != null &&
!m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
if (m_rootElementName.equalsIgnoreCase(e.getName())){
dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
break;
}
}else{
dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
}

}
}
/**
* Method dumpContentItem.
* @param item
* @param level
*
* This is a recursive method called for handling (currently)
XSDElementDeclaration,
* XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
XSDAttributeDeclaration,
* XSDModelGroupDefinition, XSDAttributeGroupDefinition
*
*/
private void dumpContentItem(XSDSchemaContent item, int level, String
xPath , String xPathwithTNS)
{
try{

if(item instanceof XSDElementDeclaration)
{

XSDElementDeclarationImpl elem = (XSDElementDeclarationImpl)item;

if(elem.getName() != null)
{
if(xPath.startsWith("/")) //$NON-NLS-1$
{
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();

}
else
{
// For the root element
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
}

System.out.println(xPath);

}

m_isRoot = false; // Need to do this after writing the root header,
//because the processing of child elements starts here
// Get the type definition of the Element
XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();

if(elemTypeDef==null &&
elem.getResolvedElementDeclaration().getValue() == null)
{
dumpContentItem(elem.getResolvedElementDeclaration(), level, xPath ,
xPathwithTNS);

}else if((elemTypeDef instanceof XSDSimpleTypeDefinition) &&
((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
{
// Element doesn't have any children i.e simpleContent but might have
restrictions etc
}
else // The 'type' attribute is another element in the same XSD file
{
/* name of the Complex type must be ignored when an element refers to
* it with the 'type' attribute
*/
if(elemTypeDef instanceof XSDComplexTypeDefinition)
{
if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
m_ignoreCpexTypeDefnName = true;

}

dumpContentItem(elemTypeDef, level, xPath , xPathwithTNS);
}
}
else if (item instanceof XSDComplexTypeDefinition)
{

XSDComplexTypeDefinitionImpl cplex = (XSDComplexTypeDefinitionImpl)item;

if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
{
xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
System.out.println(xPath);
}


// Process the attributes
for(Iterator i = cplex.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeGroupContent att_grp = (XSDAttributeGroupContent) i.next();

// For attribute group
if(att_grp instanceof XSDAttributeGroupDefinition)
{
dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath
, xPathwithTNS);

}else if(att_grp instanceof XSDAttributeUse)
{


dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
xPath , xPathwithTNS);
}
else
{
System.out.println("2. UNHANDLED"); //$NON-NLS-1$
}

}

if(cplex.getContent() != null)
{
if(cplex.getContent() instanceof XSDParticle)
{
dumpParticle((XSDParticle)cplex.getContent(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContent() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}else if (cplex.getContentType() != null){
XSDComplexTypeContent cont = cplex.getContentType();
if (cont instanceof XSDParticle)
dumpParticle((XSDParticle)cont,level, xPath , xPathwithTNS);
}
/*if (cplex.getContentType() != null){
if(cplex.getContentType() instanceof XSDParticle)
{

dumpParticle((XSDParticle)cplex.getContentType(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContentType() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}*/

}
else if (item instanceof XSDSimpleTypeDefinition)
{
XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition)item;

xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;

System.out.println(xPath);

if(simple.getContents() != null )
{
if(!simple.getContents().isEmpty())
{
for(Iterator i = simple.getContents().iterator(); i.hasNext(); )
{
XSDSimpleTypeDefinition child_simple =
(XSDSimpleTypeDefinition)i.next();
dumpContentItem(child_simple, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("4. UNHANDLED"); //$NON-NLS-1$
}
}
else
{
System.out.println("3. UNHANDLED"); //$NON-NLS-1$
}
}
else if (item instanceof XSDAttributeDeclaration)
{
XSDAttributeDeclaration attrib = (XSDAttributeDeclaration)item;

xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();

System.out.println(xPath);

} else if(item instanceof XSDModelGroupDefinition)
{
// For sequence, all, choice
XSDModelGroupDefinition mgdef = (XSDModelGroupDefinition)item;
if(mgdef.getModelGroup() != null)
{
for(Iterator i = mgdef.getModelGroup().getContents().iterator();
i.hasNext(); )
{
XSDParticle pcle = (XSDParticle)i.next();
dumpParticle(pcle, level+1, xPath , xPathwithTNS);
}
} else
{
// for "<xs:group ref=
dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
xPath , xPathwithTNS);
}

} else if(item instanceof XSDAttributeGroupDefinition)
{
XSDAttributeGroupDefinition att_grp = (XSDAttributeGroupDefinition)item;
if(!att_grp.getAttributeUses().isEmpty())
{
for(Iterator i = att_grp.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeUse att_use = (XSDAttributeUse)i.next();

dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
xPathwithTNS);
}
}else // For an attributeGroup reference
{
int k = 0 ;
//dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
level, xPath);
}

}
else
{
// example : XSDAnnotation, XSDNotationDeclaration, XSDImport,
XSDInclude,
if(item != null)
System.out.println("Unhandled Schema Content : "+ item.toString());
//$NON-NLS-1$
}
}catch(Exception e)
{
e.printStackTrace();
}
// Set the class level helper variables to default values
m_ignoreCpexTypeDefnName = false;
m_isRoot = false;

}

//---------------------------------------------------------- -----------------------------------------------------



/**
* Method dumpParticle.
* @param p
* @param level
*/
private void dumpParticle(XSDParticle p, int level,String xPath , String
xPathwithTNS)
{

XSDParticleContent partContent= p.getContent();
//XSDComplexTypeDefinition com =
partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
if(partContent instanceof XSDSimpleTypeDefinition){
dumpContentItem((XSDSimpleTypeDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDElementDeclaration)
{

dumpContentItem((XSDElementDeclaration)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroupDefinition)
{
dumpContentItem((XSDModelGroupDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroup)
{
XSDModelGroup temp = (XSDModelGroup)partContent;

for(Iterator i = temp.getContents().iterator(); i.hasNext(); )
{
XSDParticle part = (XSDParticle)i.next();

dumpParticle(part, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("unhandled particle "+ p.toString()); //$NON-NLS-1$
//XSDTerm
//XSDWildcard
}
}


public static void main(String args[])
{
// System.exit(((Integer)new XSDMainExample().run(args)).intValue());
XSDParserStandAlone xp = new XSDParserStandAlone();
try {
//xp.parseXSD(" C:\\Trandev501048\\schematest\\Schemas\\ProcessRemittanceAdv ice.xsd ","Header");
xp.parseXSD("NYAddress.xsd","address");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Thanks
Param
Re: extension base [message #66798 is a reply to message #66764] Tue, 21 March 2006 11:57 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Param,

I don' have time to set up a sample to run it. If you want me to run
something, please zip up the whole self-contained project. I think you
are doing a recursive traversal assuming a tree structure when in fact
you are traversing a graph with cycles.


Param wrote:

> Hi Ed,
>
> Here is the simplified sample files.
>
> I am running into infinite loop when resolving for Address element
>
> Sample Schema files.
>
> 1) NyAddress.xsd
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by
> Param (GXS) -->
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:import namespace="http://gxs.com/1" schemaLocation="imp1.xsd"/>
> <xs:import namespace="http://gxs.com/2" schemaLocation="imp2.xsd"/>
> <xs:complexType name="NYAddress">
> <xs:complexContent>
> <xs:extension base="imp1:USAddress"/>
> </xs:complexContent>
> </xs:complexType>
> <xs:element name="address">
> <xs:complexType>
> <xs:complexContent>
> <xs:extension base="NYAddress"/>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
>
> 2)imp1.xsd
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
> targetNamespace="http://gxs.com/1" elementFormDefault="qualified"
> attributeFormDefault="unqualified">
> <xs:complexType name="USAddress" block="extension">
> <xs:sequence>
> <xs:element ref="imp1:PAddress"/>
> <xs:element name="state" type="xs:string"/>
> <xs:element name="zip" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Surname" type="xs:string"/>
> </xs:complexType>
> <xs:element name="USAddress" type="imp1:USAddress"/>
> <xs:element name="PAddress" type="imp1:PAddress"/>
> <xs:complexType name="PAddress">
> <xs:sequence>
> <xs:element name="start" type="xs:string"/>
> <xs:element ref="imp2:Address"/>
> <xs:element name="end" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
> 3)imp2.xsd
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp2="http://gxs.com/2" targetNamespace="http://gxs.com/2"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:complexType name="Address">
> <xs:sequence>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> <xs:element name="Address" type="imp2:Address"/>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
>
> Here is the java code I am using
>
> /*
> * Created on Mar 9, 2005
> *
> * TODO To change the template for this generated file go to
> * Window - Preferences - Java - Code Style - Code Templates
> */
> package com.gxs.ai.ngwb.xsdparser;
>
>
>
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.util.ArrayList;
> import java.util.Iterator;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.xsd.XSDAttributeDeclaration;
> import org.eclipse.xsd.XSDAttributeGroupContent;
> import org.eclipse.xsd.XSDAttributeGroupDefinition;
> import org.eclipse.xsd.XSDAttributeUse;
> import org.eclipse.xsd.XSDComplexTypeContent;
> import org.eclipse.xsd.XSDComplexTypeDefinition;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDDiagnosticSeverity;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDModelGroup;
> import org.eclipse.xsd.XSDModelGroupDefinition;
> import org.eclipse.xsd.XSDParticle;
> import org.eclipse.xsd.XSDParticleContent;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.XSDSchemaContent;
> import org.eclipse.xsd.XSDSimpleTypeDefinition;
> import org.eclipse.xsd.XSDTypeDefinition;
> import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
> import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
> import org.eclipse.xsd.util.XSDResourceFactoryImpl;
> import org.eclipse.xsd.util.XSDResourceImpl;
>
> import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
>
>
>
> /**
> * @author tatinip
> *
> * TODO To change the template for this generated type comment go to
> * Window - Preferences - Java - Code Style - Code Templates
> */
> public class XSDParserStandAlone {
>
> private String m_rootElementName;
> private boolean m_isRoot = false;
> private boolean m_ignoreCpexTypeDefnName = false;
>
> public XSDParserStandAlone(){
>
>
> }
> public boolean parseXSD(String xsdFileName ,
> String rootElementName) throws
> FileNotFoundException , IOException , Exception
> {
> m_rootElementName = rootElementName;
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl()); //$NON-NLS-1$
>
> try
> {
> boolean status = validateAndProcess(xsdFileName);
> return status;
> }catch(IOException ioe)
> {
> throw ioe;
> }catch(Exception e)
> { throw e;
> }
>
>
> }
>
>
> private boolean validateAndProcess(String xsdFile) throws Exception
> {
> /*
> * This let's us test whether the string exists as a file.
> * It not, we try as a URI.
> */
>
> ArrayList retVal = new ArrayList();
>
> URI uri;
> if (xsdFile==null ){
> //DMParseError parserError = new DMParseError(4,"","Schema
> file not specified",0,0);
> //m_error_List.add(parserError);
>
>
> //this.rootNode = null;
> return true;
> }
> File file = new File(xsdFile);
> if (!file.exists()){
> //DMParseError parserError = new DMParseError(4,"","File
> Not Found",0,0);
> //m_error_List.add(parserError);
>
> //this.rootNode = null;
> return true;
> }
> if (file.isFile())
> {
> uri = URI.createFileURI(file.getCanonicalFile().toString());
> }
> else
> {
> uri = URI.createURI(xsdFile);
> }
>
> // Create a resource set, create a schema resource, and load
> the main schema file into it.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
> XSDResourceImpl xsdMainResource =
> (XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
> //$NON-NLS-1$
> xsdMainResource.setURI(uri);
> xsdMainResource.load(resourceSet.getLoadOptions());
>
> // Get the first resource, i.e., the main resource and those
> that have been included or imported.
> Object resource = resourceSet.getResources().get(0);
> if (resource instanceof XSDResourceImpl)
> {
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
> XSDSchema xsdSchema = xsdResource.getSchema();
> xsdSchema.validate();
> /*
> * Schema file is valid if Diagnostics is empty
> */
> if (!xsdSchema.getAllDiagnostics().isEmpty())
> {
> boolean errorFound = false;
> for (Iterator i =
> xsdSchema.getAllDiagnostics().iterator(); i.hasNext(); )
> {
> XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
> if (xsdDiagnostic.getSeverity().getValue()!=
> XSDDiagnosticSeverity.WARNING)
> errorFound = true;
> xsdDiagnostic.getLocation();
> // print errors here
>
>
> }
> if (errorFound){
> xsdResource.unload();
> return false;
> }
> }
> // If the user not selected root element set
> first element as root element
> if (m_rootElementName == null ||
> m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
> Iterator i = xsdSchema.getContents().iterator();
> while(i.hasNext()){
> XSDSchemaContent e = (XSDSchemaContent)i.next();
> // probable root element
> if (e instanceof XSDElementDeclaration){
> m_rootElementName =
> e.getElement().getAttribute("name"); //$NON-NLS-1$
> break;
> }
> }
> }
> processRoot(xsdSchema);
> xsdResource.unload();
> return true;
> }
> return false;
> }
>
> private void processRoot(XSDSchema schema)
> {
> //process each element directly under the 'schema' element
> for(Iterator i = schema.getElementDeclarations().iterator();
> i.hasNext(); )
> {
> XSDElementDeclaration e =
> (XSDElementDeclaration)i.next(); // probable root element
> m_isRoot = true;
> if (m_rootElementName != null &&
> !m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
> if (m_rootElementName.equalsIgnoreCase(e.getName())){
> dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
> break;
> }
> }else{
> dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
> }
>
> }
> }
> /**
> * Method dumpContentItem.
> * @param item
> * @param level
> *
> * This is a recursive method called for handling (currently)
> XSDElementDeclaration,
> * XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
> XSDAttributeDeclaration,
> * XSDModelGroupDefinition, XSDAttributeGroupDefinition
> *
> */
> private void dumpContentItem(XSDSchemaContent item, int level,
> String xPath , String xPathwithTNS)
> {
> try{
>
> if(item instanceof XSDElementDeclaration)
> {
>
> XSDElementDeclarationImpl elem =
> (XSDElementDeclarationImpl)item;
>
> if(elem.getName() != null)
> {
> if(xPath.startsWith("/")) //$NON-NLS-1$
> {
> xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
>
> }
> else
> {
> // For the root element
> xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
> }
>
> System.out.println(xPath);
>
> }
>
> m_isRoot = false; // Need to do this after writing the
> root header,
> //because the processing of child
> elements starts here
> // Get the type definition of the Element
> XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();
>
> if(elemTypeDef==null &&
> elem.getResolvedElementDeclaration().getValue() == null)
> {
> dumpContentItem(elem.getResolvedElementDeclaration(),
> level, xPath , xPathwithTNS);
>
> }else if((elemTypeDef instanceof XSDSimpleTypeDefinition)
> &&
> ((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
>
> {
> // Element doesn't have any children i.e
> simpleContent but might have restrictions etc
> }
> else // The 'type' attribute is another element in the
> same XSD file
> {
> /* name of the Complex type must be ignored when an
> element refers to
> * it with the 'type' attribute
> */
> if(elemTypeDef instanceof XSDComplexTypeDefinition)
> {
>
> if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
> m_ignoreCpexTypeDefnName = true;
>
> }
> dumpContentItem(elemTypeDef, level,
> xPath , xPathwithTNS);
> }
> }
> else if (item instanceof XSDComplexTypeDefinition)
> {
>
> XSDComplexTypeDefinitionImpl cplex =
> (XSDComplexTypeDefinitionImpl)item;
>
> if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
> {
> xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
> System.out.println(xPath);
> }
>
>
> // Process the attributes
> for(Iterator i = cplex.getAttributeUses().iterator();
> i.hasNext(); )
> {
> XSDAttributeGroupContent att_grp =
> (XSDAttributeGroupContent) i.next();
>
> // For attribute group
> if(att_grp instanceof XSDAttributeGroupDefinition)
> {
>
> dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath ,
> xPathwithTNS);
>
> }else if(att_grp instanceof XSDAttributeUse)
> {
>
>
> dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
> xPath , xPathwithTNS);
> }
> else
> {
> System.out.println("2. UNHANDLED"); //$NON-NLS-1$
> }
>
> }
>
> if(cplex.getContent() != null)
> {
> if(cplex.getContent() instanceof XSDParticle)
> {
> dumpParticle((XSDParticle)cplex.getContent(),
> level, xPath , xPathwithTNS);
> }
> else if(cplex.getContent() instanceof
> XSDSimpleTypeDefinition)
> {
>
> dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
> xPath , xPathwithTNS);
> }
> else
> {
>
> System.out.println("1. UNHANDLED"+
> cplex.getContent().toString()); //$NON-NLS-1$
> }
> }else if (cplex.getContentType() != null){
> XSDComplexTypeContent cont = cplex.getContentType();
> if (cont instanceof XSDParticle)
> dumpParticle((XSDParticle)cont,level, xPath ,
> xPathwithTNS);
> }
> /*if (cplex.getContentType() != null){
> if(cplex.getContentType() instanceof XSDParticle)
> {
>
> dumpParticle((XSDParticle)cplex.getContentType(),
> level, xPath , xPathwithTNS);
> }
> else if(cplex.getContentType() instanceof
> XSDSimpleTypeDefinition)
> {
>
> dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
> xPath , xPathwithTNS);
> }
> else
> {
>
> System.out.println("1. UNHANDLED"+
> cplex.getContent().toString()); //$NON-NLS-1$
> }
> }*/
>
> }
> else if (item instanceof XSDSimpleTypeDefinition)
> {
> XSDSimpleTypeDefinition simple =
> (XSDSimpleTypeDefinition)item;
>
> xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;
>
> System.out.println(xPath);
>
> if(simple.getContents() != null )
> {
> if(!simple.getContents().isEmpty())
> {
> for(Iterator i = simple.getContents().iterator();
> i.hasNext(); )
> {
> XSDSimpleTypeDefinition child_simple =
> (XSDSimpleTypeDefinition)i.next();
> dumpContentItem(child_simple, level+1, xPath ,
> xPathwithTNS);
> }
> }
> else
> {
> System.out.println("4. UNHANDLED"); //$NON-NLS-1$
> }
> }
> else
> {
> System.out.println("3. UNHANDLED"); //$NON-NLS-1$
> }
> }
> else if (item instanceof XSDAttributeDeclaration)
> {
> XSDAttributeDeclaration attrib =
> (XSDAttributeDeclaration)item;
>
> xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();
>
> System.out.println(xPath);
>
> } else if(item instanceof XSDModelGroupDefinition)
> {
> // For sequence, all, choice
> XSDModelGroupDefinition mgdef =
> (XSDModelGroupDefinition)item;
> if(mgdef.getModelGroup() != null)
> {
> for(Iterator i =
> mgdef.getModelGroup().getContents().iterator(); i.hasNext(); )
> {
> XSDParticle pcle = (XSDParticle)i.next();
> dumpParticle(pcle, level+1, xPath , xPathwithTNS);
> }
> } else
> {
> // for "<xs:group ref=
>
> dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
> xPath , xPathwithTNS);
> }
>
> } else if(item instanceof XSDAttributeGroupDefinition)
> {
> XSDAttributeGroupDefinition att_grp =
> (XSDAttributeGroupDefinition)item;
> if(!att_grp.getAttributeUses().isEmpty())
> {
> for(Iterator i =
> att_grp.getAttributeUses().iterator(); i.hasNext(); )
> {
> XSDAttributeUse att_use = (XSDAttributeUse)i.next();
>
>
> dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
> xPathwithTNS);
> }
> }else // For an attributeGroup reference
> {
> int k = 0 ;
>
> //dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
> level, xPath);
> }
>
> }
> else
> {
> // example : XSDAnnotation, XSDNotationDeclaration,
> XSDImport, XSDInclude,
> if(item != null)
> System.out.println("Unhandled Schema Content : "+
> item.toString()); //$NON-NLS-1$
> }
> }catch(Exception e)
> {
> e.printStackTrace();
> }
> // Set the class level helper variables to default values
> m_ignoreCpexTypeDefnName = false;
> m_isRoot = false;
>
> }
>
> //---------------------------------------------------------- -----------------------------------------------------
>
>
>
>
> /**
> * Method dumpParticle.
> * @param p
> * @param level
> */
> private void dumpParticle(XSDParticle p, int level,String xPath ,
> String xPathwithTNS)
> {
>
> XSDParticleContent partContent= p.getContent();
> //XSDComplexTypeDefinition com =
> partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
>
> if(partContent instanceof XSDSimpleTypeDefinition){
> dumpContentItem((XSDSimpleTypeDefinition)partContent,
> level, xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDElementDeclaration)
> {
>
> dumpContentItem((XSDElementDeclaration)partContent, level,
> xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDModelGroupDefinition)
> {
> dumpContentItem((XSDModelGroupDefinition)partContent,
> level, xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDModelGroup)
> {
> XSDModelGroup temp = (XSDModelGroup)partContent;
>
> for(Iterator i = temp.getContents().iterator();
> i.hasNext(); )
> {
> XSDParticle part = (XSDParticle)i.next();
> dumpParticle(part, level+1, xPath , xPathwithTNS);
> }
> }
> else
> {
> System.out.println("unhandled particle "+ p.toString());
> //$NON-NLS-1$
> //XSDTerm
> //XSDWildcard
> }
> }
>
>
> public static void main(String args[]) {
> // System.exit(((Integer)new
> XSDMainExample().run(args)).intValue());
> XSDParserStandAlone xp = new XSDParserStandAlone();
> try {
>
> //xp.parseXSD(" C:\\Trandev501048\\schematest\\Schemas\\ProcessRemittanceAdv ice.xsd ","Header");
>
> xp.parseXSD("NYAddress.xsd","address");
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
>
> Thanks
> Param
>
Re: extension base [message #67338 is a reply to message #66798] Tue, 21 March 2006 21:58 Go to previous messageGo to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,
Thank you for spending time. I do not know how to upload the zip file.
please suggest me.

Thanks
Param
Re: extension base [message #67355 is a reply to message #67338] Wed, 22 March 2006 06:39 Go to previous messageGo to next message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,

I sent a mail to merks@ca.ibm.com with attachment.

Thanks
Param
Re: extension base [message #67374 is a reply to message #67355] Wed, 22 March 2006 12:05 Go to previous message
Eclipse User
Originally posted by: merks.ca.ibm.com

Param,

As I replied in your direct note, imp1.xsd is missing the required
import of imp2.xsd.


Param wrote:

> Hi Ed Merks,
>
> I sent a mail to merks@ca.ibm.com with attachment.
>
> Thanks
> Param
>
>
Re: extension base [message #597797 is a reply to message #66641] Fri, 17 March 2006 08:44 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25946
Registered: July 2009
Senior Member
Param,

Have a look at XSDMainExample.java for how to load the schema properly.
Then if you are stuck, show exactly what you've tried to do.


Param wrote:
> Hi
>
> I am new to this group and XSD.
>
> I have gone through news group and understand that to resolve
> extension base elements we should use GetContentType(). But I was not
> able to resolve.
>
> Can any one please share me code snipped to resolve the below schema.
>
> Advance Thanks
> Param.
>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:complexType name="Address">
> <xs:sequence>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Person" type="xs:string"/>
> </xs:complexType>
> <xs:complexType name="USAddress">
> <xs:complexContent>
> <xs:extension base="Address">
> <xs:sequence>
> <xs:element name="state" type="xs:string"/>
> <xs:element name="zip" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Surname" type="xs:string"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:complexType name="NYAddress">
> <xs:complexContent>
> <xs:extension base="USAddress">
> <xs:attribute name="RentControlled" type="xs:boolean"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> <xs:element name="address" type="NYAddress"/>
> </xs:schema>
>
Re: extension base [message #597814 is a reply to message #66667] Mon, 20 March 2006 11:40 Go to previous message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,
Thanks for your support.

I have used XSDComplexTypeDefinition.getContentType() to get resolved
elements from imported/included schema files.

I have created standalone java class file which will resolve elements and
prints their names in tree format for your reference.

The problem with current code is it is not able to resolve element
'oa:Parties' defination the xpath for this element is Parties
/ProcessRemittanceAdvice/DataArea/RemittanceAdvice/Header/Pa rties

From this element onwards it is going to infinite loop.

sorry for my english. here are the code and sample files I have used.

Advance Thanks
Param

Java Class (XSDParserStandAlone.java):
/*
* Created on Mar 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.gxs.ai.ngwb.xsdparser;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupContent;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDAttributeUse;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaContent;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;



/**
* @author tatinip
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XSDParserStandAlone
{

private String m_rootElementName;
private boolean m_isRoot = false;
private boolean m_ignoreCpexTypeDefnName = false;

public XSDParserStandAlone(){


}
public boolean parseXSD(String xsdFileName ,
String rootElementName)
throws FileNotFoundException , IOException , Exception
{
m_rootElementName = rootElementName;
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl()); //$NON-NLS-1$

try
{
boolean status = validateAndProcess(xsdFileName);
return status;
}catch(IOException ioe)
{
throw ioe;
}catch(Exception e)
{
throw e;
}


}


private boolean validateAndProcess(String xsdFile) throws Exception
{
/*
* This let's us test whether the string exists as a file.
* It not, we try as a URI.
*/

ArrayList retVal = new ArrayList();

URI uri;

if (xsdFile==null ){
//DMParseError parserError = new DMParseError(4,"","Schema file not
specified",0,0);
//m_error_List.add(parserError);


//this.rootNode = null;
return true;
}

File file = new File(xsdFile);
if (!file.exists()){
//DMParseError parserError = new DMParseError(4,"","File Not
Found",0,0);
//m_error_List.add(parserError);

//this.rootNode = null;
return true;
}

if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

// Create a resource set, create a schema resource, and load the main
schema file into it.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);
XSDResourceImpl xsdMainResource =
(XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
//$NON-NLS-1$
xsdMainResource.setURI(uri);
xsdMainResource.load(resourceSet.getLoadOptions());

// Get the first resource, i.e., the main resource and those that
have been included or imported.
Object resource = resourceSet.getResources().get(0);
if (resource instanceof XSDResourceImpl)
{
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
XSDSchema xsdSchema = xsdResource.getSchema();
xsdSchema.validate();


/*
* Schema file is valid if Diagnostics is empty
*/
if (!xsdSchema.getAllDiagnostics().isEmpty())
{
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
xsdDiagnostic.getLocation();
// print errors here


}

//this.rootNode = null;
xsdResource.unload();
return false;
}

// If the user not selected root element set first element as
root element
if (m_rootElementName == null ||
m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
Iterator i = xsdSchema.getContents().iterator();
while(i.hasNext()){
XSDSchemaContent e = (XSDSchemaContent)i.next(); // probable root
element
if (e instanceof XSDElementDeclaration){
m_rootElementName = e.getElement().getAttribute("name");
//$NON-NLS-1$
break;
}
}
}

processRoot(xsdSchema);
xsdResource.unload();
return true;
}
return false;
}

private void processRoot(XSDSchema schema)
{
//process each element directly under the 'schema' element
for(Iterator i = schema.getElementDeclarations().iterator();
i.hasNext(); )
{
XSDElementDeclaration e = (XSDElementDeclaration)i.next(); // probable
root element
m_isRoot = true;
if (m_rootElementName != null &&
!m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
if (m_rootElementName.equalsIgnoreCase(e.getName())){
dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
break;
}
}else{
dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
}

}
}
/**
* Method dumpContentItem.
* @param item
* @param level
*
* This is a recursive method called for handling (currently)
XSDElementDeclaration,
* XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
XSDAttributeDeclaration,
* XSDModelGroupDefinition, XSDAttributeGroupDefinition
*
*/
private void dumpContentItem(XSDSchemaContent item, int level, String
xPath , String xPathwithTNS)
{
try{

if(item instanceof XSDElementDeclaration)
{

XSDElementDeclarationImpl elem = (XSDElementDeclarationImpl)item;

if(elem.getName() != null)
{
if(xPath.startsWith("/")) //$NON-NLS-1$
{
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();

}
else
{
// For the root element
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
}

System.out.println(xPath);

}

m_isRoot = false; // Need to do this after writing the root header,
//because the processing of child elements starts here
// Get the type definition of the Element
XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();

if(elemTypeDef==null &&
elem.getResolvedElementDeclaration().getValue() == null)
{
// For a reference

dumpContentItem(elem.getResolvedElementDeclaration(), level, xPath ,
xPathwithTNS);

}else if((elemTypeDef instanceof XSDSimpleTypeDefinition) &&
((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
{
// Element doesn't have any children i.e simpleContent but might have
restrictions etc
}
else // The 'type' attribute is another element in the same XSD file
{
/* name of the Complex type must be ignored when an element refers to
* it with the 'type' attribute
*/
if(elemTypeDef instanceof XSDComplexTypeDefinition)
{
if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
m_ignoreCpexTypeDefnName = true;

}

dumpContentItem(elemTypeDef, level, xPath , xPathwithTNS);
}
}
else if (item instanceof XSDComplexTypeDefinition)
{

XSDComplexTypeDefinitionImpl cplex = (XSDComplexTypeDefinitionImpl)item;

if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
{
xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
System.out.println(xPath);
}


// Process the attributes
for(Iterator i = cplex.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeGroupContent att_grp = (XSDAttributeGroupContent) i.next();

// For attribute group
if(att_grp instanceof XSDAttributeGroupDefinition)
{
dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath
, xPathwithTNS);

}else if(att_grp instanceof XSDAttributeUse)
{


dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
xPath , xPathwithTNS);
}
else
{
System.out.println("2. UNHANDLED"); //$NON-NLS-1$
}

}

if (cplex.getContentType() != null){
XSDComplexTypeContent cont = cplex.getContentType();
if (cont instanceof XSDParticle)
dumpParticle((XSDParticle)cont,level, xPath , xPathwithTNS);
}else if(cplex.getContent() != null)
{
if(cplex.getContent() instanceof XSDParticle)
{
dumpParticle((XSDParticle)cplex.getContent(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContent() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}
/*if (cplex.getContentType() != null){
if(cplex.getContentType() instanceof XSDParticle)
{

dumpParticle((XSDParticle)cplex.getContentType(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContentType() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}*/

}
else if (item instanceof XSDSimpleTypeDefinition)
{
XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition)item;

xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;

System.out.println(xPath);

if(simple.getContents() != null )
{
if(!simple.getContents().isEmpty())
{
for(Iterator i = simple.getContents().iterator(); i.hasNext(); )
{
XSDSimpleTypeDefinition child_simple =
(XSDSimpleTypeDefinition)i.next();
dumpContentItem(child_simple, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("4. UNHANDLED"); //$NON-NLS-1$
}
}
else
{
System.out.println("3. UNHANDLED"); //$NON-NLS-1$
}
}
else if (item instanceof XSDAttributeDeclaration)
{
XSDAttributeDeclaration attrib = (XSDAttributeDeclaration)item;

xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();

System.out.println(xPath);

} else if(item instanceof XSDModelGroupDefinition)
{
// For sequence, all, choice
XSDModelGroupDefinition mgdef = (XSDModelGroupDefinition)item;
if(mgdef.getModelGroup() != null)
{
for(Iterator i = mgdef.getModelGroup().getContents().iterator();
i.hasNext(); )
{
XSDParticle pcle = (XSDParticle)i.next();
dumpParticle(pcle, level+1, xPath , xPathwithTNS);
}
} else
{
// for "<xs:group ref=
dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
xPath , xPathwithTNS);
}

} else if(item instanceof XSDAttributeGroupDefinition)
{
XSDAttributeGroupDefinition att_grp = (XSDAttributeGroupDefinition)item;
if(!att_grp.getAttributeUses().isEmpty())
{
for(Iterator i = att_grp.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeUse att_use = (XSDAttributeUse)i.next();

dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
xPathwithTNS);
}
}else // For an attributeGroup reference
{
int k = 0 ;
//dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
level, xPath);
}

}
else
{
// example : XSDAnnotation, XSDNotationDeclaration, XSDImport,
XSDInclude,
if(item != null)
System.out.println("Unhandled Schema Content : "+ item.toString());
//$NON-NLS-1$
}
}catch(Exception e)
{
e.printStackTrace();
}
// Set the class level helper variables to default values
m_ignoreCpexTypeDefnName = false;
m_isRoot = false;

}

//---------------------------------------------------------- -----------------------------------------------------



/**
* Method dumpParticle.
* @param p
* @param level
*/
private void dumpParticle(XSDParticle p, int level,String xPath , String
xPathwithTNS)
{

XSDParticleContent partContent= p.getContent();
//XSDComplexTypeDefinition com =
partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
if(partContent instanceof XSDSimpleTypeDefinition){
dumpContentItem((XSDSimpleTypeDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDElementDeclaration)
{

dumpContentItem((XSDElementDeclaration)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroupDefinition)
{
dumpContentItem((XSDModelGroupDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroup)
{
XSDModelGroup temp = (XSDModelGroup)partContent;
for(Iterator i = temp.getParticles().iterator(); i.hasNext(); )
{
dumpParticle((XSDParticle)i.next(), level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("unhandled particle "+ p.toString()); //$NON-NLS-1$
//XSDTerm
//XSDWildcard
}
}


public static void main(String args[])
{
// System.exit(((Integer)new XSDMainExample().run(args)).intValue());
XSDParserStandAlone xp = new XSDParserStandAlone();
try {
xp.parseXSD("ProcessRemittanceAdvice.xsd","ProcessRemittanceAdvice ");
//
xp.parseXSD("C:\\Trandev501048\\schematest\\Schemas\\NYAddress.xsd ","address");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Sample XSD files :

1) ProcessRemittanceAdvice.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
xmlns:agilent="http://www.agilent.com/oagis"
targetNamespace="http://www.agilent.com/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://www.openapplications.org/oagis"
schemaLocation="RemittanceAdviceTemplate.xsd"/>
<xs:include schemaLocation="RemittanceAdvice.xsd"/>
<xs:element name="ProcessRemittanceAdvice"
type="agilent:ProcessRemittanceAdvice"/>
<xs:complexType name="ProcessRemittanceAdvice">
<xs:complexContent>
<xs:extension base="oa:BusinessObjectDocument">
<xs:sequence>
<xs:element name="DataArea"
type="agilent:ProcessRemittanceAdviceDataArea"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ProcessRemittanceAdviceDataArea">
<xs:sequence>
<xs:element ref="agilent:RemittanceAdvice" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

2) RemittanceAdviceTemplate.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Components_renamed.xsd"/>
</xs:schema>

3) RemittanceAdvice.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:agilent="http://www.agilent.com/oagis"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.agilent.com/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Header" type="agilent:RemittanceHeader"/>
<xs:element name="RemittanceAdvice" type="agilent:RemittanceAdvice"/>
<xs:complexType name="PaymentOrder">
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RemittanceAdvice">
<xs:sequence>
<xs:element ref="agilent:Header"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RemittanceHeader">
<xs:sequence>
<xs:element name="Id" type="xs:string" minOccurs="0"/>
<xs:element ref="oa:Parties" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

4) Meta.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="0.02">
<xs:complexType name="BusinessObjectDocument">
<xs:attribute name="revision" use="required">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:element name="BusinessObjectDocument"
type="oa:BusinessObjectDocument" abstract="true"/>
</xs:schema>

5) Fields_renamed.xsd
<?xml version="1.0" encoding="utf-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="0.02">
<xs:include schemaLocation="Meta.xsd"/>
<xs:complexType name="PartyIdType">
<xs:sequence/>
</xs:complexType>
</xs:schema>

6)Components_renamed.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:oa="http://www.openapplications.org/oagis"
targetNamespace="http://www.openapplications.org/oagis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="Fields_renamed.xsd"/>
<xs:element name="Parties" type="oa:Parties">
<xs:annotation>
<xs:documentation
source="http://www.openapplications.org/oagis">Grouping of semantically
named Parties.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="Parties" block="extension">
<xs:sequence>
<xs:element ref="oa:PartyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PartyBase" abstract="true">
<xs:attribute name="active" type="xs:boolean" use="optional"
default="false"/>
<xs:attribute name="oneTime" type="xs:boolean" use="optional"
default="false"/>
</xs:complexType>
<xs:element name="PartyType" type="oa:PartyBase" abstract="true"/>
</xs:schema>

End of Schema files

Thankyou very much
Param
Re: extension base [message #597820 is a reply to message #66730] Mon, 20 March 2006 11:42 Go to previous message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed,

I am using XSD plugin 2.0.2 with Eclipse 3.1

Once again Thank you

Param
Re: extension base [message #597824 is a reply to message #66748] Tue, 21 March 2006 04:36 Go to previous message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed,

Here is the simplified sample files.

I am running into infinite loop when resolving for Address element

Sample Schema files.

1) NyAddress.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://gxs.com/1" schemaLocation="imp1.xsd"/>
<xs:import namespace="http://gxs.com/2" schemaLocation="imp2.xsd"/>
<xs:complexType name="NYAddress">
<xs:complexContent>
<xs:extension base="imp1:USAddress"/>
</xs:complexContent>
</xs:complexType>
<xs:element name="address">
<xs:complexType>
<xs:complexContent>
<xs:extension base="NYAddress"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>

---- End of sample XSD------------------------------------

2)imp1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
targetNamespace="http://gxs.com/1" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="USAddress" block="extension">
<xs:sequence>
<xs:element ref="imp1:PAddress"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Surname" type="xs:string"/>
</xs:complexType>
<xs:element name="USAddress" type="imp1:USAddress"/>
<xs:element name="PAddress" type="imp1:PAddress"/>
<xs:complexType name="PAddress">
<xs:sequence>
<xs:element name="start" type="xs:string"/>
<xs:element ref="imp2:Address"/>
<xs:element name="end" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

---- End of sample XSD------------------------------------
3)imp2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:imp2="http://gxs.com/2" targetNamespace="http://gxs.com/2"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Address" type="imp2:Address"/>
</xs:schema>

---- End of sample XSD------------------------------------

Here is the java code I am using

/*
* Created on Mar 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.gxs.ai.ngwb.xsdparser;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupContent;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
import org.eclipse.xsd.XSDAttributeUse;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDDiagnosticSeverity;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSchemaContent;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;



/**
* @author tatinip
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XSDParserStandAlone
{

private String m_rootElementName;
private boolean m_isRoot = false;
private boolean m_ignoreCpexTypeDefnName = false;

public XSDParserStandAlone(){


}
public boolean parseXSD(String xsdFileName ,
String rootElementName)
throws FileNotFoundException , IOException , Exception
{
m_rootElementName = rootElementName;
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl()); //$NON-NLS-1$

try
{
boolean status = validateAndProcess(xsdFileName);
return status;
}catch(IOException ioe)
{
throw ioe;
}catch(Exception e)
{
throw e;
}


}


private boolean validateAndProcess(String xsdFile) throws Exception
{
/*
* This let's us test whether the string exists as a file.
* It not, we try as a URI.
*/

ArrayList retVal = new ArrayList();

URI uri;

if (xsdFile==null ){
//DMParseError parserError = new DMParseError(4,"","Schema file not
specified",0,0);
//m_error_List.add(parserError);


//this.rootNode = null;
return true;
}

File file = new File(xsdFile);
if (!file.exists()){
//DMParseError parserError = new DMParseError(4,"","File Not
Found",0,0);
//m_error_List.add(parserError);

//this.rootNode = null;
return true;
}

if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

// Create a resource set, create a schema resource, and load the main
schema file into it.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);
XSDResourceImpl xsdMainResource =
(XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
//$NON-NLS-1$
xsdMainResource.setURI(uri);
xsdMainResource.load(resourceSet.getLoadOptions());

// Get the first resource, i.e., the main resource and those that
have been included or imported.
Object resource = resourceSet.getResources().get(0);
if (resource instanceof XSDResourceImpl)
{
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
XSDSchema xsdSchema = xsdResource.getSchema();
xsdSchema.validate();


/*
* Schema file is valid if Diagnostics is empty
*/
if (!xsdSchema.getAllDiagnostics().isEmpty())
{
boolean errorFound = false;
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
if (xsdDiagnostic.getSeverity().getValue()!=
XSDDiagnosticSeverity.WARNING)
errorFound = true;
xsdDiagnostic.getLocation();
// print errors here


}

if (errorFound){
xsdResource.unload();

return false;
}
}

// If the user not selected root element set first element as
root element
if (m_rootElementName == null ||
m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
Iterator i = xsdSchema.getContents().iterator();
while(i.hasNext()){
XSDSchemaContent e = (XSDSchemaContent)i.next(); // probable root
element
if (e instanceof XSDElementDeclaration){
m_rootElementName = e.getElement().getAttribute("name");
//$NON-NLS-1$
break;
}
}
}

processRoot(xsdSchema);
xsdResource.unload();
return true;
}
return false;
}

private void processRoot(XSDSchema schema)
{
//process each element directly under the 'schema' element
for(Iterator i = schema.getElementDeclarations().iterator();
i.hasNext(); )
{
XSDElementDeclaration e = (XSDElementDeclaration)i.next(); // probable
root element
m_isRoot = true;
if (m_rootElementName != null &&
!m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
if (m_rootElementName.equalsIgnoreCase(e.getName())){
dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
break;
}
}else{
dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
}

}
}
/**
* Method dumpContentItem.
* @param item
* @param level
*
* This is a recursive method called for handling (currently)
XSDElementDeclaration,
* XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
XSDAttributeDeclaration,
* XSDModelGroupDefinition, XSDAttributeGroupDefinition
*
*/
private void dumpContentItem(XSDSchemaContent item, int level, String
xPath , String xPathwithTNS)
{
try{

if(item instanceof XSDElementDeclaration)
{

XSDElementDeclarationImpl elem = (XSDElementDeclarationImpl)item;

if(elem.getName() != null)
{
if(xPath.startsWith("/")) //$NON-NLS-1$
{
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();

}
else
{
// For the root element
xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
}

System.out.println(xPath);

}

m_isRoot = false; // Need to do this after writing the root header,
//because the processing of child elements starts here
// Get the type definition of the Element
XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();

if(elemTypeDef==null &&
elem.getResolvedElementDeclaration().getValue() == null)
{
dumpContentItem(elem.getResolvedElementDeclaration(), level, xPath ,
xPathwithTNS);

}else if((elemTypeDef instanceof XSDSimpleTypeDefinition) &&
((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
{
// Element doesn't have any children i.e simpleContent but might have
restrictions etc
}
else // The 'type' attribute is another element in the same XSD file
{
/* name of the Complex type must be ignored when an element refers to
* it with the 'type' attribute
*/
if(elemTypeDef instanceof XSDComplexTypeDefinition)
{
if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
m_ignoreCpexTypeDefnName = true;

}

dumpContentItem(elemTypeDef, level, xPath , xPathwithTNS);
}
}
else if (item instanceof XSDComplexTypeDefinition)
{

XSDComplexTypeDefinitionImpl cplex = (XSDComplexTypeDefinitionImpl)item;

if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
{
xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
System.out.println(xPath);
}


// Process the attributes
for(Iterator i = cplex.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeGroupContent att_grp = (XSDAttributeGroupContent) i.next();

// For attribute group
if(att_grp instanceof XSDAttributeGroupDefinition)
{
dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath
, xPathwithTNS);

}else if(att_grp instanceof XSDAttributeUse)
{


dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
xPath , xPathwithTNS);
}
else
{
System.out.println("2. UNHANDLED"); //$NON-NLS-1$
}

}

if(cplex.getContent() != null)
{
if(cplex.getContent() instanceof XSDParticle)
{
dumpParticle((XSDParticle)cplex.getContent(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContent() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}else if (cplex.getContentType() != null){
XSDComplexTypeContent cont = cplex.getContentType();
if (cont instanceof XSDParticle)
dumpParticle((XSDParticle)cont,level, xPath , xPathwithTNS);
}
/*if (cplex.getContentType() != null){
if(cplex.getContentType() instanceof XSDParticle)
{

dumpParticle((XSDParticle)cplex.getContentType(), level, xPath ,
xPathwithTNS);
}
else if(cplex.getContentType() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
xPath , xPathwithTNS);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
//$NON-NLS-1$
}
}*/

}
else if (item instanceof XSDSimpleTypeDefinition)
{
XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition)item;

xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;

System.out.println(xPath);

if(simple.getContents() != null )
{
if(!simple.getContents().isEmpty())
{
for(Iterator i = simple.getContents().iterator(); i.hasNext(); )
{
XSDSimpleTypeDefinition child_simple =
(XSDSimpleTypeDefinition)i.next();
dumpContentItem(child_simple, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("4. UNHANDLED"); //$NON-NLS-1$
}
}
else
{
System.out.println("3. UNHANDLED"); //$NON-NLS-1$
}
}
else if (item instanceof XSDAttributeDeclaration)
{
XSDAttributeDeclaration attrib = (XSDAttributeDeclaration)item;

xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();

System.out.println(xPath);

} else if(item instanceof XSDModelGroupDefinition)
{
// For sequence, all, choice
XSDModelGroupDefinition mgdef = (XSDModelGroupDefinition)item;
if(mgdef.getModelGroup() != null)
{
for(Iterator i = mgdef.getModelGroup().getContents().iterator();
i.hasNext(); )
{
XSDParticle pcle = (XSDParticle)i.next();
dumpParticle(pcle, level+1, xPath , xPathwithTNS);
}
} else
{
// for "<xs:group ref=
dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
xPath , xPathwithTNS);
}

} else if(item instanceof XSDAttributeGroupDefinition)
{
XSDAttributeGroupDefinition att_grp = (XSDAttributeGroupDefinition)item;
if(!att_grp.getAttributeUses().isEmpty())
{
for(Iterator i = att_grp.getAttributeUses().iterator(); i.hasNext(); )
{
XSDAttributeUse att_use = (XSDAttributeUse)i.next();

dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
xPathwithTNS);
}
}else // For an attributeGroup reference
{
int k = 0 ;
//dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
level, xPath);
}

}
else
{
// example : XSDAnnotation, XSDNotationDeclaration, XSDImport,
XSDInclude,
if(item != null)
System.out.println("Unhandled Schema Content : "+ item.toString());
//$NON-NLS-1$
}
}catch(Exception e)
{
e.printStackTrace();
}
// Set the class level helper variables to default values
m_ignoreCpexTypeDefnName = false;
m_isRoot = false;

}

//---------------------------------------------------------- -----------------------------------------------------



/**
* Method dumpParticle.
* @param p
* @param level
*/
private void dumpParticle(XSDParticle p, int level,String xPath , String
xPathwithTNS)
{

XSDParticleContent partContent= p.getContent();
//XSDComplexTypeDefinition com =
partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
if(partContent instanceof XSDSimpleTypeDefinition){
dumpContentItem((XSDSimpleTypeDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDElementDeclaration)
{

dumpContentItem((XSDElementDeclaration)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroupDefinition)
{
dumpContentItem((XSDModelGroupDefinition)partContent, level, xPath ,
xPathwithTNS);
}
else if(partContent instanceof XSDModelGroup)
{
XSDModelGroup temp = (XSDModelGroup)partContent;

for(Iterator i = temp.getContents().iterator(); i.hasNext(); )
{
XSDParticle part = (XSDParticle)i.next();

dumpParticle(part, level+1, xPath , xPathwithTNS);
}
}
else
{
System.out.println("unhandled particle "+ p.toString()); //$NON-NLS-1$
//XSDTerm
//XSDWildcard
}
}


public static void main(String args[])
{
// System.exit(((Integer)new XSDMainExample().run(args)).intValue());
XSDParserStandAlone xp = new XSDParserStandAlone();
try {
//xp.parseXSD(" C:\\Trandev501048\\schematest\\Schemas\\ProcessRemittanceAdv ice.xsd ","Header");
xp.parseXSD("NYAddress.xsd","address");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Thanks
Param
Re: extension base [message #597835 is a reply to message #66764] Tue, 21 March 2006 11:57 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25946
Registered: July 2009
Senior Member
Param,

I don' have time to set up a sample to run it. If you want me to run
something, please zip up the whole self-contained project. I think you
are doing a recursive traversal assuming a tree structure when in fact
you are traversing a graph with cycles.


Param wrote:

> Hi Ed,
>
> Here is the simplified sample files.
>
> I am running into infinite loop when resolving for Address element
>
> Sample Schema files.
>
> 1) NyAddress.xsd
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by
> Param (GXS) -->
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:import namespace="http://gxs.com/1" schemaLocation="imp1.xsd"/>
> <xs:import namespace="http://gxs.com/2" schemaLocation="imp2.xsd"/>
> <xs:complexType name="NYAddress">
> <xs:complexContent>
> <xs:extension base="imp1:USAddress"/>
> </xs:complexContent>
> </xs:complexType>
> <xs:element name="address">
> <xs:complexType>
> <xs:complexContent>
> <xs:extension base="NYAddress"/>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
>
> 2)imp1.xsd
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp1="http://gxs.com/1" xmlns:imp2="http://gxs.com/2"
> targetNamespace="http://gxs.com/1" elementFormDefault="qualified"
> attributeFormDefault="unqualified">
> <xs:complexType name="USAddress" block="extension">
> <xs:sequence>
> <xs:element ref="imp1:PAddress"/>
> <xs:element name="state" type="xs:string"/>
> <xs:element name="zip" type="xs:string"/>
> </xs:sequence>
> <xs:attribute name="Surname" type="xs:string"/>
> </xs:complexType>
> <xs:element name="USAddress" type="imp1:USAddress"/>
> <xs:element name="PAddress" type="imp1:PAddress"/>
> <xs:complexType name="PAddress">
> <xs:sequence>
> <xs:element name="start" type="xs:string"/>
> <xs:element ref="imp2:Address"/>
> <xs:element name="end" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
> 3)imp2.xsd
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:imp2="http://gxs.com/2" targetNamespace="http://gxs.com/2"
> elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:complexType name="Address">
> <xs:sequence>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> <xs:element name="Address" type="imp2:Address"/>
> </xs:schema>
>
> ---- End of sample XSD------------------------------------
>
> Here is the java code I am using
>
> /*
> * Created on Mar 9, 2005
> *
> * TODO To change the template for this generated file go to
> * Window - Preferences - Java - Code Style - Code Templates
> */
> package com.gxs.ai.ngwb.xsdparser;
>
>
>
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.util.ArrayList;
> import java.util.Iterator;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.xsd.XSDAttributeDeclaration;
> import org.eclipse.xsd.XSDAttributeGroupContent;
> import org.eclipse.xsd.XSDAttributeGroupDefinition;
> import org.eclipse.xsd.XSDAttributeUse;
> import org.eclipse.xsd.XSDComplexTypeContent;
> import org.eclipse.xsd.XSDComplexTypeDefinition;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDDiagnosticSeverity;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDModelGroup;
> import org.eclipse.xsd.XSDModelGroupDefinition;
> import org.eclipse.xsd.XSDParticle;
> import org.eclipse.xsd.XSDParticleContent;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.XSDSchemaContent;
> import org.eclipse.xsd.XSDSimpleTypeDefinition;
> import org.eclipse.xsd.XSDTypeDefinition;
> import org.eclipse.xsd.impl.XSDComplexTypeDefinitionImpl;
> import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
> import org.eclipse.xsd.util.XSDResourceFactoryImpl;
> import org.eclipse.xsd.util.XSDResourceImpl;
>
> import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
>
>
>
> /**
> * @author tatinip
> *
> * TODO To change the template for this generated type comment go to
> * Window - Preferences - Java - Code Style - Code Templates
> */
> public class XSDParserStandAlone {
>
> private String m_rootElementName;
> private boolean m_isRoot = false;
> private boolean m_ignoreCpexTypeDefnName = false;
>
> public XSDParserStandAlone(){
>
>
> }
> public boolean parseXSD(String xsdFileName ,
> String rootElementName) throws
> FileNotFoundException , IOException , Exception
> {
> m_rootElementName = rootElementName;
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl()); //$NON-NLS-1$
>
> try
> {
> boolean status = validateAndProcess(xsdFileName);
> return status;
> }catch(IOException ioe)
> {
> throw ioe;
> }catch(Exception e)
> { throw e;
> }
>
>
> }
>
>
> private boolean validateAndProcess(String xsdFile) throws Exception
> {
> /*
> * This let's us test whether the string exists as a file.
> * It not, we try as a URI.
> */
>
> ArrayList retVal = new ArrayList();
>
> URI uri;
> if (xsdFile==null ){
> //DMParseError parserError = new DMParseError(4,"","Schema
> file not specified",0,0);
> //m_error_List.add(parserError);
>
>
> //this.rootNode = null;
> return true;
> }
> File file = new File(xsdFile);
> if (!file.exists()){
> //DMParseError parserError = new DMParseError(4,"","File
> Not Found",0,0);
> //m_error_List.add(parserError);
>
> //this.rootNode = null;
> return true;
> }
> if (file.isFile())
> {
> uri = URI.createFileURI(file.getCanonicalFile().toString());
> }
> else
> {
> uri = URI.createURI(xsdFile);
> }
>
> // Create a resource set, create a schema resource, and load
> the main schema file into it.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
> XSDResourceImpl xsdMainResource =
> (XSDResourceImpl)resourceSet.createResource(URI.createURI("*.xsd "));
> //$NON-NLS-1$
> xsdMainResource.setURI(uri);
> xsdMainResource.load(resourceSet.getLoadOptions());
>
> // Get the first resource, i.e., the main resource and those
> that have been included or imported.
> Object resource = resourceSet.getResources().get(0);
> if (resource instanceof XSDResourceImpl)
> {
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
> XSDSchema xsdSchema = xsdResource.getSchema();
> xsdSchema.validate();
> /*
> * Schema file is valid if Diagnostics is empty
> */
> if (!xsdSchema.getAllDiagnostics().isEmpty())
> {
> boolean errorFound = false;
> for (Iterator i =
> xsdSchema.getAllDiagnostics().iterator(); i.hasNext(); )
> {
> XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
> if (xsdDiagnostic.getSeverity().getValue()!=
> XSDDiagnosticSeverity.WARNING)
> errorFound = true;
> xsdDiagnostic.getLocation();
> // print errors here
>
>
> }
> if (errorFound){
> xsdResource.unload();
> return false;
> }
> }
> // If the user not selected root element set
> first element as root element
> if (m_rootElementName == null ||
> m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
> Iterator i = xsdSchema.getContents().iterator();
> while(i.hasNext()){
> XSDSchemaContent e = (XSDSchemaContent)i.next();
> // probable root element
> if (e instanceof XSDElementDeclaration){
> m_rootElementName =
> e.getElement().getAttribute("name"); //$NON-NLS-1$
> break;
> }
> }
> }
> processRoot(xsdSchema);
> xsdResource.unload();
> return true;
> }
> return false;
> }
>
> private void processRoot(XSDSchema schema)
> {
> //process each element directly under the 'schema' element
> for(Iterator i = schema.getElementDeclarations().iterator();
> i.hasNext(); )
> {
> XSDElementDeclaration e =
> (XSDElementDeclaration)i.next(); // probable root element
> m_isRoot = true;
> if (m_rootElementName != null &&
> !m_rootElementName.equalsIgnoreCase("")){ //$NON-NLS-1$
> if (m_rootElementName.equalsIgnoreCase(e.getName())){
> dumpContentItem(e, 0,"" , ""); //$NON-NLS-1$
> break;
> }
> }else{
> dumpContentItem(e, 0,"", ""); //$NON-NLS-1$
> }
>
> }
> }
> /**
> * Method dumpContentItem.
> * @param item
> * @param level
> *
> * This is a recursive method called for handling (currently)
> XSDElementDeclaration,
> * XSDComplexTypeDefinition, XSDSimpleTypeDefinition,
> XSDAttributeDeclaration,
> * XSDModelGroupDefinition, XSDAttributeGroupDefinition
> *
> */
> private void dumpContentItem(XSDSchemaContent item, int level,
> String xPath , String xPathwithTNS)
> {
> try{
>
> if(item instanceof XSDElementDeclaration)
> {
>
> XSDElementDeclarationImpl elem =
> (XSDElementDeclarationImpl)item;
>
> if(elem.getName() != null)
> {
> if(xPath.startsWith("/")) //$NON-NLS-1$
> {
> xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
>
> }
> else
> {
> // For the root element
> xPath = xPath+"/"+elem.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + elem.getQName();
> }
>
> System.out.println(xPath);
>
> }
>
> m_isRoot = false; // Need to do this after writing the
> root header,
> //because the processing of child
> elements starts here
> // Get the type definition of the Element
> XSDTypeDefinition elemTypeDef= elem.getTypeDefinition();
>
> if(elemTypeDef==null &&
> elem.getResolvedElementDeclaration().getValue() == null)
> {
> dumpContentItem(elem.getResolvedElementDeclaration(),
> level, xPath , xPathwithTNS);
>
> }else if((elemTypeDef instanceof XSDSimpleTypeDefinition)
> &&
> ((XSDSimpleTypeDefinition)elem.getTypeDefinition()).getConte nts().isEmpty())
>
> {
> // Element doesn't have any children i.e
> simpleContent but might have restrictions etc
> }
> else // The 'type' attribute is another element in the
> same XSD file
> {
> /* name of the Complex type must be ignored when an
> element refers to
> * it with the 'type' attribute
> */
> if(elemTypeDef instanceof XSDComplexTypeDefinition)
> {
>
> if(((XSDComplexTypeDefinition)elemTypeDef).getName()!=null )
> m_ignoreCpexTypeDefnName = true;
>
> }
> dumpContentItem(elemTypeDef, level,
> xPath , xPathwithTNS);
> }
> }
> else if (item instanceof XSDComplexTypeDefinition)
> {
>
> XSDComplexTypeDefinitionImpl cplex =
> (XSDComplexTypeDefinitionImpl)item;
>
> if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
> {
> xPath = xPath+"/"+cplex.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + cplex.getQName();
> System.out.println(xPath);
> }
>
>
> // Process the attributes
> for(Iterator i = cplex.getAttributeUses().iterator();
> i.hasNext(); )
> {
> XSDAttributeGroupContent att_grp =
> (XSDAttributeGroupContent) i.next();
>
> // For attribute group
> if(att_grp instanceof XSDAttributeGroupDefinition)
> {
>
> dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath ,
> xPathwithTNS);
>
> }else if(att_grp instanceof XSDAttributeUse)
> {
>
>
> dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
> xPath , xPathwithTNS);
> }
> else
> {
> System.out.println("2. UNHANDLED"); //$NON-NLS-1$
> }
>
> }
>
> if(cplex.getContent() != null)
> {
> if(cplex.getContent() instanceof XSDParticle)
> {
> dumpParticle((XSDParticle)cplex.getContent(),
> level, xPath , xPathwithTNS);
> }
> else if(cplex.getContent() instanceof
> XSDSimpleTypeDefinition)
> {
>
> dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level,
> xPath , xPathwithTNS);
> }
> else
> {
>
> System.out.println("1. UNHANDLED"+
> cplex.getContent().toString()); //$NON-NLS-1$
> }
> }else if (cplex.getContentType() != null){
> XSDComplexTypeContent cont = cplex.getContentType();
> if (cont instanceof XSDParticle)
> dumpParticle((XSDParticle)cont,level, xPath ,
> xPathwithTNS);
> }
> /*if (cplex.getContentType() != null){
> if(cplex.getContentType() instanceof XSDParticle)
> {
>
> dumpParticle((XSDParticle)cplex.getContentType(),
> level, xPath , xPathwithTNS);
> }
> else if(cplex.getContentType() instanceof
> XSDSimpleTypeDefinition)
> {
>
> dumpContentItem((XSDSimpleTypeDefinition)cplex.getContentTyp e(),level,
> xPath , xPathwithTNS);
> }
> else
> {
>
> System.out.println("1. UNHANDLED"+
> cplex.getContent().toString()); //$NON-NLS-1$
> }
> }*/
>
> }
> else if (item instanceof XSDSimpleTypeDefinition)
> {
> XSDSimpleTypeDefinition simple =
> (XSDSimpleTypeDefinition)item;
>
> xPath = xPath+"/"+simple.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/" + simple.getQName() ;
>
> System.out.println(xPath);
>
> if(simple.getContents() != null )
> {
> if(!simple.getContents().isEmpty())
> {
> for(Iterator i = simple.getContents().iterator();
> i.hasNext(); )
> {
> XSDSimpleTypeDefinition child_simple =
> (XSDSimpleTypeDefinition)i.next();
> dumpContentItem(child_simple, level+1, xPath ,
> xPathwithTNS);
> }
> }
> else
> {
> System.out.println("4. UNHANDLED"); //$NON-NLS-1$
> }
> }
> else
> {
> System.out.println("3. UNHANDLED"); //$NON-NLS-1$
> }
> }
> else if (item instanceof XSDAttributeDeclaration)
> {
> XSDAttributeDeclaration attrib =
> (XSDAttributeDeclaration)item;
>
> xPath = xPath+"/@"+attrib.getName(); //$NON-NLS-1$
> xPathwithTNS = xPathwithTNS + "/@" + attrib.getQName();
>
> System.out.println(xPath);
>
> } else if(item instanceof XSDModelGroupDefinition)
> {
> // For sequence, all, choice
> XSDModelGroupDefinition mgdef =
> (XSDModelGroupDefinition)item;
> if(mgdef.getModelGroup() != null)
> {
> for(Iterator i =
> mgdef.getModelGroup().getContents().iterator(); i.hasNext(); )
> {
> XSDParticle pcle = (XSDParticle)i.next();
> dumpParticle(pcle, level+1, xPath , xPathwithTNS);
> }
> } else
> {
> // for "<xs:group ref=
>
> dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level,
> xPath , xPathwithTNS);
> }
>
> } else if(item instanceof XSDAttributeGroupDefinition)
> {
> XSDAttributeGroupDefinition att_grp =
> (XSDAttributeGroupDefinition)item;
> if(!att_grp.getAttributeUses().isEmpty())
> {
> for(Iterator i =
> att_grp.getAttributeUses().iterator(); i.hasNext(); )
> {
> XSDAttributeUse att_use = (XSDAttributeUse)i.next();
>
>
> dumpContentItem((att_use).getAttributeDeclaration(),level, xPath ,
> xPathwithTNS);
> }
> }else // For an attributeGroup reference
> {
> int k = 0 ;
>
> //dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(),
> level, xPath);
> }
>
> }
> else
> {
> // example : XSDAnnotation, XSDNotationDeclaration,
> XSDImport, XSDInclude,
> if(item != null)
> System.out.println("Unhandled Schema Content : "+
> item.toString()); //$NON-NLS-1$
> }
> }catch(Exception e)
> {
> e.printStackTrace();
> }
> // Set the class level helper variables to default values
> m_ignoreCpexTypeDefnName = false;
> m_isRoot = false;
>
> }
>
> //---------------------------------------------------------- -----------------------------------------------------
>
>
>
>
> /**
> * Method dumpParticle.
> * @param p
> * @param level
> */
> private void dumpParticle(XSDParticle p, int level,String xPath ,
> String xPathwithTNS)
> {
>
> XSDParticleContent partContent= p.getContent();
> //XSDComplexTypeDefinition com =
> partContent.resolveComplexTypeDefinition(partContent.getElem ent().getNamespaceURI());
>
> if(partContent instanceof XSDSimpleTypeDefinition){
> dumpContentItem((XSDSimpleTypeDefinition)partContent,
> level, xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDElementDeclaration)
> {
>
> dumpContentItem((XSDElementDeclaration)partContent, level,
> xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDModelGroupDefinition)
> {
> dumpContentItem((XSDModelGroupDefinition)partContent,
> level, xPath , xPathwithTNS);
> }
> else if(partContent instanceof XSDModelGroup)
> {
> XSDModelGroup temp = (XSDModelGroup)partContent;
>
> for(Iterator i = temp.getContents().iterator();
> i.hasNext(); )
> {
> XSDParticle part = (XSDParticle)i.next();
> dumpParticle(part, level+1, xPath , xPathwithTNS);
> }
> }
> else
> {
> System.out.println("unhandled particle "+ p.toString());
> //$NON-NLS-1$
> //XSDTerm
> //XSDWildcard
> }
> }
>
>
> public static void main(String args[]) {
> // System.exit(((Integer)new
> XSDMainExample().run(args)).intValue());
> XSDParserStandAlone xp = new XSDParserStandAlone();
> try {
>
> //xp.parseXSD(" C:\\Trandev501048\\schematest\\Schemas\\ProcessRemittanceAdv ice.xsd ","Header");
>
> xp.parseXSD("NYAddress.xsd","address");
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
>
> Thanks
> Param
>
Re: extension base [message #597846 is a reply to message #66798] Tue, 21 March 2006 21:58 Go to previous message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,
Thank you for spending time. I do not know how to upload the zip file.
please suggest me.

Thanks
Param
Re: extension base [message #597851 is a reply to message #67338] Wed, 22 March 2006 06:39 Go to previous message
tatini venkate is currently offline tatini venkate
Messages: 19
Registered: July 2009
Junior Member
Hi Ed Merks,

I sent a mail to merks@ca.ibm.com with attachment.

Thanks
Param
Re: extension base [message #597857 is a reply to message #67355] Wed, 22 March 2006 12:05 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25946
Registered: July 2009
Senior Member
Param,

As I replied in your direct note, imp1.xsd is missing the required
import of imp2.xsd.


Param wrote:

> Hi Ed Merks,
>
> I sent a mail to merks@ca.ibm.com with attachment.
>
> Thanks
> Param
>
>
Previous Topic:getting occurence
Next Topic:Find out the root element
Goto Forum:
  


Current Time: Wed Jul 30 23:23:13 EDT 2014

Powered by FUDForum. Page generated in 0.02490 seconds