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.gui.query;
21  
22  import cz.zcu.mre.sparkle.Messages;
23  import cz.zcu.mre.sparkle.data.DataAgent;
24  import cz.zcu.mre.sparkle.data.Storage;
25  import cz.zcu.mre.sparkle.gui.mainForm.MainForm;
26  import cz.zcu.mre.sparkle.gui.query.queryTypes.insert.InsertQueryFormPane;
27  import cz.zcu.mre.sparkle.gui.query.queryTypes.ask.AskQueryFormPane;
28  import cz.zcu.mre.sparkle.gui.query.queryTypes.construct.ConstructQueryFormPane;
29  import cz.zcu.mre.sparkle.gui.query.queryTypes.delete.DeleteInsertQueryFormPane;
30  import cz.zcu.mre.sparkle.gui.query.queryTypes.delete.DeleteQueryFormPane;
31  import cz.zcu.mre.sparkle.gui.query.queryTypes.describe.DescribeQueryFormPane;
32  import cz.zcu.mre.sparkle.gui.query.queryTypes.select.SelectQueryFormPane;
33  import cz.zcu.mre.sparkle.tools.Saveable;
34  import cz.zcu.mre.sparkle.tools.sparqlParser.QueryParser;
35  import org.antlr.v4.runtime.misc.ParseCancellationException;
36  import org.antlr.v4.runtime.tree.ParseTree;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  import org.w3c.dom.NodeList;
40  import javax.xml.parsers.DocumentBuilder;
41  import javax.xml.parsers.DocumentBuilderFactory;
42  import javax.xml.parsers.ParserConfigurationException;
43  import javax.xml.transform.Transformer;
44  import javax.xml.transform.TransformerException;
45  import javax.xml.transform.TransformerFactory;
46  import javax.xml.transform.dom.DOMResult;
47  import javax.xml.transform.stream.StreamSource;
48  import java.io.File;
49  import java.io.IOException;
50  import static cz.zcu.mre.sparkle.tools.Definitions.QUERY_FILE_EXTENSION;
51  import static cz.zcu.mre.sparkle.tools.Definitions.QUERY_FILE_EXTENSION_DELIMITER;
52  
53  /**
54   * Import a sestaveni panelu s dotazem ze souboru.
55   *
56   * @author Jan Smucr
57   * @author Klara Hlavacova
58   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
59   */
60  class QueryFormPaneLoad {
61  
62      public static QueryFormPane<?> load(
63              final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
64              final String queryString)
65              throws ParserConfigurationException, TransformerException, Saveable.LoadException,
66              ParseCancellationException {
67  
68          return doLoad(mainForm, dataAgent, localStorage, QueryParser.getParsedTree(queryString));
69  
70      }
71  
72      /**
73       * Načte dotaz z XML souboru.
74       *
75       * @param mainForm MainForm of Sparkle instance.
76       * @param dataAgent Připojení k úložišti.
77       * @param localStorage Lokální mapování prefixů; Lokální seznam ostatních
78       * pojmů; Lokální seznam datových typů; Lokální seznam funkcí.
79       * @param file Vstupní soubor.
80       *
81       * @return Instance {@link QueryFormPane}.
82       *
83       * @throws ParserConfigurationException from DocumentBuilderFactory
84       * @throws TransformerException from TransformerFactory
85       * @throws Saveable.LoadException on loading
86       * @throws java.io.IOException on loading
87       */
88      public static QueryFormPane<?> load(
89              final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
90              final File file)
91              throws ParserConfigurationException, TransformerException, Saveable.LoadException,
92              ParseCancellationException, IOException {
93  
94          //nacteni ze souboru
95          int extensionPos = file.getName().lastIndexOf(QUERY_FILE_EXTENSION_DELIMITER) + 1;
96          if (file.getName().substring(extensionPos).equals(QUERY_FILE_EXTENSION)) {
97              //sqf
98              return doLoad(mainForm, dataAgent, localStorage, getDOM(file));
99          } else {
100             //txt soubor
101             return doLoad(mainForm, dataAgent, localStorage, QueryParser.getParsedTree(file));
102         }
103     }
104 
105     /**
106      * Vrati DOM xml souboru
107      *
108      * @param file xml soubor
109      *
110      * @return DOM xml souboru
111      *
112      * @throws TransformerException
113      * @throws ParserConfigurationException
114      */
115     private static Document getDOM(File file) throws TransformerException, ParserConfigurationException {
116         final TransformerFactory transformerFactory = TransformerFactory.newInstance();
117         final Transformer transformer = transformerFactory.newTransformer();
118 
119         final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
120         final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
121         final Document doc = docBuilder.newDocument();
122 
123         final DOMResult result = new DOMResult(doc);
124         final StreamSource source = new StreamSource(file);
125 
126         transformer.transform(source, result);
127 
128         return doc;
129     }
130 
131     /**
132      * Načte dotaz z XML dokumentu.
133      *
134      * @param dataAgent Připojení k úložišti.
135      * @param localStorage Lokální mapování prefixů; Lokální seznam ostatních
136      * pojmů; Lokální seznam datových typů; Lokální seznam funkcí.
137      *
138      * @return Instance {@link QueryFormPane}.
139      *
140      * @throws ParserConfigurationException from *QueryFormPane
141      * @throws TransformerException from *QueryFormPane
142      * @throws Saveable.LoadException from *QueryFormPane
143      */
144     private static QueryFormPane<?> doLoad(
145             final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
146             final Object context) throws Saveable.LoadException, TransformerException, ParserConfigurationException {
147 
148         if (context instanceof Document) {
149             Document doc = (Document) context;
150             final NodeList nodeList = doc.getElementsByTagName(QueryFormPane.XML_ELEMENT_NAME);
151 
152             if (nodeList.getLength() > 0) {
153                 final Element root = (Element) nodeList.item(0);
154                 final String type = root.getAttribute(QueryFormPane.XML_QUERY_TYPE);
155 
156                 QueryFormPane.setObtainedColor(root.getAttribute(QueryFormPane.XML_TAB_COLOR));
157 
158                 final QueryFormPane<?> pane = getQueryPane(mainForm, dataAgent, localStorage, root, type);
159 
160                 if (pane == null) {
161                     throw new Saveable.LoadException(root, QueryFormPane.XML_QUERY_TYPE);
162                 }
163 
164                 return pane;
165             }
166 
167         } else if (context instanceof ParseTree) {
168             //TODO: 1
169             ParseTree treeContext = (ParseTree) context;
170             String queryType = QueryParser.getDocType(treeContext);
171             if (queryType != null) {
172                 QueryFormPane.setObtainedColor("");
173                 return getQueryPane(mainForm, dataAgent, localStorage, treeContext, queryType);
174             } else {
175                 throw new ParseCancellationException(Messages.getString("UNKNOWN_QUERY")); //$NON-NLS-1$
176             }
177         }
178         return null;
179     }
180 
181     /**
182      * It determines query type and selects relevant class.
183      *
184      * @param dataAgent Connect to the store.
185      * @param localStorage Local mapping of prefixes, Local list of other
186      * concepts, Local list of data types., Local list of functions.
187      * @param root Root of query tree.
188      * @param paneType Query type.
189      *
190      * @return Relevant query pane.
191      *
192      * @throws ParserConfigurationException from *QueryFormPane
193      * @throws TransformerException from *QueryFormPane
194      * @throws cz.zcu.mre.sparkle.tools.Saveable.LoadException *QueryFormPane
195      */
196     private static QueryFormPane<?> getQueryPane(final MainForm mainForm, final DataAgent dataAgent,
197             final Storage localStorage, final Object root, final String paneType)
198             throws ParserConfigurationException, TransformerException, Saveable.LoadException {
199         //TODO: 2
200         final QueryFormPane<?> pane;
201         if (paneType.equalsIgnoreCase(SelectQueryFormPane.XML_QUERY_TYPE_VALUE)) {
202             pane = new SelectQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
203 
204         } else if (paneType.equalsIgnoreCase(AskQueryFormPane.XML_QUERY_TYPE_VALUE)) {
205             pane = new AskQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
206 
207         } else if (paneType.equalsIgnoreCase(ConstructQueryFormPane.XML_QUERY_TYPE_VALUE)) {
208             pane = new ConstructQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
209 
210         } else if (paneType.equalsIgnoreCase(DescribeQueryFormPane.XML_QUERY_TYPE_VALUE)) {
211             pane = new DescribeQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
212 
213         } else if (paneType.equalsIgnoreCase(DeleteQueryFormPane.XML_QUERY_TYPE_VALUE)) {
214             pane = new DeleteQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
215 
216         } else if (paneType.equalsIgnoreCase(InsertQueryFormPane.XML_QUERY_TYPE_VALUE)) {
217             pane = new InsertQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
218 
219         } else if (paneType.equalsIgnoreCase(DeleteInsertQueryFormPane.XML_QUERY_TYPE_VALUE)) {
220             pane = new DeleteInsertQueryFormPane(mainForm, dataAgent, localStorage, root, paneType);
221 
222         } else {
223             return null;
224         }
225         return pane;
226     }
227 }