All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gematik.ti.epa.vzd.gemClient.command.CommandsBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Copyright (c) 2020 gematik GmbH
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.gematik.ti.epa.vzd.gemClient.command;

import de.gematik.ti.epa.vzd.gemClient.exceptions.ReadException;
import de.gematik.ti.epa.vzd.gemClient.invoker.ConfigHandler;
import generated.CommandListType;
import generated.CommandType;
import generated.ObjectFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * This class is responsible for reading the commands out of the .xml File. Before the commands can be build the ConfigHandler have to be initialized
 */
public class CommandsBuilder {

  private JAXBContext jaxbContext;
  private Logger LOGGER = LoggerFactory.getLogger(CommandsBuilder.class);

  private static DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

  /**
   * Standard constructor initialize an jaxbContext
   */
  public CommandsBuilder() {
    try {
      jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    } catch (JAXBException e) {
      throw new ReadException("Error occurred by creating JAXBContext");
    }
  }

  /**
   * Reads the given command file and creates a list of commands to execute
   *
   * @return CommandListType - List of all commands to execute
   */
  public List buildCommands() {
    CommandListType commandList;
    ConfigHandler configHandler = ConfigHandler.getInstance();
    try {
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      Document doc = builderFactory.newDocumentBuilder()
          .parse(new File(configHandler.getCommandsPath()));
      Object obj = unmarshaller.unmarshal(doc);
      Object commands = ((JAXBElement) obj).getValue();
      if (commands instanceof CommandListType) {
        commandList = (CommandListType) commands;
        LOGGER.debug("Commands have been build");
        return commandList.getCommand();
      }
    } catch (ParserConfigurationException | SAXException | JAXBException e) {
      throw new ReadException(
          "An error have been occurred while reading your command file. Please check if this file is a valid .xml file");
    } catch (IOException e) {
      throw new ReadException(
          "A problem with your named file have occurred. Please if check " + configHandler.getCommandsPath() + " exist");
    }
    return null;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy