Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Compiling XPaths

HC kicks off with a first implementation of DFA construction

Return to article.


Listing 2: DFATable.java
package org.ananas.hc.compiler;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import org.ananas.hc.QName;

public class DFATable
{
   public Map symbols;
   public int[][] dtran;
   public Object[] astate;

   public DFATable(QName[] qnames,int[][] dtran,Object[] astate)
   {
      symbols = new HashMap();
      for(int i = 0;i < qnames.length;i++)
         symbols.put(qnames[i],new Integer(i));
      this.dtran = dtran;
      this.astate = astate;
   }

   public int move(QName qname,int state)
   {
      Integer key = (Integer)symbols.get(qname);
      if(key != null)
         return dtran[state < 0 ? 0 : state][key.intValue()];
      else
         return -1;
   }

   public boolean isAcceptingState(int state)
   {
      return state < 0 ? false : astate[state] != null;
   }

   public Object getAssociatedData(int state)
   {
      return state < 0 ? null : astate[state];
   }

   public String toString()
   {
      StringBuffer buffer = new StringBuffer();
      Iterator iterator = symbols.entrySet().iterator();
      while(iterator.hasNext())
      {
         Map.Entry entry = (Map.Entry)iterator.next();
         buffer.append('#');
         buffer.append(entry.getValue().toString());
         buffer.append(' ');
         buffer.append(entry.getKey().toString());
         buffer.append('\n');
      }
      for(int i = 0;i < dtran.length;i++)
      {
         for(int j = 0;j < dtran[i].length;j++)
         {
            String st = Integer.toString(dtran[i][j]);
            for(int z = 0;z < 4 - st.length();z++)
               buffer.append(' ');
            buffer.append(dtran[i][j]);
         }
         buffer.append('\n');
      }
      for(int i = 0;i < astate.length;i++)
      {
         buffer.append('#');
         buffer.append(i);
         buffer.append(' ');
         buffer.append(astate.toString());
         buffer.append('\n');
      }
      return buffer.toString();
   }
}

Return to article.