1 20 package org.jahia.utils.xml; 21 22 import java.util.Vector ; 23 24 import org.jahia.exceptions.JahiaException; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 30 31 37 public class XMLParser { 38 39 private static final String CANT_READ_FILE_MSG = "Can't read XML file"; 40 private static final String ERROR_READING_FILE_MSG = "Error reading file"; 41 private static final String PARAMETER_TAG = "parameter"; 42 private static final String PARAMETER_TAG_NAME_ATTRIBUTE = "name"; 43 44 45 52 public static String getParameterValue( Node paramParent, 53 String 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 curNode = paramParent.getFirstChild(); 64 while (curNode != null) { 65 66 if (curNode.getNodeType() == Node.ELEMENT_NODE) { 69 70 if (curNode.getNodeName().equalsIgnoreCase(PARAMETER_TAG)) { 71 NamedNodeMap attr = curNode.getAttributes(); 73 Node paramAttrNode = attr.getNamedItem(PARAMETER_TAG_NAME_ATTRIBUTE); 74 if (paramAttrNode != null) { 75 76 if (paramAttrNode.getNodeValue().equalsIgnoreCase(parameterName)) { 77 Node textNode = curNode.getFirstChild(); 81 if ( textNode == null ){ 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 } 104 curNode = curNode.getNextSibling(); 105 } 106 107 return null; 109 } 110 111 112 113 122 public static Node nextChildOfTag( Node startNode, 123 String tagName 124 ) throws JahiaException { 125 126 129 130 Vector childs = getChildNodes(startNode,tagName); 131 int size = childs.size(); 132 for ( int i=0 ; i<size; i++ ){ 133 Node child = (Node )childs.get(i); 134 if (child.getNodeName().equalsIgnoreCase(tagName)){ 135 138 return child; 139 } 140 } 141 142 return null; 143 } 144 145 146 156 public static Node lastChildOfTag( Node parentNode, 157 String tagName 158 ) throws JahiaException { 159 160 163 164 Vector childs = getChildNodes(parentNode,tagName); 165 int size = childs.size(); 166 for ( int i=0 ; i<size; i++ ){ 167 Node child = (Node )childs.get(i); 168 if ( child.getNodeName().equalsIgnoreCase(tagName) && (i==size-1) ){ 169 172 return child; 173 } 174 return null; 175 } 176 177 return null; 178 } 179 180 181 189 public static Vector getChildNodes( Node parentNode, 190 String tagName 191 ) throws JahiaException { 192 193 Vector childs = new Vector (); 194 195 NodeList nodeList = parentNode.getChildNodes(); 196 197 if ( nodeList != null ) { 198 199 int size = nodeList.getLength(); 200 for ( int i=0; i<size ; i++ ){ 201 Node nodeItem = null; 202 nodeItem = nodeList.item(i); 203 206 if ( nodeItem.getNodeName().equalsIgnoreCase(tagName) ){ 207 childs.add(nodeItem); 208 } 209 } 210 } 211 212 return childs; 213 } 214 215 216 224 public static String getAttributeValue(Node parentNode, String attributeName){ 225 226 NamedNodeMap attribs = parentNode.getAttributes(); 227 Node attribNode = attribs.getNamedItem(attributeName); 229 if ( attribNode != null ){ 230 return attribNode.getNodeValue(); 232 } 233 return null; 234 } 236 237 244 public static void setAttribute( Element nodeItem, 245 String attribName, 246 String 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 |