Example usage for org.apache.commons.io FilenameUtils getPath

List of usage examples for org.apache.commons.io FilenameUtils getPath

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils getPath.

Prototype

public static String getPath(String filename) 

Source Link

Document

Gets the path from a full filename, which excludes the prefix.

Usage

From source file:pt.webdetails.cdf.dd.model.meta.reader.cdexml.fs.XmlFsPluginModelReader.java

private void fixWidgetMeta(IBasicFile componentXml) {
    Document doc = null;/* ww w.  j a v a  2s .  com*/

    try {
        if (CdeEnvironment.getUserContentAccess().fileExists(componentXml.getPath())) {
            doc = Utils.getDocFromFile(CdeEnvironment.getUserContentAccess().fetchFile(componentXml.getPath()),
                    null);
        } else if (CdeEnvironment.getPluginSystemReader().fileExists(componentXml.getPath())) {
            doc = Utils.getDocFromFile(CdeEnvironment.getPluginSystemReader().fetchFile(componentXml.getPath()),
                    null);
        }
    } catch (Exception e) {
        logger.error("Unable to check meta for " + componentXml.getPath() + ", moving on");
        return;
    }
    List<Element> wcdfMeta = doc.selectNodes("//meta[@name='wcdf']");
    String wcdfName = componentXml.getName().replace(".component.xml", ".wcdf");
    String wcdfPath = FilenameUtils.getPath(componentXml.getPath()) + wcdfName;

    for (Element meta : wcdfMeta) {
        String metaText = meta.getText();

        if (CdeEnvironment.getPluginSystemWriter().fileExists(metaText)) {
            wcdfPath = metaText;
        }

        if (metaText.startsWith("/") && !wcdfPath.startsWith("/")) {
            wcdfPath = "/" + wcdfPath;
        }

        if (metaText.equals(wcdfPath)) {
            logger.debug("No need to fix current wcdf meta ( " + metaText + " ) ");
        } else {
            logger.debug("Fixing wcdf meta, was " + metaText + ", setting " + wcdfPath);
            meta.setText(wcdfPath);
            try {
                if (CdeEnvironment.getUserContentAccess().fileExists(componentXml.getPath())) {
                    CdeEnvironment.getUserContentAccess().saveFile(componentXml.getPath(),
                            new ByteArrayInputStream(doc.asXML().getBytes()));
                } else if (CdeEnvironment.getPluginSystemWriter().fileExists(componentXml.getPath())) {
                    CdeEnvironment.getPluginSystemWriter().saveFile(componentXml.getPath(),
                            new ByteArrayInputStream(doc.asXML().getBytes()));
                }
            } catch (Exception e) {
                logger.error("Unable to fix meta for " + componentXml.getName() + ", moving on");
            }
        }
    }
}

From source file:pt.webdetails.cdf.dd.util.Utils.java

public static IRWAccess getUserRWAccess(String filePath) {
    if (CdeEnvironment.getUserContentAccess().fileExists(filePath)) {
        if (CdeEnvironment.canCreateContent()
                && CdeEnvironment.getUserContentAccess().hasAccess(filePath, FileAccess.WRITE)) {
            return CdeEnvironment.getUserContentAccess();
        } else {/*from  ww w  .jav a  2  s. com*/
            return null;
        }
    } else if (CdeEnvironment.canCreateContent() && CdeEnvironment.getUserContentAccess()
            .hasAccess("/" + FilenameUtils.getPath(filePath), FileAccess.WRITE)) {
        // if file does not exist yet (ex: 'save as...'), then hasAccess method will not work on the file itself;
        // it should be checked against destination folder
        return CdeEnvironment.getUserContentAccess();
    }
    return null;
}

From source file:pt.webdetails.cpk.sitemap.Link.java

private Map<String, File> getTopLevelDirectories(Map<String, IElement> elementsMap) {
    HashMap<String, File> directories = new HashMap<String, File>();

    for (IElement element : elementsMap.values()) {
        File directory = new File(FilenameUtils.getPath(element.getLocation()));
        if (directory != null) {
            try {
                directories.put(directory.getCanonicalPath(), directory);
            } catch (Exception e) {
            }// w  ww  .j a  v a2s .co  m
        }
    }
    return directories;
}

From source file:sernet.gs.ui.rcp.main.bsi.views.FileView.java

