Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » XSD schema tree
XSD schema tree [message #61612] Tue, 14 June 2005 10:42 Go to next message
Eclipse User
Originally posted by: Parameshwara.tatini.gxs.com

Hi,
I am trying to build tree from Schema. And I am able to build the tree
from schema.

But I am facing the problems if my schema has import/include schemas.

It is like A.xsd imported B.xsd and B.xsd imports C.xsd.


Say I have Element 'xa' defined in A.xsd and type="xbtype" which is
defined in B.xsd and in b.xsd the defination for 'xbtype' is defined as
type="xctype" and in C.xsd i have defined type for 'xctype'. but in parser
i am not able to get all the elements which are defined in C.XSD

Please help me to resolve this issue

Robert
Re: XSD schema tree [message #61660 is a reply to message #61612] Tue, 14 June 2005 12:24 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Robert,

It will help me to know what you've tried. The XSD model really
supports both a syntactic view of the schema as well as the abstract
component model defined in the XML Schema specification. For example,
XSDSchema.getContents() will get you what's physically in the schema
whereas something like XSDSchema.getElementDeclarations will get you all
the elements logically part of the schema (via includes and imports).
It's also possible that you are having problems with imports and
includes not resolving because you've not loaded the schema with an
absolute URI; see the note about "How to get elements from the include
schemas" which is two notes up from this one in the newsgroup.


Robert wrote:

> Hi,
> I am trying to build tree from Schema. And I am able to build the
> tree from schema.
>
> But I am facing the problems if my schema has import/include schemas.
>
> It is like A.xsd imported B.xsd and B.xsd imports C.xsd.
>
>
> Say I have Element 'xa' defined in A.xsd and type="xbtype" which is
> defined in B.xsd and in b.xsd the defination for 'xbtype' is defined
> as type="xctype" and in C.xsd i have defined type for 'xctype'. but in
> parser i am not able to get all the elements which are defined in C.XSD
>
> Please help me to resolve this issue
>
> Robert
>
>
>
Re: XSD schema tree [message #61804 is a reply to message #61660] Tue, 21 June 2005 01:29 Go to previous messageGo to next message
Eclipse User
Originally posted by: Parameshwara.tatini.gxs.com

Hi Ed Merks,

Thanks for your response.

I have gone through that link But not able to solve my problem.
Brief Des about my problem is

I have A.XSD which Includes B.XSD and B.xsd includes C.XSD.

I am able to resolve entity type referenced in B.XSD But not from C.XSD
My Requirement is to build schema tree. I am sending you sample schemas
and code which I have used (modified for demo purpose).


with the current code I am getting this as output

/envelope
/envelope/messageHeader
/envelope/messageHeader/to

Here the problem is Typedefination for element 'to' is defined in C.xsd
which is not resolved.

I should get some thing like this

/envelope
/envelope/messageHeader
/envelope/messageHeader/to
/envelope/messageHeader/to/gln

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","envelope");
} 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();
}
}

}

These are sample files.

A.XSD
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
targetNamespace="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:include schemaLocation="B.xsd"/>
<xsd:element name="envelope" type="EnvelopeType"/>
<xsd:complexType name="EnvelopeType">
<xsd:sequence>
<xsd:element name="messageHeader" type="MessageHeaderType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


B.XSD

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Ewa Iwicka
(EAN International) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:include schemaLocation="C.xsd"/>
<!--Entity Identification -->
<xsd:complexType name="MessageHeaderType">
<xsd:sequence>
<xsd:element name="to" type="PartyIdentificationType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

C.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:simpleType name="GlobalLocationNumberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{13}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="PartyIdentificationType">
<xsd:sequence>
<xsd:choice>
<xsd:element name="gln" type="GlobalLocationNumberType"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


