Package archmapper.main.model.architecture

Source Code of archmapper.main.model.architecture.ArchitectureXMLDAO

package archmapper.main.model.architecture;

import java.io.File;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.eclipse.core.resources.IFile;

import archmapper.main.exceptions.ArchMapperException;
import archmapper.main.model.IAdlConverter;
import archmapper.main.model.ParentSettingXMLUnmarshallerListener;

public class ArchitectureXMLDAO implements IAdlConverter {
  private static Configuration read(String filename, InputStream stream) {
    Configuration conf = null;
   
    try {
      JAXBContext jc = JAXBContext.newInstance(Configuration.class);
      Unmarshaller um = jc.createUnmarshaller();
     
      // Set the parent to all objects with a setParent-Method
      um.setListener(new ParentSettingXMLUnmarshallerListener());
     
      if (filename != null) {
        conf = (Configuration) um.unmarshal(new File(filename));
      } else {
        conf = (Configuration) um.unmarshal(stream);
      }
     
      fixMissingLinksInArchitecture(conf);
     
    } catch (JAXBException e) {
      throw new ArchMapperException("Could not load architecture file: "+
          e.getMessage());
    }
   
    return conf;
  }
 
  /**
   * Mappings from roles to connectors can be specified in the connectors
   * and/or the roles. If one direction is missing in the xml file,
   * then that direction is added by this method.
   *
   * @param conf The architecture to fix
   */
  private static void fixMissingLinksInArchitecture(Configuration conf) {
    // first direction: If connections are set in the
    // connector, but not in the roles...
    for (Connector conn : conf.getConnectors()) {
      for (Role role : conn.getRoles()) {
        if (role.getConnector() == null) {
          role.setConnector(conn);
        }
      }
    }
   
    // second direction: If connections are set in the roles,
    // but not in the connectors
    for (Component comp : conf.getComponents()) {
      for (Port port : comp.getPorts()) {
        for (Role role : port.getRoles()) {
          Connector conn = role.getConnector();
          if (conn != null) {
            if (!conn.getRoles().contains(role)) {
              conn.getRoles().add(role);
            }
          }
        }
      }
    }
  }
   
  public static Configuration read(InputStream stream) {
    return read(null, stream);
  }
 
  public static Configuration read(String filename) {
    return read(filename, null);
  }

  public Configuration convertFromAdl(IFile adlFile) {
    return read(adlFile.getLocation().toOSString());
  }
}
TOP

Related Classes of archmapper.main.model.architecture.ArchitectureXMLDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.