6 Digits into Numbers

<..void digitsIntoNumbers(dom)..>
 static void digitsIntoNumbers(Node dom){
    Node mrowNode = dom.getFirstChild();
    Node mrowChild = mrowNode.getFirstChild();
    short state = <.init state.>;
    Node fromNode = null,
           toNode = null;
    while( mrowChild != null ){
       switch( state ){
          case <.init state.>:
               if( mrowChild.getNodeName().equals("mn") ){
                  state = <.mn state.>;
                  fromNode = mrowChild;
                  toNode = mrowChild;
               }
             break;
          case <.mn state.>:
               boolean bool = true;
               if( mrowChild.getNodeName().equals("mn") ){
                  toNode = mrowChild;
                  bool = ( mrowChild.getNextSibling() == null );
               }
               else
               { <.handle scripted numbers.> }
               if( bool )
               { <.bool := just single digits?.>
                 if( bool ){
                    <.num := merge mn’s.>
                    node.replaceChild( ((Document) dom).createTextNode(num),
                                       node.getFirstChild()
                                     );
                 }
                 state = <.init state.>;
                 fromNode = null;
                 toNode = null;
       }       }
       mrowChild = mrowChild.getNextSibling();
 }  }
-_-_-

<..init state..>
 0
-_-_-

<..mn state..>
 1
-_-_-

<..num := merge mn’s..>
 Node next;
 String num = "";
 Node node = fromNode;
 while( node != toNode ){
   num += node.getTextContent();
   next = node.getNextSibling();
   mrowNode.removeChild( node );
   node = next;
 }
 num += node.getTextContent();
-_-_-

<..bool := just single digits?..>
 for( Node node = fromNode; ; node = node.getNextSibling()){
   String str = node.getTextContent();
   if( str.length() > 1 ){ bool = false; break; }
   if( !str.replaceAll("[0-9]","").equals("") ){ bool = false; break; }
   if( node == toNode ){ break; }
 }
-_-_-

<..handle scripted numbers..>
 String s = null;
 Node base;
 if(
     ( mrowChild.getNodeName().equals("msub")
       ||
       mrowChild.getNodeName().equals("msup")
       ||
       mrowChild.getNodeName().equals("msubsup")
     )
     &&
     ((s = (base = mrowChild.getFirstChild())
           . getTextContent()).length() == 1 )
     &&
     s.replaceAll("[0-9]","").equals("")
 ){
    <.bool := just single digits?.>
    <.num := merge mn’s.>
    mrowNode.removeChild( node );
    num += s;
    Node mn = ((Document) dom).createElement("mn");
    mn.appendChild( ((Document) dom).createTextNode(num) );
    mrowChild.replaceChild( mn, base);
 
    bool = false;
    state = <.init state.>;
    fromNode = null;
    toNode = null;
 }
-_-_-