1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.imports.xsdimports;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
19 import java.util.TreeSet;
21 import javax.ws.rs.GET;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.MediaType;
28 import org.eclipse.winery.common.RepositoryFileReference;
29 import org.eclipse.winery.common.ids.Namespace;
30 import org.eclipse.winery.common.ids.definitions.imports.XSDImportId;
31 import org.eclipse.winery.repository.Utils;
32 import org.eclipse.winery.repository.backend.Repository;
33 import org.eclipse.winery.repository.resources.AbstractComponentsResource;
34 import org.restdoc.annotations.RestDoc;
36 import com.fasterxml.jackson.core.JsonProcessingException;
39 * Manages all imports of type XML Schema Definition <br />
40 * The actual implementation is done in the AbstractComponentsResource
42 * FIXME: This class should be generalized to handle ImportId
44 public class XSDImportsResource extends AbstractComponentsResource<XSDImportResource> {
48 @RestDoc(methodDescription = "Returns all available local names of defined elements in this namespace")
49 @Produces(MediaType.APPLICATION_JSON)
50 public String getAllElementLocalNames(@PathParam("namespace") String nsString, @QueryParam(value = "elements") String returnElements, @QueryParam(value = "types") String returnTypes) {
51 // returnElements is not read as either types or elements may be read
52 Set<String> allNCNames = this.getAllElementLocalNamesAsSet(nsString, returnTypes != null);
54 return Utils.mapper.writeValueAsString(allNCNames);
55 } catch (JsonProcessingException e) {
56 throw new IllegalStateException(e);
61 * @param nsString the namesapce as String
62 * @param returnTypes true: return ElementTypes, false: return Elements
64 private Set<String> getAllElementLocalNamesAsSet(final String nsString, final boolean getTypes) {
65 Set<XSDImportId> importsOfNS = this.getImportsOfNS(nsString);
67 // TreeSet enables ordering
68 Set<String> allNCNames = new TreeSet<String>();
70 for (XSDImportId imp : importsOfNS) {
71 XSDImportResource res = new XSDImportResource(imp);
72 Collection<String> col;
74 col = res.getAllDefinedTypesLocalNames();
76 col = res.getAllDefinedElementsLocalNames();
78 allNCNames.addAll(col);
84 * Finds out all imports belonging to the given namespace
86 * @param nsString the namespace to query
88 private Set<XSDImportId> getImportsOfNS(final String nsString) {
89 // FIXME: Currently not supported by the repository, therefore, we filter by hand
90 Set<XSDImportId> allImports = Repository.INSTANCE.getAllTOSCAComponentIds(XSDImportId.class);
91 Namespace ns = new Namespace(nsString, true);
92 Set<XSDImportId> importsOfNs = new HashSet<XSDImportId>();
93 for (XSDImportId imp : allImports) {
94 if (imp.getNamespace().equals(ns)) {
102 * Returns a mapping from localnames to XSD files, containing the defined
103 * local names for the given namespace
105 public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final String nsString, final boolean getTypes) {
106 Set<XSDImportId> importsOfNS = this.getImportsOfNS(nsString);
107 Map<String, RepositoryFileReference> result = new HashMap<>();
108 for (XSDImportId imp : importsOfNS) {
109 XSDImportResource res = new XSDImportResource(imp);
110 Collection<String> col;
112 col = res.getAllDefinedTypesLocalNames();
114 col = res.getAllDefinedElementsLocalNames();
116 RepositoryFileReference ref = res.getXSDFileReference();
117 for (String localName : col) {
118 result.put(localName, ref);