Example usage for java.io FileFilter FileFilter

List of usage examples for java.io FileFilter FileFilter

Introduction

In this page you can find the example usage for java.io FileFilter FileFilter.

Prototype

FileFilter

Source Link

Usage

From source file:com.iwedia.activities.DTVActivity.java

public void loadIPChannelsFromExternalStorage(ArrayList<IPService> ipChannels) {
    ArrayList<File> ipServiceListFiles = new ArrayList<File>();
    File[] storages = new File(EXTERNAL_MEDIA_PATH).listFiles();
    if (storages != null) {
        /**//from w w w .j a  v a 2s .  c o  m
         * Loop through storages.
         */
        for (File storage : storages) {
            File[] foundIpFiles = storage.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    if (pathname.getName().equalsIgnoreCase(IP_CHANNELS)) {
                        return true;
                    }
                    return false;
                }
            });
            /**
             * Files with given name are found in this array.
             */
            if (foundIpFiles != null) {
                for (File ip : foundIpFiles) {
                    ipServiceListFiles.add(ip);
                }
            }
        }
        /**
         * Loop through found files and add it to IP service list.
         */
        for (File ipFile : ipServiceListFiles) {
            readFile(this, ipFile.getPath(), ipChannels);
        }
        /**
         * No files found.
         */
        if (ipServiceListFiles.size() == 0) {
            Toast.makeText(this, "No files found with name: " + IP_CHANNELS, Toast.LENGTH_LONG).show();
        }
    }
}

From source file:de.pangaea.fixo3.xml.ProcessXmlFiles.java

