View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst.legacy;
24  
25  import java.util.List;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.dom4j.Document;
29  import org.dom4j.Element;
30  import org.dom4j.Node;
31  import org.dom4j.Text;
32  
33  /**
34   * DocumentWalker.
35   *
36   * Created: 30 oct. 06 10:28:10
37   *
38   * @author poussin
39   * @version $Revision$
40   *
41   * Last update: $Date$
42   * by : $Author$
43   */
44  public class DocumentWalker {
45  
46      static private Log log = LogFactory.getLog(DocumentWalker.class);
47  
48      protected DocumentHandler handler;
49  
50      /**
51       * 
52       */
53      public DocumentWalker(DocumentHandler handler) {
54          this.handler = handler;
55      }
56  
57      public void walk(Document doc) {
58          handler.startDocument(doc);
59          Element elem = doc.getRootElement();
60          walk(elem);
61          handler.endDocument(doc);
62      }
63  
64      public void walk(Element elem) {
65          handler.startElement(elem);
66          for (Node node : (List<Node>) elem.content()) {
67              switch (node.getNodeType()) {
68              case Node.ELEMENT_NODE:
69                  walk((Element) node);
70                  break;
71              case Node.TEXT_NODE:
72                  handler.text((Text) node);
73                  break;
74              default:
75                  log.warn("Not supported element type: "
76                          + node.getNodeTypeName());
77                  break;
78              }
79          }
80          handler.endElement(elem);
81      }
82  }