Search in sources :

Example 1 with XSObject

use of org.apache.xerces.xs.XSObject in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.

// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
    Objects.requireNonNull(id);
    Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
    if (!ref.isPresent()) {
        return Collections.emptyList();
    }
    short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
    Date lastUpdate = RepositoryFactory.getRepository().getLastUpdate(ref.get());
    @NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
    @NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
    boolean cacheNeedsUpdate = true;
    if (RepositoryFactory.getRepository().exists(cacheRef)) {
        Date lastUpdateCache = RepositoryFactory.getRepository().getLastUpdate(cacheRef);
        if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
            cacheNeedsUpdate = false;
        }
    }
    List<String> result;
    if (cacheNeedsUpdate) {
        final Optional<XSModel> model = BackendUtils.getXSModel(ref.get());
        if (!model.isPresent()) {
            return Collections.emptyList();
        }
        XSNamedMap components = model.get().getComponents(type);
        // @SuppressWarnings("unchecked")
        int len = components.getLength();
        result = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            XSObject item = components.item(i);
            // We want to return only types defined in the namespace of this resource
            if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
                result.add(item.getName());
            }
        }
        String cacheContent = null;
        try {
            cacheContent = BackendUtils.mapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOGGER.error("Could not generate cache content", e);
        }
        try {
            RepositoryFactory.getRepository().putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } catch (IOException e) {
            LOGGER.error("Could not update cache", e);
        }
    } else {
        // cache should contain most recent information
        try (InputStream is = RepositoryFactory.getRepository().newInputStream(cacheRef)) {
            result = BackendUtils.mapper.readValue(is, java.util.List.class);
        } catch (IOException e) {
            LOGGER.error("Could not read from cache", e);
            result = Collections.emptyList();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) XSObject(org.apache.xerces.xs.XSObject) IOException(java.io.IOException) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) XSModel(org.apache.xerces.xs.XSModel) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 XSModel (org.apache.xerces.xs.XSModel)1 XSNamedMap (org.apache.xerces.xs.XSNamedMap)1 XSObject (org.apache.xerces.xs.XSObject)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1