private void makeActions() {
    addFileAction = new RightsEnabledAction(ActionRightIDs.ADDFILE) {
        @Override/*from   w  w  w  .  j  a  va2 s . c  o  m*/
        public void doRun() {
            IPreferenceStore prefStore = Activator.getDefault().getPreferenceStore();
            FileDialog fd = new FileDialog(FileView.this.getSite().getShell());
            fd.setText(Messages.FileView_14);
            String dir = prefStore.getString(PreferenceConstants.DEFAULT_FOLDER_ADDFILE);
            fd.setFilterPath(dir);
            String selected = fd.open();
            if (selected != null && selected.length() > 0) {
                String path = FilenameUtils.getPath(selected);
                prefStore.setValue(PreferenceConstants.DEFAULT_FOLDER_ADDFILE, path);
                createAndOpenAttachment(selected);
            }
        }
    };
    addFileAction.setText(Messages.FileView_16);
    addFileAction.setToolTipText(Messages.FileView_17);
    addFileAction.setImageDescriptor(ImageCache.getInstance().getImageDescriptor(ImageCache.NOTE_NEW));
    addFileAction.setEnabled(false);

    deleteFileAction = new RightsEnabledAction(ActionRightIDs.DELETEFILE) {
        @Override
        public void doRun() {
            int count = ((IStructuredSelection) viewer.getSelection()).size();
            boolean confirm = MessageDialog.openConfirm(getViewer().getControl().getShell(),
                    Messages.FileView_18, NLS.bind(Messages.FileView_19, count));
            if (!confirm) {
                return;
            }
            deleteAttachments();
            loadFiles();
        }
    };
    deleteFileAction.setText(Messages.FileView_23);
    deleteFileAction.setImageDescriptor(ImageCache.getInstance().getImageDescriptor(ImageCache.DELETE));
    deleteFileAction.setEnabled(false);

    doubleClickAction = new Action() {
        @Override
        public void run() {
            Object sel = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            EditorFactory.getInstance().openEditor(sel);
        }
    };

    saveCopyAction = new Action() {
        @Override
        public void run() {
            Attachment attachment = (Attachment) ((IStructuredSelection) viewer.getSelection())
                    .getFirstElement();
            saveCopy(attachment);
        }
    };
    saveCopyAction.setImageDescriptor(ImageCache.getInstance().getImageDescriptor(ImageCache.SAVE));
    saveCopyAction.setEnabled(false);

    openAction = new Action() {
        @Override
        public void run() {
            openFile();
        }

    };
    openAction.setImageDescriptor(ImageCache.getInstance().getImageDescriptor(ImageCache.VIEW));
    openAction.setEnabled(false);

    toggleLinkAction = new RightsEnabledAction(ActionRightIDs.SHOWALLFILES, Messages.FileView_24, SWT.TOGGLE) {
        @Override
        public void doRun() {
            isLinkingActive = !isLinkingActive;
            toggleLinkAction.setChecked(isLinkingActive());
            checkModelAndLoadFiles();
        }
    };
    toggleLinkAction.setImageDescriptor(ImageCache.getInstance().getImageDescriptor(ImageCache.LINKED));
    toggleLinkAction.setChecked(isLinkingActive());
}

From source file:squash.booking.lambdas.core.PageManagerTest.java

private void doTestCreateTodayIndexPageReturnsCorrectPage(Boolean showRedirectMessage) throws Exception {

    // We verify against a previously-saved regression file.

    // ARRANGE/*from   ww  w.  j  a va 2  s.c  o m*/
    initialisePageManager();

    String redirectionUrl = "http://squashwebsite42.s3-website-eu-west-1.amazonaws.com";
    // Load in the expected page
    String expectedIndexPage;
    String indextype = showRedirectMessage ? "Today" : "Noscript";
    try (InputStream stream = PageManagerTest.class.getResourceAsStream(
            "/squash/booking/lambdas/TestCreate" + indextype + "IndexPageReturnsCorrectPage.html")) {
        expectedIndexPage = CharStreams.toString(new InputStreamReader(stream, "UTF-8"));
    }

    // ACT
    String actualIndexPage = pageManager.createIndexPage(redirectionUrl, showRedirectMessage);

    // ASSERT
    boolean pageIsCorrect = actualIndexPage.equals(expectedIndexPage);
    if (!pageIsCorrect) {
        // Save the generated page only in the error case
        // Get path to resource so can save attempt alongside it
        URL regressionPage = PageManagerTest.class.getResource(
                "/squash/booking/lambdas/TestCreate" + indextype + "IndexPageReturnsCorrectPage.html");
        String regressionPagePath = regressionPage.getPath();
        String pathMinusFilename = FilenameUtils.getPath(regressionPagePath);
        File outputPage = new File("/" + pathMinusFilename, indextype + "PageFailingTestResult.html");
        try (PrintStream out = new PrintStream(new FileOutputStream(outputPage))) {
            out.print(actualIndexPage);
        }
    }
    assertTrue("Created " + indextype + " index page is incorrect: Actual: " + actualIndexPage + " Expected: "
            + expectedIndexPage, pageIsCorrect);
}

From source file:squash.booking.lambdas.core.PageManagerTest.java