private void run() throws FileNotFoundException, SAXException, IOException, ParserConfigurationException,
        XPathExpressionException {
    // src/test/resources/, src/main/resources/files
    File folder = new File("src/main/resources/files");
    File[] files = folder.listFiles(new FileFilter() {

        @Override/*from   ww  w  . jav a  2 s.c  o m*/
        public boolean accept(File pathname) {
            if (pathname.getName().endsWith(".xml"))
                return true;

            return false;
        }
    });

    JsonArrayBuilder json = Json.createArrayBuilder();

    Document doc;
    String deviceLabel, deviceComment, deviceImage;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    final Map<String, String> prefixToNS = new HashMap<>();
    prefixToNS.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    prefixToNS.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
    prefixToNS.put("sml", "http://www.opengis.net/sensorml/2.0");
    prefixToNS.put("gml", "http://www.opengis.net/gml/3.2");
    prefixToNS.put("gmd", "http://www.isotc211.org/2005/gmd");

    XPath x = XPathFactory.newInstance().newXPath();
    x.setNamespaceContext(new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            Objects.requireNonNull(prefix, "Namespace prefix cannot be null");
            final String uri = prefixToNS.get(prefix);
            if (uri == null) {
                throw new IllegalArgumentException("Undeclared namespace prefix: " + prefix);
            }
            return uri;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<?> getPrefixes(String namespaceURI) {
            throw new UnsupportedOperationException();
        }
    });

    XPathExpression exGmlName = x.compile("/sml:PhysicalSystem/gml:name");
    XPathExpression exGmlDescription = x.compile("/sml:PhysicalSystem/gml:description");
    XPathExpression exGmdUrl = x.compile(
            "/sml:PhysicalSystem/sml:documentation/sml:DocumentList/sml:document/gmd:CI_OnlineResource/gmd:linkage/gmd:URL");
    XPathExpression exTerm = x
            .compile("/sml:PhysicalSystem/sml:classification/sml:ClassifierList/sml:classifier/sml:Term");

    int m = 0;
    int n = 0;

    for (File file : files) {
        log.info(file.getName());
        doc = dbf.newDocumentBuilder().parse(new FileInputStream(file));

        deviceLabel = exGmlName.evaluate(doc).trim();
        deviceComment = exGmlDescription.evaluate(doc).trim();
        deviceImage = exGmdUrl.evaluate(doc).trim();
        NodeList terms = (NodeList) exTerm.evaluate(doc, XPathConstants.NODESET);

        JsonObjectBuilder jobDevice = Json.createObjectBuilder();

        jobDevice.add("name", toUri(deviceLabel));
        jobDevice.add("label", deviceLabel);
        jobDevice.add("comment", toAscii(deviceComment));
        jobDevice.add("image", deviceImage);

        JsonArrayBuilder jabSubClasses = Json.createArrayBuilder();

        for (int i = 0; i < terms.getLength(); i++) {
            Node term = terms.item(i);
            NodeList attributes = term.getChildNodes();

            String attributeLabel = null;
            String attributeValue = null;

            for (int j = 0; j < attributes.getLength(); j++) {
                Node attribute = attributes.item(j);
                String attributeName = attribute.getNodeName();

                if (attributeName.equals("sml:label")) {
                    attributeLabel = attribute.getTextContent();
                } else if (attributeName.equals("sml:value")) {
                    attributeValue = attribute.getTextContent();
                }
            }

            if (attributeLabel == null || attributeValue == null) {
                throw new RuntimeException("Attribute label or value cannot be null [attributeLabel = "
                        + attributeLabel + "; attributeValue = " + attributeValue + "]");
            }

            if (attributeLabel.equals("model")) {
                continue;
            }

            if (attributeLabel.equals("manufacturer")) {
                jobDevice.add("manufacturer", attributeValue);
                continue;
            }

            n++;

            Quantity quantity = getQuantity(attributeValue);

            if (quantity == null) {
                continue;
            }

            m++;

            JsonObjectBuilder jobSubClass = Json.createObjectBuilder();
            JsonObjectBuilder jobCapability = Json.createObjectBuilder();
            JsonObjectBuilder jobQuantity = Json.createObjectBuilder();

            String quantityLabel = getQuantityLabel(attributeLabel);
            String capabilityLabel = deviceLabel + " " + quantityLabel;

            jobCapability.add("label", capabilityLabel);

            jobQuantity.add("label", quantity.getLabel());

            if (quantity.getValue() != null) {
                jobQuantity.add("value", quantity.getValue());
            } else if (quantity.getMinValue() != null && quantity.getMaxValue() != null) {
                jobQuantity.add("minValue", quantity.getMinValue());
                jobQuantity.add("maxValue", quantity.getMaxValue());
            } else {
                throw new RuntimeException(
                        "Failed to determine quantity value [attributeValue = " + attributeValue + "]");
            }

            jobQuantity.add("unitCode", quantity.getUnitCode());
            jobQuantity.add("type", toUri(quantityLabel));

            jobCapability.add("quantity", jobQuantity);
            jobSubClass.add("capability", jobCapability);

            jabSubClasses.add(jobSubClass);
        }

        jobDevice.add("subClasses", jabSubClasses);

        json.add(jobDevice);
    }

    Map<String, Object> properties = new HashMap<>(1);
    properties.put(JsonGenerator.PRETTY_PRINTING, true);

    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(properties);
    JsonWriter jsonWriter = jsonWriterFactory.createWriter(new FileWriter(new File(jsonFileName)));
    jsonWriter.write(json.build());
    jsonWriter.close();

    System.out.println("Fraction of characteristics included: " + m + "/" + n);
}

From source file:com.snp.site.init.SystemInit.java

public static File[] get_pic_filelist(File file) {
    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) { // ??
            String name = file.getName().toLowerCase(); // ???
            String[] format = StringUtils.split(img_support_format, "|");
            for (int i = 0; i < format.length; i++) {
                if (name.endsWith(format[i])) {
                    return true;
                }/*from  ww w  . j  av a2 s .  com*/
            }
            return false;
            /*
             * return name.endsWith(".jpg") ||name.endsWith(".gif")
             * ||name.endsWith(".png") ||name.endsWith(".ico")
             * ||name.endsWith(".bmp") ;
             */
        }
    };
    return file.listFiles(fileFilter);
}

From source file:org.darwinathome.server.persistence.impl.WorldHistoryImpl.java

