Search in sources :

Example 51 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project sis by apache.

the class FolderStoreProvider method open.

/**
 * Shared implementation of public {@code open(…)} methods.
 * Note that this method may modify the given {@code options} set for its own purpose.
 *
 * @param  connector  information about the storage (URL, path, <i>etc</i>).
 * @param  format     format name for directory content, or {@code null} if unspecified.
 * @param  options    whether to create a new directory, overwrite existing content, <i>etc</i>.
 */
private DataStore open(final StorageConnector connector, final String format, final EnumSet<StandardOpenOption> options) throws DataStoreException {
    /*
         * Determine now the provider to use for directory content. We do that for determining if the component
         * has write capability. If not, then the WRITE, CREATE and related options will be ignored.  If we can
         * not determine whether the component store has write capabilities (i.e. if canWrite(…) returns null),
         * assume that the answer is "yes".
         */
    final DataStoreProvider componentProvider;
    if (format != null) {
        componentProvider = StoreUtilities.providerByFormatName(format.trim());
        if (Boolean.FALSE.equals(StoreUtilities.canWrite(componentProvider.getClass()))) {
            // No write capability.
            options.clear();
        }
    } else {
        componentProvider = null;
        // Can not write if we don't know the components format.
        options.clear();
    }
    final Path path = connector.getStorageAs(Path.class);
    final Store store;
    try {
        /*
             * If the user asked to create a new directory, we need to perform this task before
             * to create the Store (otherwise constructor will fail with NoSuchFileException).
             * In the particular case of CREATE_NEW, we unconditionally attempt to create the
             * directory in order to rely on the atomic check performed by Files.createDirectory(…).
             */
        boolean isNew = false;
        if (options.contains(StandardOpenOption.CREATE)) {
            if (options.contains(StandardOpenOption.CREATE_NEW) || Files.notExists(path)) {
                // IOException if the directory already exists.
                Files.createDirectory(path);
                isNew = true;
            }
        }
        if (isNew || (options.contains(StandardOpenOption.WRITE) && Files.isWritable(path))) {
            // May throw NoSuchFileException.
            store = new WritableStore(this, connector, path, componentProvider);
        } else {
            // May throw NoSuchFileException.
            store = new Store(this, connector, path, componentProvider);
        }
        /*
             * If there is a destructive operation to perform (TRUNCATE_EXISTING), do it last only
             * after we have successfully created the data store. The check for directory existence
             * is also done after creation to be sure to check the path used by the store.
             */
        if (!Files.isDirectory(path)) {
            throw new DataStoreException(Resources.format(Resources.Keys.FileIsNotAResourceDirectory_1, path));
        }
        if (options.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
            WritableStore.deleteRecursively(path, false);
        }
    } catch (IOException e) {
        /*
             * In case of error, Java FileSystem implementation tries to throw a specific exception
             * (NoSuchFileException or FileAlreadyExistsException), but this is not guaranteed.
             */
        int isDirectory = 0;
        final short errorKey;
        if (e instanceof FileAlreadyExistsException) {
            if (path != null && Files.isDirectory(path)) {
                isDirectory = 1;
            }
            errorKey = Resources.Keys.FileAlreadyExists_2;
        } else if (e instanceof NoSuchFileException) {
            errorKey = Resources.Keys.NoSuchResourceDirectory_1;
        } else {
            errorKey = Resources.Keys.CanNotCreateFolderStore_1;
        }
        throw new DataStoreException(Resources.format(errorKey, (path != null) ? path : connector.getStorageName(), isDirectory), e);
    }
    return store;
}
Also used : Path(java.nio.file.Path) DataStoreException(org.apache.sis.storage.DataStoreException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DataStoreProvider(org.apache.sis.storage.DataStoreProvider) NoSuchFileException(java.nio.file.NoSuchFileException) URIDataStore(org.apache.sis.internal.storage.URIDataStore) DataStore(org.apache.sis.storage.DataStore) IOException(java.io.IOException)

Example 52 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project Wurst-MC-1.12 by Wurst-Imperium.

the class ClickGui method init.

public void init() {
    LinkedHashMap<Category, Window> windowMap = new LinkedHashMap<>();
    for (Category category : Category.values()) windowMap.put(category, new Window(category.getName()));
    ArrayList<Feature> features = new ArrayList<>();
    features.addAll(WurstClient.INSTANCE.mods.getAllMods());
    features.addAll(WurstClient.INSTANCE.commands.getAllCommands());
    features.addAll(WurstClient.INSTANCE.special.getAllFeatures());
    for (Feature f : features) if (f.getCategory() != null)
        windowMap.get(f.getCategory()).add(new FeatureButton(f));
    windows.addAll(windowMap.values());
    int x = 5;
    int y = 5;
    ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft());
    for (Window window : windows) {
        window.pack();
        window.setMinimized(true);
        if (x + window.getWidth() + 5 > sr.getScaledWidth()) {
            x = 5;
            y += 18;
        }
        window.setX(x);
        window.setY(y);
        x += window.getWidth() + 5;
    }
    JsonObject json;
    try (BufferedReader reader = Files.newBufferedReader(windowsFile)) {
        json = JsonUtils.jsonParser.parse(reader).getAsJsonObject();
    } catch (NoSuchFileException e) {
        saveWindows();
        return;
    } catch (Exception e) {
        System.out.println("Failed to load " + windowsFile.getFileName());
        e.printStackTrace();
        saveWindows();
        return;
    }
    for (Window window : windows) {
        JsonElement jsonWindow = json.get(window.getTitle());
        if (jsonWindow == null || !jsonWindow.isJsonObject())
            continue;
        JsonElement jsonX = jsonWindow.getAsJsonObject().get("x");
        if (jsonX.isJsonPrimitive() && jsonX.getAsJsonPrimitive().isNumber())
            window.setX(jsonX.getAsInt());
        JsonElement jsonY = jsonWindow.getAsJsonObject().get("y");
        if (jsonY.isJsonPrimitive() && jsonY.getAsJsonPrimitive().isNumber())
            window.setY(jsonY.getAsInt());
        JsonElement jsonMinimized = jsonWindow.getAsJsonObject().get("minimized");
        if (jsonMinimized.isJsonPrimitive() && jsonMinimized.getAsJsonPrimitive().isBoolean())
            window.setMinimized(jsonMinimized.getAsBoolean());
        JsonElement jsonPinned = jsonWindow.getAsJsonObject().get("pinned");
        if (jsonPinned.isJsonPrimitive() && jsonPinned.getAsJsonPrimitive().isBoolean())
            window.setPinned(jsonPinned.getAsBoolean());
    }
    saveWindows();
}
Also used : Category(net.wurstclient.features.Category) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) JsonObject(com.google.gson.JsonObject) Feature(net.wurstclient.features.Feature) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ScaledResolution(net.minecraft.client.gui.ScaledResolution) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader)