private void doTestCreateBookingPageReturnsCorrectPage(
        ImmutablePair<LifecycleState, Optional<String>> lifecycleState, String regressionPath,
        String outputPath) throws Exception {

    // We create two single bookings, and 2 block bookings, and verify resulting
    // html directly against a previously-saved regression file.

    // ARRANGE/*from  www .  j a va  2  s . c om*/
    // Expect method to query the lifecycle manager for the lifecycle
    // state. N.B. Name mock as it's replacing the default lifecycleManager
    // mock
    mockLifecycleManager = mockery.mock(ILifecycleManager.class, "replacementLifecycleManagerMock");
    mockery.checking(new Expectations() {
        {
            oneOf(mockLifecycleManager).getLifecycleState();
            will(returnValue(lifecycleState));
        }
    });
    initialisePageManager();

    // Set some values that will get embedded into the booking page
    String s3WebsiteUrl = "http://squashwebsite.s3-website-eu-west-1.amazonaws.com";
    String reservationFormGetUrl = "reserveUrl";
    String cancellationFormGetUrl = "cancelUrl";
    // Set up 2 bookings
    Booking booking1 = new Booking();
    booking1.setSlot(3);
    booking1.setSlotSpan(1);
    booking1.setCourt(5);
    booking1.setCourtSpan(1);
    booking1.setName("A.Playera/B.Playerb");
    Booking booking2 = new Booking();
    booking2.setSlot(4);
    booking2.setSlotSpan(1);
    booking2.setCourt(3);
    booking2.setCourtSpan(1);
    booking2.setName("C.Playerc/D.Playerd");
    Booking booking3 = new Booking();
    booking3.setSlot(10);
    booking3.setSlotSpan(3);
    booking3.setCourt(2);
    booking3.setCourtSpan(2);
    booking3.setName("E.Playere/F.Playerf");
    Booking booking4 = new Booking();
    booking4.setSlot(13);
    booking4.setSlotSpan(4);
    booking4.setCourt(1);
    booking4.setCourtSpan(5);
    booking4.setName("Club Night");
    List<Booking> bookingsForPage = new ArrayList<>();
    bookingsForPage.add(booking1);
    bookingsForPage.add(booking2);
    bookingsForPage.add(booking3);
    bookingsForPage.add(booking4);

    // Load in the expected page
    String expectedBookingPage;
    try (InputStream stream = PageManagerTest.class.getResourceAsStream(regressionPath)) {
        expectedBookingPage = CharStreams.toString(new InputStreamReader(stream, "UTF-8"));
    }

    // ACT
    String actualBookingPage = pageManager.createBookingPage(fakeCurrentDateString, validDates,
            reservationFormGetUrl, cancellationFormGetUrl, s3WebsiteUrl, bookingsForPage, "DummyGuid",
            revvingSuffix);

    // ASSERT
    boolean pageIsCorrect = actualBookingPage.equals(expectedBookingPage);
    if (!pageIsCorrect) {
        // Save the generated page only in the error case
        // Get path to resource so can save attempt alongside it
        URL regressionPage = PageManagerTest.class.getResource(regressionPath);
        String regressionPagePath = regressionPage.getPath();
        String pathMinusFilename = FilenameUtils.getPath(regressionPagePath);
        File outputPage = new File("/" + pathMinusFilename, outputPath);
        try (PrintStream out = new PrintStream(new FileOutputStream(outputPage))) {
            out.print(actualBookingPage);
        }
    }
    assertTrue("Created booking page is incorrect: " + actualBookingPage, pageIsCorrect);
}

From source file:tsauto.TcpMsgReceiverAlphaFlashStream.java

private static String getUrlFile() {
    String aux = "LIVE2015_alphatest" + Calendar.getInstance().getTimeInMillis() + ".txt";
    StringBuffer buf = new StringBuffer();
    buf.append("datalog");
    buf.append(Calendar.getInstance().getTime());
    buf.append(".txt");
    aux = aux.trim();//  ww w.  j  a  v  a2  s.c  o  m
    System.out.println("-------------> " + aux);
    String path = FilenameUtils.getPath(aux.trim());
    System.out.println("-------------> " + path);
    return aux;
}

From source file:uk.ac.ebi.fg.annotare2.core.files.RemoteFileHandle.java

public String getDirectory() throws IOException {
    return FilenameUtils.getPath(uri.getPath());
}

From source file:us.terebi.lang.lpc.io.ByteArrayResource.java

public String getParentName() {
    return FilenameUtils.getPath(_name);
}

From source file:util.NameUtil.java

public static String makeUniqueName(String fileAbsPath) {
    String path = FilenameUtils.getPath(fileAbsPath);
    String base = FilenameUtils.getBaseName(fileAbsPath);
    String extension = FilenameUtils.getExtension(fileAbsPath);

    StringBuilder builder = new StringBuilder();

    int postfix = 0;

    while (isFilenameExisted(fileAbsPath)) {
        builder.setLength(0);//from w ww.  j  ava 2 s .c  om
        builder.append(path).append(base).append('_').append(postfix++).append('.').append(extension);
        fileAbsPath = builder.toString();
    }
    return fileAbsPath;
}