Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » Extension element
Extension element [message #596117] Thu, 28 July 2005 12:49
tatini venkate is currently offline tatini venkateFriend
Messages: 19
Registered: July 2009
Junior Member
I have A.XSD which has element called employee I am not able to resolve
comeplete element defination of employee as subelement uses extension base.

Please help me to resolve extension base elements


My Requirement is to build schema tree. I am sending you sample schema and
code which I have used (modified for demo purpose).


with the current code I am getting this as output

/envelope
/envelope/firstname
/envelope/lastname
/envelope/address
/envelope/city
/envelope/country

But I should get something like this


/envelope
/envelope/address
/envelope/city
/envelope/country


Here is the code I have used

/*
* 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 javax.swing.tree.TreeNode;


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.XSDAttributeUseCategory;
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.XSDPlugin;
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 XSDParserNEW {


private String m_XSDFileName;
private ArrayList m_error_List ;
private ArrayList m_warning_List ;

private int m_minOccurrence = 1;
private int m_maxOccurrence = 1;
private String m_rootElementName;
private boolean m_isRoot = false;
private boolean flag = false;
private boolean m_ignoreCpexTypeDefnName = false;

private String m_modelName;
private String m_modelDirection;
protected TreeNode m_VirtualRoot;
private String m_ModelType="";



public XSDParserNEW(){

}
public boolean parseXSD(String xsdFileName , String rootElementName)
throws FileNotFoundException , IOException , Exception
{
m_rootElementName = rootElementName;
m_XSDFileName = xsdFileName;

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl());

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.
*/

m_error_List = new ArrayList();
m_warning_List = new ArrayList();

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

m_warning_List.add("Schema file not specified in Model file " +
m_modelName);
//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);
m_warning_List.add("Schema file not Found " + xsdFile);
//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);
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.getDiagnostics().isEmpty())
{
for (Iterator i = xsdSchema.getDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();

String localizedSeverity =
XSDPlugin.INSTANCE.getString("_UI_XSDDiagnosticSeverity_" +
xsdDiagnostic.getSeverity());
//Handle Error
// DMParseError parserError = new
DMParseError(4,"" ,xsdDiagnostic.getMessage(),xsdDiagnostic.getLine(),xsdDiagn ostic.getColumn());
//m_error_List.add(parserError);
}
//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("")){
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");
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("")){
if (m_rootElementName.equalsIgnoreCase(e.getName())){
dumpContentItem(e, 0,"");
}
}else{
dumpContentItem(e, 0,"");
}
flag = false;
}
}
/**
* 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)
{
try{


if(item instanceof XSDElementDeclaration)
{

XSDElementDeclarationImpl elem = (XSDElementDeclarationImpl)item;

int min = m_minOccurrence;
int max = m_maxOccurrence;
if(elem.getName() != null)
{
xPath = xPath+"/"+elem.getName();

printTree(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);

}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);
}
}
else if (item instanceof XSDComplexTypeDefinition)
{

XSDComplexTypeDefinitionImpl cplex = (XSDComplexTypeDefinitionImpl)item;
int min = m_minOccurrence;
int max = m_maxOccurrence;


if(!m_ignoreCpexTypeDefnName && cplex.getName()!=null)
{
xPath = xPath+"/"+cplex.getName();
}
// Process the attributes
for(Iterator i = cplex.getAttributeContents().iterator(); i.hasNext(); )
{
XSDAttributeGroupContent att_grp = (XSDAttributeGroupContent) i.next();

// For attribute group
if(att_grp instanceof XSDAttributeGroupDefinition)
{
dumpContentItem(((XSDAttributeGroupDefinition)att_grp), level, xPath);
/*for(Iterator j =
((XSDAttributeGroupDefinition)att_grp).getAttributeUses().it erator();
j.hasNext(); )
{
j.next(); // need to take care
//dumpContentItem(((XSDAttributeUse)j.next()).getAttributeDe claration(),level);
}
*/
}else if(att_grp instanceof XSDAttributeUse)
{
setAttributeOccurrences(((XSDAttributeUse)att_grp).getUse()) ;
dumpContentItem(((XSDAttributeUse)att_grp).getAttributeDecla ration(),level,
xPath);
}
else
{
System.out.println("2. UNHANDLED");
}

}
// Process the element content if it is not equal to null
if(cplex.getContent() != null)
{
if(cplex.getContent() instanceof XSDParticle)
{
dumpParticle((XSDParticle)cplex.getContent(), level, xPath);
}
else if(cplex.getContent() instanceof XSDSimpleTypeDefinition)
{
dumpContentItem((XSDSimpleTypeDefinition)cplex.getContent(), level, xPath);
}
else
{

System.out.println("1. UNHANDLED"+ cplex.getContent().toString());
}
}
}
else if (item instanceof XSDSimpleTypeDefinition)
{
XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition)item;
int min = m_minOccurrence;
int max = m_maxOccurrence;

