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;
}
Aggregations