Thanks for Help.
Robert
Re: XSD schema tree [message #61854 is a reply to message #61804] Tue, 21 June 2005 08:08 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------050100040901090906070309
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Robert,

When I ran this sample it produced:

/envelope
/envelope/messageHeader
/envelope/messageHeader/to
/envelope/messageHeader/to/gln

I wonder if 99227 <https://bugs.eclipse.org/bugs/show_bug.cgi?id=99227>
solved the problem? Which version are you trying?


Robert wrote:

> Hi Ed Merks,
>
> Thanks for your response.
>
> I have gone through that link But not able to solve my problem.
> Brief Des about my problem is
>
> I have A.XSD which Includes B.XSD and B.xsd includes C.XSD.
>
> I am able to resolve entity type referenced in B.XSD But not from C.XSD
> My Requirement is to build schema tree. I am sending you sample
> schemas and code which I have used (modified for demo purpose).
>
>
> with the current code I am getting this as output
>
> /envelope
> /envelope/messageHeader
> /envelope/messageHeader/to
>
> Here the problem is Typedefination for element 'to' is defined in
> C.xsd which is not resolved.
>
> I should get some thing like this
> /envelope
> /envelope/messageHeader
> /envelope/messageHeader/to
> /envelope/messageHeader/to/gln
>
> 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","envelope");
> } 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();
> }
> }
>
> }
>
> These are sample files.
>
> A.XSD
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by
> Param (GXS) -->
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
> targetNamespace="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:include schemaLocation="B.xsd"/>
> <xsd:element name="envelope" type="EnvelopeType"/>
> <xsd:complexType name="EnvelopeType">
> <xsd:sequence>
> <xsd:element name="messageHeader" type="MessageHeaderType"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
>
> B.XSD
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Ewa
> Iwicka (EAN International) -->
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:include schemaLocation="C.xsd"/>
> <!--Entity Identification -->
> <xsd:complexType name="MessageHeaderType">
> <xsd:sequence>
> <xsd:element name="to" type="PartyIdentificationType"
> minOccurs="0"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
> C.XSD
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:simpleType name="GlobalLocationNumberType">
> <xsd:restriction base="xsd:string">
> <xsd:pattern value="\d{13}"/>
> </xsd:restriction>
> </xsd:simpleType>
> <xsd:complexType name="PartyIdentificationType">
> <xsd:sequence>
> <xsd:choice>
> <xsd:element name="gln" type="GlobalLocationNumberType"/>
> </xsd:choice>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
>
> Thanks for Help.
> Robert
>
>
>
>
>


