/* * _____________________________________________________________________________ * * INAF - OATS National Institute for Astrophysics - Astronomical Observatory of * Trieste INAF - IA2 Italian Center for Astronomical Archives * _____________________________________________________________________________ * * Copyright (C) 2017 Istituto Nazionale di Astrofisica * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License Version 3 as published by the * Free Software Foundation. * * 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, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package it.inaf.ia2.tsm.model; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.xml.bind.JAXB; /** * * @author Sonia Zorba {@literal } */ public class SchemaModels { private static final String SCHEMA_DEFINITION_FOLDER = "schema_definition"; private static final String TAP_SCHEMA_NAME = "tap_schema"; private static final String IVOA_SCHEMA_NAME = "ivoa"; // Map keys are version numbers private static final Map TAP_SCHEMA_MODELS; private static final Map IVOA_SCHEMA_MODELS; private SchemaModels() { } static { try { String[] xmlModelFileNames = getXMLModelFileNames(); List xmlModels = getXmlModels(xmlModelFileNames); Map tapSchemaXmlModelsMap = getXmlModelMapByVersionAndName(xmlModels, TAP_SCHEMA_NAME); Map ivoaSchemaXmlModelsMap = getXmlModelMapByVersionAndName(xmlModels, IVOA_SCHEMA_NAME); TAP_SCHEMA_MODELS = getModelMapByVersion(tapSchemaXmlModelsMap); IVOA_SCHEMA_MODELS = getModelMapByVersion(ivoaSchemaXmlModelsMap); } catch (IOException e) { throw new ExceptionInInitializerError(e); } } private static String[] getXMLModelFileNames() throws IOException { try (InputStream in = SchemaModels.class.getClassLoader().getResourceAsStream("core.properties")) { Properties props = new Properties(); props.load(in); String[] fileNames = props.getProperty("schema_definition_files").split(","); String[] modelFiles = new String[fileNames.length]; for (int i = 0; i < fileNames.length; i++) { modelFiles[i] = String.format("%s%s%s", SCHEMA_DEFINITION_FOLDER, File.separator, fileNames[i]); } return modelFiles; } } private static List getXmlModels(String[] xmlModelFileNames) throws IOException { List xmlModels = new ArrayList<>(); for (String modelFile : xmlModelFileNames) { try (InputStream in = SchemaModels.class.getClassLoader().getResourceAsStream(modelFile)) { SchemaXMLModel model = JAXB.unmarshal(in, SchemaXMLModel.class); xmlModels.add(model); } } return xmlModels; } private static void loadSchemaModel(SchemaModel model, SchemaXMLModel xmlModel, Map xmlModels) { for (TableXMLModel tableXmlModel : xmlModel.getTables()) { String tableName = tableXmlModel.getName(); TableModel tableModel = model.get(tableName); if (tableModel == null) { tableModel = new TableModel(tableXmlModel); } for (PropertyModel property : tableXmlModel.getAdd()) { if (tableModel.getProperties().get(property.getName()) == null) { // Add property only if it didn't exist in child structure // this allows to override properties in children tableModel.getProperties().put(property.getName(), property); } } model.getTables().put(tableName, tableModel); } if (xmlModel.getExtendsFrom() != null) { SchemaXMLModel parentModel = xmlModels.get(xmlModel.getExtendsFrom()); loadSchemaModel(model, parentModel, xmlModels); } } private static Map getXmlModelMapByVersionAndName(List xmlModels, String schemaName) { Map map = new HashMap<>(); for (SchemaXMLModel xmlModel : xmlModels) { if (schemaName.equals(xmlModel.getName())) { map.put(xmlModel.getVersion(), xmlModel); } } return map; } private static Map getModelMapByVersion(Map xmlModels) { Map map = new HashMap<>(); for (SchemaXMLModel xmlModel : xmlModels.values()) { SchemaModel model = new SchemaModel(xmlModel); loadSchemaModel(model, xmlModel, xmlModels); map.put(model.getVersion(), model); } return map; } public static SchemaModel getTapSchemaModel(String version) { return TAP_SCHEMA_MODELS.get(version); } public static Iterable getTapSchemaModels() { return TAP_SCHEMA_MODELS.values(); } public static TableModel getTapSchemaTableModel(String tableName, String version) { return getTapSchemaModel(version).getTables().get(tableName); } private static List getAvailableVersions(Map schemaModels) { List versions = new ArrayList<>(); for (SchemaModel tapSchemaModel : schemaModels.values()) { versions.add(tapSchemaModel.getVersion()); } return versions; } public static List getAvailableTapSchemaVersions() { return getAvailableVersions(TAP_SCHEMA_MODELS); } public static List getAvailableIvoaSchemaVersions() { return getAvailableVersions(IVOA_SCHEMA_MODELS); } public static SchemaModel getIvoaSchemaModel(String version) { return IVOA_SCHEMA_MODELS.get(version); } public static Iterable getIvoaSchemaModels() { return IVOA_SCHEMA_MODELS.values(); } }