use of org.eclipse.winery.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.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method deriveWPD.
/**
* Derives Winery's Properties Definition from an existing properties definition
*
* @param ci the entity type to try to modify the WPDs
* @param errors the list to add errors to
*/
public static void deriveWPD(TEntityType ci, List<String> errors) {
BackendUtils.LOGGER.trace("deriveWPD");
PropertiesDefinition propertiesDefinition = ci.getPropertiesDefinition();
QName element = propertiesDefinition.getElement();
if (element == null) {
BackendUtils.LOGGER.debug("only works for an element definition, not for types");
} else {
BackendUtils.LOGGER.debug("Looking for the definition of {" + element.getNamespaceURI() + "}" + element.getLocalPart());
// fetch the XSD defining the element
final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
Map<String, RepositoryFileReference> mapFromLocalNameToXSD = xsdImportManager.getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
RepositoryFileReference ref = mapFromLocalNameToXSD.get(element.getLocalPart());
if (ref == null) {
String msg = "XSD not found for " + element.getNamespaceURI() + " / " + element.getLocalPart();
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
final Optional<XSModel> xsModelOptional = BackendUtils.getXSModel(ref);
if (!xsModelOptional.isPresent()) {
LOGGER.error("no XSModel found");
}
XSModel xsModel = xsModelOptional.get();
XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration(element.getLocalPart(), element.getNamespaceURI());
if (elementDeclaration == null) {
String msg = "XSD model claimed to contain declaration for {" + element.getNamespaceURI() + "}" + element.getLocalPart() + ", but it did not.";
BackendUtils.LOGGER.debug(msg);
errors.add(msg);
return;
}
// go through the XSD definition and
XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
if (typeDefinition instanceof XSComplexTypeDefinition) {
XSComplexTypeDefinition cTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
XSParticle particle = cTypeDefinition.getParticle();
if (particle == null) {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Complex type does not contain particles");
} else {
XSTerm term = particle.getTerm();
if (term instanceof XSModelGroup) {
XSModelGroup modelGroup = (XSModelGroup) term;
if (modelGroup.getCompositor() == XSModelGroup.COMPOSITOR_SEQUENCE) {
XSObjectList particles = modelGroup.getParticles();
int len = particles.getLength();
boolean everyThingIsASimpleType = true;
PropertyDefinitionKVList list = new PropertyDefinitionKVList();
if (len != 0) {
for (int i = 0; i < len; i++) {
XSParticle innerParticle = (XSParticle) particles.item(i);
XSTerm innerTerm = innerParticle.getTerm();
if (innerTerm instanceof XSElementDeclaration) {
XSElementDeclaration innerElementDeclaration = (XSElementDeclaration) innerTerm;
String name = innerElementDeclaration.getName();
XSTypeDefinition innerTypeDefinition = innerElementDeclaration.getTypeDefinition();
if (innerTypeDefinition instanceof XSSimpleType) {
XSSimpleType xsSimpleType = (XSSimpleType) innerTypeDefinition;
String typeNS = xsSimpleType.getNamespace();
String typeName = xsSimpleType.getName();
if (typeNS.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
PropertyDefinitionKV def = new PropertyDefinitionKV();
def.setKey(name);
// convention at WPD: use "xsd" as prefix for XML Schema Definition
def.setType("xsd:" + typeName);
list.add(def);
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
} else {
everyThingIsASimpleType = false;
break;
}
}
}
if (everyThingIsASimpleType) {
// everything went allright, we can add a WPD
WinerysPropertiesDefinition wpd = new WinerysPropertiesDefinition();
wpd.setIsDerivedFromXSD(Boolean.TRUE);
wpd.setElementName(element.getLocalPart());
wpd.setNamespace(element.getNamespaceURI());
wpd.setPropertyDefinitionKVList(list);
ModelUtilities.replaceWinerysPropertiesDefinition(ci, wpd);
BackendUtils.LOGGER.debug("Successfully generated WPD");
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not all types in the sequence are simple types");
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Model group is not a sequence");
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: Not a model group");
}
}
} else {
BackendUtils.LOGGER.debug("XSD does not follow the requirements put by winery: No Complex Type Definition");
}
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method getTArtifactTemplate.
/**
* @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
* @return The TArtifactTemplate corresponding to the directoryId.
*/
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
Unmarshaller u = JAXBSupport.createUnmarshaller();
Definitions defs = ((Definitions) u.unmarshal(is));
for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (elem instanceof TArtifactTemplate) {
return (TArtifactTemplate) elem;
}
}
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
} catch (JAXBException e) {
LOGGER.error("Error in XML in " + ref.getFileName(), e);
}
return null;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method synchronizeReferences.
/**
* Synchronizes the known plans with the data in the XML. When there is a stored file, but no known entry in the
* XML, we guess "BPEL" as language and "build plan" as type.
*/
public static void synchronizeReferences(ServiceTemplateId id) throws IOException {
final IRepository repository = RepositoryFactory.getRepository();
final TServiceTemplate serviceTemplate = repository.getElement(id);
// locally stored plans
TPlans plans = serviceTemplate.getPlans();
// plans stored in the repository
PlansId plansContainerId = new PlansId(id);
SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
Set<PlanId> plansToAdd = new HashSet<>();
plansToAdd.addAll(nestedPlans);
if (nestedPlans.isEmpty()) {
if (plans == null) {
// data on the file system equals the data -> no plans
return;
} else {
// noinspection StatementWithEmptyBody
// we have to check for equality later
}
}
if (plans == null) {
plans = new TPlans();
serviceTemplate.setPlans(plans);
}
for (Iterator<TPlan> iterator = plans.getPlan().iterator(); iterator.hasNext(); ) {
TPlan plan = iterator.next();
if (plan.getPlanModel() != null) {
// in case, a plan is directly contained in a Model element, we do not need to do anything
continue;
}
TPlan.PlanModelReference planModelReference;
if ((planModelReference = plan.getPlanModelReference()) != null) {
String ref = planModelReference.getReference();
if ((ref == null) || ref.startsWith("../")) {
// special case (due to errors in the importer): empty PlanModelReference field
if (plan.getId() == null) {
// invalid plan entry: no id.
// we remove the entry
iterator.remove();
continue;
}
PlanId planId = new PlanId(plansContainerId, new XmlId(plan.getId(), false));
if (nestedPlans.contains(planId)) {
// everything allright
// we do NOT need to add the plan on the HDD to the XML
plansToAdd.remove(planId);
} else {
// no local storage for the plan, we remove it from the XML
iterator.remove();
}
}
}
}
// add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
List<TPlan> thePlans = plans.getPlan();
for (PlanId planId : plansToAdd) {
SortedSet<RepositoryFileReference> files = repository.getContainedFiles(planId);
if (files.size() != 1) {
throw new IllegalStateException("Currently, only one file per plan is supported.");
}
RepositoryFileReference ref = files.iterator().next();
TPlan plan = new TPlan();
plan.setId(planId.getXmlId().getDecoded());
plan.setName(planId.getXmlId().getDecoded());
plan.setPlanType(org.eclipse.winery.repository.Constants.TOSCA_PLANTYPE_BUILD_PLAN);
plan.setPlanLanguage(Namespaces.URI_BPEL20_EXECUTABLE);
// create a PlanModelReferenceElement pointing to that file
String path = Util.getUrlPath(ref);
// path is relative from the definitions element
path = "../" + path;
TPlan.PlanModelReference pref = new TPlan.PlanModelReference();
pref.setReference(path);
plan.setPlanModelReference(pref);
thePlans.add(plan);
}
if (serviceTemplate.getPlans().getPlan().isEmpty()) {
serviceTemplate.setPlans(null);
}
RepositoryFactory.getRepository().setElement(id, serviceTemplate);
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method synchronizeReferences.
public static void synchronizeReferences(ArtifactTemplateId id) throws IOException {
TArtifactTemplate template = RepositoryFactory.getRepository().getElement(id);
DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
SortedSet<RepositoryFileReference> files = RepositoryFactory.getRepository().getContainedFiles(fileDir);
if (files.isEmpty()) {
// clear artifact references
template.setArtifactReferences(null);
} else {
TArtifactTemplate.ArtifactReferences artifactReferences = new TArtifactTemplate.ArtifactReferences();
template.setArtifactReferences(artifactReferences);
List<TArtifactReference> artRefList = artifactReferences.getArtifactReference();
for (RepositoryFileReference ref : files) {
// determine path
// path relative from the root of the CSAR is ok (COS01, line 2663)
// double encoded - see ADR-0003
String path = Util.getUrlPath(ref);
// put path into data structure
// we do not use Include/Exclude as we directly reference a concrete file
TArtifactReference artRef = new TArtifactReference();
artRef.setReference(path);
artRefList.add(artRef);
}
}
BackendUtils.persist(id, template);
}
Aggregations