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.data;
21  
22  import javafx.collections.FXCollections;
23  import javafx.collections.ObservableList;
24  import org.apache.jena.datatypes.RDFDatatype;
25  import java.util.Iterator;
26  
27  /**
28   * Třída pro skladování datových typů.
29   *
30   * @author Jan Smucr
31   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
32   * @author Klara Hlavacova
33   * @see AbstractTermsStorage
34   */
35  @SuppressWarnings("SimplifyStreamApiCallChains")
36  public final class DataTypesStorage
37          extends AbstractTermsStorage<StorageEntry> {
38  
39      private static final long serialVersionUID = 4147433324159700097L;
40      private static final String XML_ELEMENT_NAME = "DataTypes";//$NON-NLS-1$
41  
42      public DataTypesStorage(final DataAgent dataAgent, final PrefixesStorage dataTypesPrefixesStorage) {
43          super(dataAgent, dataTypesPrefixesStorage);
44      }
45  
46      /**
47       * Vrací seznam vestavěných datových typů poskytovaných knihovnou Jena.
48       *
49       * @param dataAgent Instance {@link DataAgent}.
50       * @param dataTypesPrefixesStorage Instance {@link PrefixesStorage}.
51       *
52       * @return Instance {@link DataTypesStorage} obsahující vestavěné datové
53       * typy.
54       */
55      public static final DataTypesStorage getBuiltIn(final DataAgent dataAgent,
56              final PrefixesStorage dataTypesPrefixesStorage) {
57          final DataTypesStorage result = new DataTypesStorage(dataAgent, dataTypesPrefixesStorage);
58  
59          final Iterator<RDFDatatype> dataTypeIterator = org.apache.jena.datatypes.TypeMapper.getInstance().listTypes();
60          while (dataTypeIterator.hasNext()) {
61  
62              final RDFDatatype datatype = dataTypeIterator.next();
63              final String datatypeIRI = datatype.getURI();
64  
65              for (final String iri : dataTypesPrefixesStorage.getIriToPrefixReadOnly().keySet()) {
66                  if (datatypeIRI.startsWith(iri)) // Extrakce názvu datového typu z IRI
67                  {
68                      result.addTerm(new StorageEntry(iri, datatypeIRI.substring(iri.length())));
69                      break;
70                  }
71              }
72  
73              // TODO Co pokud nic nenalezne?
74          }
75  
76          return result;
77      }
78  
79      /**
80       * @return IRI všech datových typů ve zkrácené nebo nezkrácené podobě.
81       */
82      public final ObservableList<String> getAllDataTypes() {
83          final ObservableList<String> result = FXCollections.observableArrayList();
84          final PrefixesStorage prefixesStorage = getPrefixesStorage();
85  
86          getNamespaceToTermsMap().entrySet().stream().forEach((entry) -> entry.getValue().stream().forEach((storageEntry) -> {
87              final String s = storageEntry.getShortVariant(prefixesStorage);
88              result.add(s == null ? storageEntry.getWrappedIRI() : s);
89          }));
90          return result;
91      }
92  
93      /**
94       * Vrací názvy datových typů podle prefixu.
95       *
96       * @param prefix Prefix.
97       * @param includePrefix Pokud <code>true</code>, bude prefix k názvům typů
98       * přidán.
99       *
100      * @return Seznam názvů datových typů.
101      */
102     public final ObservableList<String> getDataTypesNamesByPrefix(final String prefix, final boolean includePrefix) {
103         return getTermsNamesByPrefix(prefix, includePrefix, StorageEntry.class);
104     }
105 
106     @Override
107     public final String getTermNamespace(final StorageEntry term) {
108         return term.getNamespace();
109     }
110 
111     @Override
112     public final String getTermName(final StorageEntry term) {
113         return term.getLocalName();
114     }
115 
116     @Override
117     public String getTermTitle(StorageEntry term) {
118         return term.getTitle();
119     }
120 
121     @Override
122     protected final StorageEntry createTerm(final String namespace, final String localName, String title) {
123         return createTerm(namespace, localName, title, "");
124     }
125 
126     @Override
127     protected final StorageEntry createTerm(final String namespace, final String localName, String title, String type) {
128         return new StorageEntry(namespace, localName, title);
129     }
130 
131     @Override
132     public final String getXMLElementName() {
133         return XML_ELEMENT_NAME;
134     }
135 }