Search in sources :

Example 66 with Files

use of java.nio.file.Files in project fess by codelibs.

the class AdminMaintenanceAction method writeLogFiles.

protected void writeLogFiles(final ZipOutputStream zos, final String id) {
    final String logFilePath = systemHelper.getLogFilePath();
    if (StringUtil.isNotBlank(logFilePath)) {
        final Path logDirPath = Paths.get(logFilePath);
        try (Stream<Path> stream = Files.list(logDirPath)) {
            stream.filter(entry -> isLogFilename(entry.getFileName().toString())).forEach(filePath -> {
                final ZipEntry entry = new ZipEntry(id + "/" + filePath.getFileName().toString());
                try {
                    zos.putNextEntry(entry);
                    final long len = Files.copy(filePath, zos);
                    if (logger.isDebugEnabled()) {
                        logger.debug("{}: {}", filePath.getFileName(), len);
                    }
                } catch (final IOException e) {
                    logger.warn("Failed to access {}", filePath, e);
                }
            });
        } catch (final Exception e) {
            logger.warn("Failed to access log files.", e);
        }
    }
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) Constants(org.codelibs.fess.Constants) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) InetAddress(java.net.InetAddress) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) SimpleImpl(org.codelibs.fess.mylasta.direction.FessConfig.SimpleImpl) SearchEngineUtil(org.codelibs.fess.util.SearchEngineUtil) CopyUtil(org.codelibs.core.io.CopyUtil) ActionListener(org.opensearch.action.ActionListener) HtmlResponse(org.lastaflute.web.response.HtmlResponse) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Secured(org.codelibs.fess.annotation.Secured) Properties(java.util.Properties) Files(java.nio.file.Files) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ActionResponse(org.lastaflute.web.response.ActionResponse) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Execute(org.lastaflute.web.Execute) LogManager(org.apache.logging.log4j.LogManager) CurlResponse(org.codelibs.curl.CurlResponse) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) IOException(java.io.IOException)

Example 67 with Files

use of java.nio.file.Files in project beam by apache.

the class LocalFileSystem method matchOne.

private MatchResult matchOne(String baseDir, String spec) {
    if (spec.toLowerCase().startsWith("file:")) {
        spec = spec.substring("file:".length());
    }
    if (SystemUtils.IS_OS_WINDOWS) {
        List<String> prefixes = Arrays.asList("///", "/");
        for (String prefix : prefixes) {
            if (spec.toLowerCase().startsWith(prefix)) {
                spec = spec.substring(prefix.length());
            }
        }
    }
    // BEAM-6213: Windows breaks on Paths.get(spec).toFile() with a glob because
    // it considers it an invalid file system pattern. We should use
    // new File(spec) to avoid such validation.
    // See https://bugs.openjdk.java.net/browse/JDK-8197918
    // However, new File(parent, child) resolves absolute `child` in a system-dependent
    // way that is generally incorrect, for example new File($PWD, "/tmp/foo") resolves
    // to $PWD/tmp/foo on many systems, unlike Paths.get($PWD).resolve("/tmp/foo") which
    // correctly resolves to "/tmp/foo". We add just this one piece of logic here, without
    // switching to Paths which could require a rewrite of this module to support
    // both Windows and correct file resolution.
    // The root cause is that globs are not files but we are using file manipulation libraries
    // to work with them.
    final File specAsFile = new File(spec);
    final File absoluteFile = specAsFile.isAbsolute() ? specAsFile : new File(baseDir, spec);
    if (absoluteFile.exists()) {
        return MatchResult.create(Status.OK, ImmutableList.of(toMetadata(absoluteFile)));
    }
    File parent = getSpecNonGlobPrefixParentFile(absoluteFile.getAbsolutePath());
    if (!parent.exists()) {
        return MatchResult.create(Status.NOT_FOUND, Collections.emptyList());
    }
    // Method getAbsolutePath() on Windows platform may return something like
    // "c:\temp\file.txt". FileSystem.getPathMatcher() call below will treat
    // '\' (backslash) as an escape character, instead of a directory
    // separator. Replacing backslash with double-backslash solves the problem.
    // We perform the replacement on all platforms, even those that allow
    // backslash as a part of the filename, because Globs.toRegexPattern will
    // eat one backslash.
    String pathToMatch = absoluteFile.getAbsolutePath().replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement("\\\\"));
    final PathMatcher matcher = java.nio.file.FileSystems.getDefault().getPathMatcher("glob:" + pathToMatch);
    // TODO: Avoid iterating all files: https://issues.apache.org/jira/browse/BEAM-1309
    Iterable<File> files = fileTraverser().depthFirstPreOrder(parent);
    Iterable<File> matchedFiles = StreamSupport.stream(files.spliterator(), false).filter(Predicates.and(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.Files.isFile(), input -> matcher.matches(input.toPath()))::apply).collect(Collectors.toList());
    List<Metadata> result = Lists.newLinkedList();
    for (File match : matchedFiles) {
        result.add(toMetadata(match));
    }
    if (result.isEmpty()) {
        // TODO: consider to return Status.OK for globs.
        return MatchResult.create(Status.NOT_FOUND, new FileNotFoundException(String.format("No files found for spec: %s in working directory %s", spec, baseDir)));
    } else {
        return MatchResult.create(Status.OK, result);
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) LoggerFactory(org.slf4j.LoggerFactory) Status(org.apache.beam.sdk.io.fs.MatchResult.Status) BufferedOutputStream(java.io.BufferedOutputStream) StandardCopyOption(java.nio.file.StandardCopyOption) Matcher(java.util.regex.Matcher) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) PathMatcher(java.nio.file.PathMatcher) StreamSupport(java.util.stream.StreamSupport) Metadata(org.apache.beam.sdk.io.fs.MatchResult.Metadata) Predicates(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Predicates) Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Logger(org.slf4j.Logger) Files(java.nio.file.Files) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) SystemUtils(org.apache.commons.lang3.SystemUtils) Channels(java.nio.channels.Channels) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Files.fileTraverser(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.Files.fileTraverser) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) Paths(java.nio.file.Paths) MoveOptions(org.apache.beam.sdk.io.fs.MoveOptions) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting) WritableByteChannel(java.nio.channels.WritableByteChannel) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) PathMatcher(java.nio.file.PathMatcher) Metadata(org.apache.beam.sdk.io.fs.MatchResult.Metadata) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 68 with Files

