View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.tools;
21  
22  import cz.zcu.mre.sparkle.Messages;
23  import org.antlr.v4.runtime.tree.ParseTree;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import java.util.Collection;
27  
28  /**
29   * Rozhraní pro ukládání a vyvolávání objektů z XML elementů.
30   *
31   * @author Jan Smucr
32   * @author Klara Hlavacova
33   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
34   */
35  public interface Saveable {
36  
37      /**
38       * Uloží objekt do předaného XML elementu.
39       *
40       * @param e Výstupní element.
41       */
42      void save(Element e);
43  
44      /**
45       * Načte objekt z předaného XML elementu či textového souboru.
46       *
47       * @param e Vstupní element.
48       *
49       * @throws LoadException on Load
50       */
51      void load(Object e) throws LoadException;
52  
53      /**
54       * @return Název XML elementu, pod jakým chce být objekt uložen.
55       */
56      String getXMLElementName();
57  
58      /**
59       * Provede automatické uložení {@link Saveable} objektů z kolekce do
60       * poskytnutého XML elementu.
61       *
62       * @param rootElement Výstupní element.
63       * @param children Kolekce objektů k uložení.
64       */
65      static void defaultSave(final Element rootElement, final Collection<?> children) {
66          final Document doc = rootElement.getOwnerDocument();
67          children.stream().filter(c -> c instanceof Saveable).forEach(c
68                  -> {
69              final Saveable s = (Saveable) c;
70              final Element e = doc.createElement(s.getXMLElementName());
71              rootElement.appendChild(e);
72              s.save(e);
73          });
74      }
75  
76      /**
77       * Provede automatické uložení {@link Saveable} objektů z pole do
78       * poskytnutého XML elementu.
79       *
80       * @param rootElement Výstupní element.
81       * @param saveables Pole objektů k uložení.
82       */
83      static void defaultSave(final Element rootElement, final Saveable... saveables) {
84          final Document doc = rootElement.getOwnerDocument();
85          for (final Saveable saveable : saveables) {
86              final Element e = doc.createElement(saveable.getXMLElementName());
87              rootElement.appendChild(e);
88              saveable.save(e);
89          }
90      }
91  
92      final class LoadException
93              extends Exception {
94  
95          private static final long serialVersionUID = -5502381352773721168L;
96  
97          Element element;
98          String attributeName;
99  
100         public LoadException(final String elementTagName) {
101             super(String.format(Messages.getString("REQUIRED_ELEMENT_X_NOT_FOUND"), elementTagName)); //$NON-NLS-1$
102             element = null;
103             attributeName = null;
104         }
105 
106         public LoadException(final Element e, final String attributeName) {
107             super(getMessage(e, attributeName));
108             this.element = e;
109             this.attributeName = attributeName;
110         }
111 
112         public LoadException(final ParseTree e, final String attributeName) {
113             super(getMessageFileQuery(e, attributeName));
114         }
115 
116         private static String getMessage(final Element e, final String attributeName) {
117             if (e.hasAttribute(attributeName)) {
118                 return String
119                         .format(Messages.getString("VALUE_X_NOT_VALID_FOR_ELEMENT_Y"), e.getAttribute(attributeName),
120                                 e.getTagName()); //$NON-NLS-1$
121             }
122             return String.format(Messages.getString("ELEMENT_X_ATTRIBUTE_Y_IS_MISSING"), e.getTagName(),
123                     attributeName); //$NON-NLS-1$
124         }
125 
126         private static String getMessageFileQuery(final ParseTree e, final String attributeName) {
127             return String.format(Messages.getString("ELEMENT_X_ATTRIBUTE_Y_IS_MISSING"), e.getText(),
128                     attributeName); //$NON-NLS-1$
129         }
130     }
131 }