Example 53 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project Wurst-MC-1.12 by Wurst-Imperium.

the class KeybindList method init.

public void init() {
    JsonObject json;
    try (BufferedReader reader = Files.newBufferedReader(path)) {
        json = JsonUtils.jsonParser.parse(reader).getAsJsonObject();
    } catch (NoSuchFileException e) {
        loadDefaults();
        return;
    } catch (Exception e) {
        System.out.println("Failed to load " + path.getFileName());
        e.printStackTrace();
        loadDefaults();
        return;
    }
    keybinds.clear();
    TreeMap<String, String> keybinds2 = new TreeMap<>();
    for (Entry<String, JsonElement> entry : json.entrySet()) {
        String key = entry.getKey().toUpperCase();
        if (Keyboard.getKeyIndex(key) == Keyboard.KEY_NONE)
            continue;
        JsonElement value = entry.getValue();
        String commands;
        if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString())
            commands = value.getAsString();
        else if (value.isJsonArray()) {
            ArrayList<String> commands2 = new ArrayList<>();
            for (JsonElement e : value.getAsJsonArray()) if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isString())
                commands2.add(e.getAsString());
            commands = String.join(";", commands2);
        } else
            continue;
        keybinds2.put(key, commands);
    }
    for (Entry<String, String> entry : keybinds2.entrySet()) keybinds.add(new Keybind(entry.getKey(), entry.getValue()));
    save();
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) TreeMap(java.util.TreeMap) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader)

Example 54 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project OpenOLAT by OpenOLAT.

the class QTIImportProcessor method processSidecarMetadata.

private boolean processSidecarMetadata(QuestionItemImpl item, DocInfos docInfos) {
    InputStream metadataIn = null;
    try {
        Path path = docInfos.root;
        if (path != null && path.getFileName() != null) {
            Path metadata = path.resolve(path.getFileName().toString() + "_metadata.xml");
            metadataIn = Files.newInputStream(metadata);
            SAXReader reader = new SAXReader();
            Document document = reader.read(metadataIn);
            Element rootElement = document.getRootElement();
            QTIMetadataConverter enricher = new QTIMetadataConverter(rootElement, qItemTypeDao, qEduContextDao, qpoolService);
            enricher.toQuestion(item);
        }
        return true;
    } catch (NoSuchFileException e) {
        // nothing to do
        return true;
    } catch (Exception e) {
        log.error("", e);
        return false;
    } finally {
        IOUtils.closeQuietly(metadataIn);
    }
}
Also used : Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) NoSuchFileException(java.nio.file.NoSuchFileException) Document(org.dom4j.Document) QTIDocument(org.olat.ims.qti.editor.beecom.objects.QTIDocument) NoSuchFileException(java.nio.file.NoSuchFileException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Example 55 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project microservice_framework by CJSCommonPlatform.

the class SubscriptionDescriptorParser method read.

public SubscriptionDescriptor read(final Path absolutePath) {
    try {
        subscriptionDescriptorFileValidator.validate(absolutePath);
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()).registerModule(new Jdk8Module()).registerModule(new ParameterNamesModule(PROPERTIES));
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        return mapper.readValue(absolutePath.toFile(), SubscriptionDescriptorDef.class).getSubscriptionDescriptor();
    } catch (final NoSuchFileException e) {
        throw new SubscriptionDescriptorException(format("No such subscriptions YAML file %s ", absolutePath), e);
    } catch (final ValidationException e) {
        throw new SubscriptionDescriptorException(format("Failed to validate subscriptions yaml file %s ", absolutePath), e);
    } catch (final IOException e) {
        throw new SubscriptionDescriptorException(format("Failed to read subscriptions yaml file %s ", absolutePath), e);
    }
}
Also used : SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) ValidationException(org.everit.json.schema.ValidationException) SubscriptionDescriptorDef(uk.gov.justice.subscription.domain.SubscriptionDescriptorDef) NoSuchFileException(java.nio.file.NoSuchFileException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11