--------------050100040901090906070309
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Robert,<br>
<br>
When I ran this sample it produced:<br>
<blockquote>/envelope<br>
/envelope/messageHeader<br>
/envelope/messageHeader/to<br>
/envelope/messageHeader/to/gln<br>
</blockquote>
I wonder if
Re: XSD schema tree [message #61879 is a reply to message #61854] Tue, 21 June 2005 08:19 Go to previous messageGo to next message
Eclipse User
Originally posted by: Parameshwara.tatini.gxs.com

Hi Ed Merks,

Thanks for very quick response.

I am using XSD 2.0.0 plugin and eclipse 3.0.0

Can you suggest me which version of XSD plugin I have to use?.
We are using eclipse 3.0.0 and that I can't change as we are developing
all products in 3.0.0.

Thanks again for quick response

Robert.
Re: XSD schema tree [message #61932 is a reply to message #61879] Tue, 21 June 2005 09:12 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------010906030604020109030401
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Robert,

I'm not able to reproduce this on an old 3.0.1 (with EMF/XSD/SDO 2.0.1)
base I have kicking around either:


Maybe the problem was fixed in the 2.0.x stream. The 2.0.2 version of
EMF/XSD/SDO is a released supported version that should still work with
Eclipse 3.0.0.


Robert wrote:

> Hi Ed Merks,
>
> Thanks for very quick response.
>
> I am using XSD 2.0.0 plugin and eclipse 3.0.0
>
> Can you suggest me which version of XSD plugin I have to use?.
> We are using eclipse 3.0.0 and that I can't change as we are
> developing all products in 3.0.0.
>
> Thanks again for quick response
>
> Robert.
>


--------------010906030604020109030401
Content-Type: multipart/related;
boundary="------------020404090102070604030907"


--------------020404090102070604030907
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Robert,<br>
<br>
I'm not able to reproduce this on an old 3.0.1 (with EMF/XSD/SDO 2.0.1)
base I have kicking around either:<br>
<blockquote><img src="cid:part1.09070607.03000306@ca.ibm.com" alt=""><br>
</blockquote>
Maybe the problem was fixed in the 2.0.x stream.
Re: XSD schema tree [message #61954 is a reply to message #61932] Tue, 21 June 2005 09:34 Go to previous messageGo to next message
Eclipse User
Originally posted by: Parameshwara.tatini.gxs.com

Hi Ed Merks,

Yes you are correct. This is fixed in 2.0.3 or may be before.

Your valueble suggistions really helped us.

Thanks again
Robert.
Re: XSD schema tree [message #62116 is a reply to message #61954] Wed, 29 June 2005 09:43 Go to previous messageGo to next message
Eclipse User
Originally posted by: Parameshwara.tatini.gxs.com

HI Ed Merks,
I am here again with new question.

My requirement is I have to copy main Schema file and imported/included
schema files from remote site.

I have another utility which will copy file from remote site to my local
disk if I send file name.

So first time I will send main schema file to my utility. So that I can
have main file in local disk.

Now my requirement comes here. I have to parse main schema file and get
Import/Include files list.

So when I load schema and validate I am getting getAllDiagnostics errors.
Because I do not have imported files (they are not resolved)

To get imported files I am using the below code. Please advise me weather
I am following right way or not ?

I am posting my code here. The below method prints import/include file
names.


Thanks
Robert

public String[] getImpIncList(String schemaFileName)throws IOException{
String impincList[];


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

File file = new File(schemaFileName);
URI uri;

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

// 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 "));
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())
{
m_error_List = new ArrayList();
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
EList elist = xsdDiagnostic.getComponents();
if (elist != null){
for (Iterator ie = elist.iterator(); ie.hasNext(); )
{
Object obj = ie.next();
if (obj instanceof XSDImport){
XSDImport imp = (XSDImport)obj;
System.out.println(imp.getSchemaLocation());
}else if (obj instanceof XSDInclude){
XSDInclude inc = (XSDInclude)obj;
System.out.println(inc.getSchemaLocation());
}
}
}

}
return null;

}


}




return null;
}
Re: XSD schema tree [message #62140 is a reply to message #62116] Wed, 29 June 2005 17:36 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Robert,

Your code looks okay, but the problem is that the schema locations could
be relative URIs (needing to be resolved against the base URI of the
schema in which they occur) or could even be missing (for imports), in
which case the namespace itself is used as a location.

If you loaded the main remote file into a resource set, all the imports
and includes will resolve (to the correct relative locations). This
will fill the resource set up with all the schema resources you need.
You could look at the URI of each resource to know what file to copy
over and you have the complete list. This seems better.


Robert wrote:

> HI Ed Merks,
> I am here again with new question.
>
> My requirement is I have to copy main Schema file and
> imported/included schema files from remote site.
>
> I have another utility which will copy file from remote site to my
> local disk if I send file name.
>
> So first time I will send main schema file to my utility. So that I
> can have main file in local disk.
>
> Now my requirement comes here. I have to parse main schema file and
> get Import/Include files list.
>
> So when I load schema and validate I am getting getAllDiagnostics
> errors. Because I do not have imported files (they are not resolved)
>
> To get imported files I am using the below code. Please advise me
> weather I am following right way or not ?
>
> I am posting my code here. The below method prints import/include file
> names.
>
>
> Thanks
> Robert
>
> public String[] getImpIncList(String schemaFileName)throws
> IOException{
> String impincList[];
>
>
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl());
>
> File file = new File(schemaFileName);
> URI uri;
>
> if (file.isFile())
> {
> uri = URI.createFileURI(file.getCanonicalFile().toString());
> }
> else
> {
> uri = URI.createURI(schemaFileName);
> }
>
> // 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 "));
> 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())
> {
> m_error_List = new ArrayList();
> for (Iterator i =
> xsdSchema.getAllDiagnostics().iterator(); i.hasNext(); )
> {
> XSDDiagnostic xsdDiagnostic =
> (XSDDiagnostic)i.next();
> EList elist = xsdDiagnostic.getComponents();
> if (elist != null){
> for (Iterator ie = elist.iterator();
> ie.hasNext(); )
> {
> Object obj = ie.next();
> if (obj instanceof XSDImport){
> XSDImport imp = (XSDImport)obj;
>
> System.out.println(imp.getSchemaLocation());
> }else if (obj instanceof XSDInclude){
> XSDInclude inc = (XSDInclude)obj;
>
> System.out.println(inc.getSchemaLocation());
> }
> }
> }
>
> }
> return null;
> }
>
> }
>
>
>
>
> return null;
> }
>
>
Re: XSD schema tree [message #65650 is a reply to message #61612] Sun, 08 January 2006 05:16 Go to previous messageGo to next message
Eclipse User
Originally posted by: erezh.magicsoftware.com

hi,
i'm new to the XSD schema infoset API.
can u help me just extract the tree information from a given schema file?
a simple example would be appreciated (via email as well).

10x
erez
Re: XSD schema tree [message #65670 is a reply to message #65650] Sun, 08 January 2006 05:18 Go to previous messageGo to next message
Eclipse User
Originally posted by: erezh.magicsoftware.com

my email is erezh@magicsoftware.com
Re: XSD schema tree [message #65712 is a reply to message #65650] Mon, 09 January 2006 10:34 Go to previous message
David Steinberg is currently offline David Steinberg
Messages: 489
Registered: July 2009
Senior Member
erezh wrote:

> i'm new to the XSD schema infoset API.
> can u help me just extract the tree information from a given schema file?
> a simple example would be appreciated (via email as well).

Hi Erez,

I'd invite you look at the samples in the org.eclipse.xsd.example
plug-in. XSDMainExample is a good place to start. If you get stuck,
please feel free to come back here and ask specific questions.

Cheers,
Dave
Re: XSD schema tree [message #595689 is a reply to message #61612] Tue, 14 June 2005 12:24 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25950
Registered: July 2009
Senior Member
Robert,

It will help me to know what you've tried. The XSD model really
supports both a syntactic view of the schema as well as the abstract
component model defined in the XML Schema specification. For example,
XSDSchema.getContents() will get you what's physically in the schema
whereas something like XSDSchema.getElementDeclarations will get you all
the elements logically part of the schema (via includes and imports).
It's also possible that you are having problems with imports and
includes not resolving because you've not loaded the schema with an
absolute URI; see the note about "How to get elements from the include
schemas" which is two notes up from this one in the newsgroup.


Robert wrote:

> Hi,
> I am trying to build tree from Schema. And I am able to build the
> tree from schema.
>
> But I am facing the problems if my schema has import/include schemas.
>
> It is like A.xsd imported B.xsd and B.xsd imports C.xsd.
>
>
> Say I have Element 'xa' defined in A.xsd and type="xbtype" which is
> defined in B.xsd and in b.xsd the defination for 'xbtype' is defined
> as type="xctype" and in C.xsd i have defined type for 'xctype'. but in
> parser i am not able to get all the elements which are defined in C.XSD
>
> Please help me to resolve this issue
>
> Robert
>
>
>
Re: XSD schema tree [message #595752 is a reply to message #61660] Tue, 21 June 2005 01:29 Go to previous message
Robert is currently offline Robert
Messages: 45
Registered: July 2009
Member
Hi Ed Merks,

Thanks for your response.

I have gone through that link But not able to solve my problem.
Brief Des about my problem is

I have A.XSD which Includes B.XSD and B.xsd includes C.XSD.

I am able to resolve entity type referenced in B.XSD But not from C.XSD
My Requirement is to build schema tree. I am sending you sample schemas
and code which I have used (modified for demo purpose).


with the current code I am getting this as output

/envelope
/envelope/messageHeader
/envelope/messageHeader/to

Here the problem is Typedefination for element 'to' is defined in C.xsd
which is not resolved.

I should get some thing like this

/envelope
/envelope/messageHeader
/envelope/messageHeader/to
/envelope/messageHeader/to/gln

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","envelope");
} 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();
}
}

}

These are sample files.

A.XSD
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Param
(GXS) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
targetNamespace="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:include schemaLocation="B.xsd"/>
<xsd:element name="envelope" type="EnvelopeType"/>
<xsd:complexType name="EnvelopeType">
<xsd:sequence>
<xsd:element name="messageHeader" type="MessageHeaderType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


B.XSD

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Ewa Iwicka
(EAN International) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:include schemaLocation="C.xsd"/>
<!--Entity Identification -->
<xsd:complexType name="MessageHeaderType">
<xsd:sequence>
<xsd:element name="to" type="PartyIdentificationType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

C.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:simpleType name="GlobalLocationNumberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{13}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="PartyIdentificationType">
<xsd:sequence>
<xsd:choice>
<xsd:element name="gln" type="GlobalLocationNumberType"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


Thanks for Help.
Robert
Re: XSD schema tree [message #595775 is a reply to message #61804] Tue, 21 June 2005 08:08 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25950
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------050100040901090906070309
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Robert,

When I ran this sample it produced:

/envelope
/envelope/messageHeader
/envelope/messageHeader/to
/envelope/messageHeader/to/gln

I wonder if 99227 <https://bugs.eclipse.org/bugs/show_bug.cgi?id=99227>
solved the problem? Which version are you trying?


Robert wrote:

> Hi Ed Merks,
>
> Thanks for your response.
>
> I have gone through that link But not able to solve my problem.
> Brief Des about my problem is
>
> I have A.XSD which Includes B.XSD and B.xsd includes C.XSD.
>
> I am able to resolve entity type referenced in B.XSD But not from C.XSD
> My Requirement is to build schema tree. I am sending you sample
> schemas and code which I have used (modified for demo purpose).
>
>
> with the current code I am getting this as output
>
> /envelope
> /envelope/messageHeader
> /envelope/messageHeader/to
>
> Here the problem is Typedefination for element 'to' is defined in
> C.xsd which is not resolved.
>
> I should get some thing like this
> /envelope
> /envelope/messageHeader
> /envelope/messageHeader/to
> /envelope/messageHeader/to/gln
>
> 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","envelope");
> } 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();
> }
> }
>
> }
>
> These are sample files.
>
> A.XSD
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by
> Param (GXS) -->
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
> targetNamespace="http://www.ean-ucc.org/schemas/1.3.1/eanucc"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:include schemaLocation="B.xsd"/>
> <xsd:element name="envelope" type="EnvelopeType"/>
> <xsd:complexType name="EnvelopeType">
> <xsd:sequence>
> <xsd:element name="messageHeader" type="MessageHeaderType"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
>
> B.XSD
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Ewa
> Iwicka (EAN International) -->
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:include schemaLocation="C.xsd"/>
> <!--Entity Identification -->
> <xsd:complexType name="MessageHeaderType">
> <xsd:sequence>
> <xsd:element name="to" type="PartyIdentificationType"
> minOccurs="0"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
> C.XSD
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="unqualified" attributeFormDefault="unqualified">
> <xsd:simpleType name="GlobalLocationNumberType">
> <xsd:restriction base="xsd:string">
> <xsd:pattern value="\d{13}"/>
> </xsd:restriction>
> </xsd:simpleType>
> <xsd:complexType name="PartyIdentificationType">
> <xsd:sequence>
> <xsd:choice>
> <xsd:element name="gln" type="GlobalLocationNumberType"/>
> </xsd:choice>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
>
> Thanks for Help.
> Robert
>
>
>
>
>


