Java tutorial
/* * OpenKennel - Open source software for manage kennels and farms * Copyright (C) 2017 Marco Magliano * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.openkennel.model.filemngr; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.List; import java.util.ListIterator; import javax.swing.JOptionPane; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import org.openkennel.ErrorLanguageContent; import org.openkennel.RegisterConstants; import org.openkennel.model.AnimalCoat; import org.openkennel.model.AnimalColor; import org.openkennel.model.AnimalGender; import org.openkennel.model.AnimalSize; import org.openkennel.model.AnimalType; import org.openkennel.view.UtilityClass; public class XMLFileHandler { /* The class logger */ private static final Log LOGGER = LogFactory.getLog(XMLFileHandler.class); /* Represents the xml file as DOM tree in memory. */ private Document xmlFile; /* The root element of the xml file. */ private Element xmlRoot; /* Sax parser to read the xml file and store it in a DOM tree in memory. */ SAXBuilder docBuilder; /** * Creates a XMLFileHandler to handle specific XML files. */ public XMLFileHandler() { docBuilder = new SAXBuilder(); xmlRoot = null; xmlFile = new Document(); } /** * Creates a XMLFileHandler to handle specific XML files. * Aswell the root element specified by parameter root is set. * * @param root */ public XMLFileHandler(String root) { docBuilder = new SAXBuilder(); xmlRoot = new Element(root); xmlFile = new Document(xmlRoot); } /** * Builds a XML file from the given parameter <code>File file</code>. * * * @param file * XML file to load from */ public void loadXMLFromFile(File file) { try { xmlFile = docBuilder.build(file); xmlRoot = this.xmlFile.getRootElement(); } catch (JDOMException | IOException e) { LOGGER.error(e); JOptionPane.showMessageDialog(UtilityClass.getActiveFrame(), ErrorLanguageContent.error_io, // ErrorLanguageContent.error_title, JOptionPane.ERROR_MESSAGE); } } /** * Writes the XML representation in memory to the given file. * * @param file * XML file to store in */ public void writeXMLToFile(File file) { if (xmlExists()) { XMLOutputter xMLOutput = null; try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) { xMLOutput = new XMLOutputter(Format.getPrettyFormat()); xMLOutput.output(xmlFile, outputStream); outputStream.close(); } catch (IOException e) { LOGGER.error(e); JOptionPane.showMessageDialog(UtilityClass.getActiveFrame(), ErrorLanguageContent.error_io, // ErrorLanguageContent.error_title, JOptionPane.ERROR_MESSAGE); } } } /** * Renames the given file to the new name. * * @param file * File to rename. * @param newName * New name of the file. */ public void renameExistingXMLFile(File file, String newName) { File renamedFile = new File(RegisterConstants.ANIMAL_FOLDER + newName + RegisterConstants.XML_FILE_SUFFIX); writeXMLToFile(file); if (!file.renameTo(renamedFile)) { deleteXMLFile(file); writeXMLToFile(renamedFile); } } /** * Moves the given source in the target file if it exists, is not a directory and if it's an XML file. * * @param source * The source file * @param target * The destination file */ public boolean moveXMLFile(File source, File target) { boolean result = false; if (source.exists() && source.isFile()) { String path = source.getAbsolutePath(); if (path.endsWith(RegisterConstants.XML_FILE_SUFFIX)) { try { Files.move(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); result = true; } catch (IOException e) { LOGGER.error(e); JOptionPane.showMessageDialog(UtilityClass.getActiveFrame(), ErrorLanguageContent.error_io, // ErrorLanguageContent.error_title, JOptionPane.ERROR_MESSAGE); } } } return result; } /** * Deletes the given file if it exists, is not a directory, is located in the animal folder * and if it's an XML file. * * @param file * File to delete */ public void deleteXMLFile(File file) { if (file.exists() && file.isFile()) { String path = file.getAbsolutePath(); if (path.contains(RegisterConstants.ANIMAL_FOLDER.substring(1)) && path.endsWith(RegisterConstants.XML_FILE_SUFFIX)) { file.delete(); } } } /** * Utility method to check whether a XML file was loaded and the root element was set. * * @return * True file was loaded and root element was set. * False otherwise */ public boolean xmlExists() { if (xmlFile != null && xmlRoot != null) { return true; } else { return false; } } /** * Creates a new memory image for a XML file, without data. * Root element with name 'root' is set. */ public void createXMLDocument(String root) { if (!xmlExists()) { xmlRoot = new Element(root); xmlFile = new Document(xmlRoot); } } /** * Removes the whole content given in the memory image of the XML file. */ private void deleteContent() { if (xmlExists()) { xmlFile.removeContent(); } } /** * Creates the root element for the XML file. * <animal name=""></animal> * * @param name * Name for the animal. Set as text for the animal-tag. * @param type * Type of the animal. Attribute of the animal-tag. * @param gender * Gender of the animal. Attribute of the animal-tag. */ public void createAnimalElement(String name, AnimalType type, AnimalGender gender) { if (xmlExists()) { xmlFile.setRootElement(new Element(RegisterConstants.ANIMAL).setAttribute(RegisterConstants.NAME, name) .setAttribute(RegisterConstants.TYPE, type.name()) .setAttribute(RegisterConstants.GENDER, gender.name())); xmlRoot = xmlFile.getRootElement(); } } /** * Changes the animal name. * * @param name * New animal name */ public void changeAnimalName(String name) { if (xmlExists()) { xmlRoot.setAttribute(RegisterConstants.NAME, name); } } /** * Changes the animal type. * * @param name * New animal type */ public void changeAnimalType(AnimalType type) { if (xmlExists()) { xmlRoot.setAttribute(RegisterConstants.TYPE, type.name()); } } /** * Changes the animal gender. * * @param name * New animal gender */ public void changeAnimalGender(AnimalGender gender) { if (xmlExists()) { xmlRoot.setAttribute(RegisterConstants.GENDER, gender.name()); } } /** * Gets the animal name. * * @return * Animal name */ public String getAnimalName() { String name = RegisterConstants.EMPTY_STRING; if (xmlExists()) { name = xmlRoot.getAttributeValue(RegisterConstants.NAME); } return name; } /** * Gets the animal type. * * @return * Animal type */ public String getAnimalType() { String type = RegisterConstants.EMPTY_STRING; if (xmlExists()) { type = xmlRoot.getAttributeValue(RegisterConstants.TYPE); } return type; } /** * Gets the animal gender. * * @return * Animal gender */ public String getAnimalGender() { String gender = RegisterConstants.EMPTY_STRING; if (xmlExists()) { gender = xmlRoot.getAttributeValue(RegisterConstants.GENDER); } return gender; } /** * Creates the breed tag for the XML file. * * @param breed * Breed text */ public void createBreedElement(String breed) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.BREED).setText(breed)); } } /** * Changes the animal breed. * * @param name * New animal breed */ public void changeAnimalBreed(String breed) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.BREED).setText(breed); } } /** * Gets the animal breed. * * @return * Animal breed */ public String getAnimalBreed() { String breed = RegisterConstants.EMPTY_STRING; if (xmlExists()) { breed = xmlRoot.getChildText(RegisterConstants.BREED); } return breed; } /** * Creates the size tag for the XML file. * * @param size * Size value */ public void createSizeElement(AnimalSize size) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.SIZE).setText(size.name())); } } /** * Changes the animal size. * * @param size * New animal size */ public void changeAnimalSize(AnimalSize size) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.SIZE).setText(size.name()); } } /** * Gets the animal size. * * @return * Animal size */ public String getAnimalSize() { String size = AnimalSize.ALL.name(); if (xmlExists()) { size = xmlRoot.getChildText(RegisterConstants.SIZE); } return size; } /** * Creates the color tag for the XML file. * * @param color * Color value */ public void createColorElement(AnimalColor color) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.COLOR).setText(color.name())); } } /** * Changes the animal color. * * @param color * New animal color */ public void changeAnimalColor(AnimalColor color) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.COLOR).setText(color.name()); } } /** * Gets the animal color. * * @return * Animal color */ public String getAnimalColor() { String color = AnimalColor.ALL.name(); if (xmlExists()) { color = xmlRoot.getChildText(RegisterConstants.COLOR); } return color; } /** * Creates the coat tag for the XML file. * * @param coat * Coat value */ public void createCoatElement(AnimalCoat coat) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.COAT).setText(coat.name())); } } /** * Changes the animal coat. * * @param coat * New animal coat */ public void changeAnimalCoat(AnimalCoat coat) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.COAT).setText(coat.name()); } } /** * Gets the animal coat. * * @return * Animal coat */ public String getAnimalCoat() { String coat = AnimalCoat.ALL.name(); if (xmlExists()) { coat = xmlRoot.getChildText(RegisterConstants.COAT); } return coat; } /** * Creates the weight tag for the XML file. * * @param weight * Weight value */ public void createWeightElement(int weight) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.WEIGHT).setText(String.valueOf(weight))); } } /** * Changes the animal weight. * * @param weight * New animal weight */ public void changeAnimalWeight(int weight) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.WEIGHT).setText(String.valueOf(weight)); } } /** * Gets the animal weight. * * @return * Animal weight */ public int getAnimalWeight() { int weight = RegisterConstants.ZERO; if (xmlExists()) { weight = Integer.valueOf(xmlRoot.getChildText(RegisterConstants.WEIGHT)); } return weight; } /** * Creates the details tag for the XML file. * * @param details * details text */ public void createDetailsElement(String details) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.DETAILS).setText(details)); } } /** * Changes the details text. * * @param details * New details text */ public void changeDetailsText(String details) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.DETAILS).setText(details); } } /** * Gets the details text. * * @return * details text */ public String getDetailsText() { String text = RegisterConstants.EMPTY_STRING; if (xmlExists()) { text = xmlRoot.getChildText(RegisterConstants.DETAILS); } return text; } /** * Creates the microchip tag for the XML file. * * @param microchip * microchip text */ public void createMicrochipElement(String microchip) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.MICROCHIP).setText(microchip)); } } /** * Changes the animal microchip. * * @param microchip * New animal microchip */ public void changeAnimalMicrochip(String microchip) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.MICROCHIP).setText(microchip); } } /** * Gets the animal microchip. * * @return * Animal microchip */ public String getAnimalMicrochip() { String microchip = RegisterConstants.EMPTY_STRING; if (xmlExists()) { microchip = xmlRoot.getChildText(RegisterConstants.MICROCHIP); } return microchip; } /** * Creates the tattoo tag for the XML file. * * @param tattoo * tattoo text */ public void createTattooElement(String tattoo) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.TATTOO).setText(tattoo)); } } /** * Changes the animal tattoo. * * @param tattoo * New animal tattoo */ public void changeAnimalTattoo(String tattoo) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.TATTOO).setText(tattoo); } } /** * Gets the animal tattoo. * * @return * Animal tattoo */ public String getAnimalTattoo() { String tattoo = RegisterConstants.EMPTY_STRING; if (xmlExists()) { tattoo = xmlRoot.getChildText(RegisterConstants.TATTOO); } return tattoo; } /** * Creates the health state tag for the XML file. * * @param healthState * health state text */ public void createHealthStateElement(String healthState) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.HEALTHSTATE).setText(healthState)); } } /** * Changes the health state text. * * @param healthState * New health state text */ public void changeHealthStateText(String healthState) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.HEALTHSTATE).setText(healthState); } } /** * Gets the health state text. * * @return * health state text */ public String getHealthStateText() { String text = RegisterConstants.EMPTY_STRING; if (xmlExists()) { text = xmlRoot.getChildText(RegisterConstants.HEALTHSTATE); } return text; } /** * Creates the exams tag. */ private void createExamsTag() { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.EXAMS)); } } /** * Creates a single exam tag. * * @param examName * Name of the exam * @param examDate * Date of the exam * @param examComment * Comment of the exam */ private void createSingleExamElement(String examName, String examDate, String examComment) { String exam = null; String date = null; String comment = null; if (examName != null) { exam = examName; if (examDate != null && examComment != null) { date = examDate; comment = examComment; } else { date = RegisterConstants.EMPTY_STRING; comment = RegisterConstants.EMPTY_STRING; } } if (exam != null) { xmlRoot.getChild(RegisterConstants.EXAMS) .addContent(new Element(RegisterConstants.EXAM).addContent(exam) .setAttribute(RegisterConstants.DATE, date) .setAttribute(RegisterConstants.COMMENT, comment)); } } /** * Creates multiple exam tags using * <code>createExamElement(String examName, String examDate, String examComment)</code>. * * @param examsNames * Array holding the exam names * @param examsDates * Array holding the exam dates * @param examsComments * Array holding the exam comments */ public void createMultipleExamElements(String[] examsNames, String[] examsDates, String[] examsComments) { createExamsTag(); for (int i = 0; i <= examsNames.length - 1; i++) { createSingleExamElement(examsNames[i], examsDates[i], examsComments[i]); } } /** * Replaces all exam-content from the XML file and creates new exam * tags. * * @param names * Array with new exams names * @param dates * Array with new exams dates * @param comments * Array with the new exams comments */ public void replaceExamElements(String[] names, String[] dates, String[] comments) { if (xmlExists()) { List<Element> examList = xmlRoot.getChild(RegisterConstants.EXAMS).getChildren(); examList.clear(); for (int i = 0; i <= names.length - 1; i++) { createSingleExamElement(names[i], dates[i], comments[i]); } } } /** * Gets all exams names. * Due to filtering it guarantees that there are no empty exams. * * @return * String array with the names */ public String[] getExamsNames() { String[] names = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> exams = xmlRoot.getChild(RegisterConstants.EXAMS).getChildren(RegisterConstants.EXAM); exams = filterEmptyEntries(exams); names = new String[exams.size()]; ListIterator<Element> iterator = exams.listIterator(); for (int i = 0; i <= names.length - 1; i++) { names[i] = iterator.next().getText(); } } return names; } /** * Gets all exams dates. * Due to filtering it guarantees that there are no empty exams. * * @return * String array with the dates */ public String[] getExamsDates() { String[] dates = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> exams = xmlRoot.getChild(RegisterConstants.EXAMS).getChildren(RegisterConstants.EXAM); exams = filterEmptyEntries(exams); dates = new String[exams.size()]; ListIterator<Element> counter = exams.listIterator(); for (int i = 0; i <= dates.length - 1; i++) { dates[i] = counter.next().getAttributeValue(RegisterConstants.DATE); } } return dates; } /** * Gets all exams comments. * Due to filtering it guarantees that there are no empty exams. * * @return * String array with the comments */ public String[] getExamsComments() { String[] comments = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> exams = xmlRoot.getChild(RegisterConstants.EXAMS).getChildren(RegisterConstants.EXAM); exams = filterEmptyEntries(exams); comments = new String[exams.size()]; ListIterator<Element> counter = exams.listIterator(); for (int i = 0; i <= comments.length - 1; i++) { comments[i] = counter.next().getAttributeValue(RegisterConstants.COMMENT); } } return comments; } /** * Creates the vaccines tag. */ private void createVaccinesTag() { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.VACCINES)); } } /** * Creates a single vaccine tag. * * @param vaccineName * Name of the vaccine * @param vaccineDate * Date of the vaccine * @param vaccineDeadlines * Deadlines of the vaccine */ private void createSingleVaccineElement(String vaccineName, String vaccineDate, String vaccineDeadlines) { String name = null; String date = null; String deadlines = null; if (vaccineName != null) { name = vaccineName; if (vaccineDate != null && vaccineDeadlines != null) { date = vaccineDate; deadlines = vaccineDeadlines; } else { date = RegisterConstants.EMPTY_STRING; deadlines = RegisterConstants.EMPTY_STRING; } } if (name != null) { xmlRoot.getChild(RegisterConstants.VACCINES) .addContent(new Element(RegisterConstants.VACCINE).addContent(name) .setAttribute(RegisterConstants.DATE, date) .setAttribute(RegisterConstants.DEADLINES, deadlines)); } } /** * Creates multiple vaccine tags using * <code>createVaccineElement(String vaccineName, String vaccineDate, String vaccineDeadlines)</code>. * * @param vaccinesNames * Array holding the vaccine names * @param vaccinesDates * Array holding the vaccine dates * @param vaccinesDeadlines * Array holding the vaccine deadlines */ public void createMultipleVaccineElements(String[] vaccinesNames, String[] vaccinesDates, String[] vaccinesDeadlines) { createVaccinesTag(); for (int i = 0; i <= vaccinesNames.length - 1; i++) { createSingleVaccineElement(vaccinesNames[i], vaccinesDates[i], vaccinesDeadlines[i]); } } /** * Replaces all vaccine-content from the XML file and creates new vaccine * tags. * * @param names * Array with new vaccines names * @param dates * Array with new vaccines dates * @param deadlines * Array with the new vaccines comments */ public void replaceVaccineElements(String[] names, String[] dates, String[] deadlines) { if (xmlExists()) { List<Element> vaccineList = xmlRoot.getChild(RegisterConstants.VACCINES).getChildren(); vaccineList.clear(); for (int i = 0; i <= names.length - 1; i++) { createSingleVaccineElement(names[i], dates[i], deadlines[i]); } } } /** * Gets all vaccines names. * Due to filtering it guarantees that there are no empty vaccines. * * @return * String array with the names */ public String[] getVaccinesNames() { String[] names = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> vaccines = xmlRoot.getChild(RegisterConstants.VACCINES) .getChildren(RegisterConstants.VACCINE); vaccines = filterEmptyEntries(vaccines); names = new String[vaccines.size()]; ListIterator<Element> iterator = vaccines.listIterator(); for (int i = 0; i <= names.length - 1; i++) { names[i] = iterator.next().getText(); } } return names; } /** * Gets all vaccines dates. * Due to filtering it guarantees that there are no empty vaccines. * * @return * String array with the dates */ public String[] getVaccinesDates() { String[] dates = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> vaccines = xmlRoot.getChild(RegisterConstants.VACCINES) .getChildren(RegisterConstants.VACCINE); vaccines = filterEmptyEntries(vaccines); dates = new String[vaccines.size()]; ListIterator<Element> counter = vaccines.listIterator(); for (int i = 0; i <= dates.length - 1; i++) { dates[i] = counter.next().getAttributeValue(RegisterConstants.DATE); } } return dates; } /** * Gets all vaccines deadlines. * Due to filtering it guarantees that there are no empty vaccines. * * @return * String array with the deadlines */ public String[] getVaccinesDeadlines() { String[] deadlines = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { List<Element> vaccines = xmlRoot.getChild(RegisterConstants.VACCINES) .getChildren(RegisterConstants.VACCINE); vaccines = filterEmptyEntries(vaccines); deadlines = new String[vaccines.size()]; ListIterator<Element> counter = vaccines.listIterator(); for (int i = 0; i <= deadlines.length - 1; i++) { deadlines[i] = counter.next().getAttributeValue(RegisterConstants.DEADLINES); } } return deadlines; } private List<Element> filterEmptyEntries(List<Element> list) { for (ListIterator<Element> iterator = list.listIterator(); iterator.hasNext();) { Element element = iterator.next(); if (element.getTextNormalize().isEmpty()) { iterator.remove(); } } return list; } /** * Creates the birth tag for the XML file. * * @param birth * Birth text * @param estimated * Estimated attribute value */ public void createBirthElement(Long birth, Boolean estimated) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.BIRTH).setText(String.valueOf(birth)) .setAttribute(RegisterConstants.ESTIMATED, String.valueOf(estimated))); } } /** * Changes the animal birth. * * @param birth * New animal birth */ public void changeAnimalBirth(Long birth) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.BIRTH).setText(String.valueOf(birth)); } } /** * Changes the animal birth attribute. * * @param estimated * New estimated value */ public void changeAnimalBirthEstimated(Boolean estimated) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.BIRTH).setAttribute(RegisterConstants.ESTIMATED, String.valueOf(estimated)); } } /** * Gets the animal birth. * * @return * Animal birth */ public String getAnimalBirth() { String birth = RegisterConstants.EMPTY_STRING; if (xmlExists()) { birth = xmlRoot.getChildText(RegisterConstants.BIRTH); } return birth; } /** * Gets the animal birth estimated. * * @return * Animal birth estimated value */ public String getAnimalBirthEstimated() { String estimated = RegisterConstants.EMPTY_STRING; if (xmlExists()) { estimated = xmlRoot.getChild(RegisterConstants.BIRTH).getAttributeValue(RegisterConstants.ESTIMATED); } return estimated; } /** * Creates the death tag for the XML file. * * @param death * Death text */ public void createDeathElement(Long death) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.DEATH).setText(String.valueOf(death))); } } /** * Changes the animal death. * * @param death * New animal death */ public void changeAnimalDeath(Long death) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.DEATH).setText(String.valueOf(death)); } } /** * Gets the animal death. * * @return * Animal death */ public String getAnimalDeath() { String death = RegisterConstants.EMPTY_STRING; if (xmlExists()) { death = xmlRoot.getChildText(RegisterConstants.DEATH); } return death; } /** * Creates the history tag for the XML file. * * @param history * history text */ public void createHistoryElement(String history) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.HISTORY).setText(history)); } } /** * Changes the history text. * * @param history * New history text */ public void changeHistoryText(String history) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.HISTORY).setText(history); } } /** * Gets the history text. * * @return * history text */ public String getHistoryText() { String text = RegisterConstants.EMPTY_STRING; if (xmlExists()) { text = xmlRoot.getChildText(RegisterConstants.HISTORY); } return text; } /** * Creates the holder tag for the XML file. * * @param holder * Holder value */ public void createHolderElement(String holder) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.HOLDER).setText(holder)); } } /** * Changes the animal holder. * * @param holder * New animal holder */ public void changeAnimalHolder(String holder) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.HOLDER).setText(holder); } } /** * Gets the animal holder. * * @return * Animal holder */ public String getAnimalHolder() { String holder = RegisterConstants.EMPTY_STRING; if (xmlExists()) { holder = xmlRoot.getChildText(RegisterConstants.HOLDER); } return holder; } /** * Creates the address tag for the XML file. * * @param address * Address value */ public void createAddressElement(String address) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.ADDRESS).setText(address)); } } /** * Changes the animal address. * * @param address * New animal address */ public void changeAnimalAddress(String address) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.ADDRESS).setText(address); } } /** * Gets the animal address. * * @return * Animal address */ public String getAnimalAddress() { String address = RegisterConstants.EMPTY_STRING; if (xmlExists()) { address = xmlRoot.getChildText(RegisterConstants.ADDRESS); } return address; } /** * Creates the comment tag for the XML file. * * @param comment * comment text */ public void createCommentElement(String comment) { if (xmlExists()) { xmlRoot.addContent(new Element(RegisterConstants.COMMENT).setText(comment)); } } /** * Changes the comment text. * * @param comment * New comment text */ public void changeCommentText(String comment) { if (xmlExists()) { xmlRoot.getChild(RegisterConstants.COMMENT).setText(comment); } } /** * Gets the comment text. * * @return * comment text */ public String getCommentText() { String text = RegisterConstants.EMPTY_STRING; if (xmlExists()) { text = xmlRoot.getChildText(RegisterConstants.COMMENT); } return text; } /** * Creates multiple image elements in the XML file. * Each element contains an attribute that represents the path to the image file. * * @param paths * File paths. For each of this there will be added an image element. */ public void createMultipleImageElements(String[] paths) { if (xmlExists()) { for (String path : paths) { createSingleImageElement(path); } } } /** * Replaces all current image elements. * It first clears the element list from {@code xmlRoot.getChildren(RegisterConstants.IMAGE)} * and then invokes {@code createMultipleImageElements(paths)}. * * @param paths * File paths. For each of this there will be added an image element. */ public void replaceImageElements(String[] paths) { List<Element> imageElements = xmlRoot.getChildren(RegisterConstants.IMAGE); imageElements.clear(); createMultipleImageElements(paths); } /** * Creates the image tag for the XML file. * * @param path * File path to for the image */ private void createSingleImageElement(String path) { if (!path.trim().equals(RegisterConstants.EMPTY_STRING)) { xmlRoot.addContent(new Element(RegisterConstants.IMAGE).setAttribute(RegisterConstants.PATH, path)); } } /** * Gets image paths contained in the animal file * * @return * Array containing all file paths for images stored in the animal. */ public String[] getImagePaths() { String[] paths = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { int index = 0; List<Element> imageChildren = xmlRoot.getChildren(RegisterConstants.IMAGE); paths = new String[imageChildren.size()]; for (Element element : imageChildren) { paths[index++] = element.getAttributeValue(RegisterConstants.PATH); } } return paths; } /** * Creates multiple document elements in the XML file. * Each element contains an attribute that represents the path to the document file. * * @param paths * File paths. For each of this there will be added an document element. */ public void createMultipleDocumentElements(String[] paths) { if (xmlExists()) { for (String path : paths) { createSingleDocumentElement(path); } } } /** * Replaces all current document elements. * It first clears the element list from {@code xmlRoot.getChildren(RegisterConstants.DOCUMENT)} * and then invokes {@code createMultipleDocumentElements(paths)}. * * @param paths * File paths. For each of this there will be added an document element. */ public void replaceDocumentElements(String[] paths) { List<Element> imageElements = xmlRoot.getChildren(RegisterConstants.DOCUMENT); imageElements.clear(); createMultipleDocumentElements(paths); } /** * Creates the document tag for the XML file. * * @param path * File path to for the document */ private void createSingleDocumentElement(String path) { if (!path.trim().equals(RegisterConstants.EMPTY_STRING)) { xmlRoot.addContent(new Element(RegisterConstants.DOCUMENT).setAttribute(RegisterConstants.PATH, path)); } } /** * Gets document paths contained in the animal file * * @return * Array containing all file paths for documents stored in the animal. */ public String[] getDocumentPaths() { String[] paths = RegisterConstants.EMPTY_STRING_ARRAY; if (xmlExists()) { int index = 0; List<Element> documentChildren = xmlRoot.getChildren(RegisterConstants.DOCUMENT); paths = new String[documentChildren.size()]; for (Element element : documentChildren) { paths[index++] = element.getAttributeValue(RegisterConstants.PATH); } } return paths; } }