private File fetchLatestFile(File directory) {
    File[] subdirectories = directory.listFiles(new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory() && file.getName().matches("[DH]\\d{2,3}");
        }//w w  w. ja va  2  s . com
    });
    if (subdirectories == null || subdirectories.length == 0) {
        File[] worldFiles = directory.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return !file.isDirectory() && file.getName().endsWith(EXTENSION);
            }
        });
        if (worldFiles == null || worldFiles.length == 0) {
            return null;
        } else {
            Arrays.sort(worldFiles, new FileNameComparator());
            return worldFiles[worldFiles.length - 1];
        }
    } else {
        Arrays.sort(subdirectories, new FileNameComparator());
        return fetchLatestFile(subdirectories[subdirectories.length - 1]);
    }
}

From source file:com.adguard.compiler.FileUtil.java

private static void copyDirectory(File source, File dest) throws IOException {
    FileUtils.copyDirectory(source, dest, new FileFilter() {
        public boolean accept(File pathname) {
            return !pathname.getName().startsWith(".");
        }/*from  w w  w . j  a  v a  2 s.co m*/
    });
}

From source file:org.apache.bcel.generic.JDKGenericDumpTestCase.java

private File[] listJDKjars() throws Exception {
    final File javaLib = new File(javaHome, "lib");
    return javaLib.listFiles(new FileFilter() {
        @Override/*from www .j  a v  a2  s .  c  om*/
        public boolean accept(final File file) {
            return file.getName().endsWith(".jar");
        }
    });
}

From source file:net.timewalker.ffmq4.storage.data.impl.BlockBasedDataStoreTools.java

/**
 * Find recycled journal files for a given base name
 * @param baseName/*from w w  w  . j a va2 s.  c o  m*/
 * @param dataFolder
 * @return an array of journal files
 */
public static File[] findRecycledJournalFiles(String baseName, File dataFolder) {
    final String journalBase = baseName + JournalFile.SUFFIX;
    File[] recycledFiles = dataFolder.listFiles(new FileFilter() {
        /*
         * (non-Javadoc)
         * @see java.io.FileFilter#accept(java.io.File)
         */
        @Override
        public boolean accept(File pathname) {
            if (!pathname.isFile())
                return false;

            return pathname.getName().startsWith(journalBase)
                    && pathname.getName().endsWith(JournalFile.RECYCLED_SUFFIX);
        }
    });

    return recycledFiles;
}

From source file:org.apache.accumulo.minicluster.MiniAccumuloCluster.java

private boolean containsSiteFile(File f) {
    return f.isDirectory() && f.listFiles(new FileFilter() {

        @Override/*from   w  w w  . j a v  a2s.  com*/
        public boolean accept(File pathname) {
            return pathname.getName().endsWith("site.xml");
        }
    }).length > 0;
}

From source file:edu.unc.lib.dl.services.BatchIngestQueue.java

/**
 * @return/*from w ww .  j av a2  s .c om*/
 */
public File[] getFinishedDirectories() {
    File[] batchDirs = this.finishedDirectory.listFiles(new FileFilter() {
        @Override
        public boolean accept(File arg0) {
            return arg0.isDirectory();
        }
    });
    if (batchDirs != null) {
        Arrays.sort(batchDirs, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1 == null || o2 == null)
                    return 0;
                return (int) (o1.lastModified() - o2.lastModified());
            }
        });
    } else {
        return new File[] {};
    }
    return batchDirs;
}

From source file:es.uvigo.ei.sing.adops.datatypes.SingleExperiment.java

@Override
public void clear() {
    final FileFilter filter = new FileFilter() {
        @Override//w  w  w  . ja v a2s  .c om
        public boolean accept(File pathname) {
            return !(pathname.equals(SingleExperiment.this.getNotesFile())
                    || pathname.equals(SingleExperiment.this.getPropertiesFile())
                    || pathname.equals(SingleExperiment.this.getFastaFile())
                    || pathname.equals(SingleExperiment.this.getNamesFile())
                    || pathname.equals(SingleExperiment.this.getFilesFolder()));
        }
    };

    for (File file : this.getFolder().listFiles(filter)) {
        if (file.isFile())
            file.delete();
        else if (file.isDirectory())
            try {
                FileUtils.deleteDirectory(file);
            } catch (IOException e) {
            }
    }

    try {
        FileUtils.cleanDirectory(this.getFilesFolder());
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.result = null;
}