Source for gnu.xml.dom.DomNodeIterator

   1: /* DomNodeIterator.java --
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: package gnu.xml.dom;
  38: 
  39: import org.w3c.dom.DOMException;
  40: import org.w3c.dom.Node;
  41: import org.w3c.dom.traversal.NodeFilter;
  42: import org.w3c.dom.traversal.NodeIterator;
  43: import org.w3c.dom.traversal.TreeWalker;
  44: 
  45: /**
  46:  * Node iterator and tree walker.
  47:  *
  48:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  49:  */
  50: public class DomNodeIterator
  51:   implements NodeIterator, TreeWalker
  52: {
  53: 
  54:   Node root;
  55:   final int whatToShow;
  56:   final NodeFilter filter;
  57:   final boolean entityReferenceExpansion;
  58:   final boolean walk;
  59:   Node current;
  60: 
  61:   public DomNodeIterator(Node root, int whatToShow, NodeFilter filter,
  62:                          boolean entityReferenceExpansion, boolean walk)
  63:   {
  64:     if (root == null)
  65:       {
  66:         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
  67:       }
  68:     this.root = root;
  69:     this.whatToShow = whatToShow;
  70:     this.filter = filter;
  71:     this.entityReferenceExpansion = entityReferenceExpansion;
  72:     this.walk = walk;
  73:     current = root;
  74:   }
  75: 
  76:   public Node getRoot()
  77:   {
  78:     return root;
  79:   }
  80: 
  81:   public int getWhatToShow()
  82:   {
  83:     return whatToShow;
  84:   }
  85: 
  86:   public NodeFilter getFilter()
  87:   {
  88:     return filter;
  89:   }
  90: 
  91:   public boolean getExpandEntityReferences()
  92:   {
  93:     return entityReferenceExpansion;
  94:   }
  95: 
  96:   public Node nextNode()
  97:     throws DOMException
  98:   {
  99:     if (root == null)
 100:       {
 101:         throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
 102:       }
 103:     Node ret;
 104:     do
 105:       {
 106:         if (current.equals(root))
 107:           {
 108:             ret = root.getFirstChild();
 109:           }
 110:         else if (walk)
 111:           {
 112:             ret = current.getFirstChild();
 113:             if (ret == null)
 114:               {
 115:                 ret = current.getNextSibling();
 116:               }
 117:             if (ret == null)
 118:               {
 119:                 Node tmp = current;
 120:                 ret = tmp.getParentNode();
 121:                 while (!ret.equals(root) && tmp.equals(ret.getLastChild()))
 122:                   {
 123:                     tmp = ret;
 124:                     ret = tmp.getParentNode();
 125:                   }
 126:                 if (ret.equals(root))
 127:                   {
 128:                     ret = null;
 129:                   }
 130:                 else
 131:                   {
 132:                     ret = ret.getNextSibling();
 133:                   }
 134:               }
 135:           }
 136:         else
 137:           {
 138:             ret = current.getNextSibling();
 139:           }
 140:         current = (ret == null) ? current : ret;
 141:       }
 142:     while (!accept(ret));
 143: 
 144:     return ret;
 145:   }
 146: 
 147:   public Node previousNode()
 148:     throws DOMException
 149:   {
 150:     if (root == null)
 151:       {
 152:         throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
 153:       }
 154:     Node ret;
 155:     do
 156:       {
 157:         if (current.equals(root))
 158:           {
 159:             ret = current.getLastChild();
 160:           }
 161:         else if (walk)
 162:           {
 163:             ret = current.getLastChild();
 164:             if (ret == null)
 165:               {
 166:                 ret = current.getPreviousSibling();
 167:               }
 168:             if (ret == null)
 169:               {
 170:                 Node tmp = current;
 171:                 ret = tmp.getParentNode();
 172:                 while (!ret.equals(root) && tmp.equals(ret.getFirstChild()))
 173:                   {
 174:                     tmp = ret;
 175:                     ret = tmp.getParentNode();
 176:                   }
 177:                 if (ret.equals(root))
 178:                   {
 179:                     ret = null;
 180:                   }
 181:                 else
 182:                   {
 183:                     ret = ret.getPreviousSibling();
 184:                   }
 185:               }
 186:           }
 187:         else
 188:           {
 189:             ret = current.getPreviousSibling();
 190:           }
 191:       }
 192:     while (!accept(ret));
 193:     current = (ret == null) ? current : ret;
 194:     return ret;
 195:   }
 196: 
 197:   public Node getCurrentNode()
 198:   {
 199:     return current;
 200:   }
 201: 
 202:   public void setCurrentNode(Node current)
 203:     throws DOMException
 204:   {
 205:     if (current == null)
 206:       {
 207:         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
 208:       }
 209:     this.current = current;
 210:   }
 211: 
 212:   public Node parentNode()
 213:   {
 214:     Node ret = current.getParentNode();
 215:     if (!accept (ret))
 216:       {
 217:         ret = null;
 218:       }
 219:     current = (ret == null) ? current : ret;
 220:     return ret;
 221:   }
 222: 
 223:   public Node firstChild ()
 224:   {
 225:     Node ret = current.getFirstChild();
 226:     while (!accept(ret))
 227:       {
 228:         ret = ret.getNextSibling();
 229:       }
 230:     current = (ret == null) ? current : ret;
 231:     return ret;
 232:   }
 233: 
 234:   public Node lastChild()
 235:   {
 236:     Node ret = current.getLastChild();
 237:     while (!accept(ret))
 238:       {
 239:         ret = ret.getPreviousSibling();
 240:       }
 241:     current = (ret == null) ? current : ret;
 242:     return ret;
 243:   }
 244: 
 245:   public Node previousSibling()
 246:   {
 247:     Node ret = current.getPreviousSibling();
 248:     while (!accept(ret))
 249:       {
 250:         ret = ret.getPreviousSibling();
 251:       }
 252:     current = (ret == null) ? current : ret;
 253:     return ret;
 254:   }
 255: 
 256:   public Node nextSibling()
 257:   {
 258:     Node ret = current.getNextSibling();
 259:     while (!accept(ret))
 260:       {
 261:         ret = ret.getNextSibling();
 262:       }
 263:     current = (ret == null) ? current : ret;
 264:     return ret;
 265:   }
 266: 
 267:   public void detach()
 268:   {
 269:     root = null;
 270:   }
 271: 
 272:   boolean accept(Node node)
 273:   {
 274:     if (node == null)
 275:       {
 276:         return true;
 277:       }
 278:     boolean ret;
 279:     switch (node.getNodeType())
 280:       {
 281:       case Node.ATTRIBUTE_NODE:
 282:         ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0;
 283:         break;
 284:       case Node.CDATA_SECTION_NODE:
 285:         ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0;
 286:         break;
 287:       case Node.COMMENT_NODE:
 288:         ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0;
 289:         break;
 290:       case Node.DOCUMENT_NODE:
 291:         ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0;
 292:         break;
 293:       case Node.DOCUMENT_FRAGMENT_NODE:
 294:         ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0;
 295:         break;
 296:       case Node.DOCUMENT_TYPE_NODE:
 297:         ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0;
 298:         break;
 299:       case Node.ELEMENT_NODE:
 300:         ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0;
 301:         break;
 302:       case Node.ENTITY_NODE:
 303:         ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0;
 304:         break;
 305:       case Node.ENTITY_REFERENCE_NODE:
 306:         ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0;
 307:         ret = ret && entityReferenceExpansion;
 308:         break;
 309:       case Node.NOTATION_NODE:
 310:         ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0;
 311:         break;
 312:       case Node.PROCESSING_INSTRUCTION_NODE:
 313:         ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0;
 314:         break;
 315:       case Node.TEXT_NODE:
 316:         ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0;
 317:         break;
 318:       default:
 319:         ret = true;
 320:       }
 321:     if (ret && filter != null)
 322:       {
 323:         ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT);
 324:       }
 325:     return ret;
 326:   }
 327: 
 328: }