/src/mpv5/utils/xml/XMLReader.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 371 lines · 198 code · 71 blank · 102 comment · 29 complexity · 5dfb7b8ab1a23862ba658915c4cadad7 MD5 · raw file

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