use of java.nio.file.Files in project robolectric by robolectric.

the class ShadowTypeface method createFromAsset.

@Implementation
protected static Typeface createFromAsset(AssetManager mgr, String path) {
    ShadowAssetManager shadowAssetManager = Shadow.extract(mgr);
    Collection<Path> assetDirs = shadowAssetManager.getAllAssetDirs();
    for (Path assetDir : assetDirs) {
        Path assetFile = assetDir.resolve(path);
        if (Files.exists(assetFile)) {
            return createUnderlyingTypeface(path, Typeface.NORMAL);
        }
        // maybe path is e.g. "myFont", but we should match "myFont.ttf" too?
        Path[] files;
        try {
            files = Fs.listFiles(assetDir, f -> f.getFileName().toString().startsWith(path));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (files.length != 0) {
            return createUnderlyingTypeface(path, Typeface.NORMAL);
        }
    }
    throw new RuntimeException("Font asset not found " + path);
}
Also used : Path(java.nio.file.Path) Typeface(android.graphics.Typeface) RealObject(org.robolectric.annotation.RealObject) ClassParameter(org.robolectric.util.ReflectionHelpers.ClassParameter) O_MR1(android.os.Build.VERSION_CODES.O_MR1) HashMap(java.util.HashMap) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) LOLLIPOP(android.os.Build.VERSION_CODES.LOLLIPOP) Implements(org.robolectric.annotation.Implements) SuppressLint(android.annotation.SuppressLint) ReflectionHelpers(org.robolectric.util.ReflectionHelpers) Fs(org.robolectric.res.Fs) AssetManager(android.content.res.AssetManager) Map(java.util.Map) Path(java.nio.file.Path) ArrayMap(android.util.ArrayMap) O(android.os.Build.VERSION_CODES.O) Files(java.nio.file.Files) Shadow(org.robolectric.shadow.api.Shadow) Q(android.os.Build.VERSION_CODES.Q) P(android.os.Build.VERSION_CODES.P) Collection(java.util.Collection) S(android.os.Build.VERSION_CODES.S) RuntimeEnvironment.getApiLevel(org.robolectric.RuntimeEnvironment.getApiLevel) R(android.os.Build.VERSION_CODES.R) IOException(java.io.IOException) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Implementation(org.robolectric.annotation.Implementation) File(java.io.File) HiddenApi(org.robolectric.annotation.HiddenApi) Resetter(org.robolectric.annotation.Resetter) Objects(java.util.Objects) AtomicLong(java.util.concurrent.atomic.AtomicLong) N_MR1(android.os.Build.VERSION_CODES.N_MR1) Collections(java.util.Collections) FontFamily(android.graphics.FontFamily) IOException(java.io.IOException) Implementation(org.robolectric.annotation.Implementation)

