001         package com.croftsoft.core.text.sml;
002    
003         import java.util.*;
004    
005         /*********************************************************************
006         * Constructs SmlNodes from parsed SML tokens.
007         *
008         * <p>
009         * Java 1.1 compatible.
010         * </p>
011         *
012         * @version
013         *   2001-07-26
014         * @since
015         *   2001-05-10
016         * @author
017         *   <a href="https://www.croftsoft.com/">David W. Croft</a>
018         *********************************************************************/
019    
020         public final class  SmlNodeParseHandler
021           implements SmlParseHandler
022         //////////////////////////////////////////////////////////////////////
023         //////////////////////////////////////////////////////////////////////
024         {
025    
026         private boolean  allowMixedChildren;
027    
028         private SmlNode  smlNode;
029    
030         private Vector   stackVector;
031    
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034    
035         public  SmlNodeParseHandler ( boolean  allowMixedChildren )
036         //////////////////////////////////////////////////////////////////////
037         {
038           this.allowMixedChildren = allowMixedChildren;
039    
040           stackVector = new Vector ( );
041         }
042    
043         public  SmlNodeParseHandler ( )
044         //////////////////////////////////////////////////////////////////////
045         {
046           this ( false );
047         }
048    
049         //////////////////////////////////////////////////////////////////////
050         //////////////////////////////////////////////////////////////////////
051    
052         public SmlNode  getSmlNode ( ) { return smlNode; }
053    
054         //////////////////////////////////////////////////////////////////////
055         //////////////////////////////////////////////////////////////////////
056    
057         public void  handleCData ( String  cData )
058         //////////////////////////////////////////////////////////////////////
059         {
060    // System.out.println ( "handleCData(" + cData + ")" );
061    
062           if ( smlNode != null )
063           {
064             if ( allowMixedChildren
065               || !smlNode.hasChild ( ) )
066             {
067               smlNode.add ( cData );
068             }
069           }
070         }
071    
072         public void  handleElementOpen ( String  elementName )
073         //////////////////////////////////////////////////////////////////////
074         {
075    // System.out.println ( "handleElementOpen(" + elementName + ")" );
076    
077           SmlNode  smlNode = new SmlNode ( elementName );
078    
079           if ( this.smlNode != null )
080           {
081             if ( allowMixedChildren )
082             {
083               this.smlNode.add ( smlNode );
084             }
085             else
086             {
087               Object  firstChild = this.smlNode.getChild ( 0 );
088    
089               if ( firstChild instanceof String )
090               {
091                 this.smlNode.getChildren ( ) [ 0 ] = smlNode;
092               }
093               else
094               {
095                 this.smlNode.add ( smlNode );
096               }
097             }
098    
099             stackVector.addElement ( this.smlNode ); // stack push
100           }
101      
102           this.smlNode = smlNode;
103         }
104    
105         public void  handleElementClose ( String  elementName )
106         //////////////////////////////////////////////////////////////////////
107         {
108    // System.out.println ( "handleElementClose(" + elementName + ")" );
109    
110           int  index = stackVector.size ( ) - 1;
111    
112           if ( index > -1 )
113           {
114             this.smlNode = ( SmlNode ) stackVector.elementAt ( index );
115    
116             stackVector.removeElementAt ( index ); // stack pop
117           }
118         }
119    
120         public void  handleParseError ( )
121         //////////////////////////////////////////////////////////////////////
122         {
123    // System.out.println ( "handleParseError()" );
124    
125           throw new RuntimeException ( "bad SML stream" );
126         }
127    
128         //////////////////////////////////////////////////////////////////////
129         //////////////////////////////////////////////////////////////////////
130         }