--------------050100040901090906070309
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Robert,<br>
<br>
When I ran this sample it produced:<br>
<blockquote>/envelope<br>
/envelope/messageHeader<br>
/envelope/messageHeader/to<br>
/envelope/messageHeader/to/gln<br>
</blockquote>
I wonder if
Re: XSD schema tree [message #595783 is a reply to message #61854] Tue, 21 June 2005 08:19 Go to previous message
Robert is currently offline Robert
Messages: 45
Registered: July 2009
Member
Hi Ed Merks,

Thanks for very quick response.

I am using XSD 2.0.0 plugin and eclipse 3.0.0

Can you suggest me which version of XSD plugin I have to use?.
We are using eclipse 3.0.0 and that I can't change as we are developing
all products in 3.0.0.

Thanks again for quick response

Robert.
Re: XSD schema tree [message #595806 is a reply to message #61879] Tue, 21 June 2005 09:12 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25950
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------010906030604020109030401
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Robert,

I'm not able to reproduce this on an old 3.0.1 (with EMF/XSD/SDO 2.0.1)
base I have kicking around either:


Maybe the problem was fixed in the 2.0.x stream. The 2.0.2 version of
EMF/XSD/SDO is a released supported version that should still work with
Eclipse 3.0.0.


Robert wrote:

> Hi Ed Merks,
>
> Thanks for very quick response.
>
> I am using XSD 2.0.0 plugin and eclipse 3.0.0
>
> Can you suggest me which version of XSD plugin I have to use?.
> We are using eclipse 3.0.0 and that I can't change as we are
> developing all products in 3.0.0.
>
> Thanks again for quick response
>
> Robert.
>


--------------010906030604020109030401
Content-Type: multipart/related;
boundary="------------020404090102070604030907"


--------------020404090102070604030907
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Robert,<br>
<br>
I'm not able to reproduce this on an old 3.0.1 (with EMF/XSD/SDO 2.0.1)
base I have kicking around either:<br>
<blockquote><img src="cid:part1.09070607.03000306@ca.ibm.com" alt=""><br>
</blockquote>
Maybe the problem was fixed in the 2.0.x stream.
Re: XSD schema tree [message #595818 is a reply to message #61932] Tue, 21 June 2005 09:34 Go to previous message
Robert is currently offline Robert
Messages: 45
Registered: July 2009
Member
Hi Ed Merks,

Yes you are correct. This is fixed in 2.0.3 or may be before.

Your valueble suggistions really helped us.

Thanks again
Robert.
Re: XSD schema tree [message #595888 is a reply to message #61954] Wed, 29 June 2005 09:43 Go to previous message
Robert is currently offline Robert
Messages: 45
Registered: July 2009
Member
HI Ed Merks,
I am here again with new question.

My requirement is I have to copy main Schema file and imported/included
schema files from remote site.

I have another utility which will copy file from remote site to my local
disk if I send file name.

So first time I will send main schema file to my utility. So that I can
have main file in local disk.

Now my requirement comes here. I have to parse main schema file and get
Import/Include files list.

So when I load schema and validate I am getting getAllDiagnostics errors.
Because I do not have imported files (they are not resolved)

To get imported files I am using the below code. Please advise me weather
I am following right way or not ?

I am posting my code here. The below method prints import/include file
names.


Thanks
Robert

public String[] getImpIncList(String schemaFileName)throws IOException{
String impincList[];


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

File file = new File(schemaFileName);
URI uri;

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

// 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 "));
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())
{
m_error_List = new ArrayList();
for (Iterator i = xsdSchema.getAllDiagnostics().iterator();
i.hasNext(); )
{
XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)i.next();
EList elist = xsdDiagnostic.getComponents();
if (elist != null){
for (Iterator ie = elist.iterator(); ie.hasNext(); )
{
Object obj = ie.next();
if (obj instanceof XSDImport){
XSDImport imp = (XSDImport)obj;
System.out.println(imp.getSchemaLocation());
}else if (obj instanceof XSDInclude){
XSDInclude inc = (XSDInclude)obj;
System.out.println(inc.getSchemaLocation());
}
}
}

}
return null;

}


}




return null;
}
Re: XSD schema tree [message #595894 is a reply to message #62116] Wed, 29 June 2005 17:36 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25950
Registered: July 2009
Senior Member
Robert,

Your code looks okay, but the problem is that the schema locations could
be relative URIs (needing to be resolved against the base URI of the
schema in which they occur) or could even be missing (for imports), in
which case the namespace itself is used as a location.

If you loaded the main remote file into a resource set, all the imports
and includes will resolve (to the correct relative locations). This
will fill the resource set up with all the schema resources you need.
You could look at the URI of each resource to know what file to copy
over and you have the complete list. This seems better.


Robert wrote:

> HI Ed Merks,
> I am here again with new question.
>
> My requirement is I have to copy main Schema file and
> imported/included schema files from remote site.
>
> I have another utility which will copy file from remote site to my
> local disk if I send file name.
>
> So first time I will send main schema file to my utility. So that I
> can have main file in local disk.
>
> Now my requirement comes here. I have to parse main schema file and
> get Import/Include files list.
>
> So when I load schema and validate I am getting getAllDiagnostics
> errors. Because I do not have imported files (they are not resolved)
>
> To get imported files I am using the below code. Please advise me
> weather I am following right way or not ?
>
> I am posting my code here. The below method prints import/include file
> names.
>
>
> Thanks
> Robert
>
> public String[] getImpIncList(String schemaFileName)throws
> IOException{
> String impincList[];
>
>
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl());
>
> File file = new File(schemaFileName);
> URI uri;
>
> if (file.isFile())
> {
> uri = URI.createFileURI(file.getCanonicalFile().toString());
> }
> else
> {
> uri = URI.createURI(schemaFileName);
> }
>
> // 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 "));
> 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())
> {
> m_error_List = new ArrayList();
> for (Iterator i =
> xsdSchema.getAllDiagnostics().iterator(); i.hasNext(); )
> {
> XSDDiagnostic xsdDiagnostic =
> (XSDDiagnostic)i.next();
> EList elist = xsdDiagnostic.getComponents();
> if (elist != null){
> for (Iterator ie = elist.iterator();
> ie.hasNext(); )
> {
> Object obj = ie.next();
> if (obj instanceof XSDImport){
> XSDImport imp = (XSDImport)obj;
>
> System.out.println(imp.getSchemaLocation());
> }else if (obj instanceof XSDInclude){
> XSDInclude inc = (XSDInclude)obj;
>
> System.out.println(inc.getSchemaLocation());
> }
> }
> }
>
> }
> return null;
> }
>
> }
>
>
>
>
> return null;
> }
>
>
Re: XSD schema tree [message #597425 is a reply to message #61612] Sun, 08 January 2006 05:16 Go to previous message
Eclipse User
Originally posted by: erezh.magicsoftware.com

hi,
i'm new to the XSD schema infoset API.
can u help me just extract the tree information from a given schema file?
a simple example would be appreciated (via email as well).

10x
erez
Re: XSD schema tree [message #597433 is a reply to message #65650] Sun, 08 January 2006 05:18 Go to previous message
Eclipse User
Originally posted by: erezh.magicsoftware.com

my email is erezh@magicsoftware.com
Re: XSD schema tree [message #597444 is a reply to message #65650] Mon, 09 January 2006 10:34 Go to previous message
David Steinberg is currently offline David Steinberg
Messages: 489
Registered: July 2009
Senior Member
erezh wrote:

> i'm new to the XSD schema infoset API.
> can u help me just extract the tree information from a given schema file?
> a simple example would be appreciated (via email as well).

Hi Erez,

I'd invite you look at the samples in the org.eclipse.xsd.example
plug-in. XSDMainExample is a good place to start. If you get stuck,
please feel free to come back here and ask specific questions.

Cheers,
Dave
Previous Topic:XSD:How to extract xsd:simpleContent ?
Next Topic:XSD:How to extract xsd:simpleContent ?
Goto Forum:
  


Current Time: Fri Aug 01 18:38:53 EDT 2014

Powered by FUDForum. Page generated in 0.03184 seconds