DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=13817>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=13817

Getting mandatory root elements from schema (SchemaGrammar or XSModel)

[EMAIL PROTECTED] changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |



------- Additional Comments From [EMAIL PROTECTED]  2002-10-22 09:29 -------
I think that If being an XMLEditor user (application I'm developing) 
you would like following methods included in Xerces, to know which elements
you are allowed to create when creating a new document...

(I.e for your personal.xsd file you get Personnel as the unique element of 
root tag vector, for other .xsd files you get more tags as root elements if 
this is the case). 

For the xsi:type problem? I make a recursive visit thru types to avoid it...

Cheers. 

Daniel Macho.

 /**
     *  Given an SchemaGrammar, returns a list of the root element names that a 
Document can 
     *  have associated as children.
     *  Candidates: those top elements that doesn't need to belong to another 
elements...
     */    
    public Vector getRootElementNames(SchemaGrammar _sg)
    {
            Vector ret = new Vector();
        
            // Get all schema root elements (defined)
            XSNamedMap elementDeclarations = _sg.getComponents
(XSConstants.ELEMENT_DECLARATION);

            int n = elementDeclarations.getMapLength();

            // Visit every top element in grammar. For each of them, store
            // all elements referenced in those types.            
            for (int i=0;i<n;i++)
            {
                XSObject xso=elementDeclarations.getItem(i);                
                XSElementDeclaration xsed = (XSElementDeclaration)xso;
                XSTypeDefinition xtd = xsed.getTypeDefinition();                
                if(xtd!=null)
                    storeReferencedElements(xtd);
                                 
            }
        

            // Make a 2nd visit to fill vector of root element names to return.
            for (int i=0;i<n;i++)
            {
                XSObject xso=elementDeclarations.getItem(i);
                boolean isNotRoot=referencedElements.containsKey(xso.getName());
                if(!isNotRoot)
                    ret.addElement(xso.getName());                    
            }


            return ret;
    }

    /**
     *  For the given type, store referenced elements in referencedElements 
hashtable
     */
    public void storeReferencedElements(XSTypeDefinition xtd)
    {
        // REVISIT: a) Do not use hashtable attribute, but a dynamic one OR b) 
synchronize methods
        
        String typeName = xtd.getName();
        
        // To avoid a named type to be revisited several times, we mark current 
type as a visited one...
        // For in-line (un-named) types there's no solution at all...
        if(typeName!=null && visitedTypes.get(typeName)!=null)
            return;
        
        // REVISIT: 
        // A) Foo value 'v'. We should use a vector of unique values (now hash 
keys...)
        // B) Keep NameSpaces in mind as well!!
        if(typeName!=null)
            visitedTypes.put(typeName,"v");
        
        //REVISIT: ? Used better than: if(xtd.getTypeCategory()
==XSTypeDefinition.COMPLEX_TYPE)
         if(xtd instanceof XSComplexTypeDecl)
         {
              XSComplexTypeDecl xtdc = (XSComplexTypeDecl)
xtd;                        
              XSParticle xsp = xtdc.getParticle();
              if(xsp!=null)
                  refElemsFromParticle(xsp);                  
          } // else ... REVISIT: Simple types may hide some valueable info?
        
    }
    
   public void refElemsFromParticle(XSParticle xsp)
    {
       XSTerm xsterm=xsp.getTerm();
       
       // If it is a complexType, call this very method for every particle
       if(xsterm instanceof XSModelGroupImpl)
       {
         // Obtain particles for this group implementation
         XSModelGroupImpl xmgi = (XSModelGroupImpl)xsterm;
                                
         XSObjectList ol = xmgi.getParticles();
         if(ol!=null)
         {
            int n = ol.getLength();
            for (int j=0;j<n;j++)
            {
                Object o = ol.getItem(j);
                                        
                refElemsFromParticle((XSParticle)o);
            }
         }
       }
       // If it is an element: A) store its name in referencedElements hash and 
       // b) visit its type as well, searching other referenced elements.
       else if (xsterm instanceof XSElementDecl)
       {
          XSElementDecl xeldec = (XSElementDecl) 
xsterm;                                
          String elemName = xeldec.getName();
            
          // REVISIT: Foo value 'r'. We should use a vector of unique values 
(now hash keys...)
          referencedElements.put(elemName,"r");
                                
          XSTypeDefinition xtd = xeldec.getTypeDefinition();
          if(xtd!=null)
            storeReferencedElements(xtd);
        }    
       
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to