1 46 package org.mr.core.configuration; 47 48 import java.io.File ; 49 import java.util.ArrayList ; 50 import java.util.HashMap ; 51 52 import javax.xml.parsers.DocumentBuilder ; 53 import javax.xml.parsers.DocumentBuilderFactory ; 54 55 import org.mr.core.log.StartupLogger; 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Element ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 66 public class ConfigurationElement { 67 private String name; 68 private ArrayList subConfigurationElements = null; 70 private HashMap subConfigurationElementsMap = null; 71 private String value = null; 73 74 private boolean isLeaf; 75 76 private Element myRoot; 77 78 79 public ConfigurationElement(Element element) { 80 name =element.getNodeName(); 81 myRoot = element; 82 NodeList sub = element.getChildNodes(); 83 if(sub.getLength()==1 && sub.item(0).getNodeType() == Node.TEXT_NODE){ 84 value = sub.item(0).getNodeValue(); 86 value = value.trim(); 87 isLeaf = true; 88 }else{ 89 isLeaf = false; 90 subConfigurationElements = new ArrayList (); 91 subConfigurationElementsMap = new HashMap (); 92 for (int i = 0; i < sub.getLength(); i++) { 93 if(sub.item(i).getNodeType() == Node.ELEMENT_NODE){ 94 Element subE = (Element )sub.item(i); 95 ConfigurationElement subElement =getRefOrRealConfigurationElement(subE); subConfigurationElements.add(subElement); 97 String subName = subElement.getName(); 98 ArrayList toBeAdded = (ArrayList ) subConfigurationElementsMap.get(subName); 99 100 if(toBeAdded == null){ 101 toBeAdded = new ArrayList (); 102 subConfigurationElementsMap.put(subName,toBeAdded); 103 } 104 toBeAdded.add(subElement); 105 106 } 107 } 109 } } 112 public Element getMyElement() { 113 return myRoot; 114 } 115 116 public ArrayList getSubConfigurationElements(String key) { 117 ConfigurationElement element = this; 118 ArrayList result = null; 119 String [] subKeys = key.split("\\."); 120 for (int i = 0; i < subKeys.length; i++) { 121 String subKey = subKeys[i]; 122 if(element != null) 123 result = element.getSubConfigurationElementsByName(subKey); 124 if(result == null){ 125 return null; 126 }else{ 127 element = (ConfigurationElement) result.get(0); 128 } 129 } 130 return result; 131 } 132 133 public ConfigurationElement getSubConfigurationElement(String key) { 134 ConfigurationElement element = this; 135 ArrayList result = null; 136 String [] subKeys = key.split("\\."); 137 for (int i = 0; i < subKeys.length; i++) { 138 String subKey = subKeys[i]; 139 result = element.getSubConfigurationElementsByName(subKey); 140 if(result == null){ 141 return null; 142 }else{ 143 element = (ConfigurationElement) result.get(0); 144 } 145 } 146 return (ConfigurationElement) result.get(0); 147 } 148 149 private ConfigurationElement getRefOrRealConfigurationElement(Element subE) { 150 ConfigurationElement result = null; 151 if(subE.getNodeName().equals("file_ref")){ 152 String fileRef = subE.getChildNodes().item(0).getNodeValue(); 153 File f = new File (fileRef); 154 if(!f.exists()){ 155 StartupLogger.log.fatal("Did not find configuration file reference -"+fileRef, "ConfigurationElement"); 157 throw new IllegalArgumentException ("Did not find configuration file reference -"+fileRef); 158 } 159 Element rootElement; 160 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 161 factory.setIgnoringElementContentWhitespace(true); 162 163 try{ 164 DocumentBuilder xmlBuilder= factory.newDocumentBuilder(); 165 Document xmlDoc = xmlBuilder.parse(f); 166 167 rootElement = xmlDoc.getDocumentElement(); 168 result = new ConfigurationElement(rootElement); 169 }catch(Exception e){ 170 e.printStackTrace(); 171 throw new IllegalArgumentException (e.getLocalizedMessage()); 172 } 173 }else{ 174 result = new ConfigurationElement(subE); 175 } 176 return result; 177 } 178 179 public boolean isLeaf() { 180 return isLeaf; 181 } 182 183 public ArrayList getSubConfigurationElements() { 184 return subConfigurationElements; 185 } 186 187 public ArrayList getSubConfigurationElementsByName(String subName) { 188 ArrayList result = null; 189 if(subConfigurationElementsMap!=null) 190 result =(ArrayList ) subConfigurationElementsMap.get(subName); 191 return result; 192 } 193 public ConfigurationElement getSubConfigurationElementByName(String subName) { 194 ConfigurationElement result = null; 195 ArrayList sub = (ArrayList ) subConfigurationElementsMap.get(subName); 196 if(sub!=null){ 197 result = (ConfigurationElement) sub.get(0); 198 } 199 return result; 200 } 201 202 public String getValue() { 203 return value; 204 } 205 public String getName() { 206 return name; 207 } 208 } 209 | Popular Tags |