The question was arisen from following example
(put this text into test.xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE book [
<!ELEMENT book (article)+ >
<!ELEMENT article (title,(para|para1|para2)+)>
<!ELEMENT title (#PCDATA) >
<!ELEMENT para (#PCDATA)>
<!ELEMENT para1 (#PCDATA)>
<!ELEMENT para2 (#PCDATA)>
  ]>
<book>
 <article>
   <title>Title</title>
   <para>para</para>
 </article>
</book>
 

(put this in TestGrammar.java)
-----------------------------------------------------------------------

import java.util.StringTokenizer;
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.util.XMLGrammarPoolImpl;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
import org.apache.xerces.parsers.XMLGrammarCachingConfiguration;
import org.apache.xerces.xni.QName;
import org.apache.xerces.impl.dtd.XMLElementDecl;


public class TestGrammar 
    extends org.apache.xerces.parsers.DOMASBuilderImpl{


    public TestGrammar(SymbolTable symbolTable,
org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool) {
        super(symbolTable,grammarPool) ;
    }

    public TestGrammar(XMLGrammarCachingConfiguration config){
        super(config);
    }


    public static void main (String[] args) {
        String systemId;
        String featureId ;
        boolean state ;
        StringBuffer errorMsg = new StringBuffer();

        String NAMESPACES_FEATURE_ID =
"http://xml.org/sax/features/namespaces";;
        String VALIDATION_FEATURE_ID =
"http://xml.org/sax/features/validation";;
        String SCHEMA_VALIDATION_FEATURE_ID =
"http://apache.org/xml/features/validation/schema";;
        String SCHEMA_FULL_CHECKING_FEATURE_ID =
"http://apache.org/xml/features/validation/schema-full-checking";;

        /** Default Schema full checking support (false). */
        try {

            SymbolTable symbolTable = new SymbolTable();
            XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();

            XMLGrammarCachingConfiguration parserConfiguration = 
                new XMLGrammarCachingConfiguration(symbolTable,
grammarPool);

            parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true);
            parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true);

            parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID,
true);
           
parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);

            TestGrammar test = 
                new TestGrammar(parserConfiguration);
            
            featureId = "http://xml.org/sax/features/validation";;
            state = true;
            test.setFeature(featureId,state);

            featureId =
"http://apache.org/xml/features/dom/defer-node-expansion";;
            state = false;
            test.setFeature(featureId,state);

            systemId = "test.xml";
            test.parseURI(systemId);

            if (
grammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_DTD).length >0 
) {
                DTDGrammar gdtd =
(DTDGrammar)(grammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_DTD))[0];
                String element = "article";
                int index = gdtd.getElementDeclIndex(element);
                System.out.println(element+" index="+ index );
                String content = gdtd.getContentSpecAsString(index);
                System.out.println( "content="+content );
                XMLElementDecl xmlElement = new XMLElementDecl();
                if (gdtd.getElementDecl(index,xmlElement)){
                    System.out.println(
"xmlElement.name="+xmlElement.name);
                    System.out.println(
"xmlElement.contentModelValidator="+xmlElement.contentModelValidator );
                }

                QName title = new QName(null,"title","title",null);
                QName para1 = new QName(null,"para","para",null);
                QName para2 = new QName(null,"para","para",null);

                StringTokenizer _st ;
                _st = new StringTokenizer(content,"
(?)|,*+\t\n\r\f",false);
                QName[] children = {title,para1,para2};

                int out=0;
                String token;

                System.out.println("Element before para:");
                while (_st.hasMoreTokens()) {
                    token = _st.nextToken().trim();
                    children[1].setValues(null,token,token,null);
                    out =
xmlElement.contentModelValidator.validate(children,0,children.length);
                    if (out<0) {
                        System.out.println("==>"+token );
                    }

                } // end of while 
                System.out.println();

                title = new QName(null,"title","title",null);
                para1 = new QName(null,"para","para",null);
                para2 = new QName(null,"para","para",null);
                _st = new StringTokenizer(content,"
(?)|,*+\t\n\r\f",false);
                children[0]=title;
                children[1]=para1;
                children[2]=para2;
                gdtd.getElementDecl(index,xmlElement);
                System.out.println("Element after para:");
                while (_st.hasMoreTokens()) {
                    token = _st.nextToken().trim();
                    children[2].setValues(null,token,token,null);
                    out =
xmlElement.contentModelValidator.validate(children,0,children.length);
                    if (out<0) {
                        System.out.println("==>"+token );
                    }
                } // end of while 
                System.out.println();

            }else {
                errorMsg.append("no grammar"+"\n");
            } // end of if (1>0)else
     

        }catch (java.lang.Exception exc) {
            errorMsg.append(exc.getLocalizedMessage()+"\n");
        } // end of catch
        

        if (errorMsg.length()>0) {
            System.out.println("Error:"+ errorMsg.toString());
        } else {
            System.out.println("Done");
        } // end of else

        
    } // end of main ()
    

}//TestGrammar

The ouput is 

 article index=1 
 content=(title,(para|para1|para2)+)
 xmlElement.name=localpart="article",rawname="article"

[EMAIL PROTECTED]
 Element before para:

 Element after para:

 Done

ie, no elements, while i expected para, para1 and para2.

Do i make some mistakes ? 



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

Reply via email to