xPath = xPath+"/"+simple.getName();

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);
}
}
else
{
System.out.println("4. UNHANDLED");
}
}
else
{
System.out.println("3. UNHANDLED");
}
}
else if (item instanceof XSDAttributeDeclaration)
{
XSDAttributeDeclaration attrib = (XSDAttributeDeclaration)item;

xPath = xPath+"/"+attrib.getName();

} 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);
}
} else
{
// for "<xs:group ref=
dumpContentItem((mgdef.getResolvedModelGroupDefinition()), level, xPath);
}

} 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();
setAttributeOccurrences(att_use.getUse());
dumpContentItem((att_use).getAttributeDeclaration(),level, xPath);
}
}else // For an attributeGroup reference
{
//dumpContentItem(att_grp.getResolvedAttributeGroupDefinitio n(), level,
xPath);
}

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

}

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

private void setAttributeOccurrences(XSDAttributeUseCategory e)
{
if(e == XSDAttributeUseCategory.OPTIONAL_LITERAL)
{
m_minOccurrence = 0;
m_maxOccurrence = 1;
} else
if(e == XSDAttributeUseCategory.REQUIRED_LITERAL)
{
m_minOccurrence = 1;
m_maxOccurrence = 1;
} else
if(e == XSDAttributeUseCategory.PROHIBITED_LITERAL)
{
m_minOccurrence = 0;
m_maxOccurrence = 0;
}
else // default
{
m_minOccurrence = 0;
m_maxOccurrence = 1;

}

}

/**
* Method dumpParticle.
* @param p
* @param level
*/
private void dumpParticle(XSDParticle p, int level,String xPath)
{
m_minOccurrence = p.getMinOccurs();

if (p.getMaxOccurs() == XSDParticle.UNBOUNDED ) {
m_maxOccurrence = Integer.MAX_VALUE;
}else {
m_maxOccurrence = p.getMaxOccurs();
}
XSDParticleContent partContent= p.getContent();
if(partContent instanceof XSDElementDeclaration)
{
dumpContentItem((XSDElementDeclaration)partContent, level, xPath);
}
else if(partContent instanceof XSDModelGroupDefinition)
{
dumpContentItem((XSDModelGroupDefinition)partContent, level, xPath);
}
else if(partContent instanceof XSDModelGroup)
{
XSDModelGroup temp = (XSDModelGroup)partContent;

for(Iterator i = temp.getContents().iterator(); i.hasNext(); )
{
dumpParticle((XSDParticle)i.next(), level+1, xPath);
}
}
else
{
System.out.println("unhandled particle "+ p.toString());
//XSDTerm
//XSDWildcard
}
}


private void printTree(String xPath){

System.out.println(xPath);

}


public static void main(String arg[]){
try {
new XSDParserNEW().parseXSD("A.xsd","employee");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

here is XSD file.

A.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>



Thanks in Advance.
Param
Previous Topic:XML generating wizard
Next Topic:Extension element
Goto Forum:
  


Current Time: Fri Apr 26 13:14:44 GMT 2024

Powered by FUDForum. Page generated in 0.03084 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top