001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.gwt.client.util; 023 024import com.google.gwt.dom.client.Document; 025import com.google.gwt.dom.client.Node; 026import com.google.gwt.dom.client.NodeList; 027 028/** 029 * @author Alexandre Russel 030 */ 031public class Visitor { 032 private final NodeProcessor processor; 033 034 private boolean processing = true; 035 036 private boolean partialVisit = false; 037 038 private Node startNode; 039 040 private Node endNode; 041 042 public Visitor(NodeProcessor visitor) { 043 this.processor = visitor; 044 } 045 046 /** 047 * process all the children of this node. 048 * 049 * @param node 050 */ 051 public void process(Node node) { 052 visit(node); 053 } 054 055 /** 056 * process all the node of this document. 057 * 058 * @param document 059 */ 060 public void process(Document document) { 061 visit(document); 062 } 063 064 /** 065 * process all the node from startNode to endNode. 066 * 067 * @param startNode 068 * @param endNode 069 */ 070 public void process(Node startNode, Node endNode) { 071 this.processing = false; 072 partialVisit = true; 073 this.startNode = startNode; 074 this.endNode = endNode; 075 visit(startNode.getOwnerDocument()); 076 } 077 078 public void visit(Node node) { 079 if (startNode == node) { 080 processing = true; 081 } else if (endNode == node) { 082 processing = false; 083 } 084 if (processor.doBreak()) { 085 return; 086 } 087 088 NodeList list = node.getChildNodes(); 089 if (list == null || list.getLength() == 0) { 090 processIf(node); 091 } else { 092 int length = list.getLength(); 093 Node[] nodes = new Node[list.getLength()]; 094 for (int x = 0; x < length; x++) { 095 nodes[x] = list.getItem(x); 096 } 097 processIf(node); 098 for (int x = 0; x < length; x++) { 099 visit(nodes[x]); 100 } 101 } 102 103 } 104 105 private void processIf(Node node) { 106 if (processing || !partialVisit) { 107 processor.process(node); 108 } 109 } 110 111}