KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > XMLContextDepthReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.io.File JavaDoc;
13
14 import org.mmbase.util.logging.*;
15 import org.w3c.dom.*;
16
17 /**
18  * Reads a contextdepth type of application export configuration file.
19  * Such a file conatins paramters for the ContextDepth appliaction export.
20  * Parameters exits of a start node, and a maximum depth to which to collect nodes for export.
21  * The start node is identified either an alias or a combination of buildername and where clause.
22  * This class can be used to easily retrive these parameters.
23  *
24  * @application Applications
25  * @move org.mmbase.util.xml
26  * @rename ContextDepthReader
27  * @duplicate extend from org.mmbase.util.xml.DocumentReader
28  * @author Daniel Ockeloen
29  * @version $Id: XMLContextDepthReader.java,v 1.9 2006/01/06 17:59:28 michiel Exp $
30  */

31 public class XMLContextDepthReader {
32
33     // logger
34
private static final Logger log = Logging.getLoggerInstance(XMLContextDepthReader.class);
35
36     Document document;
37
38     /**
39      * Creates the Context Depth Reader
40      */

41     public XMLContextDepthReader(String JavaDoc filename) {
42         try {
43             document = XMLBasicReader.getDocumentBuilder(false, null, null).parse(new File JavaDoc(filename));
44         } catch (Exception JavaDoc e) {
45             log.error(e.getMessage());
46             log.error(Logging.stackTrace(e));
47         }
48     }
49
50     /**
51      * Retrieves the depth to which to serach.
52      */

53     public int getDepth() {
54         Node n1 = document.getFirstChild();
55         if (n1 != null) {
56             Node n2 = n1.getFirstChild();
57             while (n2 != null) {
58                 if (n2.getNodeName().equals("depth")) {
59                     Node n3 = n2.getFirstChild();
60                     String JavaDoc tmp = n3.getNodeValue();
61                     try {
62                         return Integer.parseInt(tmp);
63                     } catch (Exception JavaDoc e) {
64                         return -1;
65                     }
66
67                 }
68                 n2 = n2.getNextSibling();
69             }
70         }
71         return -1;
72     }
73
74     /**
75      * Retrieves the content of the buidler attribute of the startnode.
76      */

77     public String JavaDoc getStartBuilder() {
78         Node n1 = document.getFirstChild();
79         if (n1 != null) {
80             Node n2 = n1.getFirstChild();
81             while (n2 != null) {
82                 if (n2.getNodeName().equals("startnode")) {
83                     Node n3 = n2.getFirstChild();
84                     while (n3 != null) {
85                         if (n3.getNodeName().equals("builder")) {
86                             Node n4 = n3.getFirstChild();
87                             return n4.getNodeValue();
88                         }
89                         n3 = n3.getNextSibling();
90                     }
91                 }
92                 n2 = n2.getNextSibling();
93             }
94         }
95         return null;
96     }
97
98     /**
99      * Retrieves the content of the alias attribute of the startnode.
100      */

101     public String JavaDoc getStartAlias() {
102         Node n1 = document.getFirstChild();
103         if (n1 != null) {
104             Node n2 = n1.getFirstChild();
105             while (n2 != null) {
106                 if (n2.getNodeName().equals("startnode")) {
107                     NamedNodeMap nm = n2.getAttributes();
108                     if (nm != null) {
109                         Node n4 = nm.getNamedItem("alias");
110                         if (n4 != null)
111                             return n4.getNodeValue();
112                     }
113
114                 }
115                 n2 = n2.getNextSibling();
116             }
117         }
118         return null;
119     }
120
121     /**
122      * Retrieves the content of the where attribute of the startnode.
123      */

124     public String JavaDoc getStartWhere() {
125         Node n1 = document.getFirstChild();
126         if (n1 != null) {
127             Node n2 = n1.getFirstChild();
128             while (n2 != null) {
129                 if (n2.getNodeName().equals("startnode")) {
130                     Node n3 = n2.getFirstChild();
131                     while (n3 != null) {
132                         if (n3.getNodeName().equals("where")) {
133                             Node n4 = n3.getFirstChild();
134                             return n4.getNodeValue();
135                         }
136                         n3 = n3.getNextSibling();
137                     }
138                 }
139                 n2 = n2.getNextSibling();
140             }
141         }
142         return null;
143     }
144 }
145
Popular Tags