Use schema API to query annotation. But failed to obtain it in the case B(see below, search keyword case B): How to get the annotation in the case B?
Enclosed the test program and testc.xsd. The working environment is java 6 and xerces-j-9 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="urn:2008" xmlns:s="urn:set" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:2008" elementFormDefault="unqualified" attributeFormDefault="unqualified" xml:lang="en-US"> <xsd:element name="Message"> <xsd:complexType> <xsd:annotation> <xsd:appinfo>case A</xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element name="Information"> <xsd:complexType> <xsd:annotation> <xsd:appinfo>case A-B</xsd:appinfo> </xsd:annotation> <xsd:complexContent> <xsd:extension base="InformationType"> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="InformationType"> <xsd:annotation> <!-- case B , problem ? --> <xsd:appinfo>case B</xsd:appinfo> </xsd:annotation> <xsd:complexContent> <xsd:extension base="SetBase"> <xsd:sequence> <xsd:element name="Classification"> <xsd:complexType> <xsd:annotation><xsd:appinfo>case C </xsd:appinfo> </xsd:annotation> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:annotation> <xsd:appinfo>case D </xsd:appinfo> </xsd:annotation> <xsd:attribute name="a" type="xsd:string"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="SetBase"> <xsd:sequence> <xsd:element name="AAAA" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
testC.xsd
Description: Binary data
package xs;
import org.apache.xerces.xs.XSAnnotation;
import org.apache.xerces.xs.XSComplexTypeDefinition;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSElementDeclaration;
import org.apache.xerces.xs.XSImplementation;
import org.apache.xerces.xs.XSLoader;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSModelGroup;
import org.apache.xerces.xs.XSNamedMap;
import org.apache.xerces.xs.XSObject;
import org.apache.xerces.xs.XSObjectList;
import org.apache.xerces.xs.XSParticle;
import org.apache.xerces.xs.XSTerm;
import org.apache.xerces.xs.XSTypeDefinition;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.LSParser;
/**
* This sample program illustrates how to use load XML Schemas and
* use XML Schema API (org.apache.xerces.xs) to navigate XML Schema components.
*
* @author Elena Litani, IBM
* @version $Id: QueryXS.java 447691 2006-09-19 02:42:47Z mrglavas $
*/
public class QueryAnnotation implements DOMErrorHandler {
/** Default namespaces support (true). */
protected static final boolean DEFAULT_NAMESPACES = true;
/** Default validation support (false). */
protected static final boolean DEFAULT_VALIDATION = false;
/** Default Schema validation support (false). */
protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
private static final String SUFFIX_SEGMENT="Segment";
static LSParser builder;
public QueryAnnotation(){ }
public static void main(String[] argv) {
if (argv.length == 0) {
printUsage();
System.exit(1);
}
try {
// get DOM Implementation using DOM Registry
System.setProperty(
DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
DOMConfiguration config = schemaLoader.getConfig();
// create Error Handler
DOMErrorHandler errorHandler = new QueryXS();
// set error handler
config.setParameter("error-handler", errorHandler);
// set validation feature
config.setParameter("validate", Boolean.TRUE);
//config.setParameter(name, value)
// parse document
System.out.println("Parsing " + argv[0] + "...");
QueryAnnotation query= new QueryAnnotation();
XSModel model = schemaLoader.loadURI(argv[0]);
if (model != null) {
// element declarations
XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);
if (map.getLength() != 0) {
System.out.println("*************************************************");
System.out.println(" Global element declarations: {namespace} name ");
System.out.println("*************************************************");
for (int i = 0; i < map.getLength(); i++) {
XSObject item = map.item(i);
XSElementDeclaration elementDecl=(XSElementDeclaration)item;
query.traverElementDeclaration(elementDecl);
//System.out.println("{" + item.getNamespace() + "}" + item.getName());
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void printUsage() {
System.err.println("usage: java dom.QueryXS uri ...");
System.err.println();
} // printUsage()
public boolean handleError(DOMError error){
short severity = error.getSeverity();
if (severity == DOMError.SEVERITY_ERROR) {
System.out.println("[xs-error]: "+error.getMessage());
}
if (severity == DOMError.SEVERITY_WARNING) {
System.out.println("[xs-warning]: "+error.getMessage());
}
return true;
}
private void traverParticle(XSParticle particle ){
if( particle !=null ){
XSTerm term=particle.getTerm();
if(term instanceof XSElementDeclaration ){
XSElementDeclaration elementDecl=(XSElementDeclaration)term;
traverElementDeclaration(elementDecl);
}
else if( term instanceof XSModelGroup){
XSModelGroup modelGroup=(XSModelGroup)term;
traverModelGroup(modelGroup);
}
else{//wildcard
System.out.println("traverParticel wildcard");
}
}
}
private void traverElementDeclaration(XSElementDeclaration elementDecl){
if(elementDecl !=null ){
XSAnnotation annotation=elementDecl.getAnnotation();
XSTypeDefinition typeDefinition= elementDecl.getTypeDefinition();
System.out.println("element name "+ elementDecl.getName() );
if( typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE){
XSComplexTypeDefinition complexTypeDefinition=(XSComplexTypeDefinition)typeDefinition;
XSObjectList annotations=complexTypeDefinition.getAnnotations();
System.out.println("check complexType");
if(annotations !=null ){
for(int i=0; i<annotations.getLength(); i++){
XSAnnotation anno=(XSAnnotation)annotations.item(i);
System.out.println("annotation : "+ anno.getAnnotationString() );
}
}
XSParticle particle=complexTypeDefinition.getParticle();
traverParticle(particle);
}
else{
//System.out.println("traverElementDecl simpleType elementName "+ elementDecl.getName());
}
}
}
private void traverModelGroup(XSModelGroup modelGroup){//choice or sequence
if(modelGroup !=null ){
short compositorType=modelGroup.getCompositor();
if( compositorType == XSModelGroup.COMPOSITOR_SEQUENCE ){
}
else if( compositorType == XSModelGroup.COMPOSITOR_CHOICE ){
}
else{//All
}
XSObjectList list=modelGroup.getParticles();
for( int i=0; i< list.getLength(); i++){
XSParticle particle=(XSParticle)(list.item(i));
traverParticle(particle);
}
}
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
