6.6 Set Levels on Roots

<..set levels for roots..>
 <dom name="." xml="." method="rootLevel" class="tex4ht.HtSpk" />
 <.remove xml declaration.>
-_-_-

<..static void rootLevel(dom)..>
 public static void rootLevel(Node dom) {
    setRootLevel(dom.getFirstChild());
 }
 private static int setRootLevel( Node node ){
   int level = 0;
   String clName = null;
   if (node.hasChildNodes()) {
     if (node.hasAttributes()) {
       Node cl = node.getAttributes().getNamedItem("class");
       if (cl != null) { clName = cl.getNodeValue(); }
     }
     <.count root levels inherited from children.>
     if( clName != null ){
       <.block for roots.>
       if( clName.equals("msqrt") || clName.equals("root") ){
         <.set extra levels for roots.>
         level++;
   } } }
   return level;
 }
-_-_-

<..count root levels inherited from children..>
 NodeList children = node.getChildNodes();
 int max = 0;
 for (int i = 0; i < children.getLength(); i++) {
   Node child = children.item(i);
   if (child.getNodeType() == Node.ELEMENT_NODE) {
     int d = setRootLevel(child);
     if( d > max ){ max = d; }
 } }
 level += max;
-_-_-

<..block for roots..>
 if( clName.equals("msub") || clName.equals("msup") ||
     clName.equals("msubsup")
 ) {
    return 0;
 }
-_-_-

<..set extra levels for roots..>
 for (int i = 0; i < children.getLength(); i++) {
   Node child = children.item(i);
   if (child.getNodeType() == Node.ELEMENT_NODE) {
     Node cls = child.getAttributes()
                     .getNamedItem("class");
     if (cls != null) {
       String clsName = cls.getNodeValue();
       if( clsName.equals("begin-root")
           || clsName.equals("mid-root")
           || clsName.equals("end-root")
       ){
          child = child.getFirstChild();
          String s = child.getNodeValue();
          String nested = "";
          for(int j=0; j<level; j++){
             nested += " nested ";
          }
          ((org.w3c.dom.Text) child).setData( nested + s);
 } } } }
-_-_-