package archmapper.main.model.archmapping;
import java.io.File;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import archmapper.main.exceptions.ArchMapperException;
import archmapper.main.model.ParentSettingXMLUnmarshallerListener;
public class ArchitectureMappingDAO {
private static ArchitectureMapping read(String filename, InputStream stream) {
ArchitectureMapping mapping = null;
try {
JAXBContext jc = JAXBContext.newInstance(ArchitectureMapping.class);
Unmarshaller um = jc.createUnmarshaller();
// Set the parent to all objects with a setParent-Method
um.setListener(new ParentSettingXMLUnmarshallerListener());
if (filename != null) {
mapping = (ArchitectureMapping) um.unmarshal(new File(filename));
} else {
mapping = (ArchitectureMapping) um.unmarshal(stream);
}
} catch (JAXBException e) {
throw new ArchMapperException("Could not load architecture mapping file: "+
e.getMessage());
}
return mapping;
}
public static ArchitectureMapping read(InputStream stream) {
return read(null, stream);
}
public static ArchitectureMapping read(String filename) {
return read(filename, null);
}
public static void main(String[] args) {
ArchitectureMapping mapping = read("examples/architecturemapping.xml");
System.out.println("Architecture Mapping: "+ mapping.getStyleTypeInformation().get(0).getType()+
"\n"+ mapping.getProperties().get("configfile"));
}
}