Example 69 with Files

use of java.nio.file.Files in project es6draft by anba.

the class IntlDataTools method numberingSystems.

static void numberingSystems(Path cldr) throws IOException {
    // Late additions? [bali, limb]
    LinkedHashSet<String> bcp47Numbers = new LinkedHashSet<>();
    Path bcp47 = cldr.resolve("bcp47/number.xml");
    try (Reader reader = Files.newBufferedReader(bcp47, StandardCharsets.UTF_8)) {
        Document document = document(reader);
        elementsByTagName(document, "type").map(type -> type.getAttribute("name")).forEach(bcp47Numbers::add);
    }
    System.out.println(bcp47Numbers.size());
    System.out.println(bcp47Numbers);
    LinkedHashSet<String> numberingSystems = new LinkedHashSet<>();
    Path supplemental = cldr.resolve("supplemental/numberingSystems.xml");
    try (Reader reader = Files.newBufferedReader(supplemental, StandardCharsets.UTF_8)) {
        Document document = document(reader);
        elementsByTagName(document, "numberingSystem").filter(ns -> !"algorithmic".equals(ns.getAttribute("type"))).peek(ns -> {
            assert "numeric".equals(ns.getAttribute("type"));
            String digits = ns.getAttribute("digits");
            int radix = Character.codePointCount(digits, 0, digits.length());
            if (radix != 10) {
                System.out.printf("%s - %s [%d]%n", ns.getAttribute("id"), digits, radix);
            }
        }).map(ns -> ns.getAttribute("id")).forEach(numberingSystems::add);
    }
    System.out.println(numberingSystems.size());
    System.out.println(numberingSystems);
    // numberingSystems.forEach(s -> System.out.printf("\"%s\",", s));
    TreeSet<String> defaultNames = new TreeSet<>();
    TreeSet<String> otherNames = new TreeSet<>();
    Files.walk(cldr.resolve("main")).filter(Files::isRegularFile).forEach(p -> {
        try (Reader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
            Document document = document(reader);
            elementByTagName(document, "numbers").ifPresent(numbers -> {
                elementByTagName(numbers, "defaultNumberingSystem").map(Element::getTextContent).ifPresent(defaultNames::add);
                elementByTagName(numbers, "otherNumberingSystems").ifPresent(otherNumberingSystems -> {
                    Stream.of("finance", "native", "traditional").map(name -> elementByTagName(otherNumberingSystems, name)).filter(Optional::isPresent).map(Optional::get).map(Element::getTextContent).forEach(otherNames::add);
                });
            });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
    System.out.println(defaultNames);
    System.out.println(otherNames);
    TreeSet<String> allNames = new TreeSet<>();
    allNames.addAll(defaultNames);
    allNames.addAll(otherNames);
    System.out.println(allNames.stream().filter(n -> numberingSystems.contains(n)).collect(Collectors.toList()));
    System.out.println(allNames.stream().filter(n -> !numberingSystems.contains(n)).collect(Collectors.toList()));
}
Also used : Path(java.nio.file.Path) TimeZone(com.ibm.icu.util.TimeZone) java.util(java.util) Function(java.util.function.Function) DirectoryStream(java.nio.file.DirectoryStream) Matcher(java.util.regex.Matcher) ULocale(com.ibm.icu.util.ULocale) Document(org.w3c.dom.Document) StreamSupport(java.util.stream.StreamSupport) Collator(com.ibm.icu.text.Collator) Path(java.nio.file.Path) InputSource(org.xml.sax.InputSource) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) SystemTimeZoneType(com.ibm.icu.util.TimeZone.SystemTimeZoneType) IOException(java.io.IOException) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Reader(java.io.Reader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.w3c.dom.Document)

Example 70 with Files

use of java.nio.file.Files in project es6draft by anba.

the class IntlDataTools method oldStyleLanguageTags.

/**
 * {@link IntlAbstractOperations#oldStyleLanguageTags}
 *
 * @param cldrMainDir
 *            the CLDR directory
 * @throws IOException
 *             if an I/O error occurs
 */
static void oldStyleLanguageTags(Path cldr) throws IOException {
    LinkedHashMap<String, String> likelySubtags = new LinkedHashMap<>();
    try (Reader reader = Files.newBufferedReader(cldr.resolve("supplemental/likelySubtags.xml"), StandardCharsets.UTF_8)) {
        Document document = document(reader);
        elementsByTagName(document, "likelySubtag").forEach(likelySubtag -> {
            String from = likelySubtag.getAttribute("from").replace('_', '-');
            String to = likelySubtag.getAttribute("to").replace('_', '-');
            likelySubtags.put(from, to);
        });
    }
    Set<String> allTags = Files.walk(cldr.resolve("main")).filter(Files::isRegularFile).map(Path::getFileName).map(Path::toString).map(p -> p.substring(0, p.indexOf(".xml")).replace('_', '-')).collect(Collectors.toCollection(LinkedHashSet::new));
    class Entry implements Comparable<Entry> {

        final String tag;

        final String languageRegion;

        final int priority;

        Entry(String tag, String languageRegion, int priority) {
            this.tag = tag;
            this.languageRegion = languageRegion;
            this.priority = priority;
        }

        @Override
        public int compareTo(Entry o) {
            int c = languageRegion.compareTo(o.languageRegion);
            return c < 0 ? -1 : c > 0 ? 1 : Integer.compare(priority, o.priority);
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Entry) {
                return languageRegion.equals(((Entry) obj).languageRegion);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return languageRegion.hashCode();
        }
    }
    Function<Locale, String> toLanguageScript = locale -> new Locale.Builder().setLanguage(locale.getLanguage()).setScript(locale.getScript()).build().toLanguageTag();
    Function<Locale, String> toLanguageRegion = locale -> new Locale.Builder().setLanguage(locale.getLanguage()).setRegion(locale.getCountry()).build().toLanguageTag();
    Function<Locale, String> toLanguage = locale -> new Locale.Builder().setLanguage(locale.getLanguage()).build().toLanguageTag();
    System.out.printf("private static final String[] oldStyleLanguageTags = {%n");
    allTags.stream().map(Locale::forLanguageTag).filter(locale -> !locale.getScript().isEmpty() && !locale.getCountry().isEmpty()).filter(locale -> allTags.contains(toLanguageScript.apply(locale))).map(locale -> {
        String languageTag = locale.toLanguageTag();
        String languageScript = toLanguageScript.apply(locale);
        String languageRegion = toLanguageRegion.apply(locale);
        String language = toLanguage.apply(locale);
        int prio;
        if (languageTag.equals(likelySubtags.get(languageScript))) {
            prio = 1;
        } else if (languageTag.equals(likelySubtags.get(languageRegion))) {
            prio = 2;
        } else if (languageTag.equals(likelySubtags.get(language))) {
            prio = 3;
        } else if (likelySubtags.getOrDefault(language, "").startsWith(languageScript)) {
            prio = 4;
        } else {
            prio = 5;
        }
        return new Entry(languageTag, languageRegion, prio);
    }).sorted().distinct().forEach(e -> {
        System.out.printf("    \"%s\", \"%s\",%n", e.tag, e.languageRegion);
    });
    System.out.printf("};%n");
}
Also used : Path(java.nio.file.Path) TimeZone(com.ibm.icu.util.TimeZone) java.util(java.util) Function(java.util.function.Function) DirectoryStream(java.nio.file.DirectoryStream) Matcher(java.util.regex.Matcher) ULocale(com.ibm.icu.util.ULocale) Document(org.w3c.dom.Document) StreamSupport(java.util.stream.StreamSupport) Collator(com.ibm.icu.text.Collator) Path(java.nio.file.Path) InputSource(org.xml.sax.InputSource) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) SystemTimeZoneType(com.ibm.icu.util.TimeZone.SystemTimeZoneType) IOException(java.io.IOException) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ULocale(com.ibm.icu.util.ULocale) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Reader(java.io.Reader) Document(org.w3c.dom.Document) Files(java.nio.file.Files)

Aggregations

Files (java.nio.file.Files)243 IOException (java.io.IOException)210 Path (java.nio.file.Path)196 List (java.util.List)176 Collectors (java.util.stream.Collectors)154 Paths (java.nio.file.Paths)133 File (java.io.File)127 ArrayList (java.util.ArrayList)117 Map (java.util.Map)109 Set (java.util.Set)96 Collections (java.util.Collections)89 Arrays (java.util.Arrays)81 Stream (java.util.stream.Stream)77 HashMap (java.util.HashMap)74 HashSet (java.util.HashSet)58 InputStream (java.io.InputStream)55 Collection (java.util.Collection)55 Logger (org.slf4j.Logger)54 Pattern (java.util.regex.Pattern)53 Optional (java.util.Optional)51