KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > DocumentProcessor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.dynamic;
12
13 import org.eclipse.help.IUAElement;
14 import org.eclipse.help.internal.UAElement;
15
16 /*
17  * A document processor that traverses every node of a document and allows
18  * handlers to perform operations on the nodes.
19  */

20 public class DocumentProcessor {
21
22     private ProcessorHandler[] handlers;
23     
24     /*
25      * Creates a processor with no handlers.
26      */

27     public DocumentProcessor() {
28         handlers = new ProcessorHandler[0];
29     }
30     
31     /*
32      * Creates a processor with the given handlers.
33      */

34     public DocumentProcessor(ProcessorHandler[] handlers) {
35         setHandlers(handlers);
36     }
37     
38     /*
39      * Processes the given node and all its descendants, which exist
40      * inside a document identified by the given id.
41      */

42     public void process(UAElement element, String JavaDoc id) {
43         for (int i=0;i<handlers.length;++i) {
44             short result = handlers[i].handle(element, id);
45             if (result == ProcessorHandler.HANDLED_CONTINUE) {
46                 // handler wants us to keep processing children
47
break;
48             }
49             if (result == ProcessorHandler.HANDLED_SKIP) {
50                 // handler wants us to skip children
51
return;
52             }
53         }
54         // process each child
55
IUAElement[] children = element.getChildren();
56         for (int i=0;i<children.length;++i) {
57             process((UAElement)children[i], id);
58         }
59     }
60     
61     /*
62      * Sets the handlers for this processor.
63      */

64     public void setHandlers(ProcessorHandler[] handlers) {
65         if (this.handlers != handlers) {
66             this.handlers = handlers;
67             for (int i=0;i<handlers.length;++i) {
68                 handlers[i].setProcessor(this);
69             }
70         }
71     }
72 }
73
Popular Tags