KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > xml > XMLParser


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// XMLParser
15
//
16
// Loom 11.01.2001
17
// NK 13.01.2001 Factorised as a utils class
18
//
19

20 package org.jahia.utils.xml;
21
22 import java.util.Vector JavaDoc;
23
24 import org.jahia.exceptions.JahiaException;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NamedNodeMap JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30
31 /**
32  * Some tools for loading up data from an xml file
33  *
34  * @author Loom
35  * @version 1.0
36  */

37 public class XMLParser {
38
39     private static final String JavaDoc CANT_READ_FILE_MSG = "Can't read XML file";
40     private static final String JavaDoc ERROR_READING_FILE_MSG = "Error reading file";
41     private static final String JavaDoc PARAMETER_TAG = "parameter";
42     private static final String JavaDoc PARAMETER_TAG_NAME_ATTRIBUTE = "name";
43
44
45     /**
46      * Processes through all the parameter tags of a given node to find the value of
47      * a certain named parameter
48      *
49      *
50      * @return (String) the value of the parameter, null if not found , "" if value=empty
51      */

52     public static String JavaDoc getParameterValue( Node JavaDoc paramParent,
53                                     String JavaDoc parameterName)
54                                     throws JahiaException {
55
56         if (!paramParent.hasChildNodes()) {
57             throw new JahiaException("No parameters available on portlet XML tag",
58                      "Parent has no children at all",
59                      JahiaException.CRITICAL_SEVERITY,
60                      JahiaException.CONFIG_ERROR);
61         }
62
63         Node JavaDoc curNode = paramParent.getFirstChild();
64         while (curNode != null) {
65
66             //JahiaConsole.println("XMLParser.getParameterValue", "Looking for param...["+curNode.getNodeName()+"]");
67
// let's go through all the children nodes
68
if (curNode.getNodeType() == Node.ELEMENT_NODE) {
69
70                 if (curNode.getNodeName().equalsIgnoreCase(PARAMETER_TAG)) {
71                     // we have found a parameter tag, let's check further for match of param name
72
NamedNodeMap JavaDoc attr = curNode.getAttributes();
73                     Node JavaDoc paramAttrNode = attr.getNamedItem(PARAMETER_TAG_NAME_ATTRIBUTE);
74                     if (paramAttrNode != null) {
75
76                         if (paramAttrNode.getNodeValue().equalsIgnoreCase(parameterName)) {
77                             // we found the parameter
78
//JahiaConsole.println("XMLParser.getParameterValue", "Found parameter " + parameterName);
79
// we must now extract value of text node below this node.
80
Node JavaDoc textNode = curNode.getFirstChild();
81                             if ( textNode == null ){ // check this otherwise nullpointer exception NK
82
return "";
83                             } else {
84                                 if (textNode.getNodeType() == Node.TEXT_NODE) {
85                                     return textNode.getNodeValue();
86                                 } else {
87                                     throw new JahiaException(ERROR_READING_FILE_MSG,
88                                     "Value of paramater is not in correct format, should only be text",
89                                     JahiaException.CRITICAL_SEVERITY,
90                                     JahiaException.CONFIG_ERROR);
91                                 }
92                             }
93                         }
94                     } else {
95                         throw new JahiaException(ERROR_READING_FILE_MSG,
96                             "No attribute name found on parameter !",
97                             JahiaException.CRITICAL_SEVERITY,
98                             JahiaException.CONFIG_ERROR);
99                     }
100                 }
101             } else {
102                 // we just ignore other type of tags
103
}
104             curNode = curNode.getNextSibling();
105        }
106
107        return null; // better return null than throw an exception , NK ?
108

109     }
110
111
112
113     /**
114      * nextChildOfTag
115      * Go to the next Child Element Node that is equals
116      * with the gived tag value
117      *
118      * @param (Node) startNode, the parent node
119      * @param (String) tag, the tag name
120      * @author NK
121      */

122     public static Node JavaDoc nextChildOfTag( Node JavaDoc startNode,
123                               String JavaDoc tagName
124                             ) throws JahiaException {
125
126         /*
127         JahiaConsole.println(">>", " nextChildOfTag, tag " + tagName + " started ");
128         */

129
130         Vector JavaDoc childs = getChildNodes(startNode,tagName);
131         int size = childs.size();
132         for ( int i=0 ; i<size; i++ ){
133             Node JavaDoc child = (Node JavaDoc)childs.get(i);
134             if (child.getNodeName().equalsIgnoreCase(tagName)){
135                 /*
136                 JahiaConsole.println(">>", " nextChildOfTag, current child = " + child.getNodeName() );
137                 */

138                 return child;
139             }
140         }
141
142         return null;
143     }
144
145
146     /**
147      * lastChildOfTag
148      * Go to the last Child Element Node that is equals
149      * with the gived tag value
150      *
151      * @param (Node) parentNode, the parent node
152      * @param (String) tag, the tag name
153      * @return (Node) the last child of this tag or null if not found
154      * @author NK
155      */

156     public static Node JavaDoc lastChildOfTag( Node JavaDoc parentNode,
157                               String JavaDoc tagName
158                             ) throws JahiaException {
159
160         /*
161         JahiaConsole.println(">>", " nextChildOfTag, tag " + tagName + " started ");
162         */

163
164         Vector JavaDoc childs = getChildNodes(parentNode,tagName);
165         int size = childs.size();
166         for ( int i=0 ; i<size; i++ ){
167             Node JavaDoc child = (Node JavaDoc)childs.get(i);
168             if ( child.getNodeName().equalsIgnoreCase(tagName) && (i==size-1) ){
169                 /*
170                 JahiaConsole.println(">>", " nextChildOfTag, current child = " + child.getNodeName() );
171                 */

172                 return child;
173             }
174             return null;
175         }
176
177         return null;
178     }
179
180
181     /**
182      * Get a Vector of child nodes equals with a gived tag
183      *
184      * @param (Node) startNode, the parent node
185      * @param (String) tagName, the Children's tag name
186      * @return (Vector) childs, a Vector of child node
187      * @author NK
188      */

189     public static Vector JavaDoc getChildNodes( Node JavaDoc parentNode,
190                                 String JavaDoc tagName
191                               ) throws JahiaException {
192
193         Vector JavaDoc childs = new Vector JavaDoc();
194
195         NodeList JavaDoc nodeList = parentNode.getChildNodes();
196
197         if ( nodeList != null ) {
198
199             int size = nodeList.getLength();
200             for ( int i=0; i<size ; i++ ){
201                 Node JavaDoc nodeItem = null;
202                 nodeItem = nodeList.item(i);
203                 /*
204                 JahiaConsole.println(">>", " getChildNodes, current child node = " + nodeItem.getNodeName() );
205                 */

206                 if ( nodeItem.getNodeName().equalsIgnoreCase(tagName) ){
207                     childs.add(nodeItem);
208                 }
209             }
210         }
211
212         return childs;
213     }
214
215
216     /**
217      * Return the value of a Node's Attribute
218      *
219      * @param (Node) parentNode the parent node
220      * @param (String) attributeName , the name of a gived attribute
221      * @return (String) the value or null
222      * @author NK
223      */

224     public static String JavaDoc getAttributeValue(Node JavaDoc parentNode, String JavaDoc attributeName){
225
226         NamedNodeMap JavaDoc attribs = parentNode.getAttributes();
227         //System.out.println(" node " + parentNode.getNodeName() + " has " + attribs.getLength() + " attributes") ;
228
Node JavaDoc attribNode = attribs.getNamedItem(attributeName);
229         if ( attribNode != null ){
230             //System.out.println(" node " + parentNode.getNodeName() + ", " + attributeName + " =" + attribNode.getNodeValue() ) ;
231
return attribNode.getNodeValue();
232         }
233         return null;
234     } // end getAttributeValue()
235

236
237    /**
238     * Set the attibute of a node only if the value is not null or empty
239     *
240     * @param (ElementNode) a node
241     * @param (String) attribName , the name of the attribute
242     * @param (String) value, the value of the attribute
243     */

244    public static void setAttribute( Element JavaDoc nodeItem,
245                              String JavaDoc attribName,
246                              String JavaDoc value
247                          ){
248
249       if ( value != null && value.length()>0 ){
250          nodeItem.setAttribute(attribName, value);
251       }
252
253    }
254
255
256
257 }
258
Popular Tags