use of org.eclipse.winery.common.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(id);
Assert.assertEquals(true, importLocation.isPresent());
}
use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class RepositoryBasedXsdImportManager method getMapFromLocalNameToXSD.
@Override
public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final Namespace namespace, final boolean getTypes) {
Set<XSDImportId> importsOfNamespace = this.getImportsOfNamespace(namespace);
Map<String, RepositoryFileReference> result = new HashMap<>();
for (XSDImportId imp : importsOfNamespace) {
final List<String> allDefinedLocalNames = this.getAllDefinedLocalNames(namespace, getTypes);
Optional<RepositoryFileReference> ref = getXsdFileReference(imp);
if (!ref.isPresent()) {
LOGGER.error("Ref is not defined");
} else {
for (String localName : allDefinedLocalNames) {
result.put(localName, ref.get());
}
}
}
return result;
}
use of org.eclipse.winery.common.ids.definitions.imports.XSDImportId in project winery by eclipse.
the class CsarImporter method importOtherImport.
/**
* SIDE EFFECT: modifies the location of imp to point to the correct relative location (when read from the exported
* CSAR)
*
* @param rootPath the absolute path where to resolve files from
*/
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, boolean overwrite) {
assert (!type.equals(Namespaces.TOSCA_NAMESPACE));
String loc = imp.getLocation();
if (!Util.isRelativeURI(loc)) {
// This is just an information message
errors.add("Absolute URIs are not resolved by Winery (" + loc + ")");
return;
}
// location URLs are encoded: http://www.w3.org/TR/2001/WD-charmod-20010126/#sec-URIs, RFC http://www.ietf.org/rfc/rfc2396.txt
loc = Util.URLdecode(loc);
Path path;
try {
path = rootPath.resolve(loc);
} catch (Exception e) {
// java.nio.file.InvalidPathException could be thrown which is a RuntimeException
errors.add(e.getMessage());
return;
}
if (!Files.exists(path)) {
// fallback for older CSARs, where the location is given from the root
path = rootPath.getParent().resolve(loc);
if (!Files.exists(path)) {
errors.add(String.format("File %1$s does not exist", loc));
return;
}
}
String namespace = imp.getNamespace();
String fileName = path.getFileName().toString();
String id = fileName;
id = FilenameUtils.removeExtension(id);
// Convention: id of import is filename without extension
GenericImportId rid;
if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
rid = new XSDImportId(namespace, id, false);
} else {
rid = new GenericImportId(namespace, id, false, type);
}
boolean importDataExistsInRepo = RepositoryFactory.getRepository().exists(rid);
if (!importDataExistsInRepo) {
// We have to
// a) create a .definitions file
// b) put the file itself in the repo
// Create the definitions file
TDefinitions defs = BackendUtils.createWrapperDefinitions(rid);
defs.getImport().add(imp);
// QUICK HACK: We change the imp object's location here and below again
// This is "OK" as "storeDefinitions" serializes the current state and not the future state of the imp object
// change the location to point to the file in the folder of the .definitions file
imp.setLocation(fileName);
// put the definitions file to the repository
CsarImporter.storeDefinitions(rid, defs);
}
// put the file itself to the repo
// ref is required to generate fileRef
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
// location is relative to Definitions/
// even if the import already exists, we have to adapt the path
// URIs are encoded
String newLoc = "../" + Util.getUrlPath(fileRef);
imp.setLocation(newLoc);
if (!importDataExistsInRepo || overwrite) {
// finally write the file to the storage
try (InputStream is = Files.newInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is)) {
MediaType mediaType;
if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
mediaType = MediaTypes.MEDIATYPE_XSD;
} else {
mediaType = BackendUtils.getMimeType(bis, path.getFileName().toString());
}
RepositoryFactory.getRepository().putContentToFile(fileRef, bis, mediaType);
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
// we have to update the cache in case of a new XSD to speedup usage of winery
if (rid instanceof XSDImportId) {
// We do the initialization asynchronously
// We do not check whether the XSD has already been checked
// We cannot just checck whether an XSD already has been handled since the XSD could change over time
// Synchronization at org.eclipse.winery.repository.resources.imports.xsdimports.XSDImportResource.getAllDefinedLocalNames(short) also isn't feasible as the backend doesn't support locks
CsarImporter.xsdParsingService.submit(() -> {
CsarImporter.LOGGER.debug("Updating XSD import cache data");
// We call the queries without storing the result:
// We use the SIDEEFFECT that a cache is created
final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
xsdImportManager.getAllDeclaredElementsLocalNames();
xsdImportManager.getAllDefinedTypesLocalNames();
CsarImporter.LOGGER.debug("Updated XSD import cache data");
});
}
}
}
Aggregations