use of org.eclipse.winery.model.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class CsarImporter method adjustEntityType.
/**
* All EntityTypes may contain properties definition. In case a winery properties definition is found, the TOSCA
* conforming properties definition is removed
*
* @param ci the entity type
* @param wid the Winery id of the entityType
* @param newDefs the definitions, the entity type is contained in. The imports might be adjusted here
* @param errors Used to collect the errors
*/
private void adjustEntityType(TEntityType ci, EntityTypeId wid, TDefinitions newDefs, final List<String> errors) {
TEntityType.PropertiesDefinition propertiesDefinition = ci.getProperties();
if (propertiesDefinition != null) {
WinerysPropertiesDefinition winerysPropertiesDefinition = ci.getWinerysPropertiesDefinition();
boolean deriveWPD;
if (winerysPropertiesDefinition == null) {
deriveWPD = true;
} else {
if (winerysPropertiesDefinition.getIsDerivedFromXSD() == null) {
// no derivation from properties required as the properties are generated by Winery
deriveWPD = false;
// we have to remove the import, too
// Determine the location
String elementName = winerysPropertiesDefinition.getElementName();
String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD(wid, null, elementName);
// remove the import matching that location
List<TImport> imports = newDefs.getImport();
boolean found = false;
if (imports != null) {
Iterator<TImport> iterator = imports.iterator();
TImport imp;
while (iterator.hasNext()) {
imp = iterator.next();
// TODO: add check for QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE instead of import location. The current routine, however, works, too.
if (imp.getLocation().equals(loc)) {
found = true;
break;
}
}
// noinspection StatementWithEmptyBody
if (found) {
// imp with Winery's k/v location found
iterator.remove();
// the XSD has been imported in importOtherImport
// it was too difficult to do the location check there, therefore we just remove the XSD from the repository here
XSDImportId importId = new XSDImportId(winerysPropertiesDefinition.getNamespace(), elementName, false);
try {
this.targetRepository.forceDelete(importId);
} catch (IOException e) {
CsarImporter.LOGGER.debug("Could not delete Winery's generated XSD definition", e);
errors.add("Could not delete Winery's generated XSD definition");
}
} else {
// K/V properties definition was incomplete
}
}
} else {
// winery's properties are derived from an XSD
// The export does NOT add an imports statement: only the wpd exists
// We remove that as
ModelUtilities.removeWinerysPropertiesDefinition(ci);
// derive the WPDs again from the properties definition
deriveWPD = true;
}
}
if (deriveWPD) {
BackendUtils.deriveWPD(ci, errors, targetRepository);
}
}
}
use of org.eclipse.winery.model.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class CsarImporter method importTypes.
/**
* Imports the specified types into the repository. The types are converted to an import statement
*
* @param errors Container for error messages
*/
private void importTypes(TDefinitions defs, final List<String> errors) {
Types typesContainer = defs.getTypes();
if (typesContainer != null) {
List<Object> types = typesContainer.getAny();
for (Object type : types) {
if (type instanceof Element) {
Element element = (Element) type;
// generate id part of ImportId out of definitions' id
// we do not use the name as the name has to be URLencoded again and we have issues with the interplay with org.eclipse.winery.common.ids.definitions.imports.GenericImportId.getId(TImport) then.
String id = defs.getId();
// try to make the id unique by hashing the "content" of the definition
id = id + "-" + Integer.toHexString(element.hashCode());
// set importId
DefinitionsChildId importId;
String ns;
if (element.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
ns = element.getAttribute("targetNamespace");
importId = new XSDImportId(ns, id, false);
} else {
// Quick hack for non-XML-Schema-definitions
ns = "unknown";
importId = new GenericImportId(ns, id, false, element.getNamespaceURI());
}
// Following code is adapted from importOtherImports
TDefinitions wrapperDefs = BackendUtils.createWrapperDefinitions(importId, targetRepository);
TImport imp = new TImport();
String fileName = id + ".xsd";
imp.setLocation(fileName);
imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
imp.setNamespace(ns);
wrapperDefs.getImport().add(imp);
CsarImporter.storeDefinitions(targetRepository, importId, wrapperDefs);
// put the file itself to the repo
// ref is required to generate fileRef
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(importId);
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
// convert element to document
// QUICK HACK. Alternative: Add new method targetRepository.getOutputStream and transform DOM node to OuptputStream
String content = Util.getXMLAsString(element);
try {
targetRepository.putContentToFile(fileRef, content, MediaTypes.MEDIATYPE_TEXT_XML);
} catch (IOException e) {
CsarImporter.LOGGER.debug("Could not put XML Schema definition to file " + fileRef.toString(), e);
errors.add("Could not put XML Schema definition to file " + fileRef.toString());
}
// add import to definitions
// adapt path - similar to importOtherImport
String newLoc = "../" + Util.getUrlPath(fileRef);
imp.setLocation(newLoc);
defs.getImport().add(imp);
} else {
// This is a known type. Otherwise JAX-B would render it as Element
errors.add("There is a Type of class " + type.getClass().toString() + " which is unknown to Winery. The type element is imported as is");
}
}
}
}
use of org.eclipse.winery.model.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static TDefinitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final TDefinitions definitions = createWrapperDefinitions(id, repository);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof DataTypeId) {
element = new TDataType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof ComplianceRuleId) {
element = new OTComplianceRule(new OTComplianceRule.Builder(id.getXmlId().getDecoded()));
} else if (id instanceof PatternRefinementModelId) {
element = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TopologyFragmentRefinementModelId) {
element = new OTTopologyFragmentRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TestRefinementModelId) {
element = new OTTestRefinementModel(new OTTestRefinementModel.Builder());
} else if (id instanceof InterfaceTypeId) {
element = new TInterfaceType();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class ImportUtilsTest method getLocationForImportTest.
@Test
public void getLocationForImportTest() throws Exception {
this.setRevisionTo("5fdcffa9ccd17743d5498cab0914081fc33606e9");
XSDImportId id = new XSDImportId(new Namespace("http://opentosca.org/nodetypes", false), new XmlId("CloudProviderProperties", false));
Optional<String> importLocation = ImportUtils.getLocation(repository, id);
assertEquals(true, importLocation.isPresent());
}
use of org.eclipse.winery.model.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class WriterUtils method storeTypes.
public static void storeTypes(IRepository repository, Path path, String namespace, String id) {
LOGGER.debug("Store type: {}", id);
try {
MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
builder.setNamespace(namespace);
builder.setLocation(id + ".xsd");
GenericImportId rid = new XSDImportId(namespace, id, false);
TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid, repository);
definitions.getImport().add(builder.build());
CsarImporter.storeDefinitions(repository, rid, definitions);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
for (File file : files) {
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
repository.putContentToFile(fileRef, stream, mediaType);
}
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
}
Aggregations