use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method getRepositoryFileReference.
public static RepositoryFileReference getRepositoryFileReference(Path rootPath, Path path, DirectoryId directoryId) {
final Path relativePath = rootPath.relativize(path);
Path parent = relativePath.getParent();
if (parent == null) {
return new RepositoryFileReference(directoryId, path.getFileName().toString());
} else {
return new RepositoryFileReference(directoryId, parent, path.getFileName().toString());
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class IRepository method setMimeType.
/**
* Stores the mime type of the given file reference in a separate file
* <p>
* This method calls putContentToFile(), where the filename is appended with Constants.SUFFIX_MIMETYPE and a null
* mime type. The latter indicates that no "normal" file is stored.
*
* @param ref the file reference
* @param mediaType the mimeType
*/
default void setMimeType(RepositoryFileReference ref, MediaType mediaType) throws IOException {
RepositoryFileReference mimeFileRef = ref.setFileName(ref.getFileName() + Constants.SUFFIX_MIMETYPE);
this.putContentToFile(mimeFileRef, mediaType.toString(), null);
}
use of org.eclipse.winery.repository.common.RepositoryFileReference 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.repository.common.RepositoryFileReference in project winery by eclipse.
the class ToscaExportUtil method specifyImports.
private TDefinitions specifyImports(IRepository repository, DefinitionsChildId tcId, Collection<DefinitionsChildId> referencedDefinitionsChildIds) {
TDefinitions entryDefinitions = repository.getDefinitions(tcId);
// BEGIN: Definitions modification
// the "imports" collection contains the imports of Definitions, not of other definitions
// the other definitions are stored in entryDefinitions.getImport()
// we modify the internal definitions object directly. It is not written back to the storage. Therefore, we do not need to clone it
// the imports (pointing to not-definitions (xsd, wsdl, ...)) already have a correct relative URL. (quick hack)
URI uri = (URI) this.exportConfiguration.get(CsarExportConfiguration.REPOSITORY_URI.name());
if (uri != null) {
// we are in the plain-XML mode, the URLs of the imports have to be adjusted
for (TImport i : entryDefinitions.getImport()) {
String loc = i.getLocation();
if (!loc.startsWith("../")) {
LOGGER.warn("Location is not relative for id " + tcId.toReadableString());
}
loc = loc.substring(3);
loc = uri + loc;
// now the location is an absolute URL
i.setLocation(loc);
}
}
// files of imports have to be added to the CSAR, too
for (TImport i : entryDefinitions.getImport()) {
String loc = i.getLocation();
if (Util.isRelativeURI(loc)) {
// locally stored, add to CSAR
GenericImportId iid = new GenericImportId(i);
String fileName = IdUtil.getLastURIPart(loc);
fileName = EncodingUtil.URLdecode(fileName);
RepositoryFileReference ref = new RepositoryFileReference(iid, fileName);
putRefAsReferencedItemInCsar(repository, ref);
}
}
Set<DefinitionsChildId> collect = referencedDefinitionsChildIds.stream().filter(id -> id instanceof NodeTypeImplementationId).collect(Collectors.toSet());
if (collect.stream().anyMatch(DefinitionsChildId::isSelfContained)) {
if (this.exportConfiguration.containsKey(CsarExportConfiguration.INCLUDE_DEPENDENCIES.name())) {
referencedDefinitionsChildIds.removeAll(collect.stream().filter(id -> !id.isSelfContained()).collect(Collectors.toList()));
} else if (collect.size() > 1 && collect.stream().anyMatch(id -> !id.isSelfContained())) {
referencedDefinitionsChildIds.removeAll(collect.stream().filter(DefinitionsChildId::isSelfContained).collect(Collectors.toList()));
}
}
// adjust imports: add imports of definitions to it
Collection<TImport> imports = new ArrayList<>();
for (DefinitionsChildId id : referencedDefinitionsChildIds) {
this.addToImports(repository, id, imports);
}
entryDefinitions.getImport().addAll(imports);
// END: Definitions modification
return entryDefinitions;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class ToscaExportUtil method prepareForExport.
/**
* Synchronizes the plan model references and adds the plans to the csar (putRefAsReferencedItemInCsar)
*/
private void prepareForExport(IRepository repository, ServiceTemplateId id) throws IOException {
// ensure that the plans stored locally are the same ones as stored in the definitions
BackendUtils.synchronizeReferences(id, repository);
// add all plans as reference in the CSAR
// the data model is consistent with the repository
// we crawl through the repository to as putRefAsReferencedItemInCsar expects a repository file reference
PlansId plansContainerId = new PlansId(id);
SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
for (PlanId planId : nestedPlans) {
SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(planId);
// even if we currently support only one file in the directory, we just add everything
for (RepositoryFileReference ref : containedFiles) {
putRefAsReferencedItemInCsar(repository, ref);
}
}
}
Aggregations