/src/mpv5/utils/xml/XMLReader.java
Java | 371 lines | 198 code | 71 blank | 102 comment | 29 complexity | 5dfb7b8ab1a23862ba658915c4cadad7 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
1 2/* 3 * This file is part of YaBS. 4 * 5 * YaBS is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * YaBS is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 17 */ 18package mpv5.utils.xml; 19 20//~--- non-JDK imports -------------------------------------------------------- 21import mpv5.data.PropertyStore; 22 23import mpv5.db.common.Context; 24import mpv5.db.common.DatabaseObject; 25import mpv5.db.objects.Contact; 26 27import mpv5.logging.Log; 28 29import org.jdom.Attribute; 30import org.jdom.Document; 31import org.jdom.Element; 32import org.jdom.JDOMException; 33import org.jdom.filter.ElementFilter; 34import org.jdom.input.SAXBuilder; 35 36//~--- JDK imports ------------------------------------------------------------ 37 38import java.io.File; 39import java.io.FileInputStream; 40import java.io.FileReader; 41import java.io.IOException; 42import java.nio.charset.Charset; 43 44import java.util.ArrayList; 45import java.util.Hashtable; 46import java.util.List; 47import java.util.Vector; 48 49/** 50 * 51 * 52 */ 53public class XMLReader { 54 55 private Element rootElement = new Element(mpv5.globals.Constants.XML_ROOT); 56 private boolean overwriteExisting = false; 57 private Document myDocument = new Document(); 58 59 /** 60 * 61 * @param name 62 * @return 63 */ 64 public Element getSubRootElement(String name) { 65 @SuppressWarnings("unchecked") 66 List<Element> l = rootElement.getContent(new ElementFilter()); 67 68 for (int i = 0; i < l.size(); i++) { 69 Element element = l.get(i); 70 71 if (element.getName().equals(name)) { 72 return element; 73 } 74 } 75 76 return new Element(name); 77 } 78 79 /** 80 * Parses a XML document 81 * @param xmlfile 82 * @return The resulting xml document 83 * @throws JDOMException 84 * @throws IOException 85 */ 86 public Document newDoc(File xmlfile) throws JDOMException, IOException { 87 return createDocument(xmlfile, false); 88 } 89 90 /** 91 * Parses a XML document. If validate is TRUE, the file will need to have a valid DOCTYPE declaration. 92 * The <?xml version="1.0" encoding="UTF-8"?> tag MUST be on the first line! 93 * @param xmlfile 94 * @param validate 95 * @return The resulting xml document 96 * @throws JDOMException 97 * @throws IOException 98 */ 99 public Document newDoc(File xmlfile, boolean validate) throws JDOMException, IOException { 100 return createDocument(xmlfile, validate); 101 } 102 103 /** 104 * Gets an element value 105 * @param type 106 * @param nodename The name of the node 107 * @param attributevalue The ID of the node 108 * @param name The name of the element 109 * @return The value of the element 110 */ 111 public String getElement(String type, String nodename, String attributevalue, String name) { 112 @SuppressWarnings("unchecked") 113 List<Element> list = (List<Element>) rootElement.getChild(type).getContent(); 114 115 for (int i = 0; i < list.size(); i++) { 116 if (list.get(i) instanceof Element) { 117 if (list.get(i).getName().equals(nodename) && (list.get(i).getAttribute("id") != null) 118 && list.get(i).getAttribute("id").getValue().equals(attributevalue)) { 119 return list.get(i).getChild(name).getValue(); 120 } 121 } 122 } 123 124 return null; 125 } 126 127 /** 128 * Tries to parse the xml file into a list of matching database objects. 129 * @param <T> 130 * @param template 131 * @return 132 * @throws Exception 133 */ 134 @SuppressWarnings({"unchecked"}) 135 public synchronized <T extends DatabaseObject> ArrayList<T> getObjects(T template) throws Exception { 136 Log.Debug(this, "Looking for: " + template.getDbIdentity()); 137 138 String ident = template.getType(); 139 ArrayList<DatabaseObject> arrlist = new ArrayList<DatabaseObject>(); 140 @SuppressWarnings("unchecked") 141 List<Element> list = 142 (List<Element>) rootElement.getChild(template.getDbIdentity()).getContent(new ElementFilter()); 143 144 Log.Debug(this, "Found items: " + list.size()); 145 146 for (int i = 0; i < list.size(); i++) { 147 if ((list.get(i) instanceof Element) && list.get(i).getName().equalsIgnoreCase(template.getType())) { 148 149// Log.Debug(this, "Found item: " + list.get(i).getName()); 150 if (list.get(i).getName().equals(ident)) { 151 Element element = list.get(i); 152 DatabaseObject obj = template.clone(); 153 154 obj.parse(toHashTable(element)); 155 156 if (isOverwriteExisting() && (list.get(i).getAttribute("id") != null)) { 157 obj.setIDS(Integer.valueOf(list.get(i).getAttribute("id").getValue())); 158 Log.Debug(this, 159 "Overwriting/updating dataset id " + obj.getDbIdentity() + ": " + obj.__getIDS()); 160 } else { 161 obj.ensureUniqueness(); 162 } 163 164 arrlist.add(obj); 165 } 166 } 167 } 168 169 return (ArrayList<T>) arrlist; 170 } 171 172 /** 173 * Tries to parse the xml file into a list of database objects. <br/> 174 * May contain empty lists 175 * @return 176 */ 177 public synchronized ArrayList<ArrayList<DatabaseObject>> getObjects() { 178 ArrayList<Context> c = Context.getImportableContexts(); 179 ArrayList<ArrayList<DatabaseObject>> t = new ArrayList<ArrayList<DatabaseObject>>(); 180 DatabaseObject template = null; 181 Context context = null; 182 183 for (int i = 0; i < c.size(); i++) { 184 try { 185 context = c.get(i); 186 template = DatabaseObject.getObject(context); 187 t.add(getObjects(template)); 188 } catch (Exception ignore) { 189 Log.Debug(this, "Element of typ: " + context.getDbIdentity() + " not found in this document!"); 190 } 191 } 192 193 return t; 194 } 195 196 /** 197 * Prints a node to debug out 198 * @param nodename 199 */ 200 public void print(String nodename) { 201 @SuppressWarnings("unchecked") 202 List<Element> list = 203 (List<Element>) rootElement.getChild(nodename).getContent(new ElementFilter()); 204 205 for (int i = 0; i < list.size(); i++) { 206 Element element = list.get(i); 207 208 Log.Debug(this, element.getName() + ": " + element.getValue()); 209 } 210 } 211 212 /** 213 * Reads a node with the given name into a property store object 214 * @param type 215 * @param nodename 216 * @param nodeid 217 * @param store 218 * @return 219 */ 220 public synchronized PropertyStore readInto(String type, String nodename, String nodeid, PropertyStore store) { 221 @SuppressWarnings("unchecked") 222 List<Element> list = 223 (List<Element>) rootElement.getChild(type).getContent(new ElementFilter()); 224 225 for (int i = 0; i < list.size(); i++) { 226 Element element = list.get(i); 227 228 if (element.getName().equals(nodename) && element.getAttribute("id").getValue().equals(nodeid)) { 229 @SuppressWarnings("unchecked") 230 List<Element> list2 = 231 (List<Element>) element.getContent(new ElementFilter()); 232 233 for (int j = 0; j < list2.size(); j++) { 234 Element element1 = list2.get(j); 235 236 store.addProperty(element1.getName(), element1.getValue()); 237 } 238 } 239 } 240 241 return store; 242 } 243 244 /** 245 * Reads all nodes with the given name into a property store list, with additional property "nodeid" 246 * @param type 247 * @param nodename 248 * @return 249 */ 250 public synchronized List<PropertyStore> readInto(String type, String nodename) { 251 @SuppressWarnings("unchecked") 252 List<Element> list = 253 (List<Element>) rootElement.getChild(type).getContent(new ElementFilter()); 254 List<PropertyStore> plist = new Vector<PropertyStore>(); 255 256 for (int i = 0; i < list.size(); i++) { 257 Element element = list.get(i); 258 259 if (element.getName().equals(nodename)) { 260 PropertyStore st = new PropertyStore(); 261 262 st.addProperty("nodeid", element.getAttribute("id").getValue()); 263 264 try { 265 st.addProperty("nodename", element.getAttribute("name").getValue()); 266 } catch (Exception e) { 267 // name not mandatory 268 } 269 @SuppressWarnings("unchecked") 270 List<Element> list2 = 271 (List<Element>) element.getContent(new ElementFilter()); 272 273 for (int j = 0; j < list2.size(); j++) { 274 Element element1 = list2.get(j); 275 276 st.addProperty(element1.getName(), element1.getValue()); 277 } 278 279 plist.add(st); 280 } 281 } 282 283 return plist; 284 } 285 286 private Document createDocument(File xmlfile, boolean validate) throws JDOMException, IOException { 287 288 SAXBuilder parser = new SAXBuilder(validate); 289 FileReader fis = new FileReader(xmlfile); 290 Log.Debug(this, "Reading Document: " + xmlfile + " using encoding: " + fis.getEncoding()); 291 292 myDocument = parser.build(fis); 293 rootElement = myDocument.getRootElement(); 294 295 if (validate) { 296 Log.Debug(this, "Document validated: " + xmlfile); 297 } 298 299 return myDocument; 300 } 301 302 /** 303 * Converts a node into a hashtable<br/> 304 * {name, value}, will not return any attributes 305 * @param node 306 * @return 307 */ 308 public synchronized Hashtable<String, Object> toHashTable(Element node) { 309 @SuppressWarnings("unchecked") 310 List<Element> liste = node.getChildren(); 311 Hashtable<String, Object> table = new Hashtable<String, Object>(); 312 313 for (int i = 0; i < liste.size(); i++) { 314 Element element = liste.get(i); 315 316 table.put(element.getName(), element.getValue()); 317 } 318 319 return table; 320 } 321 322 /** 323 * Reads a node into a dynamic String array 324 * {name, vale, attribute1, attribute2.. 5} 325 * @param node 326 * @return 327 */ 328 public String[][] toArray(Element node) { 329 @SuppressWarnings("unchecked") 330 List<Element> liste = node.getContent(new ElementFilter()); 331 332 Log.Debug(this, liste.size() + " elements found in " + node); 333 334 String[][] table = new String[liste.size()][5]; 335 336 for (int i = 0; i < liste.size(); i++) { 337 Element element = liste.get(i); 338 339 table[i][0] = element.getName(); 340 table[i][1] = element.getValue(); 341 @SuppressWarnings("unchecked") 342 List<Attribute> atts = element.getAttributes(); 343 344 for (int j = 2; j < atts.size() + 2; j++) { 345 Attribute a = atts.get(j - 2); 346 347 table[i][j] = a.getValue(); 348 } 349 } 350 351 return table; 352 } 353 354 /** 355 * @return the overwriteExisting 356 */ 357 public boolean isOverwriteExisting() { 358 return overwriteExisting; 359 } 360 361 /** 362 * @param overwriteExisting the overwriteExisting to set 363 */ 364 public void setOverwriteExisting(boolean overwriteExisting) { 365 this.overwriteExisting = overwriteExisting; 366 } 367} 368 369 370//~ Formatted by Jindent --- http://www.jindent.com 371